mirror of
https://github.com/OpenNebula/one.git
synced 2026-12-04 22:21:36 +09:00
F OpenNebula/One#6813: message LDAP server is not reachable (#4573)
- Improves failed LDAP login feedback by distinguishing invalid credentials from unreachable LDAP servers. - Checks the LDAP endpoints selected for the user and returns HTTP 503 with the affected endpoints when none are reachable. - Preserves the existing unauthorized response when LDAP is reachable, the user does not use the LDAP driver, or the connectivity check cannot be completed.
This commit is contained in:
1 parent
9707f80e62
commit
bed3fa2ee1
3 files changed
+170
-1
No files matched your search
@@ -0,0 +1,140 @@
|
||||
/* ------------------------------------------------------------------------- *
|
||||
* Copyright 2002-2026, OpenNebula Project, OpenNebula Systems *
|
||||
* *
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
|
||||
* not use this file except in compliance with the License. You may obtain *
|
||||
* a copy of the License at *
|
||||
* *
|
||||
* http://www.apache.org/licenses/LICENSE-2.0 *
|
||||
* *
|
||||
* Unless required by applicable law or agreed to in writing, software *
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, *
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
|
||||
* See the License for the specific language governing permissions and *
|
||||
* limitations under the License. *
|
||||
* ------------------------------------------------------------------------- */
|
||||
|
||||
const { readFile } = require('fs').promises
|
||||
const net = require('net')
|
||||
const { global } = require('window-or-global')
|
||||
const yaml = require('yaml')
|
||||
|
||||
const DEFAULT_LDAP_PORT = 389
|
||||
const DEFAULT_LDAP_TIMEOUT = 3000
|
||||
const MAX_LDAP_TIMEOUT = 5000
|
||||
|
||||
const flattenServerNames = (servers) => {
|
||||
if (Array.isArray(servers)) return servers.flatMap(flattenServerNames)
|
||||
|
||||
if (servers && typeof servers === 'object') {
|
||||
const groups = Object.values(servers)
|
||||
|
||||
return groups.length === 1 ? flattenServerNames(groups[0]) : []
|
||||
}
|
||||
|
||||
return typeof servers === 'string' && servers.length ? [servers] : []
|
||||
}
|
||||
|
||||
const getMatchedServerNames = (config, user) => {
|
||||
const matches = config?.[':match_user_regex']
|
||||
|
||||
if (!matches || typeof matches !== 'object' || Array.isArray(matches)) {
|
||||
return []
|
||||
}
|
||||
|
||||
for (const [pattern, servers] of Object.entries(matches)) {
|
||||
try {
|
||||
if (new RegExp(pattern, 'i').test(user)) {
|
||||
return flattenServerNames(servers)
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the LDAP endpoints which the authentication driver may query.
|
||||
*
|
||||
* @param {string} source - ldap_auth.conf contents
|
||||
* @param {string} user - User being authenticated
|
||||
* @returns {Array<{host: string, port: number, timeout: number}>} endpoints
|
||||
*/
|
||||
const parseLdapEndpoints = (source = '', user = '') => {
|
||||
const config = yaml.parse(source) ?? {}
|
||||
if (config[':order'] && config[':match_user_regex']) return []
|
||||
if (config[':order'] && !Array.isArray(config[':order'])) return []
|
||||
|
||||
const matchedServers = getMatchedServerNames(config, user)
|
||||
const orderedServers = flattenServerNames(config[':order'])
|
||||
const serverNames = config[':match_user_regex']
|
||||
? matchedServers
|
||||
: orderedServers
|
||||
|
||||
return [...new Set(serverNames)]
|
||||
.map((name) => config[name])
|
||||
.filter((server) => server && typeof server === 'object')
|
||||
.map((server) => {
|
||||
const configuredTimeout = Number(server[':timeout']) * 1000
|
||||
|
||||
return {
|
||||
host: String(server[':host'] || 'localhost'),
|
||||
port: Number(server[':port']) || DEFAULT_LDAP_PORT,
|
||||
timeout:
|
||||
Number.isFinite(configuredTimeout) && configuredTimeout > 0
|
||||
? Math.min(configuredTimeout, MAX_LDAP_TIMEOUT)
|
||||
: DEFAULT_LDAP_TIMEOUT,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const canConnect = ({ host, port, timeout }) =>
|
||||
new Promise((resolve) => {
|
||||
const socket = net.createConnection({ host, port })
|
||||
let settled = false
|
||||
|
||||
const finish = (reachable) => {
|
||||
if (settled) return
|
||||
|
||||
settled = true
|
||||
socket.destroy()
|
||||
resolve(reachable)
|
||||
}
|
||||
|
||||
socket.setTimeout(timeout)
|
||||
socket.once('connect', () => finish(true))
|
||||
socket.once('timeout', () => finish(false))
|
||||
socket.once('error', () => finish(false))
|
||||
})
|
||||
|
||||
/**
|
||||
* Check whether every LDAP endpoint selected for a user is unreachable.
|
||||
* Configuration and parsing errors are ignored so they do not mask the
|
||||
* original authentication failure.
|
||||
*
|
||||
* @param {string} user - User being authenticated
|
||||
* @returns {Promise<Array<{host: string, port: number}>>} unreachable endpoints
|
||||
*/
|
||||
const getUnavailableLdapEndpoints = async (user) => {
|
||||
try {
|
||||
const configPath = global?.paths?.LDAP_AUTH_CONFIG
|
||||
if (!configPath) return []
|
||||
|
||||
const endpoints = parseLdapEndpoints(
|
||||
await readFile(configPath, 'utf8'),
|
||||
user
|
||||
)
|
||||
if (!endpoints.length) return []
|
||||
|
||||
const results = await Promise.all(endpoints.map(canConnect))
|
||||
|
||||
return results.some(Boolean) ? [] : endpoints
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getUnavailableLdapEndpoints,
|
||||
parseLdapEndpoints,
|
||||
}
|
||||
@@ -31,7 +31,8 @@ const { Actions: ActionZones } = require('server/utils/constants/commands/zone')
|
||||
const { defaults, httpCodes } = require('server/utils/constants')
|
||||
const { getFireedgeConfig, getSunstoneConfig } = require('server/utils/yml')
|
||||
const { createJWT, validate2FA } = require('server/utils/jwt')
|
||||
const { ok, internalServerError, unauthorized, accepted } = httpCodes
|
||||
const { ok, internalServerError, unauthorized, accepted, serviceUnavailable } =
|
||||
httpCodes
|
||||
const {
|
||||
httpResponse,
|
||||
encrypt,
|
||||
@@ -42,6 +43,7 @@ const {
|
||||
generateNewResourceTemplate,
|
||||
getDefaultParamsOfOpennebulaCommand,
|
||||
} = require('server/utils/opennebula')
|
||||
const { getUnavailableLdapEndpoints } = require('server/routes/api/auth/ldap')
|
||||
|
||||
const {
|
||||
httpMethod,
|
||||
@@ -466,6 +468,25 @@ const fetchUserPool = async ({ connect }) =>
|
||||
)
|
||||
.flat()
|
||||
|
||||
const formatLdapEndpoint = ({ host, port }) =>
|
||||
`${host.includes(':') ? `[${host}]` : host}:${port}`
|
||||
|
||||
const getLdapUnavailableMessage = async ({ user, connect }) => {
|
||||
try {
|
||||
const endpoints = await getUnavailableLdapEndpoints(user)
|
||||
if (!endpoints.length) return
|
||||
|
||||
const userPool = await fetchUserPool({ connect })
|
||||
const authUser = userPool.find(({ NAME }) => NAME === user)
|
||||
|
||||
if (authUser?.AUTH_DRIVER !== 'ldap') return
|
||||
|
||||
const endpointList = endpoints.map(formatLdapEndpoint).join(', ')
|
||||
|
||||
return `Can't contact LDAP server at ${endpointList}. Couldn't authenticate user ${user}`
|
||||
} catch {}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} params - Params
|
||||
* @param {Function} params.connect - OpenNebula connector callback
|
||||
@@ -567,6 +588,11 @@ const verifyUserExists = async ({ user, token, connect, protocol }) => {
|
||||
case AUTH_TYPES.CORE: {
|
||||
const userData = await fetchUserInfo({ connect: connect(user, token) })
|
||||
if (!userData?.USER) {
|
||||
const ldapError = await getLdapUnavailableMessage({ user, connect })
|
||||
if (ldapError) {
|
||||
throw httpResponse(serviceUnavailable, '', ldapError)
|
||||
}
|
||||
|
||||
throw httpResponse(unauthorized, '', 'core user not found')
|
||||
}
|
||||
|
||||
|
||||
@@ -617,6 +617,9 @@ const genPathResources = () => {
|
||||
if (!global.paths.FIREEDGE_CONFIG) {
|
||||
global.paths.FIREEDGE_CONFIG = `${ETC_LOCATION}/${defaultConfigFile}`
|
||||
}
|
||||
if (!global.paths.LDAP_AUTH_CONFIG) {
|
||||
global.paths.LDAP_AUTH_CONFIG = `${ETC_LOCATION}/auth/ldap_auth.conf`
|
||||
}
|
||||
if (!global.paths.FIREEDGE_LOG) {
|
||||
global.paths.FIREEDGE_LOG = `${LOG_LOCATION}/${defaultLogFilename}`
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user