From 55553dee13dec9a93ae745558cdb4203f7fafcc7 Mon Sep 17 00:00:00 2001 From: Sanket Saurav Date: Mon, 16 Sep 2019 11:34:54 +0530 Subject: [PATCH] Fix some bug risks and code quality issues Changes: - In `hammer/library/aws/s3.py`, use `in` operator to check if variable is equal to either of two values. - In `hammer/tools/ddb_inject_credentials.py`, use `is` to compare with `None`. - In `hammer/reporting-remediation/bot/commands.py`, remove unnecessary `else` used after `raise`. - In `hammer/library/aws/s3.py` and `hammer/library/ddb_issues.py`, fix indentation not multiple of four. Also added a `.deepsource.toml` configuration file to run continuous static analysis on the repository with DeepSource. --- .deepsource.toml | 19 +++++++++++++++++++ hammer/library/aws/s3.py | 5 ++--- hammer/library/ddb_issues.py | 2 +- hammer/reporting-remediation/bot/commands.py | 6 ++---- hammer/tools/ddb_inject_credentials.py | 4 ++-- 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 00000000..bc18d85a --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,19 @@ +# generated by deepsource.io + +version = 1 + +test_patterns = [ + 'tests/**' +] + +exclude_patterns = [ + 'deployment/**', + 'docs/**' +] + +[[ analyzers ]] + name = 'python' + enabled = true + + [ analyzers.meta ] + max_line_length = 100 diff --git a/hammer/library/aws/s3.py b/hammer/library/aws/s3.py index ddd1329b..372a4a0d 100755 --- a/hammer/library/aws/s3.py +++ b/hammer/library/aws/s3.py @@ -31,8 +31,7 @@ def get_public_acls(acl): # http://acs.amazonaws.com/groups/global/AuthenticatedUsers # http://acs.amazonaws.com/groups/global/AllUsers who = grant["Grantee"]["URI"].split("/")[-1] - if who == "AllUsers" or \ - who == "AuthenticatedUsers": + if who in ("AllUsers", "AuthenticatedUsers"): perm = grant["Permission"] # group all permissions (READ(_ACP), WRITE(_ACP), FULL_CONTROL) by AWS predefined groups public_acls.setdefault(who, []).append(perm) @@ -259,7 +258,7 @@ def set_bucket_encryption(s3_client, bucket, kms_master_key_id=None): :return: nothing """ if kms_master_key_id: - rules = [ + rules = [ { 'ApplyServerSideEncryptionByDefault': { 'SSEAlgorithm': 'aws:kms', diff --git a/hammer/library/ddb_issues.py b/hammer/library/ddb_issues.py index d9ae7de2..287f64ff 100755 --- a/hammer/library/ddb_issues.py +++ b/hammer/library/ddb_issues.py @@ -234,7 +234,7 @@ def __init__(self, *args): class PublicAMIIssue(Issue): - def __init__(self, *args): + def __init__(self, *args): super().__init__(*args) diff --git a/hammer/reporting-remediation/bot/commands.py b/hammer/reporting-remediation/bot/commands.py index 7d79a24c..40f3f762 100644 --- a/hammer/reporting-remediation/bot/commands.py +++ b/hammer/reporting-remediation/bot/commands.py @@ -226,8 +226,7 @@ def parse_regions(response): for region in requested_regions: if region.strip() not in supported_regions: raise Exception(f'Wrong region {region}. Allowed values: \n ```{allowed_values}```') - else: - regions.append(region.strip()) + regions.append(region.strip()) return regions @@ -241,8 +240,7 @@ def parse_security_features(response): for sec_feature in requested_security_features: if sec_feature.strip() not in supported_features: raise Exception(f"Unsupported security feature {sec_feature}. Allowed values: \n ```{allowed_values}```") - else: - security_features.append(sec_feature.strip()) + security_features.append(sec_feature.strip()) return security_features diff --git a/hammer/tools/ddb_inject_credentials.py b/hammer/tools/ddb_inject_credentials.py index 0d91a85c..c66e31d9 100755 --- a/hammer/tools/ddb_inject_credentials.py +++ b/hammer/tools/ddb_inject_credentials.py @@ -58,11 +58,11 @@ } } - if args.hammer_api_token != None: + if args.hammer_api_token is not None: # generate new secret if secret value is not set creds["api"] = {"token": secrets.token_hex() if args.hammer_api_token == -1 else args.hammer_api_token} - if args.hammer_api_url != None: + if args.hammer_api_url is not None: creds["api"]["url"] = args.hammer_api_url if not creds: