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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,5 @@ test/fs_tmp/*
!test/fs_tmp/.gitkeep

temp
.vscode/settings.json
.vscode/settings.json
test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { frodo } from '@rockcarver/frodo-lib';
import { Option } from 'commander';

import { configManagerImportMappings } from '../../../configManagerOps/FrConfigConnectorMappingOps';
import { getTokens } from '../../../ops/AuthenticateOps';
import { printMessage, 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 push connector-mappings',
[],
deploymentTypes
);

program
.description('Import connector mappings.')
Comment thread
dallinjsevy marked this conversation as resolved.
.addOption(
new Option(
'-n, --name <name>',
'Connector mapping name; imports only the connector mapping with the specified name.'
)
)
.action(async (host, realm, user, password, options, command) => {
command.handleDefaultArgsAndOpts(
host,
realm,
user,
password,
options,
command
);

if (await getTokens(false, true, deploymentTypes)) {
verboseMessage('Importing connector mappings');
const outcome = await configManagerImportMappings(options.name);
if (!outcome) process.exitCode = 1;
}
// unrecognized combination of options or no options
else {
printMessage(
'Unrecognized combination of options or no options...',
'error'
);
program.help();
process.exitCode = 1;
}
});

return program;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FrodoStubCommand } from '../../FrodoCommand';
import AccessConfig from './config-manager-push-access-config';
import Audit from './config-manager-push-audit';
import ConnectorMappings from './config-manager-push-connector-mappings';
import CookieDomains from './config-manager-push-cookie-domain';
import EmailProvider from './config-manager-push-email-provider';
import EmailTemplates from './config-manager-push-email-templates';
Expand Down Expand Up @@ -39,6 +40,7 @@ export default function setup() {
program.addCommand(CookieDomains().name('cookie-domains'));
program.addCommand(ServiceObjects().name('service-objects'));
program.addCommand(UiConfig().name('ui-config'));
program.addCommand(ConnectorMappings().name('connector-mappings'));

return program;
}
82 changes: 81 additions & 1 deletion src/configManagerOps/FrConfigConnectorMappingOps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +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';

const { getFilePath, saveJsonToFile } = frodo.utils;
const { readConfigEntity } = frodo.idm.config;
const { readConfigEntity, importConfigEntities, importSubConfigEntity } =
frodo.idm.config;

function processMappings(mapping, targetDir, name) {
try {
Expand Down Expand Up @@ -58,3 +61,80 @@ export async function configManagerExportMappings(): Promise<boolean> {
}
return false;
}

/**
* Helper that recursively reads in extracted files and stores them back in the connector
* @param {Record<string, any>} obj The connector configuration
* @param {string} connectorMappingDirectory The directory where the connector resides
*/
function getExtractedFiles(obj: any, connectorMappingDirectory: string): void {
if (!obj || typeof obj !== 'object') return;
for (const key of Object.keys(obj)) {
const value = obj[key];
if (value?.type === 'text/javascript' && value.file) {
const scriptPath = path.join(connectorMappingDirectory, value.file);
if (fs.existsSync(scriptPath)) {
value.source = fs.readFileSync(scriptPath, { encoding: 'utf-8' });
delete value.file;
}
} else if (typeof value === 'object') {
getExtractedFiles(value, connectorMappingDirectory);
}
}
}

/**
* Helper that returns the import data for a connector mapping given the file where it is saved
* @param {string} file The file where the connector mapping is saved
* @returns {object} The connector mapping data from the file, including data from any extracted files
*/
function getConnectorMappingImportData(file: string): object {
const readManagedObject = fs.readFileSync(file, 'utf-8');
const importData = JSON.parse(readManagedObject);
const connectorMappingDirectory = path.dirname(file);
getExtractedFiles(importData, connectorMappingDirectory);
return importData;
}

