index.js

"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;
var deep_object_diff_1 = require("deep-object-diff");
var managedPolicies_json_1 = __importDefault(require("./managedPolicies.json"));
var sortObjectByPropertyNames = function (obj) { return Object.keys(obj).sort().reduce(function (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.
 */
var listPolicies = function () {
    return Object.keys(sortObjectByPropertyNames(managedPolicies_json_1.default));
};
exports.listPolicies = listPolicies;
/**
 * Get the number of included IAM Managed Polcies.
 * @returns {number} The count of IAM Managed Policies.
 */
var getPolicyCount = function () {
    return Object.keys(managedPolicies_json_1.default).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.
 */
var getPolicyByName = function (policyName) {
    var policy = managedPolicies_json_1.default[policyName];
    if (policy) {
        return policy;
    }
    else {
        throw new Error("IAM Managed Policy '".concat(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.
 */
var getLatestPolicyDocument = function (policyName) {
    var policy = managedPolicies_json_1.default[policyName];
    if (policy) {
        var latestVersionId = policy.latestVersionId;
        return policy.versions[latestVersionId].document;
    }
    else {
        throw new Error("IAM Managed Policy '".concat(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.
 */
var getPolicyDocumentByVersion = function (policyName, policyVersion) {
    var policy = managedPolicies_json_1.default[policyName];
    if (policy && policy.versions["v".concat(policyVersion)]) {
        return policy.versions["v".concat(policyVersion)].document;
    }
    else {
        throw new Error("IAM Managed Policy '".concat(policyName, "' wasn't found in version '").concat(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.
 */
var getPolicyDiffByVersions = function (policyName, oldPolicyVersion, newPolicyVersion) {
    var policy = managedPolicies_json_1.default[policyName];
    if (policy) {
        if (!policy.versions["v".concat(oldPolicyVersion)]) {
            throw new Error("IAM Managed Policy '".concat(policyName, "' wasn't found in version '").concat(oldPolicyVersion, "'!"));
        }
        if (!policy.versions["v".concat(newPolicyVersion)]) {
            throw new Error("IAM Managed Policy '".concat(policyName, "' wasn't found in version '").concat(newPolicyVersion, "'!"));
        }
        return (0, deep_object_diff_1.detailedDiff)(policy.versions["v".concat(oldPolicyVersion)].document, policy.versions["v".concat(newPolicyVersion)].document);
    }
    else {
        throw new Error("IAM Managed Policy '".concat(policyName, "' wasn't found!"));
    }
};
exports.getPolicyDiffByVersions = getPolicyDiffByVersions;