UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

256 lines (250 loc) 13.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testables = exports.gatewayConnectivity = exports.completeInstall = exports.askConfigType = exports.askBundleType = exports.amplifyAgentsNs = exports.ConfigFiles = exports.AkamaiInstallMethods = void 0; var _chalk = _interopRequireDefault(require("chalk")); var _snooplogg = _interopRequireDefault(require("snooplogg")); var _Kubectl = require("../../common/Kubectl"); var _basicPrompts = require("../../common/basicPrompts"); var _inputs = require("./helpers/inputs"); var _types = require("../../common/types"); var helpers = _interopRequireWildcard(require("./helpers")); var _akamaiTemplates = require("./helpers/templates/akamaiTemplates"); var _utils = require("../../common/utils"); var _agents = require("./agents"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // @ts-ignore const { log } = (0, _snooplogg.default)('central: install: agents: Akamai'); const caImage = `${_types.PublicDockerRepoBaseUrl}${_types.BasePaths.DockerAgentPublicRepo}/${_types.AgentNames.AKAMAI_CA}`; const amplifyAgentsNs = exports.amplifyAgentsNs = 'amplify-agents'; // ConfigFiles - all the config file that are used in the setup const ConfigFiles = exports.ConfigFiles = { helmOverride: 'agent-overrides.yaml', agentEnvVars: `${helpers.configFiles.AGENT_ENV_VARS}` }; const prompts = { configTypeMsg: 'Select the mode of installation', agentNamespace: 'Enter the namespace to use for the Amplify Akamai Agents', enterBaseUrl: 'Enter the Akamai Base URL', enterClientId: 'Enter the Akamai Client ID', enterClientSecret: 'Enter the Akamai Client Secret', enterSegmentLength: 'Enter the Akamai Segment Length', enterEnvironments: 'Enter an Akamai environment', enterMoreEnvironments: 'Do you want to enter another mapping?', selectCentralMappingEnvironment: 'Select an Engage environment to map to the provided Akamai environment', environmentsDescription: 'Configure a mapping of Akamai environment to Engage environment that the agent will use' }; const askBundleType = async () => { return _types.BundleType.TRACEABILITY; }; exports.askBundleType = askBundleType; const askConfigType = async () => { return await (0, _basicPrompts.askList)({ msg: prompts.configTypeMsg, choices: [_types.AgentConfigTypes.DOCKERIZED, _types.AgentConfigTypes.HELM] }); }; // // Questions for the configuration of Akamai agents // exports.askConfigType = askConfigType; const askAkamaiBaseUrl = async () => await (0, _basicPrompts.askInput)({ msg: prompts.enterBaseUrl, validate: (0, _basicPrompts.validateRegex)(helpers.AkamaiRegexPatterns.baseURLRegex, helpers.invalidValueExampleErrMsg('baseURL', 'https://akamai.com')) }); const askAkamaiClientId = async () => await (0, _basicPrompts.askInput)({ msg: prompts.enterClientId }); const askAkamaiClientSecret = async () => await (0, _basicPrompts.askInput)({ msg: prompts.enterClientSecret }); const askAkamaiSegmentLength = async () => await (0, _basicPrompts.askInput)({ msg: prompts.enterSegmentLength, type: 'number', validate: (0, _basicPrompts.validateValueRange)(0) }); const askEnvironments = async (centralEnvs, akamaiAgentValues, excludeEnvironment) => { // Filter out the already-selected agent installation environment if (excludeEnvironment) { centralEnvs = centralEnvs.filter(env => env.name !== excludeEnvironment); } // If no central environments are available, exit the installation if (centralEnvs.length === 0) { console.log(_chalk.default.red('Installation cannot proceed: No Engage environments are available for mapping.')); console.log(_chalk.default.yellow('Please create at least one Engage environment before installing the Akamai agent.')); console.log(_chalk.default.gray('You can create an environment using: axway engage create environment')); process.exit(1); } let askEnvs = true; let envs = []; let mappedCentralEnvs = []; console.log(_chalk.default.gray(prompts.environmentsDescription)); while (askEnvs) { const env = await (0, _basicPrompts.askInput)({ msg: prompts.enterEnvironments, allowEmptyInput: true }); if (envs.length === 0 && (!env || env.toString().trim() === "")) { break; } if (env && env.toString().trim() !== "") { envs.push(env); } const centralMappingEnv = await (0, _basicPrompts.askList)({ msg: prompts.selectCentralMappingEnvironment, choices: centralEnvs.map(e => e.name) }); if (centralMappingEnv && centralMappingEnv.toString().trim() !== "") { mappedCentralEnvs.push(centralMappingEnv); } // Remove the selected environment from available choices for next iteration centralEnvs = centralEnvs.filter(env => env.name !== centralMappingEnv); // Only ask to continue if there are remaining central environments if (centralEnvs.length > 0) { askEnvs = (await (0, _basicPrompts.askList)({ msg: prompts.enterMoreEnvironments, default: _types.YesNo.No, choices: _types.YesNoChoices })) === _types.YesNo.Yes; } else { askEnvs = false; // Auto-stop when no environments remain } } akamaiAgentValues.environments = envs; akamaiAgentValues.centralEnvironments = mappedCentralEnvs; }; const gatewayConnectivity = async installConfig => { let akamaiAgentValues = new _akamaiTemplates.AkamaiAgentValues(); if (installConfig.switches.isHelmInstall) { console.log(_chalk.default.gray(`The Amplify Akamai Agent needs to be deployed to your Kubernetes cluster to discover APIs for publishing to Amplify Central.`)); const { error } = await _Kubectl.kubectl.isInstalled(); if (error) { throw new Error(`Kubectl is required to fill out the following prompts. It appears to be missing or misconfigured.\n${error}`); } akamaiAgentValues.namespace = await (0, _inputs.askNamespace)(prompts.agentNamespace, amplifyAgentsNs); } if (installConfig.switches.isDockerInstall) { console.log('\nCONNECTION TO AKAMAI API GATEWAY:'); console.log(_chalk.default.gray("The Compliance Agent needs to connect to the Akamai API Gateway to discover API's for publishing to Amplify Central.")); } akamaiAgentValues.baseUrl = await askAkamaiBaseUrl(); akamaiAgentValues.clientId = await askAkamaiClientId(); akamaiAgentValues.clientSecret = await askAkamaiClientSecret(); akamaiAgentValues.segmentLength = await askAkamaiSegmentLength(); await helpers.getCentralEnvironments(installConfig.centralConfig.apiServerClient, installConfig.centralConfig.definitionManager).then(async envs => { if (envs) { var _installConfig$centra; // Pass the already-selected agent installation environment to exclude it from mapping choices const agentInstallEnv = (_installConfig$centra = installConfig.centralConfig.ampcEnvInfo) === null || _installConfig$centra === void 0 ? void 0 : _installConfig$centra.name; await askEnvironments(envs, akamaiAgentValues, agentInstallEnv); } }); return akamaiAgentValues; }; exports.gatewayConnectivity = gatewayConnectivity; const dockerSuccessMsg = installConfig => { let dockerInfo; const runAgentLinuxMsg = `docker run -it --env-file ${helpers.pwd}/${helpers.configFiles.AGENT_ENV_VARS} -v ${helpers.pwd}:/keys ${helpers.eolChar}`; const runAgentWinMsg = `docker run -it --env-file ${helpers.pwdWin}/${helpers.configFiles.AGENT_ENV_VARS} -v ${helpers.pwdWin}:/keys ${helpers.eolCharWin}`; const startAgentLinuxMsg = `\nStart the Akamai Agent on a Linux based machine`; const startAgentWinMsg = `\nStart the Akamai Agent on a Windows machine`; dockerInfo = `To utilize the agent, pull the latest Docker image and run it using the appropriate supplied environment file, (${helpers.configFiles.AGENT_ENV_VARS}):`; console.log(_chalk.default.whiteBright(dockerInfo), '\n'); const caImageVersion = `${caImage}:${installConfig.caVersion}`; console.log(_chalk.default.white('Pull the latest image of the Agent:')); console.log(_chalk.default.cyan(`docker pull ${caImageVersion}`)); console.log(_chalk.default.white(_utils.isWindows ? startAgentWinMsg : startAgentLinuxMsg)); console.log(_chalk.default.cyan(_utils.isWindows ? runAgentWinMsg : runAgentLinuxMsg)); console.log('\t', _chalk.default.cyan(`-v /data ${caImageVersion}`), '\n'); }; const helmSuccessMsg = namespace => { console.log(`Akamai Agent override file has been placed at ${process.cwd()}/${ConfigFiles.helmOverride}`); helpers.helmImageSecretInfo(namespace); let agentHelmInfo = new Set(); agentHelmInfo.add({ helmReleaseName: 'akamai-agent', helmChartName: ' axway/akamai-agent', overrideFileName: ConfigFiles.helmOverride, imageSecretOverrides: `--set image.pullSecret=<image-pull-secret-name>` }); helpers.helmInstallInfo('Akamai', namespace, agentHelmInfo); }; const generateSuccessHelpMsg = installConfig => { const akamaiAgentValues = installConfig.gatewayConfig; const configType = installConfig.deploymentType; if (installConfig.centralConfig.ampcDosaInfo.isNew && !installConfig.switches.isHelmInstall) { console.log(_chalk.default.yellow(_agents.svcAccMsg)); } if (configType === _types.AgentConfigTypes.DOCKERIZED) { dockerSuccessMsg(installConfig); } else if (configType === _types.AgentConfigTypes.HELM) { helmSuccessMsg(akamaiAgentValues.namespace.name); } console.log('Configuration file(s) have been successfully created.\n'); console.log(_chalk.default.gray(`\nAdditional information about agent features can be found here:\n${helpers.agentsDocsUrl.AKAMAI}`)); }; const completeInstall = async installConfig => { // Add final settings to AkamaiAgentValues const akamaiAgentValues = installConfig.gatewayConfig; akamaiAgentValues.centralConfig = installConfig.centralConfig; akamaiAgentValues.traceabilityConfig = installConfig.traceabilityConfig; if (installConfig.switches.isHelmInstall) { akamaiAgentValues.akamaiSecret = helpers.amplifyAgentsCredsSecret; akamaiAgentValues.agentKeysSecret = helpers.amplifyAgentsKeysSecret; if (akamaiAgentValues.namespace.isNew) { await helpers.createNamespace(akamaiAgentValues.namespace.name); } await helpers.createSecret(akamaiAgentValues.namespace.name, helpers.amplifyAgentsKeysSecret, async () => { if (installConfig.centralConfig.ampcDosaInfo.isNew) { console.log(_chalk.default.yellow(`The secret '${helpers.amplifyAgentsKeysSecret}' will be created with the same "private_key.pem" and "public_key.pem" that was auto generated to create the Service Account.`)); } await helpers.createAmplifyAgentKeysSecret(akamaiAgentValues.namespace.name, helpers.amplifyAgentsKeysSecret, 'publicKey', akamaiAgentValues.centralConfig.dosaAccount.publicKey, 'privateKey', akamaiAgentValues.centralConfig.dosaAccount.privateKey); }); await helpers.createSecret(akamaiAgentValues.namespace.name, helpers.amplifyAgentsCredsSecret, async () => { await createAkamaiCredsSecret(akamaiAgentValues.namespace.name, helpers.amplifyAgentsCredsSecret, akamaiAgentValues.akamaiSecret, akamaiAgentValues.agentKeysSecret); }); } console.log('Generating the configuration file(s)...'); if (installConfig.switches.isDockerInstall) { (0, _utils.writeTemplates)(ConfigFiles.agentEnvVars, akamaiAgentValues, helpers.akamaiEnvVarTemplate); } else if (installConfig.switches.isHelmInstall) { (0, _utils.writeTemplates)(ConfigFiles.helmOverride, akamaiAgentValues, helpers.akamaiHelmOverrideTemplate); } generateSuccessHelpMsg(installConfig); }; exports.completeInstall = completeInstall; const createAkamaiCredsSecret = async (namespace, secretName, clientID, clientSecret) => { const { error } = await _Kubectl.kubectl.create('secret', `-n ${namespace} generic ${secretName} \ --from-literal=clientID=${clientID} \ --from-literal=clientSecret=${clientSecret}`); if (error) { throw Error(error); } console.log(`Created ${secretName} in the ${namespace} namespace.`); }; const AkamaiInstallMethods = exports.AkamaiInstallMethods = { GetBundleType: askBundleType, GetDeploymentType: askConfigType, AskGatewayQuestions: gatewayConnectivity, FinalizeGatewayInstall: completeInstall, ConfigFiles: Object.values(ConfigFiles), AgentNameMap: { [_types.AgentTypes.ca]: _types.AgentNames.AKAMAI_CA }, GatewayDisplay: _types.GatewayTypes.AKAMAI }; const testables = exports.testables = { prompts, ConfigFiles, askEnvironments, gatewayConnectivity, createAkamaiCredsSecret };