From 91d7f1a5298eb7a7bd1943a248631513b688435c Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Wed, 16 Mar 2022 20:13:41 +0100 Subject: [PATCH] fix: error in toArrayConfig If an empty string or undefined is provided the method should not return [], but undefined instead. This way defaults defined in Joi function as expected. Signed-off-by: Philip Molares --- src/config/auth.config.ts | 22 +++++++++---------- src/config/utils.spec.ts | 3 ++- src/config/utils.ts | 7 ++++-- .../frontend-config.service.spec.ts | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/config/auth.config.ts b/src/config/auth.config.ts index 9d43ab6d7..9432eb432 100644 --- a/src/config/auth.config.ts +++ b/src/config/auth.config.ts @@ -66,7 +66,7 @@ export interface AuthConfig { searchAttributes: string[]; usernameField: string; useridField: string; - tlsCa: string[]; + tlsCa?: string[]; }[]; saml: { identifier: string; @@ -78,8 +78,8 @@ export interface AuthConfig { identifierFormat: string; disableRequestedAuthnContext: string; groupAttribute: string; - requiredGroups: string[]; - externalGroups: string; + requiredGroups?: string[]; + externalGroups?: string[]; attribute: { id: string; username: string; @@ -241,18 +241,18 @@ const authSchema = Joi.object({ export default registerAs('authConfig', () => { // ToDo: Validate these with Joi to prevent duplicate entries? - const gitlabNames = toArrayConfig(process.env.HD_AUTH_GITLABS, ',').map( + const gitlabNames = ( + toArrayConfig(process.env.HD_AUTH_GITLABS, ',') ?? [] + ).map((name) => name.toUpperCase()); + const ldapNames = (toArrayConfig(process.env.HD_AUTH_LDAPS, ',') ?? []).map( (name) => name.toUpperCase(), ); - const ldapNames = toArrayConfig(process.env.HD_AUTH_LDAPS, ',').map((name) => - name.toUpperCase(), - ); - const samlNames = toArrayConfig(process.env.HD_AUTH_SAMLS, ',').map((name) => - name.toUpperCase(), - ); - const oauth2Names = toArrayConfig(process.env.HD_AUTH_OAUTH2S, ',').map( + const samlNames = (toArrayConfig(process.env.HD_AUTH_SAMLS, ',') ?? []).map( (name) => name.toUpperCase(), ); + const oauth2Names = ( + toArrayConfig(process.env.HD_AUTH_OAUTH2S, ',') ?? [] + ).map((name) => name.toUpperCase()); const gitlabs = gitlabNames.map((gitlabName) => { return { diff --git a/src/config/utils.spec.ts b/src/config/utils.spec.ts index 26ebd8186..6249611cc 100644 --- a/src/config/utils.spec.ts +++ b/src/config/utils.spec.ts @@ -14,7 +14,8 @@ import { describe('config utils', () => { describe('toArrayConfig', () => { it('empty', () => { - expect(toArrayConfig('')).toEqual([]); + expect(toArrayConfig('')).toEqual(undefined); + expect(toArrayConfig(undefined)).toEqual(undefined); }); it('one element', () => { expect(toArrayConfig('one')).toEqual(['one']); diff --git a/src/config/utils.ts b/src/config/utils.ts index c52f69e23..9f1433a79 100644 --- a/src/config/utils.ts +++ b/src/config/utils.ts @@ -5,9 +5,12 @@ */ import { Loglevel } from './loglevel.enum'; -export function toArrayConfig(configValue?: string, separator = ','): string[] { +export function toArrayConfig( + configValue?: string, + separator = ',', +): string[] | undefined { if (!configValue) { - return []; + return undefined; } if (!configValue.includes(separator)) { return [configValue.trim()]; diff --git a/src/frontend-config/frontend-config.service.spec.ts b/src/frontend-config/frontend-config.service.spec.ts index c2f8088ff..87d5dc345 100644 --- a/src/frontend-config/frontend-config.service.spec.ts +++ b/src/frontend-config/frontend-config.service.spec.ts @@ -121,7 +121,7 @@ describe('FrontendConfigService', () => { disableRequestedAuthnContext: 'samlTestUrl', groupAttribute: 'samlTestUrl', requiredGroups: ['samlTestUrl'], - externalGroups: 'samlTestUrl', + externalGroups: ['samlTestUrl'], attribute: { id: 'samlTestUrl', username: 'samlTestUrl',