Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { frodo } from '@rockcarver/frodo-lib';
import { Option } from 'commander';

import { configManagerImportIgaWorkflows } from '../../../configManagerOps/FrConfigIgaWorkflowsOps';
import { getTokens } from '../../../ops/AuthenticateOps';
import { verboseMessage } from '../../../utils/Console';
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,
];

export default function setup() {
const program = new FrodoCommand(
'frodo config-manager pull iga-workflows',
[],
deploymentTypes
);
program
.description('Import iga-workflows.')
.addOption(
new Option(
'-n, --name <name>',
'Workflow name. Only import the workflow with this name.'
)
)
.addOption(
new Option('--draft', 'Push as draft version instead of publishing.')
)
.action(async (host, realm, user, password, options, command) => {
command.handleDefaultArgsAndOpts(
host,
realm,
user,
password,
options,
command
);

if (await getTokens(false, true, deploymentTypes)) {
verboseMessage('Importing config entity iga-workflows');
const outcome = await configManagerImportIgaWorkflows(
options.name,
options.draft
);
if (!outcome) process.exitCode = 1;
}
});
return program;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CookieDomains from './config-manager-push-cookie-domain';
import EmailProvider from './config-manager-push-email-provider';
import EmailTemplates from './config-manager-push-email-templates';
import Endpoints from './config-manager-push-endpoints';
import IgaWorkflows from './config-manager-push-iga-workflows';
import InternalRoles from './config-manager-push-internal-roles';
import Kba from './config-manager-push-kba';
import Locales from './config-manager-push-locales';
Expand Down Expand Up @@ -43,6 +44,7 @@ export default function setup() {
program.addCommand(UiConfig().name('ui-config'));
program.addCommand(Authentication().name('authentication'));
program.addCommand(ConnectorDefinitions().name('connector-definitions'));
program.addCommand(IgaWorkflows().name('iga-workflows'));

return program;
}
89 changes: 87 additions & 2 deletions src/configManagerOps/FrConfigIgaWorkflowsOps.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { frodo } from '@rockcarver/frodo-lib';
import fs from 'fs';
import path from 'path';

import { extractFrConfigDataToFile } from '../utils/Config';
import { printError } from '../utils/Console';
import { printError, verboseMessage } from '../utils/Console';
import { safeFileName } from '../utils/FrConfig';

const { saveJsonToFile, getFilePath } = frodo.utils;
const { readWorkflows } = frodo.cloud.iga.workflow;
const { readWorkflows, importWorkflows } = frodo.cloud.iga.workflow;

/**
* Export IGA workflows in fr-config-manager format.
Expand Down Expand Up @@ -68,3 +70,86 @@ async function processIgaWorkflow(workflow, fileDir) {
printError(err);
}
}

/**
* Import IGA workflows in fr-config-manager format.
* @param {string} name optional workflow name to filter by
* @param {boolean} draft if true, will import workflow as draft
* @returns {Promise<boolean>} a promise that resolves to true if successful, false otherwise
*/
export async function configManagerImportIgaWorkflows(
name?: string,
draft: boolean = false
): Promise<boolean> {
try {
const workflowsPath = getFilePath('iga/workflows');
let workflowDirs = [];

if (name) {
const workflowName = `${workflowsPath}/${name}`;
workflowDirs = [workflowName];
} else {
workflowDirs = fs
.readdirSync(workflowsPath)
.map((dirName) => `${workflowsPath}/${dirName}`);
}

for (const workflowDir of workflowDirs) {
const workflow = processWorkflows(workflowDir);
if (!workflow.mutable) {
verboseMessage(`Skipping immutable workflow ${workflow.name}`);
continue;
}
const status = draft ? 'draft' : 'published';
workflow.status = status;
const importData = {
workflow: { [workflow.id]: { [status]: workflow } },
} as any;
await importWorkflows(workflow.id, importData);
}
return true;
} catch (error) {
printError(error, 'Error importing iga-workflows to files');
}
return false;
}

function processWorkflows(workflowDir: string) {
try {
const workflowName = path.parse(workflowDir).base;
const workflowFile = path.join(workflowDir, `${workflowName}.json`);
const workflow = JSON.parse(fs.readFileSync(workflowFile, 'utf8'));

const stepsDir = path.join(workflowDir, 'steps');
const steps = [];
if (fs.existsSync(stepsDir)) {
const stepDirs = fs
.readdirSync(stepsDir, { withFileTypes: true })
.map((dirent) => path.join(stepsDir, dirent.name));

for (const stepDir of stepDirs) {
const stepName = path.parse(stepDir).base;
const stepFile = path.join(stepDir, `${stepName}.json`);
const step = JSON.parse(fs.readFileSync(stepFile, 'utf8'));
const stepBody = step?.[step?.type];
if (
stepBody &&
typeof stepBody === 'object' &&
stepBody.script &&
typeof stepBody.script === 'object' &&
typeof stepBody.script.file === 'string'
) {
const filePath = path.join(stepDir, stepBody.script.file);
if (fs.existsSync(filePath)) {
stepBody.script = fs.readFileSync(filePath, 'utf8');
}
}
steps.push(step);
}
workflow.steps = steps;
}
return workflow;
} catch (error) {
printError(error, 'Error importing iga-workflows to files');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CLI help interface for 'config-manager push iga-workflows' should be expected english 1`] = `
"Usage: frodo config-manager push iga-workflows [options] [host] [realm] [username] [password]

[Experimental] Import iga-workflows.

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:
--draft Push as draft version instead of publishing.
-n, --name <name> Workflow name. Only import the workflow with this name.
-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
Expand Up @@ -22,6 +22,7 @@ Commands:
email-templates [Experimental] Import email template objects.
endpoints [Experimental] Import custom endpoints objects.
help display help for command
iga-workflows [Experimental] Import iga-workflows.
internal-roles [Experimental] Import internal roles.
kba [Experimental] Import kba configuration.
locales [Experimental] Import custom locales objects.
Expand Down
10 changes: 10 additions & 0 deletions test/client_cli/en/config-manager-push-iga-workflows.test.js
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 iga-workflows --help';
const { stdout } = await exec(CMD);

test("CLI help interface for 'config-manager push iga-workflows' 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 iga-workflows "frodo config-manager push iga-workflows --draft -D test/e2e/exports/fr-config-manager/cloud ": should import the iga workflows into cloud tenant as draft workflows" 1`] = `""`;

exports[`frodo config-manager push iga-workflows "frodo config-manager push iga-workflows -D test/e2e/exports/fr-config-manager/cloud ": should import the iga workflows into cloud tenant" 1`] = `""`;

exports[`frodo config-manager push iga-workflows "frodo config-manager push iga-workflows -n test_workflow_2 -D test/e2e/exports/fr-config-manager/cloud ": should import a specific iga workflows by name into cloud tenant" 1`] = `""`;
85 changes: 85 additions & 0 deletions test/e2e/config-manager-push-iga-workflows.e2e.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* 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.
*/

/*
// Cloud
FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://openam-trivir-fairfax.forgeblocks.com/am frodo config-manager push iga-workflows -D test/e2e/exports/fr-config-manager/cloud
FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://openam-trivir-fairfax.forgeblocks.com/am frodo config-manager push iga-workflows --draft -D test/e2e/exports/fr-config-manager/cloud
FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://openam-trivir-fairfax.forgeblocks.com/am frodo config-manager push iga-workflows -n test_workflow_2 -D test/e2e/exports/fr-config-manager/cloud

*/

import cp from 'child_process';
import { promisify } from 'util';
import { getEnv, removeAnsiEscapeCodes } from './utils/TestUtils';
import { iga_connection as ic } from './utils/TestConfig';

const exec = promisify(cp.exec);

process.env['FRODO_MOCK'] = '1';
const igaEnv = getEnv(ic);

const allDirectory = "test/e2e/exports/fr-config-manager/cloud";

describe('frodo config-manager push iga-workflows', () => {
test(`"frodo config-manager push iga-workflows -D ${allDirectory} ": should import the iga workflows into cloud tenant"`, async () => {
const CMD = `frodo config-manager push iga-workflows -D ${allDirectory} `;
const { stdout } = await exec(CMD, igaEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
});
test(`"frodo config-manager push iga-workflows --draft -D ${allDirectory} ": should import the iga workflows into cloud tenant as draft workflows"`, async () => {
const CMD = `frodo config-manager push iga-workflows -D ${allDirectory} `;
const { stdout } = await exec(CMD, igaEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
});
test(`"frodo config-manager push iga-workflows -n test_workflow_2 -D ${allDirectory} ": should import a specific iga workflows by name into cloud tenant"`, async () => {
const CMD = `frodo config-manager push iga-workflows -n test_workflow_2 -D ${allDirectory} `;
const { stdout } = await exec(CMD, igaEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"displayName": "Wait Task",
"name": "waitTask-4d9223fb79c5",
"type": "waitTask",
"waitTask": {
"events": null,
"nextStep": [
{
"condition": "true",
"outcome": "COMPLETE",
"step": null
}
],
"resumeDate": {
"isExpression": true,
"value": "(new Date(new Date().getTime()+(1*30*24*60*60*1000))).toISOString()"
}
}
}
Loading