Giter VIP home page Giter VIP logo

manwaring / serverless-plugin-iam-checker Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 675 KB

A Serverless Framework plugin which automates security checks by preventing overly broad IAM configurations (disallowing the use of * resources and actions, for example)

License: MIT License

TypeScript 93.57% Gherkin 5.70% JavaScript 0.72%
serverless-plugin serverless-framework serverless security iam

serverless-plugin-iam-checker's Introduction

Serverless plugin IAM checker

  1. Overview
  2. Installation and setup
  3. Rule configuration
    1. Default rule configuration
    2. Action rules
    3. Resource rules
    4. Setting rules via serverless.yml
    5. Setting rules via environment variables
  4. Detailed validation logging
  5. Examples

Feedback appreciated! If you have an idea for how this plugin can be improved please open an issue.

Overview

This Serverless Framework plugin checks all generated IAM resources in a serverless project and validates their permission configurations for overly-permissive actions and/or resource references. If IAM resources are invalid per the configured rules then the sls command will fail after the package step, preventing the generated CloudFormation Stack from being deployed to AWS.

Installation and setup

Install and save the package to package.json as a dev dependency:

npm i --save-dev serverless-plugin-iam-checker

Add the package to the serverless.yml plugins section:

plugins:
  - serverless-plugin-iam-checker

By default the plugin uses a restrictive set of rules for action and resource configuration. These rules can be modified using either serverless.yml custom configuration or environment variables.

Rule configuration

Rules are configured separately for actions and resources due to resources generally having a greater need for dynamic references, while actions can almost always be constrained explicitly. If any of the action or resource rules aren't found in environment variables or the serverless.yml custom config section then this plugin will use the default configurations specified in the tables below.

If rule values are found in both environment variables and serverless.yml the plugin will use the environment variable values - this is done to help ensure security compliance in build/test/deploy pipelines where developers generally don't have access to underlying environoment variables (as opposed to serverless.yml, which they typically have unlimited access to modify).

Default rule configuration

actions:
  allowWildcards: false
  allowWildcardOnly: false
  allowedPatterns: []

resources:
  allowWildcards: true
  allowWildcardOnly: false
  allowedPatterns: []
  allowedReferences: []

Action rules

Property Description Example
Allow wildcards Type: boolean
Effect: can actions include wildcards
Default: false
Config: false
Passes: dynamodb:PutItem
Fails: dynamodb:*
Allow wildcard only Type: boolean
Effect: can actions be only wildcards
Default: false
Config: true
Passes: *
Fails: dynamodb:*
Allowed patterns Type: string array
Effect: actions must match a listed pattern
Default: []
Config: ['dynamodb:']
Passes: dynamodb:PutItem
Fails: s3:PutObject

Resource rules

Property Description Example
Allow wildcards Type: boolean
Effect: can resources include wildcards
Default: true
Config: false
Passes: arn:whatever
Fails: arn:*
Allow wildcard only Type: boolean
Effect: can resources be only wildcards
Default: false
Config: true
Passes: *
Fails: arn:*
Allowed patterns Type: string array
Effect: resources must match a listed pattern
Default: []
Config: ['arn:']
Passes: arn:whatever
Fails: whatever
Allowed references Type: string array
Effect: resource references must match a listed pattern
Default: []
Config: ['Ref']
Passes: { 'Ref': 'whatever' }
Fails: { 'Fn::Sub': 'whatever' }

Setting rules via serverless.yml

custom:
  iamChecker: # This key is used by the plugin to pull in the optional rule configuration
    actions:
      allowWildcards: false
      allowWildcardOnly: false
      allowedPatterns:
        - 'dynamodb:'
    resources:
      allowWildcards: true
      allowWildcardOnly: false
      allowedPatterns:
        - 'arn:'
      allowedReferences:
        - 'Ref'
        - 'Fn::Join'
        - 'Fn::Sub'

Setting rules via environment variables

# Actions
IAM_CHECKER_ACTIONS_ALLOW_WILDCARDS=false
IAM_CHECKER_ACTIONS_ALLOW_WILDCARDONLY=false
IAM_CHECKER_ACTIONS_ALLOWED_PATTERNS=['dynamodb:']

# Resources
IAM_CHECKER_RESOURCES_ALLOW_WILDCARDS=true
IAM_CHECKER_RESOURCES_ALLOW_WILDCARDONLY=false
IAM_CHECKER_RESOURCES_ALLOWED_PATTERNS=['arn:']
IAM_CHECKER_RESOURCES_ALLOWED_REFERENCES=['Ref', 'Fn::Join', 'Fn::Sub']

Detailed validation logging

For detailed logs about which rules have caused resources to fail validation rerun your commands with SLS_DEBUG=*. Output similar to this will be logged:

Serverless: Packaging service...
Serverless: Checking IAM permissions...
  IamRoleLambdaExecution has the following validation errors:
    Wildcard-only actions are not allowed
    Wildcards in actions are not allowed
    Actions must match the following patterns: [":"]
    Wildcard-only resources are not allowed
    Resources must match the following patterns: ["arn:"]

Examples

There is one working example of how this package can be used in a simple 'hello world' serverless application:

  1. Plugin with default configuration

serverless-plugin-iam-checker's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar manwaring avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serverless-plugin-iam-checker's Issues

handle defaults and overrides in a better way

  • if specify wildcardonly allowed, should also consider allowwildcards to be true
  • generally how to handle when only some of config is given; should missing go to default? or something else?
  • maybe fix by defaulting patterns/refs to empty?

config in sls yml doesn't work if imported from another file

commented out doesn't work - values not getting set

  # iamChecker: ${self:custom.config.iamChecker}
  iamChecker:
    actions:
      allowWildcardOnly: true
      allowWildcards: true
      allowedPatterns: []
    resources:
      allowWildcardOnly: true
      allowedPatterns: []
      allowedReferences: []

clean up repository

master branch -> main
cucumber -> jest
check all configs
precommit test hooks

Action allowed pattern rule not working

When i am giving value like this ['sns:'] pipeline is throwing
SyntaxError: Unexpected token ' JSON at position 1

And when i am giving like this ["sns:"] pipeline throwing error like iamrole not matching with the action pattern.

Anyone please help me on this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.