UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

267 lines (256 loc) 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testables = exports.askBundleType = exports.AkamaiSaaSInstallMethods = void 0; var _snooplogg = _interopRequireDefault(require("snooplogg")); var _types = require("../../common/types"); var _chalk = _interopRequireDefault(require("chalk")); var _basicPrompts = require("../../common/basicPrompts"); var helpers = _interopRequireWildcard(require("./helpers")); var _crypto = _interopRequireDefault(require("crypto")); 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: Akamai'); class DataplaneConfig { constructor(type) { _defineProperty(this, "type", void 0); this.type = type || ''; } } class AkamaiDataplaneConfig extends DataplaneConfig { constructor(baseUrl, segmentLength, groups) { super("Akamai"); _defineProperty(this, "baseUrl", void 0); _defineProperty(this, "segmentLength", void 0); _defineProperty(this, "environments", void 0); this.baseUrl = baseUrl; this.segmentLength = segmentLength; this.environments = groups; } } class AkamaiGroups { constructor(akamai, environment) { _defineProperty(this, "akamai", void 0); _defineProperty(this, "environment", void 0); this.akamai = akamai; 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 SaasAkamaiAgentValues extends SaasAgentValues { constructor() { super(); _defineProperty(this, "baseUrl", void 0); _defineProperty(this, "clientId", void 0); _defineProperty(this, "clientSecret", void 0); _defineProperty(this, "segmentLength", void 0); _defineProperty(this, "environments", void 0); _defineProperty(this, "centralEnvironments", void 0); this.baseUrl = ''; this.clientId = ''; this.clientSecret = ''; this.segmentLength = 1; this.environments = []; this.centralEnvironments = []; } getAccessData() { let data = JSON.stringify({ clientID: this.clientId, clientSecret: this.clientSecret }); return data; } } // ConfigFiles - all the config file that are used in the setup const ConfigFiles = {}; // AkamaiSaaSPrompts - all Akamai Saas prompts to the user for input const SaasPrompts = { 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 _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; }; // // Questions for the configuration of Akamai agents // const askAkamaiBaseUrl = async () => await (0, _basicPrompts.askInput)({ msg: SaasPrompts.enterBaseUrl, validate: (0, _basicPrompts.validateRegex)(helpers.AkamaiRegexPatterns.baseURLRegex, helpers.invalidValueExampleErrMsg('baseURL', 'https://akamai.com')) }); const askAkamaiClientId = async () => await (0, _basicPrompts.askInput)({ msg: SaasPrompts.enterClientId }); const askAkamaiClientSecret = async () => await (0, _basicPrompts.askInput)({ msg: SaasPrompts.enterClientSecret }); const askAkamaiSegmentLength = async () => await (0, _basicPrompts.askInput)({ msg: SaasPrompts.enterSegmentLength, type: 'number', validate: (0, _basicPrompts.validateValueRange)(0) }); // @ts-ignore const gatewayConnectivity = async installConfig => { console.log('\nCONNECTION TO AKAMAI API GATEWAY:'); // DeploymentType let hostedAgentValues = new SaasAkamaiAgentValues(); if (installConfig.gatewayType === _types.SaaSGatewayTypes.AKAMAI) { var _installConfig$centra; log('gathering access details for akamai'); // Akamai connection details hostedAgentValues = new SaasAkamaiAgentValues(); hostedAgentValues.baseUrl = await askAkamaiBaseUrl(); hostedAgentValues.clientId = await askAkamaiClientId(); hostedAgentValues.clientSecret = await askAkamaiClientSecret(); hostedAgentValues.segmentLength = await askAkamaiSegmentLength(); 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 akamaiAgentValues = 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.AKAMAI) { const akamaiGroupObjs = (akamaiAgentValues.environments || []).map((env, idx) => new AkamaiGroups(env, akamaiAgentValues.centralEnvironments[idx])); akamaiAgentValues.dataplaneConfig = new AkamaiDataplaneConfig(akamaiAgentValues.baseUrl, akamaiAgentValues.segmentLength, akamaiGroupObjs); } // create the data plane resource let dataplaneRes = await helpers.createNewDataPlaneResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], akamaiAgentValues.dataplaneConfig); // create data plane secret resource try { await helpers.createNewDataPlaneSecretResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], dataplaneRes.name, await createEncryptedAccessData(akamaiAgentValues, 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 AkamaiSaaSInstallMethods = exports.AkamaiSaaSInstallMethods = { GetBundleType: askBundleType, GetDeploymentType: askConfigType, AskGatewayQuestions: gatewayConnectivity, FinalizeGatewayInstall: completeInstall, ConfigFiles: [], AgentNameMap: { [_types.AgentTypes.ca]: _types.AgentNames.AKAMAI_CA }, GatewayDisplay: _types.GatewayTypes.AKAMAI }; // These are the items that are not exported, but need to be for testing const testables = exports.testables = { SaasAgentValues, SaasAkamaiAgentValues, SaasPrompts, ConfigFiles, createEncryptedAccessData, // <-- already exported askEnvironments // <-- add this for testing the new logic };