-
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,38 @@ | ||
| import { Option } from 'commander'; | ||
|
|
||
| import { configManagerImportScripts } from '../../../configManagerOps/FrConfigScriptOps'; | ||
| import { getTokens } from '../../../ops/AuthenticateOps'; | ||
| import { verboseMessage } from '../../../utils/Console'; | ||
| import { FrodoCommand } from '../../FrodoCommand'; | ||
|
|
||
| export default function setup() { | ||
| const program = new FrodoCommand('frodo config-manager push scripts'); | ||
|
|
||
| program | ||
| .description('Import scripts.') | ||
| .addOption( | ||
| new Option( | ||
| '-n, --name <name>', | ||
| 'Script name, import only specified endpoint' | ||
|
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. Update this description to say |
||
| ) | ||
|
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. We are missing a flag for |
||
| ); | ||
|
|
||
| program.action(async (host, realm, user, password, options, command) => { | ||
| command.handleDefaultArgsAndOpts( | ||
| host, | ||
| realm, | ||
| user, | ||
| password, | ||
| options, | ||
| command | ||
| ); | ||
|
|
||
| const getTokensIsSuccessful = await getTokens(false, true); | ||
|
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. You can rewrite this to be: const getTokensIsSuccessful = await getTokens();Since the false, true are the defaults for it. |
||
| if (!getTokensIsSuccessful) process.exit(1); | ||
| verboseMessage('Importing scripts'); | ||
| const outcome = await configManagerImportScripts(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 }; | ||
|
|
@@ -33,6 +34,8 @@ export async function configManagerExportScript( | |
| justContent: boolean = false, // create only the content folder for the specified script, outranks justConfig | ||
| justConfig: boolean = false // create only the config folder, ignored if justContent is set | ||
| ): Promise<boolean> { | ||
| const realm = state.getRealm(); | ||
| const realmDir = realm === '/' ? 'root' : realm; | ||
| try { | ||
| const s: ScriptSkeleton = | ||
| 'ss' in criteria | ||
|
|
@@ -58,7 +61,7 @@ export async function configManagerExportScript( | |
| saveJsonToFile( | ||
| { ...s, script: fileObj }, | ||
| getFilePath( | ||
| `realms/${state.getRealm()}/scripts/scripts-config/${s._id}.json`, | ||
| `realms/${realmDir}/scripts/scripts-config/${s._id}.json`, | ||
| true | ||
| ), | ||
| false, | ||
|
|
@@ -75,15 +78,15 @@ export async function configManagerExportScript( | |
| // create script file | ||
| saveTextToFile( | ||
| decodedScript, | ||
| getFilePath(`realms/${state.getRealm()}/scripts/${relScriptPath}`, true) | ||
| getFilePath(`realms/${realmDir}/scripts/${relScriptPath}`, true) | ||
| ); | ||
|
|
||
| return true; | ||
| } catch (error) { | ||
| printError( | ||
| error, | ||
| 'scriptName' in criteria | ||
| ? `Script "${criteria.scriptName}" was not found is in the realm "${state.getRealm()}"` | ||
| ? `Script "${criteria.scriptName}" was not found is in the realm "${realmDir}"` | ||
| : '' | ||
| ); | ||
| return false; | ||
|
|
@@ -108,15 +111,17 @@ export async function configManagerExportScriptsRealms( | |
| ): Promise<boolean> { | ||
| try { | ||
| // create scripts directory if it doesnt exist even if there are no scripts, thats what fr-config-manager does | ||
| getFilePath(`realms/${state.getRealm()}/scripts/`, true); | ||
| const realm = state.getRealm(); | ||
| const realmDir = realm === '/' ? 'root' : realm; | ||
| getFilePath(`realms/${realmDir}/scripts/`, true); | ||
| let allScripts: ScriptSkeleton[] = await readScripts(); | ||
|
|
||
| // get scripts that start with prefix | ||
| if (prefix) { | ||
| allScripts = allScripts.filter((ss) => ss.name.startsWith(prefix)); | ||
| if (allScripts.length === 0) { | ||
| verboseMessage( | ||
| `There are no scripts that start with "${prefix}" in the ${state.getRealm()} realm.` | ||
| `There are no scripts that start with "${prefix}" in the ${realmDir} realm.` | ||
| ); | ||
| return true; | ||
| } | ||
|
|
@@ -127,7 +132,7 @@ export async function configManagerExportScriptsRealms( | |
| allScripts = allScripts.filter((ss) => ss.context === scriptType); | ||
| if (allScripts.length === 0) { | ||
| verboseMessage( | ||
| `There are no scripts of type "${scriptType}" in the ${state.getRealm()} realm.` | ||
| `There are no scripts of type "${scriptType}" in the ${realmDir} realm.` | ||
| ); | ||
| return true; | ||
| } | ||
|
|
@@ -145,7 +150,7 @@ export async function configManagerExportScriptsRealms( | |
| allScripts = allScripts.filter((ss) => ss.language === 'GROOVY'); | ||
| if (allScripts.length === 0) { | ||
| verboseMessage( | ||
| `There are no scripts written in groovy in the ${state.getRealm()} realm.` | ||
| `There are no scripts written in groovy in the ${realmDir} realm.` | ||
| ); | ||
| return true; | ||
| } | ||
|
|
@@ -154,7 +159,7 @@ export async function configManagerExportScriptsRealms( | |
| allScripts = allScripts.filter((ss) => ss.language === 'JAVASCRIPT'); | ||
| if (allScripts.length === 0) { | ||
| verboseMessage( | ||
| `There are no scripts written in javascript in the ${state.getRealm()} realm.` | ||
| `There are no scripts written in javascript in the ${realmDir} realm.` | ||
| ); | ||
| return true; | ||
| } | ||
|
|
@@ -170,7 +175,7 @@ export async function configManagerExportScriptsRealms( | |
| } | ||
| } | ||
| } else { | ||
| verboseMessage(`There are no scripts in the realm "${state.getRealm()}"`); | ||
| verboseMessage(`There are no scripts in the realm "${realmDir}"`); | ||
| } | ||
| return true; | ||
| } catch (error) { | ||
|
|
@@ -219,3 +224,58 @@ export async function configManagerExportScriptsAll( | |
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Import script in fr-config-manager format | ||
| * @param {string} realm option to determine which realm to import | ||
| * @param {string} name option to import a specific script by name | ||
| * @returns True if Import was successful | ||
| */ | ||
| export async function configManagerImportScripts( | ||
|
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. Add an indeterminate progress indicator to this function for importing |
||
| realm?: string, | ||
| name?: string | ||
| ): Promise<boolean> { | ||
| try { | ||
| const realmsDir = getFilePath('realms/'); | ||
| const realms: string[] = realm | ||
| ? [realm] | ||
| : fs | ||
| .readdirSync(realmsDir, { withFileTypes: true }) | ||
| .filter((entry) => entry.isDirectory()) | ||
| .map((entry) => entry.name); | ||
|
dallinjsevy marked this conversation as resolved.
|
||
|
|
||
| for (const realm of realms) { | ||
|
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. Before this for loop, we should have this check: if (name&& realms.length !== 1) {
printMessage('Error: for a named script, specify a single realm', 'error');
return false;
}Just like in config manager |
||
| state.setRealm(realm); | ||
|
|
||
| const configDir = getFilePath(`realms/${realm}/scripts/scripts-config/`); | ||
|
Comment on lines
+248
to
+250
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. You need to handle root realms here so you can import scripts to the root realm in forgeops |
||
|
|
||
| const configFiles = name ? [name] : fs.readdirSync(configDir); | ||
|
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. You want this to be: const configFiles = fs.readdirSync(configDir);The filtering by name should come by comparing names in the JSON, since the file name may not match exactly (since on export it gives it a safe file name) |
||
|
|
||
| const scripts = { script: {} }; | ||
|
|
||
| for (const file of configFiles) { | ||
| try { | ||
| const configPath = `${configDir}/${file}`; | ||
| if (!fs.existsSync(configPath)) continue; | ||
|
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. You no longer need this line |
||
| const importData = JSON.parse(fs.readFileSync(configPath, 'utf8')); | ||
|
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. Use |
||
| if (name && importData.script.name !== name) continue; | ||
|
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. Before this line, you should add a check to ensure the script name is not empty like in config manager: if (!importData.script.name || importData.script.name.trim() === "") {
printMessage(`ERROR script Id : ${importData.script._id} must have a valid (non-blank) name!`, 'error');
return false;
} |
||
| 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); | ||
| } | ||
|
|
||
|
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. Something else config-manager does is if no script is imported, it will error out, so you should do something similar: if (name&& scriptNotFound) {
printError("Script not found", 'error');
return false;
}You will need to stop the progress indicator in there as well with an error. |
||
| return true; | ||
| } catch (error) { | ||
| printError(error); | ||
|
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. Update this error message: printError(error, 'Error importing scripts.'); |
||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // 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. | ||
|
|
||
| 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. | ||
| If given without a password, and it matches the username | ||
| already stored in the connection profile for the target | ||
| host, frodo uses that profile's stored password instead of | ||
| requiring it on the command line. | ||
| password Password. | ||
|
|
||
| Options: | ||
| -n, --name <name> Script name, import only specified endpoint | ||
| -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,15 @@ | ||
| // 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 -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import the scripts into forgeops" 2`] = ` | ||
| "Experimental feature in use: 'frodo config-manager push scripts'. This feature may change without notice. | ||
| " | ||
| `; | ||
|
|
||
| exports[`frodo config-manager push schedules "frodo config-manager push scripts -n testing.js -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 testing.js -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import the scripts into forgeops" 2`] = ` | ||
| "Experimental feature in use: 'frodo config-manager push scripts'. This feature may change without notice. | ||
| " | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * 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 -n testing.js -D test/e2e/exports/fr-config-manager/forgeops -m forgeops | ||
|
|
||
|
|
||
| */ | ||
|
|
||
|
|
||
| import { getEnv, testSuccess } from './utils/TestUtils'; | ||
| import { forgeops_connection as fc } from './utils/TestConfig'; | ||
|
|
||
|
|
||
|
|
||
| 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`; | ||
| await testSuccess(CMD, { | ||
| env: { | ||
| ...forgeopsEnv.env, | ||
| FRODO_REALM: 'alpha' | ||
| } | ||
|
Comment on lines
+73
to
+76
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 instead test this with the other -n test, the reason being that with the changes I suggested the -n test will now fail because no realm is specified. Second, this test should import all realms to test importing everything, including the root realm, so we can't specify the realm here. |
||
| }); | ||
| }); | ||
|
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 line shouldn't be here, I imagine the tests would be failing because of it. |
||
| test(`"frodo config-manager push scripts -n testing.js -D ${allDirectory} -m forgeops": should import the scripts into forgeops"`, async () => { | ||
| const CMD = `frodo config-manager push scripts -n testing.js -D ${allDirectory} -m forgeops`; | ||
| await testSuccess(CMD, forgeopsEnv); | ||
|
|
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| { | ||
| "_id": "0575fa2f-12cf-42e3-8fe3-0b45d99d3aa8", | ||
| "_id": "832807d9-fb5d-4810-88ef-6e0a1aa89924", | ||
| "context": "SCRIPTED_DECISION_NODE", | ||
| "createdBy": "id=amadmin,ou=user,ou=am-config", | ||
| "creationDate": 1784302526002, | ||
| "creationDate": 1774470672858, | ||
| "default": false, | ||
| "description": "null", | ||
| "description": "testing", | ||
| "evaluatorVersion": "2.0", | ||
| "language": "JAVASCRIPT", | ||
| "lastModifiedBy": "id=amadmin,ou=user,ou=am-config", | ||
| "lastModifiedDate": 1784302526002, | ||
| "name": "PaseUsername", | ||
| "lastModifiedDate": 1774470672858, | ||
| "name": "testing", | ||
| "script": { | ||
| "file": "scripts-content/SCRIPTED_DECISION_NODE/PaseUsername.js" | ||
| "file": "scripts-content/SCRIPTED_DECISION_NODE/testing.js" | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.