Giter Club home page Giter Club logo

tfstate-parser's Introduction

Terraform State Compliance Checker

This Python application parses a Terraform state file and checks its compliance based on a predefined rule JSON file. The application includes a TFState class for parsing Terraform state files and a ComplianceChecker class for checking compliance.

Features

  • Parse Terraform state files and extract resources and providers.
  • Check compliance of Terraform state files against a set of predefined rules in a JSON file.
  • Log compliance checking process for debugging purposes.

Requirements

  • Python 3.7 or higher

Usage

  1. Ensure you have Python 3.7 or higher installed on your system.
  2. Place your Terraform state file (e.g., example.tfstate) and your rule JSON file (e.g., rules.json) in the same directory as the script.
  3. Adjust the main function in the script with the location of your Terraform state file and rule JSON file.
  4. Run the script using the following command: python your_script_name.py
  5. The script will generate an output JSON file (output.json) containing the compliance results.

Classes

TFState

This class is used to parse a Terraform state file and extract the resources and providers.

Attributes

  • providers: A list of providers used in the Terraform state file.
  • resources: A dictionary of resources used in the Terraform state file.
  • statefile: The path to the Terraform state file.

ComplianceChecker

This class is used to check the compliance of a Terraform state file against a compliance file.

Example

Input rules.json

{
    "provider": "hashicorp/aws",
    "resource_type": "aws_s3_bucket",
    "description": "S3 bucket must have default encryption",
    "compliance_level": "check",
    "condition": {
      "operator": "and",
      "rules": [
        {
          "key": "server_side_encryption_configuration",
          "operator": "exists",
          "value": true
        },
        {
          "key": "server_side_encryption_configuration.0.rule",
          "operator": "contains",
          "value": {
            "apply_server_side_encryption_by_default": [
              {
                "kms_master_key_id": "",
                "sse_algorithm": "AES256"
              }
            ],
            "bucket_key_enabled": false
          }
        },
        {
          "key": "versioning.0.enabled",
          "operator": "matches",
          "value": false
        }
      ]
    }
  }  

The following example demonstrates how to use the TFState and ComplianceChecker classes to parse a Terraform state file and check its compliance:

tfstate = TFState("example.tfstate")
cc = ComplianceChecker()
result = cc.check_compliance(tfstate.resources, "rules.json")

The result variable will contain a dictionary with the compliance results. You can then save it as a JSON file or use it for further processing.

{
    "rule_name": "S3 bucket must have default encryption",
    "compliance_level": "warning",
    "resource_type": "aws_s3_bucket",
    "resource_id": "dummy-example-bucket",
    "compliance_status": true,
    "reason": ""
}

tfstate-parser's People

Contributors

moerphyk avatar axa-marvin-kraus avatar

Watchers

Oliver Götz 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.