@cloud-copilot/iam-collect
Version:
Collect IAM information from AWS Accounts
65 lines • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RestApisSync = void 0;
exports.parseApiGatewayPolicy = parseApiGatewayPolicy;
const client_api_gateway_1 = require("@aws-sdk/client-api-gateway");
const log_1 = require("@cloud-copilot/log");
const typedSync_js_1 = require("../typedSync.js");
exports.RestApisSync = (0, typedSync_js_1.createTypedSyncOperation)('apigateway', 'gateways', (0, typedSync_js_1.createResourceSyncType)({
client: client_api_gateway_1.APIGatewayClient,
command: client_api_gateway_1.GetRestApisCommand,
key: 'items',
paginationConfig: {
inputKey: 'position',
outputKey: 'position'
},
arn: (api, region, account, partition) => restApiArn(api.id, region, partition),
tags: (api) => api.tags,
resourceTypeParts: (account, region) => ({
account,
service: 'apigateway',
region,
resourceType: 'restapis'
}),
results: (api) => ({
metadata: {
id: api.id,
name: api.name
},
policy: parseApiGatewayPolicy(api.id, api.policy)
})
}));
/**
* Get the ARN for an API Gateway REST API.
*
* @param apiId the ID of the API Gateway REST API
* @param region the AWS region of the API Gateway
* @param partition the AWS partition (aws, aws-cn, aws-us-gov)
* @returns the ARN of the API Gateway REST API
*/
function restApiArn(apiId, region, partition) {
return `arn:${partition}:apigateway:${region}::/restapis/${apiId}`;
}
/**
* Parse the API Gateway policy string into a JSON object.
* For some reason API Gateway policies can have \\\ at the start of every string
* and any forward slash is escaped with a double backslash (\\/)
*
* @param apiId the ID of the API Gateway
* @param policy the policy string to parse
* @returns the parsed policy object or undefined if the policy is not provided
*/
function parseApiGatewayPolicy(apiId, policy) {
if (!policy) {
return undefined;
}
policy = policy.replace(/\\"/g, '"').replace(/\\\//g, '/');
try {
return JSON.parse(policy);
}
catch (error) {
log_1.log.error('Failed to parse API Gateway policy', { apiId }, error);
throw new Error(`Unable to parse policy for API Gateway ${apiId}`);
}
}
//# sourceMappingURL=gateways.js.map