@axway/axway-central-cli
Version:
Manage APIs, services and publish to the Amplify Marketplace
174 lines (169 loc) • 9.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.CoreConfigController = exports.AccountRole = void 0;
var _amplifyCliUtils = require("@axway/amplify-cli-utils");
var _snooplogg = _interopRequireDefault(require("snooplogg"));
var _CliConfigManager = require("./CliConfigManager");
var _types = require("./types");
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: CoreConfigController');
// TODO: https://jira.axway.com/browse/APIGOV-20520
// interface AuthenticationError extends Error {
// errors?: Array<object>;
// }
let AccountRole = exports.AccountRole = /*#__PURE__*/function (AccountRole) {
AccountRole["AnalyticsSpecialist"] = "analytics_specialist";
AccountRole["ApiCentralAdmin"] = "api_central_admin";
AccountRole["FileTransferServicesAdmin"] = "fts_admin";
AccountRole["FlowCentralAccessManager"] = "fc_access_manager";
AccountRole["FlowCentralIntegration"] = "fc_integration";
AccountRole["FlowCentralITAdmin"] = "fc_it_admin";
AccountRole["FlowCentralProductsAdmin"] = "fc_products_admin";
AccountRole["FlowCentralSpecOps"] = "fc_spec_ops";
AccountRole["FlowCentralSubscriptionApprover"] = "fc_subscriptionapprover";
AccountRole["FlowCentralSubscriptionSpecialist"] = "fc_subscriptionspecialist";
AccountRole["FlowCentralTemplatePublisher"] = "fc_templatepublisher";
AccountRole["FlowCentralCftAdmin"] = "fc_cft_admin";
AccountRole["PlatformAdmin"] = "administrator";
AccountRole["PlatformAuditor"] = "auditor";
AccountRole["PlatformCollaborator"] = "collaborator";
AccountRole["PlatformConsumer"] = "consumer";
AccountRole["PlatformDeveloper"] = "developer";
AccountRole["PlatformReadOnly"] = "read_only";
AccountRole["RuntimeServicesAdmin"] = "ars_admin";
return AccountRole;
}({});
class CoreConfigController {
/**
* Get authentication info
* @param {String} clientId client id to use
* @param {String} [team] The team name or guid to use, otherwise fallsback to the default from
* the Axway CLI config.
* @returns object containing token and orgId. For service accounts orgId is undefined.
* @throws 401 if no authenticated account found.
*/
async getAuthInfo({
account,
clientId,
team,
forceGetAuthInfo
} = {}) {
var _devOpsAccount, _devOpsAccount$org, _devOpsAccount$org$id, _devOpsAccount$org2;
const configCtrl = new _CliConfigManager.CliConfigManager();
const config = (0, _amplifyCliUtils.loadConfig)();
// note: remove this validator after couple of versions
configCtrl.validateSavedConfigKeys();
log(`getAuthInfo, received clientId = ${clientId}, account = ${account}, team = ${team}`);
const baseUrl = process.env.AXWAY_CENTRAL_BASE_URL || configCtrl.get(_CliConfigManager.CliConfigKeys.BASE_URL);
// environment defined by using central cli "base-url" or axway "env" configs if set,
// otherwise its undefined (equals to prod)
const environment = !baseUrl || baseUrl === _types.ProdBaseUrls.US || baseUrl === _types.ProdBaseUrls.EU || baseUrl === _types.ProdBaseUrls.AP ? config.get('env') : 'staging';
log(`getAuthInfo, baseUrl = ${baseUrl}, environment = ${environment}`);
const {
sdk
} = (0, _amplifyCliUtils.initSDK)({
env: environment
}, config);
let {
devOpsAccount
} = CoreConfigController;
if (forceGetAuthInfo) {
devOpsAccount = null;
}
if (!devOpsAccount || account && devOpsAccount.name !== account) {
log(`getAuthInfo, no cached devOpsAccount found, or account name does not match`);
if (clientId) {
// TODO: client-id is a legacy option used only by proxies promote/create, should be removed
// https://jira.axway.com/browse/APIGOV-20818
log(`getAuthInfo, clientId value passed, trying to find a matching account`);
const list = await sdk.auth.list({
validate: true
});
const matchingAccount = list.find(a => a.auth.clientId === clientId);
if (matchingAccount) {
devOpsAccount = await sdk.auth.find(matchingAccount.name);
}
} else if (account) {
// ELSE IF: account name passed - ignoring defaultAccount and other accounts
log(`getAuthInfo, account value passed, trying to find a matching account`);
devOpsAccount = await sdk.auth.find(account);
} else {
// ELSE: trying to get any authenticated account
log(`getAuthInfo, account value not passed, trying to find default/any match`);
const list = await sdk.auth.list({
validate: true
});
log(`getAuthInfo, authenticated accounts found: ${list.length}`);
if (list.length === 1) {
log(`getAuthInfo, using a single account found with name: ${list[0].name}`);
devOpsAccount = list[0];
} else if (list.length > 1) {
// try to find the default account
devOpsAccount = list.find(a => a.name === config.get('auth.defaultAccount')) || list.find(a => a.default) || list[0];
}
}
if (!devOpsAccount) {
// TODO: piece of old logic here, move throwing out of the method?
// temporary commenting out the new functionality and reverting back to the old one, will be fixed with:
// https://jira.axway.com/browse/APIGOV-20520
log(`getAuthInfo, no devOpsAccount set after all, throwing 401`);
// const title: string = accountName
// ? `Account "${accountName}" cannot be found`
// : 'No authenticated accounts found.';
// const err: AuthenticationError = new Error(title);
// err.errors = [{ status: 401, title }];
// throw err;
throw {
errors: [{
status: 401,
title: account ? `Account "${account}" cannot be found` : 'No authenticated accounts found.'
}]
};
}
CoreConfigController.devOpsAccount = devOpsAccount;
}
const result = {
orgId: (_devOpsAccount = devOpsAccount) === null || _devOpsAccount === void 0 ? void 0 : (_devOpsAccount$org = _devOpsAccount.org) === null || _devOpsAccount$org === void 0 ? void 0 : (_devOpsAccount$org$id = _devOpsAccount$org.id) === null || _devOpsAccount$org$id === void 0 ? void 0 : _devOpsAccount$org$id.toString(),
orgRegion: (_devOpsAccount$org2 = devOpsAccount.org) === null || _devOpsAccount$org2 === void 0 ? void 0 : _devOpsAccount$org2.region,
token: process.env.AXWAY_CENTRAL_AUTH_TOKEN || config.get('central.authToken', devOpsAccount.auth.tokens.access_token)
};
// now that we have resolved the account, we can validate the team
if (team) {
const {
teams
} = await sdk.team.list(devOpsAccount);
const teamObj = teams.find(t => {
return t.guid.toLowerCase() === team.toLowerCase() || t.name.toLowerCase() === team.toLowerCase();
});
if (!teamObj) {
throw new Error(`Unable to find team "${team}" in the "${devOpsAccount.org.name}" organization`);
}
result.teamGuid = teamObj.guid;
} else if (team === null) {
result.teamGuid = null;
}
log(`getAuthInfo, returning account = ${devOpsAccount.name}`);
log(`getAuthInfo, returning token = ${result.token.substring(0, 5)}*****${result.token.substring(result.token.length - 5)}`);
log(`getAuthInfo, returning orgId = ${result.orgId}`);
log(`getAuthInfo, returning orgRegion = ${result.orgRegion}`);
log(`getAuthInfo, returning teamGuid = ${result.teamGuid}`);
return result;
}
static getEnv() {
var _CoreConfigController2, _CoreConfigController3;
return ((_CoreConfigController2 = CoreConfigController.devOpsAccount) === null || _CoreConfigController2 === void 0 ? void 0 : (_CoreConfigController3 = _CoreConfigController2.auth) === null || _CoreConfigController3 === void 0 ? void 0 : _CoreConfigController3.env) || _types.Platforms.prod;
}
static getAuthUrl() {
var _CoreConfigController4, _CoreConfigController5;
return ((_CoreConfigController4 = CoreConfigController.devOpsAccount) === null || _CoreConfigController4 === void 0 ? void 0 : (_CoreConfigController5 = _CoreConfigController4.auth) === null || _CoreConfigController5 === void 0 ? void 0 : _CoreConfigController5.baseUrl) || _types.AuthUrls.Prod;
}
}
exports.CoreConfigController = CoreConfigController;
_defineProperty(CoreConfigController, "devOpsAccount", null);