Giter Club home page Giter Club logo

cloudpatrol's Introduction

cloudpatrol.png

Policy as Code for the Cloud Development Kit

Cloud Patrol let's you define common policies with remediation strategies for your AWS CDK stacks and enforce them across your CDK stacks / applications.

NB: This is an alpha release - Everything might change.

Use Cases

Make sure your Cloud resources are:

  • Tagged properly
  • Secure by default
  • Following naming conventions
  • Within your budget
  • Not provisioned with hardcoded secrets
  • Pretty much whatever you can think of :)

Geetting Started

yarn add cloudpatrol

Example

Given this example:

import * as cdk from '@aws-cdk/core';
import { ExampleStack } from '../lib/example-stack';
import { AwsCdkPatrol } from 'cloudpatrol/lib'
import { awsDefaults } from 'cloudpatrol/policies/aws/packs/good-defaults'

const app = new cdk.App();
const stack = new ExampleStack(app, 'ExampleStack');

const cloudPatrol = new AwsCdkPatrol(awsDefaults)
cloudPatrol.check(stack)

We can do the following:

example

Check the full example.

Reports

Currently, there are two reporting mechanisms:

AWS CDK inline report

As part of your normal CDK commands (e.g. cdk synth --app bin/example.js), will perform reporting on the Construct nodes itself and stop the synth process on errors.

Terminal Report

For CI / CD workflows and local testing, just execute your CDK app directly with node (e.g. node ./bin/example.js). This is great for dedicated validation of policies without the synthesized output.

Custom Reporting

Hasn't been implemented, yet. But it's on the agenda, and probably possible right now with a bit of effort.

Policies

Full Example

/**
 * This Policy ensures that a bucket is properly versioned
 *
 * @cloudformationResource AWS::S3::Bucket
 * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-versioningconfig.html
 */
export class BucketVersioningPolicy extends Policy implements PolicyInterface {  
  public policyName = 'Bucket Versioning'
  public description = 'This ensures that a bucket is properly versioned'
  public link = 'https//docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-versioningconfig.html'
  public scope = s3.CfnBucket
  
  public validator(node: s3.CfnBucket, reporter: Reportable): void { 
    if (!node.versioningConfiguration || 
      (!cdk.Tokenization.isResolvable(node.versioningConfiguration) && node.versioningConfiguration.status !== 'Enabled')) {
      reporter.addWarning(node, this, 'Bucket versioning is not enabled');
    }
  }
}

Implemented Policies

Custom Policies

Policies have to follow this schema

class YourCustomPolicy extends Policy implements PolicyInterface {
  //...
}

Scope

There are two options to define the scope of a Policy:

Define an explicit scope:

class YourCustomPolicy extends Policy implements PolicyInterface {
  //...
  public scope = s3.CfnBucket
  //...
}

Overwrite isApplicable:

class YourCustomPolicy extends Policy implements PolicyInterface {
  //...
  public isApplicable(node: cdk.Resource): boolean {
    // your custom logic here
  }
  //...

Policy Validation Logic

class YourCustomPolicy extends Policy implements PolicyInterface {
  //...
  public validator(node: s3.CfnBucket, reporter: Reportable, context: PolicyContext): void { 
    // your custom logic here.
  }
  //...

Found issues can be reported via the reporter object. You can report multiple issues per Policy. There are three different issue severities:

  • Info
  • Warning
  • Error

context is persistent across the entire Stack validation and can be passed in for dynamic information.

How does it work?

Cloud Patrol makes use of Aspects to visit all nodes in a given Construct (e.g. your stack). Aspects will be applied in the prepare stage, which will be called before synthesizing the stack. That's great if you're going to synthesize anyway. However, if you just wanna run the Cloud Patrol checks, we have to invoke the preparation by ourselves. Something along the lines of this:

  stack.node.applyAspect(this);
  cdk.ConstructNode.prepare(stack.node);

Roadmap

  • Simplify Policy definition
  • Drop dependency to aws-cdk/core where possible, extract the rest to dedicated package
  • Publish policies as separate package (e.g. @cloudpatrol/aws-policies)
  • Implement remediation strategies
  • Documentation
  • Policy generator
  • Modularize and detangle Reporter to allow multiple ways of reporting
  • Github Actions for easy integration
  • .cloudpatrol file?
  • Provide more policies out of the box
  • CLI which autodetects Stacks for inspection
  • Integration tests against the last X releases of the AWS CDK
  • Integrate supported languages of jsii
  • Integrate in CDK based frameworks like cdk8s and terrastack

cloudpatrol's People

Contributors

skorfmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.