UNPKG

modern-pep

Version:

Modern policy enforcement point authorizer

142 lines (132 loc) 3.92 kB
const yaml = require("js-yaml"); const fs = require("fs"); const path = require("path"); const { clear } = require("console"); const absolutePath = path.resolve("./pep/pep-spec.yml"); const data = { version: "0.1.0", id: "operation", name: "Operation", description: "Heimdall management plane application", authorization_atoms: [], }; const init = function () { console.log("hi"); }; const createYamlFile = function () { const isExistFile = fs.existsSync("pep-spec.yml"); if (isExistFile) { console.log("File already exists."); } else { const isDirectoryCreated = fs.mkdirSync("./pep"); if (!isDirectoryCreated) { fs.writeFileSync("./pep/pep-spec.yml", yaml.dump(data), (err) => { if (err) console.log(err); else { console.log("File written successfully\n"); console.log("The written has the following contents:"); console.log(fs.readFileSync(absolutePath, "utf8")); } }); } } }; const getAtoms = function () { const doc = yaml.load(fs.readFileSync(absolutePath, "utf8")); console.log(JSON.stringify(doc, null, "\t")); }; const addAtom = function (atomDetails) { try { const doc = yaml.load(fs.readFileSync(absolutePath, "utf8")); if (doc && doc.authorization_atoms && doc.authorization_atoms.length > 0) { const { authorization_atoms } = doc; const foundIndex = authorization_atoms.findIndex( (atom, index) => atom.id === atomDetails.id ); if (foundIndex > 0) { console.log(`${atomDetails.id} already exsit`); } else { authorization_atoms.push(atomDetails); fs.writeFileSync(absolutePath, yaml.dump(doc), (err) => { if (err) { console.log(err); } }); } } else if ( doc && doc.authorization_atoms && doc.authorization_atoms.length === 0 ) { const { authorization_atoms } = doc; authorization_atoms.push(atomDetails); fs.writeFileSync(absolutePath, yaml.dump(doc), (err) => { if (err) { console.log(err); } }); } } catch (error) { console.log(error); } }; const updateAtom = function (atomDetails) { try { const doc = yaml.load(fs.readFileSync(absolutePath, "utf8")); if (doc && doc.authorization_atoms && doc.authorization_atoms.length > 0) { const { authorization_atoms } = doc; const foundIndex = authorization_atoms.findIndex( (atom, index) => { return atom.id === atomDetails.id } ); if (foundIndex >= 0) { doc.authorization_atoms[foundIndex] = atomDetails; fs.writeFileSync(absolutePath, yaml.dump(doc), (err) => { if (err) { console.log(err); } }); } else { console.log(`${atomDetails.id} not exist in the update list.`); } } } catch (error) { console.log(error); } }; const removeAtom = function (atomsId) { try { const doc = yaml.load(fs.readFileSync(absolutePath, "utf8")); console.log(doc.authorization_atoms.length); if (doc && doc.authorization_atoms && doc.authorization_atoms.length > 0) { const { authorization_atoms } = doc; const foundIndex = authorization_atoms.findIndex( (atom, index) => atom.id === atomsId ); if (foundIndex > 0) { const updatedAtoms = authorization_atoms.filter( (atom, index) => atom.id !== atomsId ); doc.authorization_atoms = updatedAtoms; fs.writeFileSync(absolutePath, yaml.dump(doc), (err) => { if (err) { console.log(err); } }); } else { console.log(`${atomsId} not exist in the update list.`); } } } catch (error) { console.log(error); } }; module.exports = { init, createYamlFile, getAtoms, addAtom, updateAtom, removeAtom, };