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,34 @@
import { Option } from 'commander';

import { configManagerImportServices } from '../../../configManagerOps/FrConfigServiceOps';
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 services');

program
.description('Import AM authentication services.')
.addOption(
Comment thread
dallinjsevy marked this conversation as resolved.
new Option('-n, --name <name>', 'Name of the service to import.')
)
Comment thread
dallinjsevy marked this conversation as resolved.
.action(async (host, realm, user, password, options, command) => {
command.handleDefaultArgsAndOpts(
host,
realm,
user,
password,
options,
command
);

const getTokensIsSuccessful = await getTokens();
if (!getTokensIsSuccessful) process.exit(1);
verboseMessage('Importing services.');
const outcome = await configManagerImportServices(options.name);
if (!outcome) process.exitCode = 1;
});

return program;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import OrgPrivileges from './config-manager-push-org-privileges';
import PasswordPolicy from './config-manager-push-password-policy';
import Schedules from './config-manager-push-schedules';
import ServiceObjects from './config-manager-push-service-objects';
import Services from './config-manager-push-services';
import TermsAndConditions from './config-manager-push-terms-and-conditions';
import Themes from './config-manager-push-themes';
import UiConfig from './config-manager-push-ui-config';
Expand Down Expand Up @@ -45,6 +46,7 @@ export default function setup() {
program.addCommand(Authentication().name('authentication'));
program.addCommand(ConnectorDefinitions().name('connector-definitions'));
program.addCommand(ConnectorMappings().name('connector-mappings'));
program.addCommand(Services().name('services'));

return program;
}
117 changes: 114 additions & 3 deletions src/configManagerOps/FrConfigServiceOps.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { frodo, state } from '@rockcarver/frodo-lib';
import {
FullService,
ServiceNextDescendent,
} from '@rockcarver/frodo-lib/types/api/ServiceApi';
import fs from 'fs';

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

const { getFilePath, saveJsonToFile } = frodo.utils;
const { getFullServices } = frodo.service;
const { getFullServices, importService } = frodo.service;
const { DEFAULT_REALM_KEY } = frodo.utils.constants;

