-
Notifications
You must be signed in to change notification settings - Fork 5
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 #234 from bcgov/task/I5zJ4Qfq
chore: add a kc script to print terraform import statements
- Loading branch information
Showing
7 changed files
with
102 additions
and
10 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
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
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
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
88 changes: 88 additions & 0 deletions
88
scripts/keycloak-gold-standard-client-rep-roles-terraform-imports.js
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,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(); |
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
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