UNPKG

aws-iam-policy-tool

Version:
62 lines (61 loc) 2.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = __importDefault(require("path")); const file_1 = require("../utils/file"); const varset_1 = require("../utils/varset"); const policy_1 = require("./policy"); const role_1 = require("./role"); async function readRoleFile(filePath, varSet) { let name = ''; try { name = path_1.default.basename(filePath, '.json'); const text = await file_1.readFile(filePath); return new role_1.RoleEntry(name, varset_1.parseJSON(text, varSet)); } catch (err) { console.error(`Failed to read ${name}`); throw err; } } exports.readRoleFile = readRoleFile; async function readPolicyFile(filePath, varSet, arnPrefix) { let name = ''; try { name = path_1.default.basename(filePath, '.json'); name = varset_1.substitute(name, varSet); const text = await file_1.readFile(filePath); const rawJson = varset_1.parseJSON(text, varSet); let arn; let policyInfo; let docNode; if (rawJson.Document) { // V2 const { Policy: policy } = rawJson; arn = arnPrefix + policy.Path + policy.PolicyName; policyInfo = { PolicyName: policy.PolicyName, Path: policy.Path, }; docNode = rawJson.Document; } else { // V1 arn = arnPrefix + '/' + varset_1.substitute(name, varSet); policyInfo = { PolicyName: varset_1.substitute(name, varSet), Path: '/', }; docNode = rawJson; console.warn('[WARN] %s : This policy definition is old version.', path_1.default.basename(filePath)); } return new policy_1.PolicyEntry(arn, policyInfo, docNode); } catch (err) { console.error(`Failed to read ${name}`); throw err; } } exports.readPolicyFile = readPolicyFile;