/**
* Import all mappings in fr-config-manager format
Comment thread
dallinjsevy marked this conversation as resolved.
* @param {string} [name] optional connector name to import
* @returns {Promise<boolean>} true if successful, false otherwise
*/
export async function configManagerImportMappings(
name?: string
): Promise<boolean> {
try {
if (name) {
const jsonFilePath = getFilePath(`sync/mappings/${name}/${name}.json`);
const importData = getConnectorMappingImportData(jsonFilePath) as any;
await importSubConfigEntity('sync', importData);
return true;
} else {
const mappingDir = getFilePath('sync/mappings');
const mappingFiles = fs.readdirSync(mappingDir);
const importMappingData = {
idm: { sync: { _id: 'sync', mappings: [] as any } },
};
for (const mappingFile of mappingFiles) {
const jsonFilePath = getFilePath(
`sync/mappings/${mappingFile}/${mappingFile}.json`
);
const importData = getConnectorMappingImportData(jsonFilePath) as any;
if (importData.file) {
const scriptPath = getFilePath(
`sync/mappings/${mappingFile}/${importData.file}`
);
importData.source = fs.readFileSync(scriptPath, 'utf8');
delete importData.file;
}
importMappingData.idm.sync.mappings.push(importData);
}
await importConfigEntities(importMappingData);
}
return true;
} catch (error) {
printError(error, `Error importing mappings from files`);
}
return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

[Experimental] Import connector mappings.

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.

Deployment: ForgeOps-only

Options:
-n, --name <name> Connector mapping name; imports only the connector mapping
with the specified 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 @@ -31,5 +31,8 @@ Commands:
terms-and-conditions [Experimental] Import terms and conditions.
themes [Experimental] Import themes.
ui-config [Experimental] Import UI configuration.

(ForgeOps-only):
connector-mappings [Experimental] Import connector mappings.
"
`;
10 changes: 10 additions & 0 deletions test/client_cli/en/config-manager-push-connector-mappings.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 connector-mappings --help';
const { stdout } = await exec(CMD);

test("CLI help interface for 'config-manager push connector-mappings' 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 connector mappings "frodo config-manager push connector-mappings -D test/e2e/exports/fr-config-manager/forgeops -m forgeops ": should import the connector mappings into forgeops" 1`] = `""`;

exports[`frodo config-manager push connector mappings "frodo config-manager push connector-mappings -D test/e2e/exports/fr-config-manager/forgeops -m forgeops ": should import the connector mappings into forgeops" 2`] = `
"Experimental feature in use: 'frodo config-manager push connector-mappings'. This feature may change without notice.
"
`;

exports[`frodo config-manager push connector mappings "frodo config-manager push connector-mappings -n UserToUserJavascriptSync -D test/e2e/exports/fr-config-manager/forgeops -m forgeops ": should import a specific connector mapping by name into forgeops" 1`] = `""`;

exports[`frodo config-manager push connector mappings "frodo config-manager push connector-mappings -n UserToUserJavascriptSync -D test/e2e/exports/fr-config-manager/forgeops -m forgeops ": should import a specific connector mapping by name into forgeops" 2`] = `
"Experimental feature in use: 'frodo config-manager push connector-mappings'. This feature may change without notice.
"
`;
82 changes: 82 additions & 0 deletions test/e2e/config-manager-push-connector-mappings.e2e.test.js
Comment thread
dallinjsevy marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* 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 [email protected] 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 [email protected] 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 connector-mappings -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 connector-mappings -n UserToUserJavascriptSync -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';
process.env['FRODO_NO_CACHE'] = '1';

const forgeopsEnv = getEnv(fc);

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

describe('frodo config-manager push connector mappings', () => {
test(`"frodo config-manager push connector-mappings -D ${allDirectory} -m forgeops ": should import the connector mappings into forgeops"`, async () => {
const CMD = `frodo config-manager push connector-mappings -D ${allDirectory} -m forgeops `;
const { stdout, stderr } = await exec(CMD, forgeopsEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
expect(removeAnsiEscapeCodes(stderr)).toMatchSnapshot();
});
test(`"frodo config-manager push connector-mappings -n UserToUserJavascriptSync -D ${allDirectory} -m forgeops ": should import a specific connector mapping by name into forgeops"`, async () => {
const CMD = `frodo config-manager push connector-mappings -n UserToUserJavascriptSync -D ${allDirectory} -m forgeops `;
const { stdout, stderr } = await exec(CMD, forgeopsEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
expect(removeAnsiEscapeCodes(stderr)).toMatchSnapshot();
});
});
Loading