@axway/axway-central-cli
Version:
Manage APIs, services and publish to the Amplify Marketplace
272 lines (259 loc) • 12.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.testables = exports.askBundleType = exports.SwaggerHubSaaSInstallMethods = void 0;
var _chalk = _interopRequireDefault(require("chalk"));
var _snooplogg = _interopRequireDefault(require("snooplogg"));
var _basicPrompts = require("../../common/basicPrompts");
var _types = require("../../common/types");
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)('central: install: agents: saas');
class DataplaneConfig {
constructor(type) {
_defineProperty(this, "type", void 0);
this.type = type || "";
}
}
class SwaggerHubDataplaneConfig extends DataplaneConfig {
constructor(owner, filter) {
super("SwaggerHub");
_defineProperty(this, "owner", void 0);
_defineProperty(this, "filter", void 0);
this.owner = owner;
this.filter = filter;
}
}
class SwaggerHubFilterConfig {
constructor(visibility, publication) {
_defineProperty(this, "visibility", void 0);
_defineProperty(this, "publication", void 0);
this.visibility = visibility;
this.publication = publication;
}
}
var SwaggerHubFilterVisibility = /*#__PURE__*/function (SwaggerHubFilterVisibility) {
SwaggerHubFilterVisibility["Both"] = "Both";
SwaggerHubFilterVisibility["Public"] = "Public";
SwaggerHubFilterVisibility["Private"] = "Private";
return SwaggerHubFilterVisibility;
}(SwaggerHubFilterVisibility || {});
var SwaggerHubFilterPublication = /*#__PURE__*/function (SwaggerHubFilterPublication) {
SwaggerHubFilterPublication["Both"] = "Both";
SwaggerHubFilterPublication["Published"] = "Published";
SwaggerHubFilterPublication["UnPublished"] = "UnPublished";
return SwaggerHubFilterPublication;
}(SwaggerHubFilterPublication || {});
class SaasAgentValues {
constructor() {
_defineProperty(this, "frequencyDA", void 0);
_defineProperty(this, "queueDA", void 0);
_defineProperty(this, "frequencyTA", void 0);
_defineProperty(this, "dataplaneConfig", void 0);
_defineProperty(this, "centralConfig", void 0);
_defineProperty(this, "owner", void 0);
_defineProperty(this, "visibility", void 0);
_defineProperty(this, "publication", void 0);
this.frequencyDA = '';
this.queueDA = false;
this.frequencyTA = '';
this.dataplaneConfig = new DataplaneConfig();
this.centralConfig = new _types.CentralAgentConfig();
this.owner = '';
this.visibility = SwaggerHubFilterVisibility.Both;
this.publication = SwaggerHubFilterPublication.Both;
}
}
class SaasSwaggerHubAgentValues extends SaasAgentValues {
constructor() {
super();
_defineProperty(this, "apiKey", void 0);
this.apiKey = '';
}
getAccessData() {
let data = JSON.stringify({
apiKey: this.apiKey
});
return data;
}
}
// ConfigFiles - all the config file that are used in the setup
const ConfigFiles = {};
// SwaggerHub SaaSPrompts - all SwaggerHub Saas prompts to the user for input
const SaasPrompts = {
API_KEY: 'Enter the SwaggerHub API Key the agent will use',
ORGANIZATION_OWNER: 'Enter the SwaggerHub Organization Owner the agent will use',
API_VISIBILITY: 'Enter the visibility of the APIs to be discovered (Optional).',
API_PUBLICATION: 'Enter the publication status of APIs to be discovered (Optional).',
DA_FREQUENCY: 'How often should the discovery run, leave blank for integrating in CI/CD process',
QUEUE: 'Do you want to discover immediately after installation'
};
const askBundleType = async () => {
//SwaggerHub agent has only DA
return _types.BundleType.DISCOVERY;
};
exports.askBundleType = askBundleType;
const askConfigType = async () => {
return _types.AgentConfigTypes.HOSTED;
};
const askForSwaggerHubCredentials = async hostedAgentValues => {
log("gathering access details for SwaggerHub");
hostedAgentValues.apiKey = await (0, _basicPrompts.askInput)({
msg: SaasPrompts.API_KEY,
defaultValue: hostedAgentValues.apiKey !== '' ? hostedAgentValues.apiKey : undefined
});
return hostedAgentValues;
};
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;
};
// @ts-ignore
const gatewayConnectivity = async installConfig => {
console.log('\nCONNECTION TO SwaggerHub API GATEWAY:');
console.log(_chalk.default.gray("The Discovery Agent needs to connect to the SwaggerHub API Gateway to discover API's for publishing to Amplify Central"));
// DeploymentType
let hostedAgentValues = new SaasAgentValues();
if (installConfig.gatewayType === _types.SaaSGatewayTypes.SWAGGERHUB) {
// SwaggerHub connection details
hostedAgentValues = new SaasSwaggerHubAgentValues();
hostedAgentValues = await askForSwaggerHubCredentials(hostedAgentValues);
}
// Ask to queue discovery now
log("getting the frequency and if the agent should run now");
console.log(_chalk.default.gray("\n00d00h00m format, where 30m = 30 minutes, 1h = 1 hour, 7d = 7 days, and 7d1h30m = 7 days 1 hour and 30 minutes. Minimum of 30m."));
hostedAgentValues.frequencyDA = await (0, _basicPrompts.askInput)({
msg: SaasPrompts.DA_FREQUENCY,
validate: validateFrequency(),
allowEmptyInput: true
});
hostedAgentValues.queueDA = (await (0, _basicPrompts.askList)({
msg: SaasPrompts.QUEUE,
default: _types.YesNo.No,
choices: _types.YesNoChoices
})) === _types.YesNo.Yes;
// get swaggerhub organization owner
hostedAgentValues.owner = await (0, _basicPrompts.askInput)({
msg: SaasPrompts.ORGANIZATION_OWNER,
defaultValue: hostedAgentValues.owner !== '' ? hostedAgentValues.owner : undefined
});
// get visility of APIs to be discovered
hostedAgentValues.visibility = await (0, _basicPrompts.askList)({
msg: SaasPrompts.API_VISIBILITY,
default: SwaggerHubFilterVisibility.Both,
choices: [{
name: SwaggerHubFilterVisibility.Both,
value: SwaggerHubFilterVisibility.Both
}, {
name: SwaggerHubFilterVisibility.Public,
value: SwaggerHubFilterVisibility.Public
}, {
name: SwaggerHubFilterVisibility.Private,
value: SwaggerHubFilterVisibility.Private
}]
});
// get publication status of APIs to be discovered
hostedAgentValues.publication = await (0, _basicPrompts.askList)({
msg: SaasPrompts.API_PUBLICATION,
default: SwaggerHubFilterPublication.Both,
choices: [{
name: SwaggerHubFilterPublication.Both,
value: SwaggerHubFilterPublication.Both
}, {
name: SwaggerHubFilterPublication.Published,
value: SwaggerHubFilterPublication.Published
}, {
name: SwaggerHubFilterPublication.UnPublished,
value: SwaggerHubFilterPublication.UnPublished
}]
});
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`);
}
console.log(hostedAgentValues.getAccessData());
let encData = _crypto.default.publicEncrypt({
key: key,
padding: _crypto.default.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: hash
}, Buffer.from(hostedAgentValues.getAccessData()));
return encData.toString("base64");
};
const completeInstall = async (installConfig, apiServerClient, defsManager) => {
/**
* Create agent resources
*/
console.log("\n");
let swaggerHubAgentValues = 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.SaaSGatewayTypes.SWAGGERHUB) {
swaggerHubAgentValues.dataplaneConfig = new SwaggerHubDataplaneConfig(swaggerHubAgentValues.owner, new SwaggerHubFilterConfig(swaggerHubAgentValues.visibility, swaggerHubAgentValues.publication));
}
// create the data plane resource
let dataplaneRes = await helpers.createNewDataPlaneResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], swaggerHubAgentValues.dataplaneConfig);
// create data plane secret resource
try {
await helpers.createNewDataPlaneSecretResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], dataplaneRes.name, await createEncryptedAccessData(swaggerHubAgentValues, dataplaneRes));
} catch (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 discovery agent resource
installConfig.centralConfig.daAgentName = await helpers.createNewAgentResource(apiServerClient, defsManager, installConfig.centralConfig.environment, _types.GatewayTypeToDataPlane[installConfig.gatewayType], _types.AgentResourceKind.da, _types.AgentTypes.da, installConfig.centralConfig.ampcTeamName, _types.GatewayTypeToDataPlane[installConfig.gatewayType] + " Discovery Agent", dataplaneRes.name, swaggerHubAgentValues.frequencyDA, swaggerHubAgentValues.queueDA);
console.log(await generateOutput(installConfig));
};
const SwaggerHubSaaSInstallMethods = exports.SwaggerHubSaaSInstallMethods = {
GetBundleType: askBundleType,
GetDeploymentType: askConfigType,
AskGatewayQuestions: gatewayConnectivity,
FinalizeGatewayInstall: completeInstall,
ConfigFiles: [],
AgentNameMap: {
[_types.AgentTypes.da]: _types.AgentNames.SWAGGERHUB_DA
},
GatewayDisplay: _types.SaaSGatewayTypes.SWAGGERHUB
};
// These are the items that are not exported, but need to be for testing
const testables = exports.testables = {
SaasAgentValues,
SaasSwaggerHubAgentValues,
SaasPrompts,
ConfigFiles
};