Skip to content

Commit bbb73bb

Browse files
authored
Added github action for black (#26)
* Added black linter * Formatted with black linter --------- Co-authored-by: Cibin Mathew <[email protected]>
1 parent 085458a commit bbb73bb

18 files changed

+1154
-830
lines changed

.github/workflows/lint.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://black.readthedocs.io/en/stable/integrations/github_actions.html
2+
3+
name: Lint - black
4+
5+
on: [push, pull_request]
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: psf/[email protected]

iam_check/application_error.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
SPDX-License-Identifier: MIT-0
44
"""
5+
6+
57
class ApplicationError(Exception):
68
pass
79

810

911
class SchemaValidationError(ApplicationError):
10-
pass
12+
pass

iam_check/argument_actions.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
SPDX-License-Identifier: MIT-0
44
"""
5+
56
import argparse
67

7-
from .lib.reporter import ResourceOrCodeFindingToIgnore, ResourceAndCodeFindingToIgnore, \
8-
AllowedExternalArn, AllowedExternalPrincipal
8+
from .lib.reporter import (
9+
ResourceOrCodeFindingToIgnore,
10+
ResourceAndCodeFindingToIgnore,
11+
AllowedExternalArn,
12+
AllowedExternalPrincipal,
13+
)
914
from .tools import regex_patterns
1015

1116

@@ -28,8 +33,8 @@ class ParseFindingsToIgnoreFromCLI(argparse.Action):
2833
a combination of both in the form MyResource.FindingA
2934
"""
3035

31-
def __call__(self, _, namespace, values, option_string=None):
32-
values = values.split(',')
36+
def __call__(self, _, namespace, values, option_string=None):
37+
values = values.split(",")
3338

3439
findings_to_ignore = parse_findings_to_ignore(values)
3540

@@ -47,7 +52,9 @@ def parse_findings_to_ignore(values_as_list):
4752
if "." in value:
4853
resource_and_code = value.split(".", 1)
4954
# a split must have at least two members of the array, so no need to validate
50-
finding_to_ignore = ResourceAndCodeFindingToIgnore(resource_and_code[0], resource_and_code[1])
55+
finding_to_ignore = ResourceAndCodeFindingToIgnore(
56+
resource_and_code[0], resource_and_code[1]
57+
)
5158
else:
5259
finding_to_ignore = ResourceOrCodeFindingToIgnore(value)
5360

@@ -63,7 +70,7 @@ class ParseAllowExternalPrincipalsFromCLI(argparse.Action):
6370
"""
6471

6572
def __call__(self, _, namespace, values, option_string=None):
66-
values = values.split(',')
73+
values = values.split(",")
6774

6875
allowed_external_principals = parse_allow_external_principals(values)
6976

@@ -88,10 +95,11 @@ def parse_allow_external_principals(values_as_list):
8895

8996
return allowed_external_principals
9097

98+
9199
class ParseListFromCLI(argparse.Action):
92100
def __call__(self, _, namespace, values, option_string=None):
93-
values = values.split(',')
101+
values = values.split(",")
94102
if values is None:
95103
setattr(namespace, self.dest, None)
96104
values = [value.strip() for value in values]
97-
setattr(namespace, self.dest, values)
105+
setattr(namespace, self.dest, values)

iam_check/client.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,44 @@
22
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
SPDX-License-Identifier: MIT-0
44
"""
5+
56
import boto3
67
from botocore.config import Config
78

89
config = Config(
9-
retries={
10-
# this number was chosen arbitrarily, tweak as necessary
11-
'max_attempts': 30,
12-
'mode': 'standard'
13-
}
10+
retries={
11+
# this number was chosen arbitrarily, tweak as necessary
12+
"max_attempts": 30,
13+
"mode": "standard",
14+
}
1415
)
1516

1617

1718
def get_account_and_partition(region):
18-
"""
19-
Pull the account and partition from the credentials used to execute the validator
20-
"""
19+
"""
20+
Pull the account and partition from the credentials used to execute the validator
21+
"""
2122

22-
sts_client = build('sts', region)
23-
identity = sts_client.get_caller_identity()
24-
account_id = identity['Account']
23+
sts_client = build("sts", region)
24+
identity = sts_client.get_caller_identity()
25+
account_id = identity["Account"]
2526

26-
parts = identity['Arn'].split(':')
27-
partition = parts[1]
27+
parts = identity["Arn"].split(":")
28+
partition = parts[1]
2829

29-
return account_id, partition
30+
return account_id, partition
3031

3132

3233
def build(service_name, region_name, client_config=None):
33-
if client_config is None:
34-
client_config = config
35-
session = boto3.Session(profile_name=profile_name, region_name=region_name)
36-
return session.client(service_name, config=client_config)
34+
if client_config is None:
35+
client_config = config
36+
session = boto3.Session(profile_name=profile_name, region_name=region_name)
37+
return session.client(service_name, config=client_config)
3738

3839

3940
profile_name = None
4041

4142

4243
def set_profile(profile):
43-
global profile_name
44-
profile_name = profile
44+
global profile_name
45+
profile_name = profile

iam_check/config.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,58 @@
33
import sys
44

55
# logging configuration
6-
LOGGER = logging.getLogger('iam-policy-validator-for-terraform')
6+
LOGGER = logging.getLogger("iam-policy-validator-for-terraform")
77

88
# AWS Account ID to use when unknown
9-
awsAccount = '123456789012'
9+
awsAccount = "123456789012"
1010

11-
#IAM Policy checks to run
11+
# IAM Policy checks to run
1212
# The default is to run all checks if thhe list is empty
1313
# iamChecks = []
1414

15-
#IAM policy resources
15+
# IAM policy resources
1616
iamPolicyAttributes = {}
1717

18-
#Generate fake ARN
18+
# Generate fake ARN
1919
# default substitube is {<key>?<default>}
2020
arnServiceMap = {}
2121

2222
validatePolicyResourceType = {}
2323

24+
2425
def configure_logging(enable_logging):
2526
console_handler = logging.StreamHandler(sys.stdout)
26-
#console_handler.setLevel(logging.DEBUG)
27+
# console_handler.setLevel(logging.DEBUG)
2728

2829
LOGGER.setLevel(logging.INFO)
2930

3031
# log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
31-
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
32+
log_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
3233
console_handler.setFormatter(log_formatter)
3334
LOGGER.propagate = False
3435
# for handler in LOGGER.handlers:
3536
# LOGGER.removeHandler(handler)
3637
LOGGER.addHandler(console_handler)
3738
if not enable_logging:
3839
LOGGER.disabled = True
39-
def load_config_yaml(file, exclude_resource_type = []):
40+
41+
42+
def load_config_yaml(file, exclude_resource_type=[]):
4043
global arnServiceMap
4144
global iamPolicyAttributes
4245
global validatePolicyResourceType
43-
44-
with open(file, 'r') as fh:
46+
47+
with open(file, "r") as fh:
4548
data = yaml.safe_load(fh)
4649

47-
arnServiceMap = data.get('arnServiceMap', arnServiceMap)
48-
if 'arnServiceMap' in data:
49-
arnServiceMap = data['arnServiceMap']
50-
51-
if 'iamPolicyAttributes' in data:
52-
iamPolicyAttributes = data['iamPolicyAttributes']
50+
arnServiceMap = data.get("arnServiceMap", arnServiceMap)
51+
if "arnServiceMap" in data:
52+
arnServiceMap = data["arnServiceMap"]
53+
54+
if "iamPolicyAttributes" in data:
55+
iamPolicyAttributes = data["iamPolicyAttributes"]
5356
for exclude_type in exclude_resource_type:
5457
del iamPolicyAttributes[exclude_type]
55-
56-
if 'validatePolicyResourceType' in data:
57-
validatePolicyResourceType = data['validatePolicyResourceType']
58-
58+
59+
if "validatePolicyResourceType" in data:
60+
validatePolicyResourceType = data["validatePolicyResourceType"]

0 commit comments

Comments
 (0)