/**
* Export all services to separate files in fr-config-manager format
Expand All @@ -15,7 +21,7 @@ export async function configManagerExportServices(
name?
): Promise<boolean> {
try {
if (realm && realm !== '__default__realm__') {
if (realm && realm !== DEFAULT_REALM_KEY) {
const services = await getFullServices(false);
processServices(services, realm, name);
} else {
Expand All @@ -33,7 +39,8 @@ export async function configManagerExportServices(
}

async function processServices(services, realm, name) {
const fileDir = `realms/${realm}/services`;
const realmDir = realm === '/' ? 'root' : realm;
const fileDir = `realms/${realmDir}/services`;
for (const service of services) {
if (name && name !== service._type._id) {
continue;
Expand Down Expand Up @@ -61,3 +68,107 @@ async function processServices(services, realm, name) {
);
}
}

/**
* Process services for a realm in fr-config-manager format.
* @param {string} realmDir realm directory name
* @returns {Promise<FullService[]>} services with descendants attached, or [] if the directory doesn't exist
*/
async function processImportServices(realmDir: string): Promise<FullService[]> {
Comment thread
dallinjsevy marked this conversation as resolved.
const dir = getFilePath(`realms/${realmDir}/services/`);
if (!fs.existsSync(dir)) {
return [];
}

const results: FullService[] = [];
const entries = fs.readdirSync(dir, { withFileTypes: true });

for (const entry of entries) {
if (!entry.name.endsWith('.json')) {
continue;
}

const service = JSON.parse(
fs.readFileSync(`${dir}${entry.name}`, 'utf8')
) as FullService;

const baseName = entry.name.replace('.json', '');
const subDirPath = `${dir}${baseName}`;

const descendants: ServiceNextDescendent[] = [];
if (fs.existsSync(subDirPath) && fs.statSync(subDirPath).isDirectory()) {
for (const subEntry of fs.readdirSync(subDirPath, {
withFileTypes: true,
})) {
if (!subEntry.name.endsWith('.json')) {
continue;
}
descendants.push(
JSON.parse(
fs.readFileSync(`${subDirPath}/${subEntry.name}`, 'utf8')
) as ServiceNextDescendent
);
}
}
service.nextDescendents = descendants;

results.push(service);
}
Comment thread
dallinjsevy marked this conversation as resolved.

return results;
}
/**
* Import all services from disk in fr-config-manager format. Iterates every realm
* directory under realms/, mapping the 'root' directory to the '/' realm, and skips
* the root realm on cloud deployments.
* @param {string} name optional service name to import, imports all services if omitted
* @returns {Promise<boolean>} true if all imports were successful, false otherwise
*/
export async function configManagerImportServices(
name?: string
): Promise<boolean> {
try {
const realmsDir = getFilePath('realms/');
if (!fs.existsSync(realmsDir)) {
return true;
}

const realmDirs = fs
.readdirSync(realmsDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);

for (const realmDir of realmDirs) {
state.setRealm(realmDir === 'root' ? '/' : realmDir);

if (
state.getRealm() === '/' &&
state.getDeploymentType() ===
frodo.utils.constants.CLOUD_DEPLOYMENT_TYPE_KEY
) {
continue;
}

for (const service of await processImportServices(realmDir)) {
const serviceId = service._type._id;
if (name && name !== serviceId) {
continue;
}

await importService(
serviceId,
{ service: { [serviceId]: service } },
{
clean: false,
global: false,
realm: true,
}
);
}
}
return true;
} catch (error) {
printError(error);
}
return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

[Experimental] Import AM authentication services.

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> Name of the service to import.
-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,6 +31,7 @@ Commands:
password-policy [Experimental] Import password-policy objects.
schedules [Experimental] Import schedules.
service-objects [Experimental] Import service objects.
services [Experimental] Import AM authentication services.
terms-and-conditions [Experimental] Import terms and conditions.
themes [Experimental] Import themes.
ui-config [Experimental] Import UI configuration.
Expand Down
10 changes: 10 additions & 0 deletions test/client_cli/en/config-manager-push-services.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 services --help';
const { stdout } = await exec(CMD);

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

exports[`frodo config-manager push service-objects "frodo config-manager push services --name id-repositories -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import a specific service by realm into forgeops" 1`] = `""`;

exports[`frodo config-manager push service-objects "frodo config-manager push services --name id-repositories -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import a specific service by realm into forgeops" 2`] = `
"Experimental feature in use: 'frodo config-manager push services'. This feature may change without notice.
"
`;

exports[`frodo config-manager push service-objects "frodo config-manager push services -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import all services into forgeops" 1`] = `""`;

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

exports[`frodo config-manager push service-objects "frodo config-manager push services -n id-repositories -D test/e2e/exports/fr-config-manager/forgeops -m forgeops": should import a specific service by name into forgeops" 1`] = `""`;

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


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 service-objects', () => {
test(`"frodo config-manager push services -D ${allDirectory} -m forgeops": should import all services into forgeops"`, async () => {
const CMD = `frodo config-manager push services -D ${allDirectory} -m forgeops`;
await testSuccess(CMD, forgeopsEnv);
});

test(`"frodo config-manager push services -n id-repositories -D ${allDirectory} -m forgeops": should import a specific service by name into forgeops"`, async () => {
const CMD = `frodo config-manager push services -n id-repositories -D ${allDirectory} -m forgeops`;
await testSuccess(CMD, forgeopsEnv);
});

test(`"frodo config-manager push services --name id-repositories -D ${allDirectory} -m forgeops": should import a specific service by realm into forgeops"`, async () => {
const CMD = `frodo config-manager push services --name id-repositories -D ${allDirectory} -m forgeops`;
await testSuccess(CMD,{
env: {
...forgeopsEnv.env,
FRODO_REALM: 'alpha'
}
});
});
Comment thread
phalestrivir marked this conversation as resolved.
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"_id": "",
"_rev": "-1889820858",
"_type": {
"_id": "baseurl",
"collection": false,
"name": "Base URL Source"
},
"contextPath": "/am",
"fixedValue": "https://&{fqdn}",
"source": "REQUEST_VALUES"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"_id": "",
"_rev": "-1741783487",
"_type": {
"_id": "id-repositories",
"collection": false,
"name": "sunIdentityRepositoryService"
},
"sunIdRepoAttributeCombiner": "com.iplanet.am.sdk.AttributeCombiner",
"sunIdRepoAttributeValidator": [
"class=com.sun.identity.idm.server.IdRepoAttributeValidatorImpl",
"minimumPasswordLength=8",
"usernameInvalidChars=*|(|)|&|!"
]
}
Loading