Giter VIP home page Giter VIP logo

aws-samples / aws-infra-policy-as-code-with-terraform Goto Github PK

View Code? Open in Web Editor NEW
30.0 3.0 11.0 221 KB

Implements OPA-based preventive security controls for AWS Infrastructure using Terraform Infrastructure as Code (IaC), that can establish a security baseline and safeguard resources before deployment into the AWS Accounts and reduce security risks.

License: Apache License 2.0

Open Policy Agent 100.00%
aws compliance compliance-as-code infra opa open-policy-agent policy-as-code rego terraform

aws-infra-policy-as-code-with-terraform's Introduction

Coverage Check

Policy-as-Code

This repo contains Open Policy Agent (OPA) policies to test AWS infrastructure against terraform plan.

Why OPA?

OPA provides a powerful policy engine that helps organizations enforce fine-grained policies across their applications and infrastructure, improving security, compliance, and policy management capabilities.

Benefits of using this repo policies:

Implementing OPA-based preventive security controls for Terraform Infrastructure as Code (IaC) can establish a security baseline and safeguard resources before deployment into the AWS Accounts and reduce security risks.

Introduction

What is OPA?

The OPA is an open source, general-purpose policy engine that unifies policy enforcement across the stack. OPA provides a high-level declarative language that lets you specify policy as code and simple APIs to offload policy decision-making from your software.

To learn more, refer

Folder structure

  • policy-as-code/OPA/policy/aws/<service_name> - contains the actual policy files written in rego language for each AWS service.
  • policy-as-code/OPA/policy/template - contains the sample files for each OPA rule for AWS service.
  • policy-as-code/OPA/policy/common.utils.rego - utils package for common code/functions used in actual rule rego files.

Template folder files

  1. aws-service_name-policy_id.mock.json: Terraform mock policy file, that will be used for checking rego code. i.e. ./policy/aws/efs/aws-efs-m-2.mock.json

  2. aws-service_name-policy_id.rego: Rego policy file i.e. ./policy/aws/efs/aws-efs-m-2.rego

  3. aws-service_name-policy_id.test.rego Rego tests file. Valid cases & invalid cases i.e. ./policy/aws/efs/aws-efs-m-2.test.rego

File naming convention:

"aws-<service_name>-<type_of_control>-<policy_id>"

  • service_name - any of the AWS services E.g., s3, efs, dynamodb, etc. - aws-s3-<type_of_control>-<policy_id>
  • type_of_control - can be any one of (m, r) - m refers to mandatory control, r for recommended control - aws-s3-m-<policy_id>
  • policy_id - can be a number E.g., 1, 2, 3 - aws-s3-m-1 or aws-s3-r-1

Usage

Pre-requisites:

  1. Install terraform
  2. Install opa
  3. Install conftest

Download the repo and do the following:

  1. Write your own terraform code and generate plan using below commands:

    terraform init 
    terraform plan -input=false -refresh -no-color -out=/tmp/planfile 
    terraform show -json /tmp/planfile > /tmp/plan.json
    
    
  2. Run specific OPA rule against the generated terraform plan use:

    Go to the directory of the repo - ~/policy-as-code/OPA/policy (Note: check the path correctly)

    opa eval -i <terraform_plan_json_file_path> -d <OPA_rule_rego_file_path> -d <common_utils_file_path> "data.aws.<service_name>.<policy_id>.deny"
    

    E.g.

    cd ~/policy-as-code/OPA/policy
    opa eval -i /tmp/plan.json -d aws/efs/aws-efs-m-1.rego -d common.utils.rego "data.aws.efs.m2.deny"
    
  3. Run all OPA rules in this repository against the generated terraform plan use:

    Go to the directory of the repo - ~/policy-as-code/OPA/ (Note: check the path correctly)

    conftest test  <terraform_plan_json_file_path> -o table --all-namespaces -p <OPA_rule_policy_dir_file_path>
    

    E.g.

    cd ~/policy-as-code/OPA/
    conftest test /tmp/tfplan.json -o table --all-namespaces -p policy/
    
  4. If opa evaluations are done successfully against the generated plan, you can safely deploy the infrastructure. If not, modify terraform code to comply with the policies defined.

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

aws-infra-policy-as-code-with-terraform's People

Contributors

amazon-auto avatar vijayadiddi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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

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.