Skip to main content

A comprehensive tool for linting and validating AWS IAM policies in JSON and YAML formats using Parliament

Project description

IAM Policy Lint

A comprehensive tool for linting and validating AWS IAM (Identity and Access Management) policies in JSON, YAML, and embedded formats. Built around the powerful Parliament library, this tool helps you identify security issues, policy problems, and structural errors in your IAM policies.

Features

  • 🔍 Deep Policy Analysis: Uses Parliament to detect security issues and policy problems
  • 📝 Structure Validation: Validates basic IAM policy structure and syntax
  • 🔧 Multiple Formats: Supports JSON and YAML IAM policies, plus embedded policies in YAML
  • 🎯 Embedded Policy Support: Extract and lint IAM policies embedded in Kubernetes manifests, Terraform, CloudFormation, etc.
  • 📁 Directory Scanning: Lint all policies in a directory at once
  • 🎯 Severity Filtering: Filter findings by severity level (CRITICAL, HIGH, MEDIUM, LOW)
  • 📊 Multiple Output Formats: Human-readable text or machine-readable JSON output
  • 🖥️ CLI Interface: Easy-to-use command-line interface
  • 🔗 Pre-commit Integration: Automated policy checking in Git workflows

Installation

Prerequisites

  • Python 3.12+
  • pyenv (recommended for Python version management)
  • uv (for package management)

Setup

  1. Clone the repository:

    git clone https://github.com/yourusername/iam-policy-lint.git
    cd iam-policy-lint
    
  2. Set Python version (if using pyenv):

    pyenv local 3.12
    
  3. Install dependencies:

    uv sync
    
  4. Install the package (for development):

    uv pip install -e .
    

Usage

Command Line Interface

Lint a single policy file

iam-policy-lint lint examples/policy.json

# Lint a YAML policy
iam-policy-lint lint examples/policy.yaml

# With severity filtering and JSON output
iam-policy-lint lint examples/policy.json --severity HIGH --format json

Validate policy structure

# Basic structure validation
iam-policy-lint validate examples/policy.json

# Validation with JSON output
iam-policy-lint validate examples/policy.json --format json

Lint directory of policies

# Lint all policies in a directory
iam-policy-lint lint-dir examples/

# With file pattern and severity filtering
iam-policy-lint lint-dir examples/ --pattern "*.yaml" --severity MEDIUM

# Directory linting with JSON output
iam-policy-lint lint-dir examples/ --format json

Lint embedded policies in YAML files

# Lint policies embedded in Kubernetes manifests, Terraform, CloudFormation, etc.
iam-policy-lint lint-embedded examples/kubernetes-manifest.yaml

# Lint with custom key path
iam-policy-lint lint-embedded examples/terraform.yaml --key-path "data.policy_document"

# Lint with multiple key paths
iam-policy-lint lint-embedded examples/config.yaml \
  --key-path "spec.policies[].document" \
  --key-path "metadata.annotations.\"iam.policy\""

# Only show critical issues
iam-policy-lint lint-embedded examples/manifest.yaml --severity CRITICAL

Example YAML with embedded IAM policy:

# Kubernetes ServiceAccount with IAM policy annotation
apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-service-account
  annotations:
    "iam.policy": |
      {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Effect": "Allow",
            "Action": ["s3:GetObject"],
            "Resource": "arn:aws:s3:::my-bucket/*"
          }
        ]
      }

Python API

from iam_policy_lint import IAMLinter, IAMValidator

# Initialize linter and validator
linter = IAMLinter()
validator = IAMValidator()

# Lint a policy file
findings = linter.lint_file("path/to/policy.json")
for finding in findings:
    print(f"{finding['severity']}: {finding['title']}")

# Validate policy structure
errors = validator.validate_file("path/to/policy.json")
for error in errors:
    print(f"{error['severity']}: {error['message']}")

# Lint a policy dictionary directly
policy = {
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Action": "*",
        "Resource": "*"
    }]
}

findings = linter.lint_policy(policy)
validation_errors = validator.validate_policy(policy)

# Lint embedded policies from YAML
embedded_findings = linter.lint_embedded_policies("path/to/manifest.yaml")
for finding in embedded_findings:
    print(f"Embedded policy issue: {finding['title']}")

Example Output

Text Output (Default)

🔍 Linting results for: examples/overly-permissive.json
============================================================

🔴 Issue #1 - HIGH
Title: Wildcard action
Issue: WILDCARD_ACTION
Description: Action contains a wildcard that allows all actions
Location: Statement[0].Action

🔴 Issue #2 - HIGH  
Title: Wildcard resource
Issue: WILDCARD_RESOURCE
Description: Resource contains a wildcard that allows access to all resources
Location: Statement[0].Resource

JSON Output

[
  {
    "issue": "WILDCARD_ACTION",
    "title": "Wildcard action", 
    "description": "Action contains a wildcard that allows all actions",
    "severity": "HIGH",
    "location": "Statement[0].Action",
    "detail": null
  }
]

Policy Examples

The examples/ directory contains sample IAM policies:

  • valid-policy.json - A well-structured policy with specific permissions
  • overly-permissive.json - A policy with security issues (wildcards)
  • policy.yaml - A YAML-formatted policy
  • invalid-policy.json - A structurally invalid policy

Development

Running Tests

# Run all tests
uv run pytest

# Run tests with coverage
uv run pytest --cov=src/iam_policy_lint

# Run specific test file
uv run pytest tests/test_validator.py

Code Quality

# Format code
uv run black src/ tests/

# Lint code
uv run flake8 src/ tests/

# Type checking
uv run mypy src/

Dependencies

Core Dependencies

Development Dependencies

  • pytest - Testing framework
  • pytest-cov - Coverage reporting
  • black - Code formatter
  • flake8 - Linter
  • mypy - Type checker

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite and ensure all tests pass
  6. Submit a pull request

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iam_policy_lint-0.3.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iam_policy_lint-0.3.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file iam_policy_lint-0.3.1.tar.gz.

File metadata

  • Download URL: iam_policy_lint-0.3.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for iam_policy_lint-0.3.1.tar.gz
Algorithm Hash digest
SHA256 31201f49521e2919c6751589ff4953e1e694d7a74df18c2016a567e5f89746b3
MD5 90f3016dcd7e12a07fc410fdf15f4732
BLAKE2b-256 a10eb1eaf8067abf7b3d2fe459fd1ff7397701dae0d7083d85891571bd79fb14

See more details on using hashes here.

File details

Details for the file iam_policy_lint-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for iam_policy_lint-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f8071b4c9964464475b2e9358c935da85495560e736b77603c88db2a9406e86
MD5 62ed984f421ae2c6cb759fc0661844f4
BLAKE2b-256 a16822681bbc261d6d33e046ad98c84ca9bbe9bef17a9c439343f7193bb780e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page