npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jdiamond/pbac

v1.0.0

Published

AWS IAM Policy inspired and (mostly) compatible evaluation engine

Downloads

2

Readme

npm npm Build Status license

Policy Based Access Control

AWS IAM Policy inspired and (mostly) compatible evaluation engine

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.

Installation

npm install pbac

Contents

Synopsis

var PBAC = require('pbac');

var policies = [{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "OptionalDescription",
    "Effect": "Allow",
    "Action": [
      "iam:CreateUser",
      "iam:UpdateUser",
      "iam:DeleteUser"
    ],
    "Resource": [
      "arn:aws:iam:::user/${req:UserName}"
    ],
    "Condition": {
      "IpAddress": {
        "req:IpAddress": "10.0.20.0/24"
      }
    }
  }]
}];

var pbac = new PBAC(policies);

// returns true
pbac.evaluate({
  action: 'iam:CreateUser',
  resource: 'arn:aws:iam:::user/testuser',
  context: {
    req: {
      IpAddress: '10.0.20.51',
      UserName: 'testuser',
    }
  }
});

Constructor

var pbac = new PBAC(policies, [options]);

Constructs a policy evaluator.

Properties

  • policies (Array|Object) 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.
  • options (Object)
    • schema (Object) - JSON schema that describes the policies. Defaults to the contents of schema.json that ships with this module.
    • validateSchema (Boolean) - Validate the schema when the object is constructed. This can be disabled in production environment if it can be assumed that the schema is valid to improve performance when constructing new objects.
    • validatePolicies (Boolean) - 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.
    • conditions (Object) - 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).

Methods

evaluate(params)

Tests an object against the policies and determines if the object passes. The method will first try to find a policy with an explicit Deny for the combination of resource, action and condition (matching policy). If such policy exists, evaluate returns false. If there is no explicit deny the method will look for a matching policy with an explicit Allow. evaluate will return true if such a policy is found. If no matching can be found at all, 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.

pbac.evaluate({
  action: 'iam:CreateUser',
  resource: 'arn:aws:iam:::user/testuser',
  context: {
    req: {
      IpAddress: '10.0.20.51',
      UserName: 'testuser',
    }
  }
});

Parameters

  • params (Object)
    • action (String) - Action to validate
    • resource (String) - Resource to validate
    • context (Object) - Nested object of context for interpolation of policy context. See Context.

Returns: boolean, Returns true if params passes the policies, false otherwise

validate(policy)

Validates one or many policies against the schema provided in the constructor. Will throw an error if validation fails.

Parameters

  • policy (Array|Object) Array of policies or a single policy object

Returns: boolean, Returns true if the policies are valid

Reference

Context

Have a look at https://docs.aws.amazon.com/IAM/latest/UserGuide/Policycontext.html to understand what policy context are, where they can be used and how they are interpreted. The evaluate method expects a context parameter which is a nested object that translates to colon-separated context.

Example:

var context = {
    req: {
      IpAddress: '10.0.20.51',
      UserName: 'testuser',
    },
    session: {
      LoggedInDate: '2015-09-29T15:12:42Z',
  },
};

This would translate to the context req:IpAddress, req:UserName and session:LoggedInDate.


The MIT License (MIT)

Copyright (c) 2018 Moritz Onken

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.