Skip to content

Commit

Permalink
Compiling keycloak agnostic config
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Oct 9, 2023
1 parent 422c5da commit b559356
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 140 deletions.
32 changes: 15 additions & 17 deletions web/src/core/adapters/oidc/createOidcOrFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,39 @@ import type { Oidc } from "../../ports/Oidc";
import { assert } from "tsafe/assert";

export async function createOidcOrFallback(params: {
keycloakParams:
oidcParams:
| {
url?: string;
realm?: string;
authority?: string;
clientId: string;
}
| undefined;
fallback:
| {
keycloakParams: {
url: string;
oidcParams: {
authority: string;
clientId: string;
realm: string;
};
oidc: Oidc.LoggedIn;
}
| undefined;
}): Promise<Oidc.LoggedIn> {
const { keycloakParams, fallback } = params;
const { oidcParams, fallback } = params;

const wrap = (() => {
const { url, realm, clientId } = {
...fallback?.keycloakParams,
...keycloakParams
const { authority, clientId } = {
...fallback?.oidcParams,
...oidcParams
};

assert(
url !== undefined && clientId !== undefined && realm !== undefined,
"There is no specific keycloak config in the region for s3 and no keycloak config to fallback to"
authority !== undefined && clientId !== undefined,
"There is no specific oidc config in the region for satellite service and no oidc config to fallback to"
);

if (
fallback !== undefined &&
url === fallback.keycloakParams.url &&
realm === fallback.keycloakParams.realm &&
clientId === fallback.keycloakParams.clientId
authority === fallback.oidcParams.authority &&
clientId === fallback.oidcParams.clientId
) {
return {
"type": "oidc client",
Expand All @@ -47,7 +44,7 @@ export async function createOidcOrFallback(params: {

return {
"type": "keycloak params",
"keycloakParams": { url, realm, clientId }
"oidcParams": { authority, clientId }
} as const;
})();

Expand All @@ -58,7 +55,8 @@ export async function createOidcOrFallback(params: {
const { createOidc } = await import("./oidc");

const oidc = await createOidc({
...wrap.keycloakParams,
"authority": wrap.oidcParams.authority,
"clientId": wrap.oidcParams.clientId,
"transformUrlBeforeRedirect": url => url,
"getUiLocales": () => "en"
});
Expand Down
2 changes: 1 addition & 1 deletion web/src/core/adapters/oidc/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function createOidc(params: {
"silent_redirect_uri": `${window.location.origin}/silent-sso.html`
});

const configHash = fnv1aHashToHex(`${url} ${realm} ${clientId}`);
const configHash = fnv1aHashToHex(`${authority} ${clientId}`);
const configHashKey = "configHash";

const login: Oidc.NotLoggedIn["login"] = async () => {
Expand Down
99 changes: 40 additions & 59 deletions web/src/core/adapters/onyxiaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,9 @@ export function createOnyxiaApi(params: {
};
initScript: string;
k8sPublicEndpoint: {
keycloakParams?: {
clientId: string;
realm: string;
URL: string;
oidcConfiguration?: {
issuerURI?: string;
clientID: string;
};
URL?: string;
};
Expand All @@ -144,10 +143,9 @@ export function createOnyxiaApi(params: {
monitoring?: {
URLPattern: string;
};
keycloakParams?: {
URL?: string;
realm?: string;
clientId: string;
oidcConfiguration?: {
issuerURI?: string;
clientID: string;
};
defaultDurationSeconds?: number;
} & (
Expand All @@ -169,10 +167,9 @@ export function createOnyxiaApi(params: {
kvEngine: string;
role: string;
authPath?: string;
keycloakParams?: {
URL?: string;
realm?: string;
clientId: string;
oidcConfiguration?: {
issuerURI?: string;
clientID: string;
};
};
proxyInjection?: {
Expand All @@ -192,17 +189,10 @@ export function createOnyxiaApi(params: {
pathToCaBundle: string;
};
}[];
authenticationInfo?:
| {
mode: "none";
}
| {
mode: "openidconnect";
oidcConfiguration: {
issuerURI: string;
clientID: string;
};
};
oidcConfiguration?: {
issuerURI: string;
clientID: string;
};
}>("/public/configuration")
.then(({ data }) => ({
"regions": data.regions.map(
Expand Down Expand Up @@ -230,13 +220,14 @@ export function createOnyxiaApi(params: {
const common: DeploymentRegion.S3.Common = {
"monitoringUrlPattern": S3.monitoring?.URLPattern,
"defaultDurationSeconds": S3.defaultDurationSeconds,
"keycloakParams":
S3.keycloakParams === undefined
"oidcParams":
S3.oidcConfiguration === undefined
? undefined
: {
"url": S3.keycloakParams.URL,
"realm": S3.keycloakParams.realm,
"clientId": S3.keycloakParams.clientId
"authority":
S3.oidcConfiguration.issuerURI,
"clientId":
S3.oidcConfiguration.clientID
}
};

Expand Down Expand Up @@ -291,15 +282,16 @@ export function createOnyxiaApi(params: {
"kvEngine": vault.kvEngine,
"role": vault.role,
"authPath": vault.authPath,
"keycloakParams":
vault.keycloakParams === undefined
"oidcParams":
vault.oidcConfiguration === undefined
? undefined
: {
"url": vault.keycloakParams.URL,
"realm":
vault.keycloakParams.realm,
"authority":
vault.oidcConfiguration
.issuerURI,
"clientId":
vault.keycloakParams.clientId
vault.oidcConfiguration
.clientID
}
};
})(),
Expand All @@ -314,19 +306,19 @@ export function createOnyxiaApi(params: {
? undefined
: {
"url": k8sPublicEndpoint.URL,
"keycloakParams":
k8sPublicEndpoint.keycloakParams ===
"oidcParams":
k8sPublicEndpoint.oidcConfiguration ===
undefined
? undefined
: {
"url": k8sPublicEndpoint
.keycloakParams.URL,
"realm":
"authority":
k8sPublicEndpoint
.keycloakParams.realm,
.oidcConfiguration
.issuerURI,
"clientId":
k8sPublicEndpoint
.keycloakParams.clientId
.oidcConfiguration
.clientID
}
};
})(),
Expand All @@ -335,24 +327,13 @@ export function createOnyxiaApi(params: {
"resources": region.services.defaultConfiguration?.resources
})
),
"oidcParams": (() => {
const { authenticationInfo } = data;

if (authenticationInfo === undefined) {
return undefined;
}

if (authenticationInfo.mode !== "openidconnect") {
return undefined;
}

const { oidcConfiguration } = authenticationInfo;

return {
"authority": oidcConfiguration.issuerURI,
"clientId": oidcConfiguration.clientID
};
})()
"oidcParams":
data.oidcConfiguration === undefined
? undefined
: {
"authority": data.oidcConfiguration.issuerURI,
"clientId": data.oidcConfiguration.clientID
}
}))
.catch(onError)
),
Expand Down
35 changes: 16 additions & 19 deletions web/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,14 @@ import type { Language } from "./ports/OnyxiaApi/Language";
type CoreParams = {
/** Empty string for using mock */
apiUrl: string;
/** Default: false, only considered if using mocks */
isUserInitiallyLoggedIn?: boolean;
transformUrlBeforeRedirectToLogin: (url: string) => string;
getCurrentLang: () => Language;
disablePersonalInfosInjectionInGroup: boolean;
isCommandBarEnabledByDefault: boolean;
};

export async function createCore(params: CoreParams) {
const {
apiUrl,
isUserInitiallyLoggedIn = false,
transformUrlBeforeRedirectToLogin,
getCurrentLang
} = params;
const { apiUrl, transformUrlBeforeRedirectToLogin, getCurrentLang } = params;

let oidc: Oidc | undefined = undefined;

Expand Down Expand Up @@ -83,13 +76,15 @@ export async function createCore(params: CoreParams) {
return onyxiaApi;
})();

let oidcParams: { authority: string; clientId: string } | undefined = undefined;

oidc = await (async () => {
const { oidcParams } = await onyxiaApi.getAvailableRegionsAndOidcParams();
oidcParams = (await onyxiaApi.getAvailableRegionsAndOidcParams()).oidcParams;

if (oidcParams === undefined) {
const { createOidc } = await import("core/adapters/oidcMock");

return createOidc({ isUserInitiallyLoggedIn });
return createOidc({ "isUserInitiallyLoggedIn": false });
}

const { createOidc } = await import("core/adapters/oidc");
Expand Down Expand Up @@ -130,8 +125,8 @@ export async function createCore(params: CoreParams) {
const { s3: s3Params, vault: vaultParams } = usecases.deploymentRegion.selectors.selectedDeploymentRegion(core.getState());

/* prettier-ignore */
const fallbackOidc = keycloakParams === undefined ? undefined : {
"keycloakParams": keycloakParams,
const fallback = oidcParams === undefined ? undefined : {
oidcParams,
oidc
};

Expand All @@ -151,12 +146,12 @@ export async function createCore(params: CoreParams) {
);

return createS3Client({
"oidc": await createOidcOrFallback({
"keycloakParams": s3Params.keycloakParams,
"fallback": fallbackOidc
}),
...getCreateS3ClientParams({ s3Params }),
"createAwsBucket": onyxiaApi.createAwsBucket
"createAwsBucket": onyxiaApi.createAwsBucket,
"oidc": await createOidcOrFallback({
"oidcParams": s3Params.oidcParams,
fallback
})
});
})();

Expand All @@ -176,8 +171,8 @@ export async function createCore(params: CoreParams) {
"url": vaultParams.url,
"authPath": vaultParams.authPath,
"oidc": await createOidcOrFallback({
"keycloakParams": vaultParams.keycloakParams,
"fallback": fallbackOidc
"oidcParams": vaultParams.oidcParams,
fallback
})
});
})();
Expand All @@ -190,6 +185,8 @@ export async function createCore(params: CoreParams) {
await core.dispatch(
usecases.restorablePackageConfigs.protectedThunks.initialize()
);

await core.dispatch(usecases.userAccountManagement.protectedThunks.initialize());
}

return core;
Expand Down
15 changes: 6 additions & 9 deletions web/src/core/ports/OnyxiaApi/DeploymentRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ export type DeploymentRegion = {
kvEngine: string;
role: string;
authPath: string | undefined;
keycloakParams:
oidcParams:
| {
url: string | undefined;
realm: string | undefined;
authority?: string;
clientId: string;
}
| undefined;
Expand Down Expand Up @@ -65,10 +64,9 @@ export type DeploymentRegion = {
kubernetes:
| {
url: string;
keycloakParams:
oidcParams:
| {
url: string;
realm: string;
authority?: string;
clientId: string;
}
| undefined;
Expand Down Expand Up @@ -100,10 +98,9 @@ export namespace DeploymentRegion {
export type Common = {
defaultDurationSeconds: number | undefined;
monitoringUrlPattern: string | undefined;
keycloakParams:
oidcParams:
| {
url: string | undefined;
realm: string | undefined;
authority?: string;
clientId: string;
}
| undefined;
Expand Down
3 changes: 2 additions & 1 deletion web/src/core/usecases/deploymentRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const protectedThunks = {
async (...args) => {
const [dispatch, , { onyxiaApi }] = args;

const availableDeploymentRegions = await onyxiaApi.getAvailableRegions();
const { regions: availableDeploymentRegions } =
await onyxiaApi.getAvailableRegionsAndOidcParams();

let previouslySelectedRegionIdFromLocalStorage =
localStorage.getItem(localStorageKey);
Expand Down
Loading

0 comments on commit b559356

Please sign in to comment.