"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPolicyDiffByVersions = exports.getPolicyDocumentByVersion = exports.getLatestPolicyDocument = exports.getPolicyByName = exports.getPolicyCount = exports.listPolicies = void 0;
const deep_object_diff_1 = require("deep-object-diff");
const managedPolicies_json_1 = __importDefault(require("./managedPolicies.json"));
const managedPolicies = managedPolicies_json_1.default;
const sortObjectByPropertyNames = (obj) => Object.keys(obj).sort().reduce((objEntries, key) => {
objEntries[key] = obj[key];
return objEntries;
}, {});
/**
* Retrieve a list of all included IAM Managed Polcies.
* @returns {Array} An array of IAM Managed Policy names.
*/
const listPolicies = () => {
return Object.keys(sortObjectByPropertyNames(managedPolicies));
};
exports.listPolicies = listPolicies;
/**
* Get the number of included IAM Managed Polcies.
* @returns {number} The count of IAM Managed Policies.
*/
const getPolicyCount = () => {
return Object.keys(managedPolicies).length;
};
exports.getPolicyCount = getPolicyCount;
/**
* Get the IAM Managed Policy by name with the full version history
* @param {string} policyName The IAM Managed Policy name.
* @returns {object} The IAM Managed Policy if found.
* @throws Will throw an error if not found by the name.
*/
const getPolicyByName = (policyName) => {
const policy = managedPolicies[policyName];
if (policy) {
return policy;
}
else {
throw new Error(`IAM Managed Policy '${policyName}' wasn't found!`);
}
};
exports.getPolicyByName = getPolicyByName;
/**
* Get the latest policy document for a IAM Managed Policy by name
* @param {string} policyName The IAM Managed Policy name.
* @returns {object} The IAM Managed Policy document if found.
* @throws Will throw an error if not found by the name.
*/
const getLatestPolicyDocument = (policyName) => {
const policy = managedPolicies[policyName];
if (policy) {
const latestVersionId = policy.latestVersionId;
return policy.versions[latestVersionId].document;
}
else {
throw new Error(`IAM Managed Policy '${policyName}' wasn't found!`);
}
};
exports.getLatestPolicyDocument = getLatestPolicyDocument;
/**
* Get the latest policy document for a IAM Managed Policy by name
* @param {string} policyName The IAM Managed Policy name.
* @param {number} policyVersion The version id of the IAM Managed Policy.
* @returns {object} The IAM Managed Policy document if found.
* @throws Will throw an error if not found by the name.
*/
const getPolicyDocumentByVersion = (policyName, policyVersion) => {
const policy = managedPolicies[policyName];
if (policy && policy.versions[`v${policyVersion}`]) {
return policy.versions[`v${policyVersion}`].document;
}
else {
throw new Error(`IAM Managed Policy '${policyName}' wasn't found in version '${policyVersion}'!`);
}
};
exports.getPolicyDocumentByVersion = getPolicyDocumentByVersion;
/**
* Get a diff of the policy documents for two versions of a IAM Managed Policy
* @param {string} policyName The IAM Managed Policy name.
* @param {number} oldPolicyVersion The old version id of the IAM Managed Policy.
* @param {number} newPolicyVersion The new version id of the IAM Managed Policy.
* @returns {object} The diff of the IAM Managed Policy document versions if found.
* @throws Will throw an error if not found by the name or the respective policy document versions.
*/
const getPolicyDiffByVersions = (policyName, oldPolicyVersion, newPolicyVersion) => {
const policy = managedPolicies[policyName];
if (policy) {
if (!policy.versions[`v${oldPolicyVersion}`]) {
throw new Error(`IAM Managed Policy '${policyName}' wasn't found in version '${oldPolicyVersion}'!`);
}
if (!policy.versions[`v${newPolicyVersion}`]) {
throw new Error(`IAM Managed Policy '${policyName}' wasn't found in version '${newPolicyVersion}'!`);
}
return (0, deep_object_diff_1.detailedDiff)(policy.versions[`v${oldPolicyVersion}`].document, policy.versions[`v${newPolicyVersion}`].document);
}
else {
throw new Error(`IAM Managed Policy '${policyName}' wasn't found!`);
}
};
exports.getPolicyDiffByVersions = getPolicyDiffByVersions;