UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

275 lines (264 loc) 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testables = exports.askTraceableRegion = exports.askBundleType = exports.TraceableSaaSInstallMethods = void 0; var _chalk = _interopRequireDefault(require("chalk")); var _snooplogg = _interopRequireDefault(require("snooplogg")); var _basicPrompts = require("../../common/basicPrompts"); var _types = require("../../common/types"); var _crypto = _interopRequireDefault(require("crypto")); var helpers = _interopRequireWildcard(require("./helpers")); 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 }; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } const { log } = (0, _snooplogg.default)('engage: install: agents: Traceable'); class DataplaneConfig { constructor(type) { _defineProperty(this, "type", void 0); this.type = type || ''; } } class TraceableDataplaneConfig extends DataplaneConfig { constructor(region, environments) { super("Traceable"); _defineProperty(this, "region", void 0); _defineProperty(this, "environments", void 0); this.region = region; this.environments = environments; } } class TraceableEnvironments { constructor(traceable, environment) { _defineProperty(this, "traceable", void 0); _defineProperty(this, "environment", void 0); this.traceable = traceable; this.environment = environment; } } class SaasAgentValues { constructor() { _defineProperty(this, "dataplaneConfig", void 0); _defineProperty(this, "centralConfig", void 0); this.dataplaneConfig = new DataplaneConfig(); this.centralConfig = new _types.CentralAgentConfig(); } getAccessData() { return ''; } } class SaasTraceableAgentValues extends SaasAgentValues { constructor() { super(); _defineProperty(this, "traceableToken", void 0); _defineProperty(this, "traceableRegion", void 0); _defineProperty(this, "environments", void 0); _defineProperty(this, "centralEnvironments", void 0); this.traceableToken = ''; this.traceableRegion = _types.TraceableRegionType.US; this.environments = []; this.centralEnvironments = []; } getAccessData() { let data = JSON.stringify({ token: this.traceableToken }); return data; } } // ConfigFiles - all the config file that are used in the setup const ConfigFiles = {}; // TraceableSaaSPrompts - all Traceable Saas prompts to the user for input const SaasPrompts = { configTypeMsg: 'Select the mode of installation', agentNamespace: 'Enter the namespace to use for the Amplify Traceable Agents', enterToken: 'Enter the token that the agent will use', enterRegion: 'Enter the region that the agent will use', enterEnvironments: 'Enter a Traceable environment', enterMoreEnvironments: 'Do you want to enter another mapping?', selectCentralMappingEnvironment: 'Select an Engage environment to map to the provided Traceable environment', environmentsDescription: 'Configure a mapping of Traceable environment to Engage environment that the agent will use' }; const askBundleType = async () => { return _types.BundleType.TRACEABILITY; }; exports.askBundleType = askBundleType; const askConfigType = async () => { return _types.AgentConfigTypes.HOSTED; }; const askEnvironments = async (centralEnvs, hostedAgentValues, excludeEnvironment) => { // Filter out the already-selected agent installation environment if (excludeEnvironment) { centralEnvs = centralEnvs.filter(env => env.name !== excludeEnvironment); } let askEnvs = true; let envs = []; let mappedCentralEnvs = []; console.log(_chalk.default.gray(SaasPrompts.environmentsDescription)); while (askEnvs) { const env = await (0, _basicPrompts.askInput)({ msg: SaasPrompts.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: SaasPrompts.selectCentralMappingEnvironment, choices: centralEnvs.map(e => e.name) }); if (centralMappingEnv && centralMappingEnv.toString().trim() !== "") { mappedCentralEnvs.push(centralMappingEnv); } centralEnvs = centralEnvs.filter(env => env.name !== centralMappingEnv); // Only ask if they want to continue if there are still environments available to map if (centralEnvs.length > 0) { askEnvs = (await (0, _basicPrompts.askList)({ msg: SaasPrompts.enterMoreEnvironments, default: _types.YesNo.No, choices: _types.YesNoChoices })) === _types.YesNo.Yes; } else { askEnvs = false; } } hostedAgentValues.environments = envs; hostedAgentValues.centralEnvironments = mappedCentralEnvs; }; const validateFrequency = () => input => { let val = (0, _basicPrompts.validateRegex)(helpers.frequencyRegex, helpers.invalidValueExampleErrMsg('frequency', '3d5h12m'))(input); if (typeof val === "string") { return val; } let r = input.toString().match(/^(\d*)m/); if (r) { // only minutes let mins = r[1]; if (parseInt(mins, 10) < 30) { return "Minimum frequency is 30m"; } } return true; }; // // Questions for the configuration of Traceable agent // const askToken = async () => await (0, _basicPrompts.askInput)({ msg: SaasPrompts.enterToken, allowEmptyInput: false }); const askTraceableRegion = async () => { return await (0, _basicPrompts.askList)({ msg: SaasPrompts.enterRegion, choices: Object.entries(_types.TraceableRegionType).reduce((accumulator, curr) => { return accumulator.concat({ name: curr[0], value: curr[1] }); }, []), default: _types.TraceableRegionType.US }); }; // @ts-ignore exports.askTraceableRegion = askTraceableRegion; const gatewayConnectivity = async installConfig => { console.log('\nCONNECTION TO TRACEABLE API GATEWAY:'); // DeploymentType let hostedAgentValues = new SaasTraceableAgentValues(); if (installConfig.gatewayType === _types.SaaSGatewayTypes.TRACEABLE) { var _installConfig$centra; log('gathering access details for traceable'); // Traceable connection details hostedAgentValues = new SaasTraceableAgentValues(); hostedAgentValues.traceableToken = await askToken(); hostedAgentValues.traceableRegion = await askTraceableRegion(); const centralEnvs = await helpers.getCentralEnvironments(installConfig.centralConfig.apiServerClient, installConfig.centralConfig.definitionManager); // 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(centralEnvs, hostedAgentValues, agentInstallEnv); } return hostedAgentValues; }; const generateOutput = async installConfig => { return `Install complete of hosted agent for ${installConfig.gatewayType} region`; }; const createEncryptedAccessData = async (hostedAgentValues, dataplaneRes) => { var _dataplaneRes$securit, _dataplaneRes$securit2; // grab key from data plane resource let key = ((_dataplaneRes$securit = dataplaneRes.security) === null || _dataplaneRes$securit === void 0 ? void 0 : _dataplaneRes$securit.encryptionKey) || ""; let hash = ((_dataplaneRes$securit2 = dataplaneRes.security) === null || _dataplaneRes$securit2 === void 0 ? void 0 : _dataplaneRes$securit2.encryptionHash) || ""; if (key === "" || hash === "") { throw Error(`cannot encrypt access data as the encryption key info was incomplete`); } const accessData = hostedAgentValues.getAccessData(); let encData = _crypto.default.publicEncrypt({ key: key, padding: _crypto.default.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: hash }, new Uint8Array(Buffer.from(accessData, 'utf8'))); return encData.toString("base64"); }; const completeInstall = async (installConfig, apiServerClient, defsManager) => { /** * Create agent resources */ console.log("\n"); let traceableAgentValues = installConfig.gatewayConfig; // create the environment, if necessary installConfig.centralConfig.environment = installConfig.centralConfig.ampcEnvInfo.isNew ? await helpers.createByResourceType(apiServerClient, defsManager, installConfig.centralConfig.ampcEnvInfo.name, 'Environment', 'env', { axwayManaged: installConfig.centralConfig.axwayManaged, production: installConfig.centralConfig.production }) : installConfig.centralConfig.ampcEnvInfo.name; if (installConfig.gatewayType === _types.GatewayTypes.TRACEABLE) { const traceableEnvObjs = (traceableAgentValues.environments || []).map((env, idx) => new TraceableEnvironments(env, traceableAgentValues.centralEnvironments[idx])); traceableAgentValues.dataplaneConfig = new TraceableDataplaneConfig(traceableAgentValues.traceableRegion, traceableEnvObjs); } // create the data plane resource let dataplaneRes = await helpers.createNewDataPlaneResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], traceableAgentValues.dataplaneConfig); // create data plane secret resource try { await helpers.createNewDataPlaneSecretResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], dataplaneRes.name, await createEncryptedAccessData(traceableAgentValues, dataplaneRes)); } catch (error) { log(error); console.log(_chalk.default.redBright("rolling back installation. Please check the credential data before re-running install")); if (installConfig.centralConfig.ampcEnvInfo.isNew) { await helpers.deleteByResourceType(apiServerClient, defsManager, installConfig.centralConfig.ampcEnvInfo.name, 'Environment', 'env'); } else { await helpers.deleteByResourceType(apiServerClient, defsManager, dataplaneRes.name, "Dataplane", "dp", installConfig.centralConfig.environment); } return; } // create compliance agent resource installConfig.centralConfig.taAgentName = await helpers.createNewAgentResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], _types.AgentResourceKind.ca, _types.AgentTypes.ca, installConfig.centralConfig.ampcTeamName, _types.GatewayTypeToDataPlane[installConfig.gatewayType] + " Compliance Agent", dataplaneRes.name); console.log(await generateOutput(installConfig)); }; const TraceableSaaSInstallMethods = exports.TraceableSaaSInstallMethods = { GetBundleType: askBundleType, GetDeploymentType: askConfigType, AskGatewayQuestions: gatewayConnectivity, FinalizeGatewayInstall: completeInstall, ConfigFiles: [], AgentNameMap: { [_types.AgentTypes.ca]: _types.AgentNames.TRACEABLE_CA }, GatewayDisplay: _types.GatewayTypes.TRACEABLE }; // These are the items that are not exported, but need to be for testing const testables = exports.testables = { SaasAgentValues, SaasTraceableAgentValues, SaasPrompts, ConfigFiles, createEncryptedAccessData, // <-- already exported validateFrequency, // <-- add this line for test access askEnvironments // <-- add this for testing the new logic };