aws-iam-policy-tool
Version:
AWS IAM role/policy management cli tool
43 lines (42 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const result_1 = require("../utils/result");
const operation_1 = require("../aws/operation");
const iam_1 = require("../aws/iam");
class PolicyCleaner {
constructor(opts = {}) {
}
async delete(policy) {
const results = [];
try {
const policyVersions = await operation_1.listPolicyVersions(policy.Arn);
for (const policyVersion of policyVersions) {
if (!policyVersion.IsDefaultVersion) {
await this.deletePolicyVersion(policy.Arn, policyVersion.VersionId);
results.push(result_1.OK('Deleted %1 %2', [policy.PolicyName, policyVersion.VersionId]));
}
}
await this.deletePolicy(policy);
results.push(result_1.OK('Deleted %1', policy.PolicyName));
return results;
}
catch (err) {
if (err.code === 'DeleteConflict') {
results.push(result_1.NG('Failed to delete %1 attached on some roles', policy.PolicyName));
return results;
}
throw err;
}
}
async deletePolicy(policy) {
return iam_1.iam.deletePolicy({ PolicyArn: policy.Arn }).promise();
}
async deletePolicyVersion(arn, versionId) {
const params = {
PolicyArn: arn,
VersionId: versionId,
};
return iam_1.iam.deletePolicyVersion(params).promise();
}
}
exports.PolicyCleaner = PolicyCleaner;