Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPSH-1400 Changes to config #768

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions charts/dbildungs-iam-server/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ data:
LDAP_BIND_DN: "{{ .Values.ldap.bindDN }}"
LDAP_OEFFENTLICHE_SCHULEN_DOMAIN: "{{ .Values.ldap.oeffentlicheSchulenDomain }}"
LDAP_ERSATZSCHULEN_DOMAIN: "{{ .Values.ldap.ersatzschulenDomain }}"
ITSLEARNING_ROOT: "{{ .Values.itslearning.root }}"
ITSLEARNING_ROOT_OEFFENTLICH: "{{ .Values.itslearning.rootOeffentlich }}"
ITSLEARNING_ROOT_ERSATZ: "{{ .Values.itslearning.rootErsatz }}"
5 changes: 5 additions & 0 deletions charts/dbildungs-iam-server/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ ldap:
oeffentlicheSchulenDomain: schule-sh.de
ersatzschulenDomain: ersatzschule-sh.de

itslearning:
root: sh
rootOeffentlich: oeffentlich
rootErsatz: ersatz

auth:
# existingSecret: Refers to a secret already present in the cluster, which is required.
existingSecret: ''
Expand Down
4 changes: 2 additions & 2 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"BACKEND_FOR_FRONTEND_MODULE_LOG_LEVEL": "debug"
},
"ITSLEARNING": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://itslearning-test.example.com",
"USERNAME": "username",
"PASSWORD": "password",
Expand All @@ -71,7 +71,7 @@
"ROOT_ERSATZ": "ersatz"
},
"OX": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "http://ox.dev.spsh.dbildungsplattform.de/webservices/",
"CONTEXT_ID": "1337",
"CONTEXT_NAME": "contextname",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class ItsLearningOrganisationsEventHandler {
) {
const itsLearningConfig: ItsLearningConfig = configService.getOrThrow<ItsLearningConfig>('ITSLEARNING');

this.ENABLED = itsLearningConfig.ENABLED === 'true';
this.ENABLED = itsLearningConfig.ENABLED;

this.ROOT_OEFFENTLICH = itsLearningConfig.ROOT_OEFFENTLICH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ItsLearningPersonsEventHandler {
) {
const itsLearningConfig: ItsLearningConfig = configService.getOrThrow<ItsLearningConfig>('ITSLEARNING');

this.ENABLED = itsLearningConfig.ENABLED === 'true';
this.ENABLED = itsLearningConfig.ENABLED;
}

@EventHandler(PersonRenamedEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ItsLearningSyncEventHandler {
) {
const itsLearningConfig: ItsLearningConfig = configService.getOrThrow<ItsLearningConfig>('ITSLEARNING');

this.ENABLED = itsLearningConfig.ENABLED === 'true';
this.ENABLED = itsLearningConfig.ENABLED;
}

@EventHandler(PersonExternalSystemsSyncEvent)
Expand Down
2 changes: 1 addition & 1 deletion src/modules/ox/domain/ox-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class OxEventHandler {
) {
const oxConfig: OxConfig = configService.getOrThrow<OxConfig>('OX');

this.ENABLED = oxConfig.ENABLED === 'true';
this.ENABLED = oxConfig.ENABLED;
this.authUser = oxConfig.USERNAME;
this.authPassword = oxConfig.PASSWORD;
this.contextID = oxConfig.CONTEXT_ID;
Expand Down
8 changes: 6 additions & 2 deletions src/shared/config/config.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LdapConfig } from './ldap.config.js';
import { PrivacyIdeaConfig } from './privacyidea.config.js';
import { OxConfig } from './ox.config.js';
import { RedisConfig } from './redis.config.js';
import { envToOptionalBoolean } from './utils.js';

export default (): {
DB: Partial<DbConfig>;
Expand Down Expand Up @@ -51,10 +52,13 @@ export default (): {
PASSWORD: process.env['REDIS_PASSWORD'],
},
ITSLEARNING: {
ENABLED: process.env['ITSLEARNING_ENABLED']?.toLowerCase() as 'true' | 'false',
ENABLED: envToOptionalBoolean('ITSLEARNING_ENABLED'),
ENDPOINT: process.env['ITSLEARNING_ENDPOINT'],
USERNAME: process.env['ITSLEARNING_USERNAME'],
PASSWORD: process.env['ITSLEARNING_PASSWORD'],
ROOT: process.env['ITSLEARNING_ROOT'],
ROOT_OEFFENTLICH: process.env['ITSLEARNING_ROOT_OEFFENTLICH'],
ROOT_ERSATZ: process.env['ITSLEARNING_ROOT_ERSATZ'],
},
PRIVACYIDEA: {
ENDPOINT: process.env['PI_BASE_URL'],
Expand All @@ -65,7 +69,7 @@ export default (): {
RENAME_WAITING_TIME_IN_SECONDS: parseInt(process.env['PI_RENAME_WAITING_TIME'] || '0'),
},
OX: {
ENABLED: process.env['OX_ENABLED']?.toLowerCase() as 'true' | 'false',
ENABLED: envToOptionalBoolean('OX_ENABLED'),
ENDPOINT: process.env['OX_ENDPOINT'],
USERNAME: process.env['OX_USERNAME'],
PASSWORD: process.env['OX_PASSWORD'],
Expand Down
8 changes: 4 additions & 4 deletions src/shared/config/config.loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('configloader', () => {
BIND_DN: 'cn=admin,dc=schule-sh,dc=de',
},
ITSLEARNING: {
ENABLED: 'true',
ENABLED: true,
ENDPOINT: 'http://itslearning',
USERNAME: 'username',
ROOT: 'sh',
Expand All @@ -69,7 +69,7 @@ describe('configloader', () => {
REALM: 'defrealm',
},
OX: {
ENABLED: 'true',
ENABLED: true,
ENDPOINT: 'https://ox_ip:ox_port/webservices/OXUserService',
CONTEXT_ID: '1337',
CONTEXT_NAME: 'context1',
Expand Down Expand Up @@ -163,7 +163,7 @@ describe('configloader', () => {
ADMIN_PASSWORD: 'password',
},
ITSLEARNING: {
ENABLED: 'true',
ENABLED: true,
ENDPOINT: 'http://itslearning',
USERNAME: 'username',
PASSWORD: 'password',
Expand All @@ -179,7 +179,7 @@ describe('configloader', () => {
REALM: 'defrealm',
},
OX: {
ENABLED: 'true',
ENABLED: true,
ENDPOINT: 'https://ox_ip:ox_port/webservices/OXUserService',
CONTEXT_ID: '1337',
CONTEXT_NAME: 'context1',
Expand Down
6 changes: 3 additions & 3 deletions src/shared/config/itslearning.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IsBooleanString, IsString } from 'class-validator';
import { IsBoolean, IsString } from 'class-validator';

export class ItsLearningConfig {
@IsBooleanString()
public readonly ENABLED!: 'true' | 'false';
@IsBoolean()
public readonly ENABLED!: boolean;

@IsString()
public readonly ENDPOINT!: string;
Expand Down
6 changes: 3 additions & 3 deletions src/shared/config/ox.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IsBooleanString, IsNumberString, IsString } from 'class-validator';
import { IsBoolean, IsNumberString, IsString } from 'class-validator';

export class OxConfig {
@IsBooleanString()
public readonly ENABLED!: 'true' | 'false';
@IsBoolean()
public readonly ENABLED!: boolean;

@IsString()
public readonly ENDPOINT!: string;
Expand Down
28 changes: 28 additions & 0 deletions src/shared/config/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { envToOptionalBoolean } from './utils.js';

const TEST_KEY: string = 'CONFIG_UTIL_TEST_KEY';

describe('Config Utils', () => {
describe('envToOptionalBoolean', () => {
it.each([
['', undefined],
['true', true],
['TRUE', true],
['false', false],
['FALSE', false],
])(
'when environment variable is "%s", should return %s',
(input: string | undefined, expected: boolean | undefined) => {
process.env[TEST_KEY] = input;

expect(envToOptionalBoolean(TEST_KEY)).toBe(expected);
},
);

it('should throw error, if the environment variable is set to an invalid string', () => {
process.env[TEST_KEY] = 'INVALID';

expect(() => envToOptionalBoolean(TEST_KEY)).toThrow();
});
});
});
28 changes: 28 additions & 0 deletions src/shared/config/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Reads the environment variable and returns an optional boolean.
* Depending on the input:
* - undefined or empty string -> undefined
* - "true" (case insensitive) -> true
* - "false" (case insensitive) -> false
* - any other string -> throws error
*
* @param key The name of the environment variable
*/
export function envToOptionalBoolean(key: string): boolean | undefined {
const value: string | undefined = process.env[key];

if (!value) {
return undefined;
}

const lower: string | undefined = value.toLowerCase();

switch (lower) {
case 'true':
return true;
case 'false':
return false;
default:
throw new Error(`Expected environment variable "${key}" to be "true" or "false", received "${value}".`);
}
}
4 changes: 2 additions & 2 deletions test/config.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"DEFAULT_LOG_LEVEL": "info"
},
"ITSLEARNING": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://itslearning-test.example.com",
"USERNAME": "username",
"PASSWORD": "password",
Expand All @@ -64,7 +64,7 @@
"REALM": "defrealm"
},
"OX": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://ox_ip:ox_port/webservices/",
"USERNAME": "username",
"PASSWORD": "password"
Expand Down
Loading