-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
154 lines (134 loc) · 5.25 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Core python packages
import json
import logging
import sys
import os
# 3rd party packages - see requirements.txt
import boto3
import botocore.exceptions
import yaml
# src python
from utils import *
def open_input(filepath: str = None) -> str:
"""
Performs a check on the --filepath argument to attempt to find the
template file for the custom control and custom framework.
It looks in three locations.
* A complete path to the file
* A file in /frameworks
* An S3 URL
Args:
filepath (*string*, REQUIRED):
A string which defined the path to the template file for the
custom controls and custome framework
Defaults to None.
Raises:
FileNotFoundError: The FileNotFoundError will be raised if it cannot
locate the file
"""
script_dir = os.path.dirname(os.path.realpath(__file__))
framework_dir = os.path.join(script_dir, 'frameworks')
try:
if os.path.isfile(filepath) is True:
logging.info(
'Input file found at {}'.format(filepath))
if filepath.endswith(".json") is True:
contents = open_json(input_file=filepath)
return contents
elif (
filepath.endswith(".yaml") is True
or
filepath.endswith(".yml") is True
):
contents = open_yaml(input_file=filepath)
return contents
else:
logging.info("Incorrect file type. An input file should be yaml or json.")
sys.exit(1)
elif os.path.isfile(os.path.join(framework_dir, filepath)) is True:
filepath = os.path.join(framework_dir, filepath)
logging.info(
'Input file found in {} directory'.format(
os.path.join(framework_dir, filepath)))
if filepath.endswith('.json') is True:
contents = open_json(input_file=filepath)
return contents
elif (
filepath.endswith(".yaml") is True
or
filepath.endswith(".yml") is True
):
contents = open_yaml(input_file=filepath)
return contents
else:
logging.info("Incorrect file type. An input file should be yaml or json.")
sys.exit(1)
elif filepath.startswith('s3://') is True:
logging.info('Input file is in S3 bucket {}'.format(filepath))
if filepath.endswith('.json') is True:
contents = json.load(get_object_from_s3(filepath))
return contents
elif (
filepath.endswith(".yaml") is True
or
filepath.endswith(".yml") is True
):
contents = yaml.safe_load(get_object_from_s3(filepath))
return contents
else:
logging.info("Incorrect file type. An input file should be yaml or json.")
sys.exit(1)
else:
raise FileNotFoundError
except FileNotFoundError as error:
raise error
def get_object_from_s3(s3_path: str = None) -> dict:
s3 = boto3.client('s3')
try:
bucket_path = s3_path.split('/')
bucket_name = bucket_path[2]
key = bucket_path[(len(bucket_path) - 1)]
response = s3.get_object(
Bucket=bucket_name,
Key=key
)
file_contents = response['Body'].read().decode()
return file_contents
except botocore.exceptions.ClientError as error:
raise error
def open_yaml(input_file: str = None) -> dict:
with open(input_file, 'r', encoding="utf8") as file_object:
contents = yaml.safe_load(file_object)
logging.debug(contents)
return contents
def open_json(input_file: str = None) -> dict:
with open(input_file, 'r', encoding="utf8") as file_object:
contents = json.load(file_object)
logging.debug(contents)
return contents
def get_account_status(region_name=None):
auditmanager_client = boto3.client('auditmanager', region_name=None)
try:
response_get_status = auditmanager_client.get_account_status()
return response_get_status
except botocore.exceptions.ClientError as error:
raise error
def register_account(region_name=None):
auditmanager_client = boto3.client('auditmanager', region_name=region_name)
try:
get_status = get_account_status(region_name=region_name)
if get_status["status"] != "ACTIVE":
response = auditmanager_client.register_account()
return response
except botocore.exceptions.ClientError as error:
raise error
def check_framework_existence(custom_report_name, region_name=None):
auditmanager_client = boto3.client('auditmanager', region_name=region_name)
existing_frameworks = (auditmanager_client.list_assessment_frameworks(frameworkType="Custom"))
already_exists = False
for existing in existing_frameworks["frameworkMetadataList"]:
# Checking if the framework being created already exists and
# updating it
if existing["name"] == custom_report_name:
already_exists = True
return already_exists