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

import { configManagerImportVariables } from '../../../configManagerOps/FrConfigVariableOps';
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 variables',
[],
deploymentTypes
);
program
.description('Import variables.')
.addOption(
new Option(
'-n, --name <name>',
'Variable name; import only the specified variable. If omitted, all variables are imported.'
)
)
.addOption(
new Option(
'-e, --env <value>',
'Value to set for the variable. Overrides .env files and environment variables.'
)
)
.action(async (host, realm, user, password, options, command) => {
command.handleDefaultArgsAndOpts(
host,
realm,
user,
password,
options,
command
);
if (await getTokens(false, true, deploymentTypes)) {
verboseMessage('Importing variables');
const outcome = await configManagerImportVariables(
options.name,
options.env
);
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
Expand Up @@ -18,6 +18,7 @@ import ServiceObjects from './config-manager-push-service-objects';
import TermsAndConditions from './config-manager-push-terms-and-conditions';
import Themes from './config-manager-push-themes';
import UiConfig from './config-manager-push-ui-config';
import Variables from './config-manager-push-variables';

export default function setup() {
const program = new FrodoStubCommand('push').description(
Expand All @@ -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(Variables().name('variables'));
return program;
}
120 changes: 117 additions & 3 deletions src/configManagerOps/FrConfigVariableOps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { frodo } from '@rockcarver/frodo-lib';
import { frodo, FrodoError } from '@rockcarver/frodo-lib';
import { VariableSkeleton } from '@rockcarver/frodo-lib/types/api/cloud/VariablesApi';
import { VariablesExportInterface } from '@rockcarver/frodo-lib/types/ops/cloud/VariablesOps';
import fs from 'fs';

import {
createProgressIndicator,
Expand All @@ -9,8 +11,8 @@ import {
} from '../utils/Console';
import { escapePlaceholders, esvToEnv } from '../utils/FrConfig';

const { getFilePath, saveJsonToFile } = frodo.utils;
const { readVariables } = frodo.cloud.variable;
const { getFilePath, saveJsonToFile, readToJson, loadEnvFile } = frodo.utils;
const { readVariables, importVariable } = frodo.cloud.variable;

/**
* Export all variables to seperate files
Expand Down Expand Up @@ -71,3 +73,115 @@ export async function configManagerExportVariables(): Promise<boolean> {
}
return false;
}


export function resolvePlaceholder(
placeholder: string,

envFile: Record<string, string> = {}
): string {
const match = placeholder.match(/^\$\{(BASE64:)?(.+)\}$/);

if (!match) {
throw new FrodoError(`Invalid placeholder format: ${placeholder}`);
}

const isBase64 = !!match[1];
const name = match[2];
let value: string;

if (name in envFile) {
value = envFile[name];
} else if (name in process.env) {
value = process.env[name];
} else {
throw new FrodoError(`No value found for ${name}`);
}

return isBase64 ? value : Buffer.from(value).toString('base64');
}
/**
* Import variables to tenant
* @returns {Promise<boolean>} true if successful, false otherwise
*/
export async function configManagerImportVariables(
variableName?: string,
value?: string
): Promise<boolean> {
const errors = [];
const spinnerId = createProgressIndicator(
'indeterminate',
0,
`Reading variables...`
);
let indicatorId: string;
try {
const variablesDir = getFilePath(`esvs/variables/`);
if (!fs.existsSync(variablesDir)) {
stopProgressIndicator(spinnerId, `No variables found`, 'fail');
return true;
}

const envFile = loadEnvFile();

const fileNames = fs
.readdirSync(variablesDir)
.filter((name) => name.toLowerCase().endsWith('.json'))
.filter((name) => !variableName || name === `${variableName}.json`);

if (fileNames.length === 0) {
stopProgressIndicator(
spinnerId,
variableName
? `No matching variable found for ${variableName}`
: 'No variables found to import',
'fail'
);
return true;
}

stopProgressIndicator(
spinnerId,
`Successfully read ${fileNames.length} variables.`,
'success'
);


indicatorId = createProgressIndicator(
'determinate',
fileNames.length,
'Importing variables'
);

for (const fileName of fileNames) {
try {
const importData = readToJson(`${variablesDir}/${fileName}`, {overrideValue: value, envFile, base64Encode: true})

if (!importData.expressionType) {
importData.expressionType = 'string';
}

const singleVariableImport: VariablesExportInterface = {
variable: { [importData._id]: importData },
};
await importVariable(importData._id, singleVariableImport);
updateProgressIndicator(
indicatorId,
`Imported variable ${importData._id}`
);
} catch (error) {
errors.push(error);
}
}

if (errors.length > 0) {
throw new FrodoError(`Error importing variables`, errors);
}
stopProgressIndicator(indicatorId, `${fileNames.length} variables imported.`);
return true;
} catch (error) {
stopProgressIndicator(indicatorId, `Error importing variables`, 'fail');
printError(error);
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 variables' should be expected english 1`] = `
"Usage: frodo config-manager push variables [options] [host] [realm] [username] [password]

[Experimental] Import variables.

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:
-e, --env <value> Value to set for the variable. Overrides .env files and
environment variables.
-n, --name <name> Variable name; import only the specified variable. If
omitted, all variables are imported.
-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 @@ -33,5 +33,6 @@ Commands:
terms-and-conditions [Experimental] Import terms and conditions.
themes [Experimental] Import themes.
ui-config [Experimental] Import UI configuration.
variables [Experimental] Import variables.
"
`;
10 changes: 10 additions & 0 deletions test/client_cli/en/config-manager-push-variables.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 variables --help';
const { stdout } = await exec(CMD);

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

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

exports[`frodo config-manager push variables "frodo config-manager push variables -D test/e2e/exports/fr-config-manager/cloud ": should import variables into cloud" 2`] = `
"Experimental feature in use: 'frodo config-manager push variables'. This feature may change without notice.
✔ Successfully read 2 variables.
• 2 variables imported.
"
`;

exports[`frodo config-manager push variables "frodo config-manager push variables -n esv-email-welcome -e "this is a third test" -D test/e2e/exports/fr-config-manager/cloud ": should import the specified variable into cloud" 1`] = `""`;

exports[`frodo config-manager push variables "frodo config-manager push variables -n esv-email-welcome -e "this is a third test" -D test/e2e/exports/fr-config-manager/cloud ": should import the specified variable into cloud" 2`] = `
"Experimental feature in use: 'frodo config-manager push variables'. This feature may change without notice.
✔ Successfully read 1 variables.
• 1 variables imported.
"
`;
86 changes: 86 additions & 0 deletions test/e2e/config-manager-push-variables.e2e.test.js
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.
*/

/*
// Cloud
FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://nightly.gcp.forgeops.com/am frodo config-manager push variables -D test/e2e/exports/fr-config-manager/cloud
FRODO_MOCK=record FRODO_NO_CACHE=1 FRODO_HOST=https://nightly.gcp.forgeops.com/am frodo config-manager push variables -n esv-email-welcome -e "this is a third test" -D test/e2e/exports/fr-config-manager/cloud
*/

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

const exec = promisify(cp.exec);

process.env['FRODO_MOCK'] = '1';
const cloudEnv = getEnv(c);

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

describe('frodo config-manager push variables', () => {
test(`"frodo config-manager push variables -D ${allDirectory} ": should import variables into cloud"`, async () => {
const CMD = `frodo config-manager push variables -D ${allDirectory} `;
const { stdout, stderr } = await exec(CMD, {
env: {
...cloudEnv.env,
ESV_EMAIL_WELCOME: "value",
ESV_CONNECTOR_TIMEOUT_RESET_COUNTER: "15"
}
});
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
expect(removeAnsiEscapeCodes(stderr)).toMatchSnapshot();
});
test(`"frodo config-manager push variables -n esv-email-welcome -e "this is a third test" -D ${allDirectory} ": should import the specified variable into cloud"`, async () => {
const CMD = `frodo config-manager push variables -n esv-email-welcome -e "this is a third test" -D ${allDirectory} `;
const { stdout, stderr } = await exec(CMD, cloudEnv);
expect(removeAnsiEscapeCodes(stdout)).toMatchSnapshot();
expect(removeAnsiEscapeCodes(stderr)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_id": "esv-connector-timeout-reset-counter",
"description": "",
"expressionType": "string",
"valueBase64": "${ESV_CONNECTOR_TIMEOUT_RESET_COUNTER}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_id": "esv-email-welcome",
"description": "Welcome email template",
"expressionType": "string",
"valueBase64": "${ESV_EMAIL_WELCOME}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"_id": "esv-welcomehub-api-host",
"description": "",
"expressionType": "string",
"valueBase64": "${ESV_WELCOMEHUB_API_HOST}"
}
Loading