|
1 | | -[](https://travis-ci.org/monken/node-pbac) |
| 1 | +[](https://npmjs.org/package/pbac) [](https://npmjs.org/package/jsdox) [](https://travis-ci.org/monken/node-pbac) |
2 | 2 |
|
3 | | -# node-pbac |
| 3 | +# Policy Based Access Control |
| 4 | + |
| 5 | +**AWS IAM Policy compatible evaluation engine** |
| 6 | + |
| 7 | +Use the power and flexibility of the AWS IAM Policy syntax in your own application to manage access control. For more details on AWS IAM Policies have a look at https://docs.aws.amazon.com/IAM/latest/UserGuide/policies_overview.html. |
| 8 | + |
| 9 | +**Note:** The policy elements `Principal`, `NotPrincipal`, `NotResource` and conditions `ArnEquals`, `ArnNotEquals`, `ArnLike`, `ArnNotLike` are not supported at the moment. |
4 | 10 |
|
5 | 11 | ## Installation |
6 | 12 |
|
7 | 13 | ``` |
8 | 14 | npm install pbac |
9 | 15 | ``` |
10 | 16 |
|
11 | | - |
12 | 17 | <!-- START doctoc generated TOC please keep comment here to allow auto update --> |
13 | 18 | <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
14 | 19 | Contents |
15 | 20 |
|
16 | | -- [Global](#global) |
17 | | - - [Class: PBAC](#class-pbac) |
18 | | - - [PBAC.Klass()](#pbacklass) |
19 | | - - [PBAC.validate(policy)](#pbacvalidatepolicy) |
20 | | - - [PBAC.evaluate(object)](#pbacevaluateobject) |
| 21 | +- [Synopsis](#synopsis) |
| 22 | +- [Constructor](#constructor) |
| 23 | + - [Properties](#properties) |
| 24 | +- [Methods](#methods) |
| 25 | + - [evaluate(params)](#evaluateparams) |
| 26 | + - [validate(policy)](#validatepolicy) |
21 | 27 |
|
22 | 28 | <!-- END doctoc generated TOC please keep comment here to allow auto update --> |
23 | | -# Global |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | 29 |
|
29 | | -* * * |
| 30 | +## Synopsis |
30 | 31 |
|
31 | | -## Class: PBAC |
| 32 | +```javascript |
| 33 | +var PBAC = require('pbac'); |
32 | 34 |
|
| 35 | +var policies = [{ |
| 36 | + "Version": "2012-10-17", |
| 37 | + "Statement": [{ |
| 38 | + "Sid": "OptionalDescription", |
| 39 | + "Effect": "Allow", |
| 40 | + "Action": [ |
| 41 | + "iam:CreateUser", |
| 42 | + "iam:UpdateUser", |
| 43 | + "iam:DeleteUser" |
| 44 | + ], |
| 45 | + "Resource": [ |
| 46 | + "arn:aws:iam:::user/${req:UserName}" |
| 47 | + ], |
| 48 | + "Condition": { |
| 49 | + "IpAddress": { |
| 50 | + "req:IpAddress": "10.0.20.0/24" |
| 51 | + } |
| 52 | + } |
| 53 | + }] |
| 54 | +}]; |
| 55 | + |
| 56 | +var pbac = new PBAC(policies); |
| 57 | + |
| 58 | +// returns true |
| 59 | +pbac.evaluate({ |
| 60 | + action: 'iam:CreateUser', |
| 61 | + resource: 'arn:aws:iam:::user/testuser', |
| 62 | + variables: { |
| 63 | + req: { |
| 64 | + IpAddress: '10.0.20.51', |
| 65 | + UserName: 'testuser', |
| 66 | + } |
| 67 | + } |
| 68 | +}); |
| 69 | +``` |
33 | 70 |
|
34 | | -### PBAC.Klass() |
| 71 | +## Constructor |
35 | 72 |
|
| 73 | +```javascript |
| 74 | +var pbac = new PBAC(policies, [options]); |
36 | 75 | ``` |
37 | | -var PBAC = require('pbac'); |
38 | | -var pbac = new PBAC({ |
39 | 76 |
|
40 | | -}); |
41 | | -``` |
| 77 | +Constructs a policy evaluator. |
42 | 78 |
|
| 79 | +### Properties |
43 | 80 |
|
44 | | -### PBAC.validate(policy) |
45 | 81 |
|
46 | | -Validates one or many policies against the schema provided in the constructor. |
47 | | -Will throw an error if validation fails. |
| 82 | +* **`policies`** (Array|Object) |
| 83 | + Either an array of policies or a single policy document. Have a look at https://docs.aws.amazon.com/IAM/latest/UserGuide/AccessPolicyLanguage_ElementDescriptions.html for a description of the policy syntax. |
| 84 | +* **`options`** (Object) |
| 85 | + * `schema` (Object) |
| 86 | + JSON schema that describes the policies. Defaults to the contents of schema.json that ships with this module. |
| 87 | + * `validateSchema` (Boolean) |
| 88 | + Validate the schema when the object is constructed. This can be disabled in a production environment if it can be assumed that the schema is valid to improve performance when constructing new objects. |
| 89 | + * `validatePolicies` (Boolean) |
| 90 | + Policies passed to the constructor will be validated against the `schema`. Defaults to `true`. Can be disabled to improve performance if the policies can be assumed to be valid. |
| 91 | + * `conditions` (Object) |
| 92 | + Object of conditions that are supported in the `Conditions` attribute of policies. Defaults to `conditions.js` in this module. If conditions are passed to the constructor they will be merged with the conditions of the `conditions.js` module (with `conditions.js` taking precedence). |
48 | 93 |
|
49 | | -**Parameters** |
50 | 94 |
|
51 | | -**policy**: `Object`, Array of policies or single policy object |
| 95 | +## Methods |
52 | 96 |
|
53 | | -**Returns**: `Bool`, Returns `true` if the policies are valid |
54 | 97 |
|
55 | | -### PBAC.evaluate(object) |
| 98 | +### evaluate(params) |
56 | 99 |
|
57 | 100 | Tests an object against the policies and determines if the object passes. |
58 | 101 | The method will first try to find a policy with an explicit `Deny` for the combination of |
59 | 102 | `resource`, `action` and `condition` (matching policy). If such policy exists, `evaulate` returns false. |
60 | 103 | If there is no explicit deny the method will look for a matching policy with an explicit `Allow`. |
61 | 104 | `evaulate` will return `true` if such a policy is found. If no matching can be found at all, |
62 | | -`evaluate` will return `false`. |
| 105 | +`evaluate` will return `false`. Please find a more thorough explanation of this process at https://docs.aws.amazon.com/IAM/latest/UserGuide/AccessPolicyLanguage_EvaluationLogic.html. |
| 106 | + |
| 107 | +```javascript |
| 108 | +pbac.evaluate({ |
| 109 | + action: 'iam:CreateUser', |
| 110 | + resource: 'arn:aws:iam:::user/testuser', |
| 111 | + variables: { |
| 112 | + req: { |
| 113 | + IpAddress: '10.0.20.51', |
| 114 | + UserName: 'testuser', |
| 115 | + } |
| 116 | + } |
| 117 | +}); |
| 118 | +``` |
63 | 119 |
|
64 | 120 | **Parameters** |
65 | 121 |
|
66 | | -**object**: `Object`, Object to test against the policies |
67 | | - |
68 | | -**Returns**: `Bool`, Returns `true` if the object passes, `false` otherwise |
69 | | - |
70 | | - |
71 | | - |
72 | | -* * * |
73 | | - |
74 | | - |
| 122 | +* **`params`** (Object) |
| 123 | + * `action` (String) - Action to validate |
| 124 | + * `resource` (String) - Resource to validate |
| 125 | + * `variables` (Object) - Nested object of variables for interpolation of policy variables. See [Variables](). |
75 | 126 |
|
| 127 | +**Returns**: `boolean`, Returns `true` if `params` passes the policies, `false` otherwise |
76 | 128 |
|
| 129 | +### validate(policy) |
77 | 130 |
|
| 131 | +Validates one or many policies against the schema provided in the constructor. |
| 132 | +Will throw an error if validation fails. |
78 | 133 |
|
| 134 | +**Parameters** |
79 | 135 |
|
| 136 | +* **`policy`** (Array|Object) |
| 137 | + Array of policies or a single policy object |
80 | 138 |
|
| 139 | +**Returns**: `boolean`, Returns `true` if the policies are valid |
81 | 140 |
|
| 141 | +* * * |
82 | 142 |
|
83 | 143 | The MIT License (MIT) |
84 | 144 |
|
|
0 commit comments