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,58 @@
import { frodo } from '@rockcarver/frodo-lib';
import { Option } from 'commander';

import { configManagerImportCustomNode } from '../../../configManagerOps/FrConfigCustomNodesOps';
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 custom-nodes',
[],
deploymentTypes
);

program
.description('Export custom nodes.')
.addOption(
new Option(
'-n, --node-name <node-name>',
'Custom node display name. If specified, only one custom node is exported.'
)
)
.action(async (host, realm, user, password, options, command) => {
command.handleDefaultArgsAndOpts(
host,
realm,
user,
password,
options,
command
);

if (await getTokens(false, true, deploymentTypes)) {
if (options.nodeName) {
verboseMessage(
`Fetching custom node with name '${options.nodeName}'`
);
} else {
verboseMessage('Fetching custom nodes');
}
const outcome = await configManagerImportCustomNode(options.nodeName);
if (!outcome) process.exitCode = 1;
} else {
process.exitCode = 1;
}
});

return program;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Audit from './config-manager-push-audit';
import Authentication from './config-manager-push-authentication';
import ConnectorDefinitions from './config-manager-push-connector-definitions';
import CookieDomains from './config-manager-push-cookie-domain';
import CustomNodes from './config-manager-push-custom-nodes';
import EmailProvider from './config-manager-push-email-provider';
import EmailTemplates from './config-manager-push-email-templates';
import Endpoints from './config-manager-push-endpoints';
Expand Down Expand Up @@ -43,6 +44,6 @@ export default function setup() {
program.addCommand(UiConfig().name('ui-config'));
program.addCommand(Authentication().name('authentication'));
program.addCommand(ConnectorDefinitions().name('connector-definitions'));

program.addCommand(CustomNodes().name('custom-nodes'));
return program;
}
55 changes: 54 additions & 1 deletion src/configManagerOps/FrConfigCustomNodesOps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { frodo } from '@rockcarver/frodo-lib';
import fs from 'fs';

import { printError } from '../utils/Console';

const { saveJsonToFile, getFilePath, saveTextToFile } = frodo.utils;
const { readCustomNode, readCustomNodes } = frodo.authn.node;
const { readCustomNode, readCustomNodes, importCustomNodes } = frodo.authn.node;

/**
* Export all custom nodes to 'custom-nodes/nodes' directory.
Expand Down Expand Up @@ -45,3 +46,55 @@ export async function configManagerExportCustomNodes(
return false;
}
}

/**
* Import all custom nodes to specified tenant.
* @param {string} name Optional display name of a custom node to import. If not provided, all custom nodes will be exported.
* @returns {Promise<boolean>} True if export was successful
*/

export async function configManagerImportCustomNode(
nodeName?: string
): Promise<boolean> {
try {
if (nodeName) {
const nodeDir = getFilePath(`custom-nodes/nodes/${nodeName}`);
const jsonFilePath = `${nodeDir}/${nodeName}.json`;
const scriptFilePath = `${nodeDir}/${nodeName}.js`;
const customNodeData = { nodeTypes: {} };

const importData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));

if (fs.existsSync(scriptFilePath)) {
importData.script = fs.readFileSync(scriptFilePath, 'utf8');
}

customNodeData.nodeTypes[importData._id] = importData;
await importCustomNodes(undefined, nodeName, customNodeData);
} else {
const nodesDir = getFilePath(`custom-nodes/nodes`);
const nodeFolders = fs.readdirSync(nodesDir);
const customNodeData = { nodeTypes: {} };

for (const nodeFolder of nodeFolders) {
const jsonFilePath = `${nodesDir}/${nodeFolder}/${nodeFolder}.json`;
const scriptFilePath = `${nodesDir}/${nodeFolder}/${nodeFolder}.js`;

const importData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));

if (fs.existsSync(scriptFilePath)) {
importData.script = fs.readFileSync(scriptFilePath, 'utf8');
}

customNodeData.nodeTypes[importData._id] = importData;

await importCustomNodes(undefined, nodeName, customNodeData);
}
}

return true;
} catch (error) {
printError(error, `Error importing custom nodes`);
}
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 custom-nodes' should be expected english 1`] = `
"Usage: frodo config-manager push custom-nodes [options] [host] [realm] [username] [password]

[Experimental] Export custom nodes.

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, --node-name <node-name> Custom node display name. If specified, only one
custom node is exported.
-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 @@ -18,6 +18,7 @@ Commands:
authentication [Experimental] Import authentication objects.
connector-definitions [Experimental] Import connector definitions.
cookie-domains [Experimental] Import cookie domains.
custom-nodes [Experimental] Export custom nodes.
email-provider [Experimental] Import email provider configuration.
email-templates [Experimental] Import email template objects.
endpoints [Experimental] Import custom endpoints objects.
Expand Down
10 changes: 10 additions & 0 deletions test/client_cli/en/config-manager-push-custom-nodes.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 custom-nodes --help';
const { stdout } = await exec(CMD);

test("CLI help interface for 'config-manager push custom-nodes' should be expected english", async () => {
expect(stdout).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`frodo config-manager push custom-nodes "frodo config-manager push custom-nodes -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import the custom-nodes into forgeops" 1`] = `""`;

exports[`frodo config-manager push custom-nodes "frodo config-manager push custom-nodes -n "Display Callback" -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import a specific custom node by name into forgeops" 1`] = `""`;
78 changes: 78 additions & 0 deletions test/e2e/config-manager-push-custom-nodes.e2e.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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 custom-nodes -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 custom-nodes -n "Display Callback" -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 custom-nodes', () => {
test(`"frodo config-manager push custom-nodes -D ${allDirectory} -m forgeops": should import the custom-nodes into forgeops"`, async () => {
const CMD = `frodo config-manager push custom-nodes -D ${allDirectory} -m forgeops`;
const { stdout } = await exec(CMD, forgeopsEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
});
test(`"frodo config-manager push custom-nodes -n "Display Callback" -D ${allDirectory} -m forgeops": should import a specific custom node by name into forgeops"`, async () => {
const CMD = `frodo config-manager push custom-nodes -n "Display Callback" -D ${allDirectory} -m forgeops`;
const { stdout } = await exec(CMD, forgeopsEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
});
});
Loading