-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Add config-manager push scripts command #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { frodo } from '@rockcarver/frodo-lib'; | ||
| import { Option } from 'commander'; | ||
|
|
||
| import { configManagerImportScripts } from '../../../configManagerOps/FrConfigScriptOps'; | ||
| import { getTokens } from '../../../ops/AuthenticateOps'; | ||
| import { FrodoCommand } from '../../FrodoCommand'; | ||
|
|
||
| const { CLOUD_DEPLOYMENT_TYPE_KEY, FORGEOPS_DEPLOYMENT_TYPE_KEY } = | ||
| frodo.utils.constants; | ||
|
|
||
| const deploymentTypes = [ | ||
| CLOUD_DEPLOYMENT_TYPE_KEY, | ||
| FORGEOPS_DEPLOYMENT_TYPE_KEY, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not specify deployment type here since all deployment types support scripts since they are AM configuration |
||
| ]; | ||
|
|
||
| export default function setup() { | ||
| const program = new FrodoCommand( | ||
| 'frodo config-manager push scripts', | ||
| deploymentTypes | ||
| ); | ||
|
|
||
|
dallinjsevy marked this conversation as resolved.
|
||
| program | ||
| .description('Import scripts to forgeops.') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect, I would just have it be |
||
| .addOption( | ||
| new Option( | ||
| '-n, --name <name>', | ||
| 'Script name, import only specified endpoint' | ||
| ) | ||
| ) | ||
| .addOption( | ||
| new Option( | ||
| '-r, --realm <realm>', | ||
| 'Realm name, import only specified realm' | ||
| ) | ||
| ); | ||
|
Comment on lines
+30
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the -r, --realm flag |
||
|
|
||
| program.action(async (host, realm, user, password, options, command) => { | ||
| command.handleDefaultArgsAndOpts( | ||
| host, | ||
| realm, | ||
| user, | ||
| password, | ||
| options, | ||
| command | ||
| ); | ||
|
|
||
| if (!(await getTokens(false, true, deploymentTypes))) { | ||
| process.exitCode = 1; | ||
| return; | ||
| } | ||
|
Comment on lines
+47
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thing, but you can do this in one line: if (!(await getTokens(false, true, deploymentTypes))) process.exit(1); |
||
|
|
||
| const outcome = await configManagerImportScripts( | ||
| options.realm, | ||
| options.name | ||
| ); | ||
| if (!outcome) process.exitCode = 1; | ||
| }); | ||
|
|
||
| return program; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import { frodo, state } from '@rockcarver/frodo-lib'; | ||
| import { ScriptSkeleton } from '@rockcarver/frodo-lib/types/api/ScriptApi'; | ||
| import fs from 'fs'; | ||
|
|
||
| import { printError, verboseMessage } from '../utils/Console'; | ||
| import { realmList, safeFileName } from '../utils/FrConfig'; | ||
|
|
||
| const { getFilePath, saveJsonToFile, decodeBase64, saveTextToFile } = | ||
| frodo.utils; | ||
| const { readScripts, readScriptByName } = frodo.script; | ||
| const { readScripts, readScriptByName, importScripts } = frodo.script; | ||
|
|
||
| type ByName = { scriptName: string }; | ||
| type BySkeleton = { ss: ScriptSkeleton }; | ||
|
|
@@ -219,3 +220,57 @@ export async function configManagerExportScriptsAll( | |
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Import script in fr-config-manager format | ||
| * @param realm option to determine which realm to import | ||
| * @param name option to import a specific script by name | ||
| * @returns True if Import was successful | ||
|
Comment on lines
+226
to
+228
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another small thing, but usually in the comments you specify the type as well: /* @param {string} realm option to determine which realm to import
* @param {string} name option to import a specific script by name
* @returns {Promise<boolean>} True if Import was successful
*/ |
||
| */ | ||
| export async function configManagerImportScripts( | ||
| realmName?: string, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rename this to |
||
| name?: string | ||
| ): Promise<boolean> { | ||
| try { | ||
| const realmsDir = getFilePath('realms/'); | ||
| const realms: string[] = realmName | ||
| ? [realmName] | ||
| : fs | ||
| .readdirSync(realmsDir, { withFileTypes: true }) | ||
| .filter((entry) => entry.isDirectory()) | ||
| .map((entry) => entry.name); | ||
|
Comment on lines
+235
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The root realm for forgeops gets exported to |
||
|
|
||
| for (const realm of realms) { | ||
| if (!realmName) state.setRealm(realm); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for the if check, I'd just do |
||
|
|
||
| const configDir = getFilePath(`realms/${realm}/scripts/scripts-config/`); | ||
|
|
||
| const configFiles = name ? [name] : fs.readdirSync(configDir); | ||
|
|
||
| const scripts = { script: {} }; | ||
|
|
||
| for (const file of configFiles) { | ||
| try { | ||
| const configPath = `${configDir}/${file}`; | ||
| if (!fs.existsSync(configPath)) continue; | ||
| const importData = JSON.parse(fs.readFileSync(configPath, 'utf8')); | ||
| const fullScriptPath = getFilePath( | ||
| `realms/${realm}/scripts/${importData.script.file}` | ||
| ); | ||
| delete importData.script.file; | ||
| importData.script = fs.readFileSync(fullScriptPath, 'utf8'); | ||
| scripts.script[importData._id] = importData; | ||
| } catch (error) { | ||
| printError(error); | ||
| } | ||
| } | ||
|
|
||
| await importScripts(null, null, scripts); | ||
| } | ||
|
|
||
| return true; | ||
| } catch (error) { | ||
| printError(error); | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`CLI help interface for 'config-manager push scripts' should be expected english 1`] = ` | ||
| "Usage: frodo config-manager push scripts [options] [host] [realm] [username] [password] | ||
|
|
||
| [Experimental] Import scripts to forgeops. | ||
|
|
||
| Arguments: | ||
| host AM base URL, e.g.: https://cdk.iam.example.com/am. To use | ||
| a connection profile, just specify a unique substring or | ||
| alias. | ||
| realm Realm. Specify realm as '/' for the root realm or 'realm' | ||
| or '/parent/child' otherwise. (default: "alpha" for | ||
| Identity Cloud tenants, "/" otherwise.) | ||
| username Username to login with. Must be an admin user with | ||
| appropriate rights to manage authentication | ||
| journeys/trees. | ||
| password Password. | ||
|
|
||
| Options: | ||
| -n, --name <name> Script name, import only specified endpoint | ||
| -r, --realm <realm> Realm name, import only specified realm | ||
| -h, --help Help | ||
| -hh, --help-more Help with all options. | ||
| -hhh, --help-all Help with all options, environment variables, and usage | ||
| examples. | ||
| " | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import cp from 'child_process'; | ||
| import { promisify } from 'util'; | ||
|
|
||
| const exec = promisify(cp.exec); | ||
| const CMD = 'frodo config-manager push scripts --help'; | ||
| const { stdout } = await exec(CMD); | ||
|
|
||
| test("CLI help interface for 'config-manager push scripts' should be expected english", async () => { | ||
| expect(stdout).toMatchSnapshot(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`frodo config-manager push schedules "frodo config-manager push scripts -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import the scripts into forgeops" 1`] = `""`; | ||
|
|
||
| exports[`frodo config-manager push schedules "frodo config-manager push scripts -n 832807d9-fb5d-4810-88ef-6e0a1aa89924 -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import the scripts into forgeops" 1`] = `""`; | ||
|
|
||
| exports[`frodo config-manager push schedules "frodo config-manager push scripts -r alpha -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import the scripts into forgeops" 1`] = `""`; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * Follow this process to write e2e tests for the CLI project: | ||
| * | ||
| * 1. Test if all the necessary mocks for your tests already exist. | ||
| * In mock mode, run the command you want to test with the same arguments | ||
| * and parameters exactly as you want to test it, for example: | ||
| * | ||
| * $ FRODO_MOCK=1 frodo conn save https://openam-frodo-dev.forgeblocks.com/am volker.scheuber@forgerock.com Sup3rS3cr3t! | ||
| * | ||
| * If your command completes without errors and with the expected results, | ||
| * all the required mocks already exist and you are good to write your | ||
| * test and skip to step #4. | ||
| * | ||
| * If, however, your command fails and you see errors like the one below, | ||
| * you know you need to record the mock responses first: | ||
| * | ||
| * [Polly] [adapter:node-http] Recording for the following request is not found and `recordIfMissing` is `false`. | ||
| * | ||
| * 2. Record mock responses for your exact command. | ||
| * In mock record mode, run the command you want to test with the same arguments | ||
| * and parameters exactly as you want to test it, for example: | ||
| * | ||
| * $ FRODO_MOCK=record frodo conn save https://openam-frodo-dev.forgeblocks.com/am volker.scheuber@forgerock.com Sup3rS3cr3t! | ||
| * | ||
| * Wait until you see all the Polly instances (mock recording adapters) have | ||
| * shutdown before you try to run step #1 again. | ||
| * Messages like these indicate mock recording adapters shutting down: | ||
| * | ||
| * Polly instance 'conn/4' stopping in 3s... | ||
| * Polly instance 'conn/4' stopping in 2s... | ||
| * Polly instance 'conn/save/3' stopping in 3s... | ||
| * Polly instance 'conn/4' stopping in 1s... | ||
| * Polly instance 'conn/save/3' stopping in 2s... | ||
| * Polly instance 'conn/4' stopped. | ||
| * Polly instance 'conn/save/3' stopping in 1s... | ||
| * Polly instance 'conn/save/3' stopped. | ||
| * | ||
| * 3. Validate your freshly recorded mock responses are complete and working. | ||
| * Re-run the exact command you want to test in mock mode (see step #1). | ||
| * | ||
| * 4. Write your test. | ||
| * Make sure to use the exact command including number of arguments and params. | ||
| * | ||
| * 5. Commit both your test and your new recordings to the repository. | ||
| * Your tests are likely going to reside outside the frodo-lib project but | ||
| * the recordings must be committed to the frodo-lib project. | ||
| */ | ||
|
|
||
| /* | ||
| // ForgeOps | ||
| FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://nightly.gcp.forgeops.com/am frodo config-manager push scripts -D test/e2e/exports/fr-config-manager/forgeops -m forgeops | ||
| FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://nightly.gcp.forgeops.com/am frodo config-manager push scripts -r alpha -D test/e2e/exports/fr-config-manager/forgeops -m forgeops | ||
| FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://nightly.gcp.forgeops.com/am frodo config-manager push scripts -n 832807d9-fb5d-4810-88ef-6e0a1aa89924.json -D test/e2e/exports/fr-config-manager/forgeops -m forgeops | ||
|
|
||
|
|
||
| */ | ||
|
|
||
| import cp from 'child_process'; | ||
| import { promisify } from 'util'; | ||
| import { getEnv, removeAnsiEscapeCodes } from './utils/TestUtils'; | ||
| import { forgeops_connection as fc } from './utils/TestConfig'; | ||
|
|
||
| const exec = promisify(cp.exec); | ||
|
|
||
| process.env['FRODO_MOCK'] = '1'; | ||
| const forgeopsEnv = getEnv(fc); | ||
|
|
||
| const allDirectory = "test/e2e/exports/fr-config-manager/forgeops"; | ||
|
|
||
|
|
||
| describe('frodo config-manager push schedules', () => { | ||
| test(`"frodo config-manager push scripts -D ${allDirectory} -m forgeops": should import the scripts into forgeops"`, async () => { | ||
| const CMD = `frodo config-manager push scripts -D ${allDirectory} -m forgeops`; | ||
| const { stdout } = await exec(CMD, forgeopsEnv); | ||
| expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot(); | ||
| }); | ||
| test(`"frodo config-manager push scripts -r alpha -D ${allDirectory} -m forgeops": should import the scripts into forgeops"`, async () => { | ||
| const CMD = `frodo config-manager push scripts -D ${allDirectory} -m forgeops`; | ||
| const { stdout } = await exec(CMD, forgeopsEnv); | ||
| expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot(); | ||
| }); | ||
| test(`"frodo config-manager push scripts -n 832807d9-fb5d-4810-88ef-6e0a1aa89924 -D ${allDirectory} -m forgeops": should import the scripts into forgeops"`, async () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
As you can see in the image, if I use ID it can't find the script, but if I use the name it can. So, update this so that -n is the script name, not the script ID. The other thing is that you'll notice if there isn't any script change no import will be attempted. We may want to do the same thing, as scripts contain a "lastUpdated" field that indicates when an update gets made, and it updates on import whether or not the actual script value changed, so preventing the update whenever possible would make sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On this same issue, once we have the library updated to support not importing changed scripts, it turns out config-manager supports with an ENV variable turning that off, so since we'll have a flag that we set on the library side to do this, we'll need a way to enable or disable that flag on the CLI side. We'll need another option for this, probably |
||
| const CMD = `frodo config-manager push scripts -D ${allDirectory} -m forgeops`; | ||
| const { stdout } = await exec(CMD, forgeopsEnv); | ||
| expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "_id": "832807d9-fb5d-4810-88ef-6e0a1aa89924", | ||
| "context": "SCRIPTED_DECISION_NODE", | ||
| "createdBy": "id=amadmin,ou=user,ou=am-config", | ||
| "creationDate": 1774470672858, | ||
| "default": false, | ||
| "description": "testing", | ||
| "evaluatorVersion": "2.0", | ||
| "language": "JAVASCRIPT", | ||
| "lastModifiedBy": "id=amadmin,ou=user,ou=am-config", | ||
| "lastModifiedDate": 1774470672858, | ||
| "name": "testing", | ||
| "script": { | ||
| "file": "scripts-content/SCRIPTED_DECISION_NODE/testing.js" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /* | ||
| - Data made available by nodes that have already executed are available in the sharedState variable. | ||
| - The script should set outcome to either "true" or "false". | ||
| */ | ||
|
|
||
| // outcome = "true"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "_id": "01e1a3c0-038b-4c16-956a-6c9d89328cff", | ||
| "context": "AUTHENTICATION_TREE_DECISION_NODE", | ||
| "createdBy": "null", | ||
| "creationDate": 0, | ||
| "default": true, | ||
| "description": "Default global script for a scripted decision node", | ||
| "evaluatorVersion": "1.0", | ||
| "language": "JAVASCRIPT", | ||
| "lastModifiedBy": "null", | ||
| "lastModifiedDate": 0, | ||
| "name": "Authentication Tree Decision Node Script", | ||
| "script": { | ||
| "file": "scripts-content/AUTHENTICATION_TREE_DECISION_NODE/Authentication Tree Decision Node Script.js" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /* | ||
| - Data made available by nodes that have already executed are available in the sharedState variable. | ||
| - The script should set outcome to either "true" or "false". | ||
| */ | ||
|
|
||
| outcome = "true"; |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert this change