Skip to content

Commit

Permalink
Merge pull request #234 from bcgov/task/I5zJ4Qfq
Browse files Browse the repository at this point in the history
chore: add a kc script to print terraform import statements
  • Loading branch information
junminahn authored Nov 22, 2022
2 parents 120d705 + 5a77096 commit 78277a0
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<#list social.providers as p>
<li class="kc-social-link">
<a id="social-${p.alias}" class="bcgov-primary mb-2" type="button" href="${p.loginUrl}">
<span class="${properties.kcFormSocialAccountNameClass!}">${p.displayName!}</span>
<span class="kc-social-title ${properties.kcFormSocialAccountNameClass!}">${p.displayName!}</span>
</a>
</li>
</#list>
Expand Down
2 changes: 1 addition & 1 deletion helm/keycloak/values-b861c7-test-4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ replicaCount: 1

image:
repository: ghcr.io/bcgov/sso
tag: 7.6.5-build.18
tag: 7.6.5-build.21
pullPolicy: IfNotPresent

rollingUpdate:
Expand Down
2 changes: 1 addition & 1 deletion helm/keycloak/values-b861c7-test-5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ replicaCount: 1

image:
repository: ghcr.io/bcgov/sso
tag: 7.6.5-build.18
tag: 7.6.5-build.21
pullPolicy: IfNotPresent

rollingUpdate:
Expand Down
2 changes: 1 addition & 1 deletion helm/keycloak/values-b861c7-test-6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ replicaCount: 1

image:
repository: ghcr.io/bcgov/sso
tag: 7.6.5-build.18
tag: 7.6.5-build.21
pullPolicy: IfNotPresent

rollingUpdate:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const _ = require('lodash');
const { argv } = require('yargs');
const Confirm = require('prompt-confirm');
const { getAdminClient } = require('./keycloak-core');
const { handleError, ignoreError } = require('./helpers');
const { env, auto } = argv;

const prefix = 'client-';

const envMap = {
alpha: 'dev',
beta: 'test',
gamma: 'prod',
};

async function main() {
if (!env || !['alpha', 'beta', 'gamma'].includes(env)) {
console.info(`
Prints Terraform import statements to import the standard client-representative realm roles.
Usages:
node keycloak-gold-standard-client-rep-roles-terraform-imports --env <env> [--auto]
`);

return;
}

try {
const adminClient = await getAdminClient(env);
if (!adminClient) return;

if (!auto) {
const prompt = new Confirm(`Are you sure to proceed?`);
const answer = await prompt.run();
if (!answer) return;
}

const max = 500;
let first = 0;
let total = 0;

const result = [];

while (true) {
const roles = await adminClient.roles.find({ realm: 'standard' });

const count = roles.length;
total += count;

for (let x = 0; x < roles.length; x++) {
const role = roles[x];
if (!role.name.startsWith(prefix)) continue;

const clientId = role.name.substring(prefix.length);

const clients = await adminClient.clients.find({ realm: 'standard', clientId: clientId });
if (clients.length === 0) {
console.log(`client not found: ${clientId}`);
continue;
}

const usersWithRole = await adminClient.roles.findUsersWithRole({ realm: 'standard', name: role.name });
if (usersWithRole.length === 0) {
continue;
}

const module = `module.keycloak_${envMap[env]}.module.standard_clients.module.${clientId}.keycloak_role.realm_role`;
const rmCmd = `terraform state rm ${module}`;
const addCmd = `terraform import ${module} standard/${role.id}`;

result.push(addCmd);
}

if (count < max) break;

first = first + max;
}

console.log(`${total} roles found.`);
result.map((v) => console.log(v));
process.exit(0);
} catch (err) {
handleError(err);
process.exit(1);
}
}

main();
9 changes: 6 additions & 3 deletions scripts/migrations/helpers/migrate-target-bceidboth-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ const parseAccount = (data) => {
const displayName = _.get(data, 'displayName.0.value.0');
const type = _.get(data, 'type.0.code.0');
const email = _.get(data, 'contact.0.email.0.value.0');
const telephone = _.get(data, 'contact.0.telephone.0.value.0');
const firstName = _.get(data, 'individualIdentity.0.name.0.firstname.0.value.0');
const lastName = _.get(data, 'individualIdentity.0.name.0.surname.0.value.0');
const businessGuid = _.get(data, 'business.0.guid.0.value.0');
const businessLegalName = _.get(data, 'business.0.legalName.0.value.0');

return { guid, userId, displayName, type, email, businessGuid, businessLegalName };
return { guid, userId, displayName, type, email, telephone, firstName, lastName, businessGuid, businessLegalName };
};

const fetchBceidUser = async ({ accountType = 'Business', matchKey = '', env = 'dev' }) => {
const fetchBceidUser = async ({ accountType = 'Business', property = 'userGuid', matchKey = '', env = 'dev' }) => {
let serviceUrl = '';
let serviceId = '';
if (env === 'dev') {
Expand All @@ -63,7 +66,7 @@ const fetchBceidUser = async ({ accountType = 'Business', matchKey = '', env = '
serviceId = process.env.BCEID_SERVICE_ID_PROD;
}

const xml = generateXML({ accountType, matchKey, serviceId });
const xml = generateXML({ accountType, property, matchKey, serviceId });

try {
const { response } = await soapRequest({
Expand Down
7 changes: 4 additions & 3 deletions scripts/migrations/test-bceid-webservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { argv } = require('yargs');
const Confirm = require('prompt-confirm');
const { handleError, ignoreError } = require('../helpers');
const { fetchBceidUser } = require('./helpers/migrate-target-bceidboth-users');
const { type, search, env, auto } = argv;
const { type, search, property, env, auto } = argv;

async function main() {
if (!env) {
Expand All @@ -14,7 +14,8 @@ async function main() {
Flags:
--env BCeID Client environment; dev | test | prod
--type BCeID account type; Business | Individual
--search BCeID account GUID to search for
--property BCeID search property; userGuid | userId
--search BCeID account search value
--auto Skips the confirmation before running the script
`);

Expand All @@ -28,7 +29,7 @@ async function main() {
if (!answer) return;
}

const result = await fetchBceidUser({ accountType: type, matchKey: search, env });
const result = await fetchBceidUser({ accountType: type, property, matchKey: search, env });
console.log('result', result);

process.exit(0);
Expand Down

0 comments on commit 78277a0

Please sign in to comment.