Package: parliament
parliament is an AWS IAM linting library. It reviews policies looking for problems such as:malformed jsonmissing required elementsincorrect prefix and action namesincorrect resources or conditions for the actions providedtype mismatchesbad policy patternsThis library duplicates (and adds to!) much of the functionality in the web console page when reviewing IAM policies in the browser.  We wanted that functionality as a library.demoInstallationpip install parliamentUsagecat > test.json << EOF
{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action":["s3:GetObject"],
        "Resource": ["arn:aws:s3:::bucket1"]
    }
}
EOF

parliament --file test.jsonThis will output:MEDIUM - No resources match for the given action -  - [{'action': 's3:GetObject', 'required_format': 'arn:*:s3:::*/*'}] - {'actions': ['s3:GetObject'], 'filepath': 'test.json'}This example is showing that the action s3:GetObject requires a resource matching an object path (ie. it must have a "/" in it).The different input types allowed include:--file: Filename--directory: A directory path, for exmaple:--directory . --include_policy_extension json --exclude_pattern ".*venv.*"--aws-managed-policies: For use specifically with the repohttps://github.com/z0ph/aws_managed_policies--auth-details-file: For use with the file returned by "aws iam get-account-authorization-details"--string: Provide a string such as '{"Version": "2012-10-17","Statement": {"Effect": "Allow","Action": ["s3:GetObject", "s3:PutBucketPolicy"],"Resource": ["arn:aws:s3:::bucket1", "arn:aws:s3:::bucket2/*"]}}'Using parliament as a libraryParliament was meant to be used a library in other projects. A basic example follows.from parliament import analyze_policy_string

analyzed_policy = analyze_policy_string(policy_doc)
for f in analyzed_policy.findings:
  print(f)Custom config fileYou may decide you want to change the severity of a finding, the text associated with it, or that you want to ignore certain types of findings.  To support this, you can provide an override config file.  First, create a test.json file:{
    "Version": "2012-10-17",
    "Id": "123",
    "Statement": [
      {
        "Effect": "Allow",
        "Action": "s3:abc",
        "Resource": "*"
      },
      {
        "Effect": "Allow",
        "Action": ["s3:*", "ec2:*"],
        "Resource": "arn:aws:s3:::test/*"
      }
    ]
 }This will have two findings:LOW - Unknown action -  - Unknown action s3:abcMEDIUM - No resources match for the given actionThe second finding will be very long, because every s3 and ec2 action are expanded and most are incorrect for the S3 object path resource that is provided.Now create a fileconfig_override.yamlwith the following contents:UNKNOWN_ACTION:
  severity: MEDIUM
  ignore_locations:
  - filepath:
    - testa.json
    - .*.py

RESOURCE_MISMATCH:
  ignore_locations:
  - actions: ".*s3.*"Now run:parliament --file test.json --config config_override.yamlYou will have only one output:MEDIUM - Unknown action -  - Unknown action s3:abcNotice that the severity of that finding has been changed from aLOWto aMEDIUM.  Also, note that the other finding is gone, because the previousRESOURCE_MISMATCHfinding contained anactionselement of["s3:*", "ec2:*"].  The ignore logic converts the value you provide, and the finding value to lowercase,
and then uses your string as a regex.  This means that we are checking ifs3is instr(["s3:*", "ec2:*"])Now renametest.jsontotesta.jsonand rerun the command.  You will no longer have any output, because we are filtering based on the filepath forUNKNOWN_ACTIONand filtering for any filepaths that containtesta.jsonor.py.You can also check the exit status withecho $?and see the exit status is 0 when there are no findings. The exit status will be non-zero when there are findings.You can have multiple elements inignore_locations.  For example,- filepath: "test.json"
  action: "s3:GetObject"
  resource: 
  - "a"
  - "b"
