Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Merge a3da98d into f075022
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Mar 30, 2018
2 parents f075022 + a3da98d commit 9d73bd6
Show file tree
Hide file tree
Showing 38 changed files with 698 additions and 179 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Florian Traverse (https://github.com/temsa)",
"Mihai Dima (https://github.com/mihaidma)",
"Paolo Chiodi (https://github.com/paolochiodi)",
"Paolo Insogna (https://github.com/ShogunPanda)",
"Paul Negrutiu (https://github.com/floridemai)",
"Mark Ireland (https://github.com/irelandm)",
"Michael O'Brien (https://github.com/mobri3n)",
Expand Down Expand Up @@ -56,13 +57,13 @@
],
"dependencies": {},
"devDependencies": {
"depcheck": "0.6.9",
"joi": "10.6.0",
"lerna": "^2.5.1",
"remark-cli": "^4.0.0",
"remark-lint": "^6.0.0",
"remark-preset-lint-recommended": "^3.0.0",
"standard": "^8.6.0",
"depcheck": "^0.6.9",
"joi": "^13.1.2",
"lerna": "^2.9.0",
"remark-cli": "^5.0.0",
"remark-lint": "^6.0.1",
"remark-preset-lint-recommended": "^3.0.1",
"standard": "^11.0.1",
"swagger-gen": "^1.1.3"
},
"standard": {
Expand Down
23 changes: 8 additions & 15 deletions packages/udaru-core/database/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict'

const postgrator = require('postgrator')
const Postgrator = require('postgrator')
const path = require('path')
const minimist = require('minimist')

Expand All @@ -19,7 +19,7 @@ if (!version) {
process.exit(1)
}

postgrator.setConfig({
const postgrator = new Postgrator({
migrationDirectory: path.join(__dirname, '/migrations'),
schemaTable: 'schemaversion', // optional. default is 'schemaversion'
driver: 'pg',
Expand All @@ -30,18 +30,11 @@ postgrator.setConfig({
password: password
})

postgrator.migrate(version, function (err, migrations) {
if (err) {
console.error(err)
process.exit(1)
}

postgrator.endConnection(function (err) {
if (err) {
console.error(err)
process.exit(1)
}

postgrator.migrate(version)
.then(migrations => {
console.log(`Migrations to ${version} done`, migrations)
})
})
.catch(err => {
console.log(err)
process.exit(1)
})
23 changes: 23 additions & 0 deletions packages/udaru-core/lib/asyncify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = function (...names) {
let promiseResolve = null
let promiseReject = null

const promise = new Promise((resolve, reject) => {
promiseResolve = resolve
promiseReject = reject
})

const cb = function (err, ...args) {
if (err) return promiseReject(err)
if (!names.length) return promiseResolve(args[0])

const obj = {}
for (let i = 0; i < names.length; i++) {
obj[names[i]] = args[i]
}

promiseResolve(obj)
}

return [promise, cb]
}
16 changes: 16 additions & 0 deletions packages/udaru-core/lib/ops/authorizeOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const async = require('async')
const Boom = require('boom')
const Joi = require('joi')
const asyncify = require('../asyncify')
const iam = require('./iam')
const validationRules = require('./validation').authorize
const buildPolicyOps = require('./policyOps')
Expand Down Expand Up @@ -51,6 +52,9 @@ function buildAuthorizeOps (db, config) {
* @param {Function} cb
*/
isUserAuthorized: function isUserAuthorized ({ resource, action, userId, organizationId, sourceIpAddress, sourcePort }, cb) {
let promise = null
if (typeof cb !== 'function') [promise, cb] = asyncify()

async.waterfall([
function validate (next) {
Joi.validate({ resource, action, userId, organizationId }, validationRules.isUserAuthorized, badRequestWrap(next))
Expand All @@ -65,6 +69,8 @@ function buildAuthorizeOps (db, config) {
], function (err, access) {
cb(err, { access })
})

return promise
},

/**
Expand All @@ -74,6 +80,9 @@ function buildAuthorizeOps (db, config) {
* @param {Function} cb
*/
listAuthorizations: function listAuthorizations ({ userId, resource, organizationId, sourceIpAddress, sourcePort }, cb) {
let promise = null
if (typeof cb !== 'function') [promise, cb] = asyncify()

async.waterfall([
function validate (next) {
Joi.validate({ resource, userId, organizationId }, validationRules.listAuthorizations, badRequestWrap(next))
Expand All @@ -88,6 +97,8 @@ function buildAuthorizeOps (db, config) {
], function (err, actions) {
cb(err, { actions })
})

return promise
},

/**
Expand All @@ -97,6 +108,9 @@ function buildAuthorizeOps (db, config) {
* @param {Function} cb
*/
listAuthorizationsOnResources: function listAuthorizationsOnResources ({ userId, resources, organizationId, sourceIpAddress, sourcePort }, cb) {
let promise = null
if (typeof cb !== 'function') [promise, cb] = asyncify()

async.waterfall([
function validate (next) {
Joi.validate({ userId, resources, organizationId }, validationRules.listAuthorizationsOnResources, badRequestWrap(next))
Expand All @@ -109,6 +123,8 @@ function buildAuthorizeOps (db, config) {
iam(policies).actionsOnResources({ resources, context }, badImplementationWrap(next))
}
], cb)

return promise
}
}

Expand Down
21 changes: 16 additions & 5 deletions packages/udaru-core/lib/ops/iam.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

const PBAC = require('pbac')
const _ = require('lodash')
const asyncify = require('../asyncify')

module.exports = function (policies) {
const pbac = new PBAC(policies)

function isAuthorized (params, done) {
function isAuthorized (params) {
return pbac.evaluate(params)
}

function actions ({ resource, context }, done) {
let promise = null
if (typeof done !== 'function') [promise, done] = asyncify()

try {
const result = _(policies)
.map('Statement')
Expand All @@ -35,13 +39,18 @@ module.exports = function (policies) {
.uniq()
.value()

return done(null, result)
done(null, result)
} catch (e) {
return done(e)
done(e)
}

return promise
}

function actionsOnResources ({ resources, context }, done) {
let promise = null
if (typeof done !== 'function') [promise, done] = asyncify()

try {
const resultMap = {}

Expand Down Expand Up @@ -73,10 +82,12 @@ module.exports = function (policies) {
return result
}, [])

return done(null, result)
done(null, result)
} catch (e) {
return done(e)
done(e)
}

return promise
}

return {
Expand Down
Loading

0 comments on commit 9d73bd6

Please sign in to comment.