@openzeppelin/defender-as-code
Version:
Configure your Defender environment via code
107 lines (106 loc) • 6.34 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = __importDefault(require("../utils/logger"));
const utils_1 = require("../utils");
class DefenderInfo {
constructor(serverless, options, logging) {
this.serverless = serverless;
this.options = options;
this.logging = logging;
this.resources = this.serverless.service.resources;
this.log = logger_1.default.getInstance();
this.hooks = {
'before:info:info': () => this.validateKeys(),
'info:info': this.info.bind(this),
};
}
validateKeys() {
this.teamKey = (0, utils_1.getTeamAPIkeysOrThrow)(this.serverless);
}
async wrapper(context, resourceType, resources, retrieveExistingResources, format, output) {
try {
this.log.progress('component-info', `Retrieving ${resourceType}`);
this.log.notice(`${resourceType}`);
const existing = (await retrieveExistingResources()).filter((e) => (0, utils_1.isTemplateResource)(context, e, resourceType, resources ?? {}));
await Promise.all(existing.map(async (e) => {
this.log.notice(`${format(e)}`, 1);
let keys = [];
// Also print relayer API keys
if (resourceType === 'Relayers') {
const listRelayerAPIKeys = await (0, utils_1.getRelayClient)((0, utils_1.getTeamAPIkeysOrThrow)(context)).listKeys(e.relayerId);
listRelayerAPIKeys.map((k) => {
this.log.notice(`${k.stackResourceId}: ${k.keyId}`, 2);
});
keys = listRelayerAPIKeys;
}
if (resourceType === 'Relayers')
output.push({ ...e, relayerKeys: keys });
else
output.push(e);
}));
}
catch (e) {
this.log.tryLogDefenderError(e);
}
}
async info() {
this.log.notice('========================================================');
const stackName = (0, utils_1.getStackName)(this.serverless);
this.log.progress('info', `Running Defender Info on stack: ${stackName}`);
const stdOut = {
stack: stackName,
monitors: [],
actions: [],
contracts: [],
relayers: [],
relayerGroups: [],
notifications: [],
secrets: [],
forkedNetworks: [],
privateNetworks: [],
};
// Forked Networks
const listForkedNetworks = () => (0, utils_1.getNetworkClient)(this.teamKey).listForkedNetworks();
await this.wrapper(this.serverless, 'Forked Networks', (0, utils_1.removeDefenderIdReferences)(this.resources?.['forked-networks']), listForkedNetworks, (resource) => `${resource.stackResourceId}: ${resource.tenantNetworkId}`, stdOut.forkedNetworks);
// Private Networks
const listPrivateNetworks = () => (0, utils_1.getNetworkClient)(this.teamKey).listPrivateNetworks();
await this.wrapper(this.serverless, 'Private Networks', (0, utils_1.removeDefenderIdReferences)(this.resources?.['private-networks']), listPrivateNetworks, (resource) => `${resource.stackResourceId}: ${resource.tenantNetworkId}`, stdOut.privateNetworks);
// Monitors
const listMonitors = () => (0, utils_1.getMonitorClient)(this.teamKey)
.list()
.then((i) => i.items);
await this.wrapper(this.serverless, 'Monitors', (0, utils_1.removeDefenderIdReferences)(this.resources?.monitors), listMonitors, (resource) => `${resource.stackResourceId}: ${resource.monitorId}`, stdOut.monitors);
// Actions
const listActions = () => (0, utils_1.getActionClient)(this.teamKey)
.list()
.then((r) => r.items);
await this.wrapper(this.serverless, 'Actions', (0, utils_1.removeDefenderIdReferences)(this.resources.actions), listActions, (resource) => `${resource.stackResourceId}: ${resource.actionId}`, stdOut.actions);
// Contracts
const listContracts = () => (0, utils_1.getProposalClient)(this.teamKey).listContracts();
await this.wrapper(this.serverless, 'Contracts', (0, utils_1.removeDefenderIdReferences)(this.resources?.contracts), listContracts, (resource) => `${resource.network}-${resource.address}: ${resource.name}`, stdOut.contracts);
// Relayers
const listRelayers = () => (0, utils_1.getRelayClient)(this.teamKey)
.list()
.then((r) => r.items);
await this.wrapper(this.serverless, 'Relayers', (0, utils_1.removeDefenderIdReferences)(this.resources?.relayers), listRelayers, (resource) => `${resource.stackResourceId}: ${resource.relayerId}`, stdOut.relayers);
// Relayer Groups
const listRelayerGroups = () => (0, utils_1.getRelayGroupClient)(this.teamKey).list();
await this.wrapper(this.serverless, 'Relayer Groups', (0, utils_1.removeDefenderIdReferences)(this.resources?.['relayer-groups']), listRelayerGroups, (resource) => `${resource.stackResourceId}: ${resource.relayerGroupId}`, stdOut.relayerGroups);
// Notifications
const listNotifications = () => (0, utils_1.getMonitorClient)(this.teamKey).listNotificationChannels();
await this.wrapper(this.serverless, 'Notifications', (0, utils_1.removeDefenderIdReferences)(this.resources?.notifications), listNotifications, (resource) => `${resource.stackResourceId}: ${resource.notificationId}`, stdOut.notifications);
// Secrets
const listSecrets = () => (0, utils_1.getActionClient)(this.teamKey)
.listSecrets()
.then((r) => r.secretNames ?? []);
const allSecrets = (0, utils_1.getConsolidatedSecrets)(this.serverless, this.resources);
await this.wrapper(this.serverless, 'Secrets', allSecrets, listSecrets, (resource) => `${resource}`, stdOut.secrets);
this.log.notice('========================================================');
if (!process.stdout.isTTY)
this.log.stdOut(JSON.stringify(stdOut, null, 2));
}
}
exports.default = DefenderInfo;
;