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 <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-03-16 20:13:41 +01:00
parent 5db2229771
commit 91d7f1a529
4 changed files with 19 additions and 15 deletions

View file

@ -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 {

View file

@ -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']);

View file

@ -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()];

View file

@ -121,7 +121,7 @@ describe('FrontendConfigService', () => {
disableRequestedAuthnContext: 'samlTestUrl',
groupAttribute: 'samlTestUrl',
requiredGroups: ['samlTestUrl'],
externalGroups: 'samlTestUrl',
externalGroups: ['samlTestUrl'],
attribute: {
id: 'samlTestUrl',
username: 'samlTestUrl',