- resource: "c.*"Assuming the finding has these types of values in thelocationelement, this will ignore any finding that matches the filepath to "test.json" AND action to "s3:GetObject" AND the resource to "a" OR "b".  It will also ignore a resource that matches "c.*".Custom auditorsPrivate AuditorsThis section will show how to create your own private auditor to look for any policies that grant access to either of the sensitive bucketssecretbucketandothersecretbucket.Create a filetest.jsonwith contents:{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::secretbucket/*"
    }
}This is an example of the policy we want to alert on. That policy will normally not generate any findings.Create the fileconfig_override.yamlwith contents:SENSITIVE_BUCKET_ACCESS:
  title: Sensitive bucket access
  description: Allows read access to an important S3 bucket
  severity: MEDIUM
  group: CUSTOMIn theparliamentdirectory (that contains iam_definition.json), create the directoryprivate_auditorsand the fileparliament/private_auditors/sensitive_bucket_access.pyfrom parliament import is_arn_match, expand_action

def audit(policy):
    action_resources = {}
    for action in expand_action("s3:*"):
        # Iterates through a list of containing elements such as
        # {'service': 's3', 'action': 'GetObject'}
        action_name = "{}:{}".format(action["service"], action["action"])
        action_resources[action_name] = policy.get_allowed_resources(action["service"], action["action"])
    
    for action_name in action_resources:
        resources = action_resources[action_name]
        for r in resources:
            if is_arn_match("object", "arn:aws:s3:::secretbucket*", r) or is_arn_match("object", "arn:aws:s3:::othersecretbucket*", r):
                policy.add_finding("SENSITIVE_BUCKET_ACCESS", location={"action": action_name, "resource": r})This will look for any s3 access to the buckets of interest, including not only object access such ass3:GetObjectaccess, but also things likes3:PutBucketAcl.Running against our test file, we'll get the following output:./bin/parliament --file test.json --config config_override.yaml --json

{"issue": "SENSITIVE_BUCKET_ACCESS", "title": "Sensitive bucket access", "severity": "MEDIUM", "description": "Allows read access to an important S3 bucket", "detail": "", "location": {"action": "s3:GetObject", "resource": "arn:aws:s3:::secretbucket/*", "filepath": "test.json"}}You can now decide if this specific situation is ok for you, and choose to ignore it by modifying theconfig_override.yamlto include:ignore_locations:
  - filepath: "test.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"Notice that I had to double-escape the escape asterisk.  If another policy is created, say in test2.json that you'd like to ignore, you can just append those values to the list:ignore_locations:
  - filepath: "test.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"
  - filepath: "test2.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"Or you could do:ignore_locations:
  - filepath:
    - "test.json"
    - "test2.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"Unit tests for private auditorsTo create unit tests for our new private auditor, create the directory./parliament/private_auditors/tests/and create the filetest_sensitive_bucket_access.pythere with the contents:from parliament import analyze_policy_string

class TestCustom():
    """Test class for custom auditor"""

    def test_my_auditor(self):
        policy = analyze_policy_string(
            """{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::secretbucket/*"}}""",
        )
        assert_equal(len(policy.findings), 1)That test ensures that for the given policy (which is granting read access to our secret bucket) one finding will be created.Now when you run./tests/scripts/unit_tests.shthere should be one additional test run.Community auditorsThe process for community auditors is the same as private auditors, except that:The community auditors are located in theparliament/community_auditorsfolder instead of theparliament/private_auditorsThe community auditors are bundled with the package and users can include them in their testing by specifying--include-community-auditorsflag.DevelopmentSetup a testing environmentpython3 -m venv ./venv && source venv/bin/activate
pip3 install -r requirements.txtRun unit tests with:make testRun locally as:bin/parliamentUpdating the privilege infoThe IAM data is obtained from scraping the docshereand parsing this information with beautifulsoup using./utils/update_iam_data.py.Projects that use ParliamentCloudMapper: Has functionality to audit AWS environments and will audit the IAM policies as part of that.tf-parliament: Runs Parliament against terraform filesiam-lint: Github action for linting AWS IAM policy documentsPaco: Cloud orchestration tool that integrates Parliament as a library to verify a project's IAM Policies and warns about findings.ConsoleMe: Web service that makes administering and using multiple AWS accounts easier, that uses Parliament for linting IAM Policiesiamlive: Generates IAM Policies from observing AWS calls through client-side monitoring