-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1453 from cloud-pi-native/refactor/keycloak-internal
Refactor/keycloak-internal
- Loading branch information
Showing
3 changed files
with
73 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
import KcAdminClient from '@keycloak/keycloak-admin-client' | ||
import { requiredEnv } from '@cpn-console/shared' | ||
|
||
export const keycloakToken = process.env.KEYCLOAK_ADMIN_PASSWORD | ||
export const keycloakUser = process.env.KEYCLOAK_ADMIN | ||
|
||
let keycloakDomain: string | undefined | ||
let keycloakProtocol: string | undefined | ||
let keycloakRealm: string | undefined | ||
import getConfig from './config.js' | ||
|
||
export async function getkcClient() { | ||
keycloakDomain = keycloakDomain ?? requiredEnv('KEYCLOAK_DOMAIN') | ||
keycloakProtocol = keycloakProtocol ?? requiredEnv('KEYCLOAK_PROTOCOL') | ||
keycloakRealm = keycloakRealm ?? requiredEnv('KEYCLOAK_REALM') | ||
|
||
const kcClient = new KcAdminClient({ | ||
baseUrl: `${keycloakProtocol}://${keycloakDomain}`, | ||
baseUrl: getConfig().url, | ||
}) | ||
|
||
await kcClient.auth({ | ||
clientId: 'admin-cli', | ||
grantType: 'password', | ||
username: keycloakUser, | ||
password: keycloakToken, | ||
username: process.env.KEYCLOAK_ADMIN, | ||
password: process.env.KEYCLOAK_ADMIN_PASSWORD, | ||
}) | ||
kcClient.setConfig({ realmName: keycloakRealm }) | ||
kcClient.setConfig({ realmName: getConfig().realm }) | ||
return kcClient | ||
} | ||
|
||
export function start() { | ||
getkcClient() | ||
getkcClient().catch((error) => { | ||
console.log(error) | ||
if (process.env.IGNORE_PLUGINS_START_FAIL?.includes('keycloak')) { | ||
return | ||
} | ||
throw new Error('failed to start keycloak plugin') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { removeTrailingSlash, requiredEnv } from '@cpn-console/shared' | ||
|
||
class Config { | ||
url: string | ||
realm: string | ||
|
||
constructor() { | ||
this.url = process.env.KEYCLOAK_INTERNAL_URL | ||
? removeTrailingSlash(process.env.KEYCLOAK_INTERNAL_URL) | ||
: `${requiredEnv('KEYCLOAK_PROTOCOL')}://${requiredEnv('KEYCLOAK_DOMAIN')}` | ||
this.realm = requiredEnv('KEYCLOAK_REALM') | ||
} | ||
} | ||
|
||
let config: Config | undefined | ||
|
||
function getConfig() { | ||
if (!config) { | ||
config = new Config() | ||
} | ||
return config | ||
} | ||
export default getConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters