@openzeppelin/defender-serverless
Version:
Configure your Defender environment via code
98 lines (97 loc) • 5.35 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.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,
sentinels: [],
autotasks: [],
contracts: [],
relayers: [],
notifications: [],
categories: [],
secrets: [],
};
// Sentinels
const listSentinels = () => (0, utils_1.getSentinelClient)(this.teamKey)
.list()
.then(i => i.items);
await this.wrapper(this.serverless, 'Sentinels', this.serverless.service.resources?.Resources?.sentinels, listSentinels, (resource) => `${resource.stackResourceId}: ${resource.subscriberId}`, stdOut.sentinels);
// Autotasks
const listAutotasks = () => (0, utils_1.getAutotaskClient)(this.teamKey)
.list()
.then(r => r.items);
await this.wrapper(this.serverless, 'Autotasks', this.serverless.service.functions, listAutotasks, (resource) => `${resource.stackResourceId}: ${resource.autotaskId}`, stdOut.autotasks);
// Contracts
const listContracts = () => (0, utils_1.getAdminClient)(this.teamKey).listContracts();
await this.wrapper(this.serverless, 'Contracts', this.serverless.service.resources?.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', this.serverless.service.resources?.Resources?.relayers, listRelayers, (resource) => `${resource.stackResourceId}: ${resource.relayerId}`, stdOut.relayers);
// Notifications
const listNotifications = () => (0, utils_1.getSentinelClient)(this.teamKey).listNotificationChannels();
await this.wrapper(this.serverless, 'Notifications', this.serverless.service.resources?.Resources?.notifications, listNotifications, (resource) => `${resource.stackResourceId}: ${resource.notificationId}`, stdOut.notifications);
// Categories
const listNotificationCategories = () => (0, utils_1.getSentinelClient)(this.teamKey).listNotificationCategories();
await this.wrapper(this.serverless, 'Categories', this.serverless.service.resources?.Resources?.categories, listNotificationCategories, (resource) => `${resource.stackResourceId}: ${resource.categoryId}`, stdOut.categories);
// Secrets
const listSecrets = () => (0, utils_1.getAutotaskClient)(this.teamKey)
.listSecrets()
.then(r => r.secretNames ?? []);
const allSecrets = (0, utils_1.getConsolidatedSecrets)(this.serverless);
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;