Skip to content

Commit

Permalink
SPSH-1400 Changes to config (#800)
Browse files Browse the repository at this point in the history
* Changes to config
  • Loading branch information
marode-cap authored Nov 29, 2024
1 parent b1a5074 commit d2c7660
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 22 deletions.
4 changes: 2 additions & 2 deletions charts/dbildungs-iam-server/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"BACKEND_FOR_FRONTEND_MODULE_LOG_LEVEL": "debug"
},
"ITSLEARNING": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://itslearning.example.com",
"USERNAME": "username",
"PASSWORD": "password",
Expand All @@ -60,7 +60,7 @@
"ROOT_ERSATZ": "ersatz"
},
"OX": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://ox_ip:ox_port/webservices/OXUserService",
"CONTEXT_ID": "1337",
"CONTEXT_NAME": "contextname",
Expand Down
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 @@ -21,3 +21,6 @@ data:
LDAP_OEFFENTLICHE_SCHULEN_DOMAIN: "{{ .Values.ldap.oeffentlicheSchulenDomain }}"
LDAP_ERSATZSCHULEN_DOMAIN: "{{ .Values.ldap.ersatzschulenDomain }}"
STATUS_REDIRECT_URL: "{{ .Values.status.url }}"
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 @@ -63,7 +63,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 @@ -72,7 +72,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 @@ -31,7 +31,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 @@ -46,7 +46,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 @@ -60,7 +60,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 @@ -8,6 +8,7 @@ import { PrivacyIdeaConfig } from './privacyidea.config.js';
import { SystemConfig } from './system.config.js';
import { OxConfig } from './ox.config.js';
import { RedisConfig } from './redis.config.js';
import { envToOptionalBoolean } from './utils.js';

export type Config = {
DB: Partial<DbConfig>;
Expand Down Expand Up @@ -56,10 +57,13 @@ export default (): Config => ({
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 @@ -69,7 +73,7 @@ export default (): Config => ({
REALM: process.env['PI_REALM'],
},
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 @@ -55,7 +55,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 @@ -70,7 +70,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 @@ -175,7 +175,7 @@ describe('configloader', () => {
ADMIN_PASSWORD: 'password',
},
ITSLEARNING: {
ENABLED: 'true',
ENABLED: true,
ENDPOINT: 'http://itslearning',
USERNAME: 'username',
PASSWORD: 'password',
Expand All @@ -191,7 +191,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 @@ -49,7 +49,7 @@
"DEFAULT_LOG_LEVEL": "info"
},
"ITSLEARNING": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://itslearning-test.example.com",
"USERNAME": "username",
"PASSWORD": "password",
Expand All @@ -65,7 +65,7 @@
"REALM": "defrealm"
},
"OX": {
"ENABLED": "false",
"ENABLED": false,
"ENDPOINT": "https://ox_ip:ox_port/webservices/",
"USERNAME": "username",
"PASSWORD": "password"
Expand Down

0 comments on commit d2c7660

Please sign in to comment.