-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiftLeftInlineApplication.py
More file actions
224 lines (181 loc) · 8.95 KB
/
ShiftLeftInlineApplication.py
File metadata and controls
224 lines (181 loc) · 8.95 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import requests
import json
import time
import os
from typing import List, Dict, Any
import sys
from requests.exceptions import RequestException, HTTPError
import requests.sessions
from requests.auth import HTTPBasicAuth
import logging
import time
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S'
)
class ShiftLeftInlineException(Exception):
pass
class ShiftLeftInlineData:
def __init__(self):
self.changes = []
self.user_name = ""
self.password = ""
self.clone_dir = ""
self.csp_name = ""
self.environment = ""
self.bps_tenant_id = ""
self.access_token = ""
self.iam_token = ""
def strip_leading_dot_slash(filename: str) -> str:
if filename.startswith("./"):
return filename[2:]
return filename
def clean_filename(filename: str) -> str:
fields = filename.split('/')
# Check if the string was split into multiple parts
if len(fields) == 1:
# If not, return the original string
return filename
else:
# Otherwise, return the last field
return fields[-1]
def perform_shift_left(args: List[str]):
shift_left_inline_data = ShiftLeftInlineData()
shift_left_inline_data.changes = [s.strip() for s in args[0].split(',') if s not in ("buildspec.yml", "azure-mvc-shiftleft-pipeline.yml")]
shift_left_inline_data.user_name = args[1]
shift_left_inline_data.password = args[2]
shift_left_inline_data.clone_dir = args[3]
shift_left_inline_data.csp_name = args[4]
shift_left_inline_data.environment = args[5]
if len(args) == 7:
shift_left_inline_data.bps_tenant_id = args[6]
if not shift_left_inline_data.changes:
logging.info("Exiting, since there are no changes available to perform evaluation")
return
update_iam_token(shift_left_inline_data)
update_access_token(shift_left_inline_data)
violated_files = []
for file in shift_left_inline_data.changes:
response = submit_file_for_scan(strip_leading_dot_slash(file), shift_left_inline_data)
submit_timestamp = time.time()
if response is None:
continue
submit_response = json.loads(response["text"])
logging.debug(response["text"])
status = submit_response["status"]
message = submit_response["message"]
if isinstance(status, int):
raise ShiftLeftInlineException(submit_response)
if status.lower() == "failure":
raise ShiftLeftInlineException(submit_response)
while True:
try:
time.sleep(30)
logging.info(f"Checking the status for the file: {file} (Processing for {time.time() - submit_timestamp} seconds)")
headers = {"x-access-token": shift_left_inline_data.access_token, "Content-Type": "application/json"}
status_response = requests.get(message, headers=headers)
except requests.exceptions.RequestException as e:
if isinstance(e, requests.exceptions.HTTPError) and e.response.status_code == 401:
update_access_token(shift_left_inline_data)
continue
logging.error(f"Error while getting the status of the scan for file: {file} for scan with exception: {e}")
continue
scan_eval_response = status_response.json()
if scan_eval_response["status"] in ("Submitted", "In Progress"):
logging.info(scan_eval_response["message"])
continue
if scan_eval_response["status"].lower() == "failure":
raise ShiftLeftInlineException(scan_eval_response["message"])
violation_details = scan_eval_response["message"] # json.loads(scan_eval_response["message"])
violation_count = violation_details["violation_count"]
policies_violated = violation_details["policies_violated"]
file_name = violation_details["file_name"]
if violation_count > 0:
violated_files.append(file_name)
logging.info(f"{violation_count} violations were found for the file: {file_name}. Violated the policies: {policies_violated} (Processed for {time.time() - submit_timestamp} seconds)" )
break
elif violation_count == 0:
logging.info(f"No violations were found for the file: {file_name} (Processed for {time.time() - submit_timestamp} seconds)")
break
if violated_files:
raise ShiftLeftInlineException(f"Failing the build, since violations were found for the files: {', '.join(violated_files)}.")
def update_iam_token(shift_left_inline_data):
headers = {
'Content-Type': 'application/json',
'x-auth-username': shift_left_inline_data.user_name,
'x-auth-password': shift_left_inline_data.password
}
auth = HTTPBasicAuth(shift_left_inline_data.user_name, shift_left_inline_data.password)
if shift_left_inline_data.bps_tenant_id:
headers['BPS-TENANT-ID'] = shift_left_inline_data.bps_tenant_id
request = requests.post(shift_left_inline_data.environment + "/shnapi/rest/external/api/v1/token?grant_type=password&token_type=iam", headers=headers, auth=auth)
if request.status_code != 200:
error_msg = f"Unable to fetch IAM token for user: {shift_left_inline_data.user_name}"
#logging.error(error_msg)
raise ShiftLeftInlineException(error_msg)
token_response = request.json()
if token_response:
shift_left_inline_data.iam_token = token_response.get('access_token')
logging.info(f"Successfully received the IAM token for user: {shift_left_inline_data.user_name}")
def update_access_token(shift_left_inline_data):
headers = {
'Content-Type': 'application/json',
'x-iam-token': shift_left_inline_data.iam_token
# 'x-auth-username': shift_left_inline_data.user_name,
# 'x-auth-password': shift_left_inline_data.password
}
if shift_left_inline_data.bps_tenant_id:
headers['BPS-TENANT-ID'] = shift_left_inline_data.bps_tenant_id
request = requests.post(shift_left_inline_data.environment + "/neo/neo-auth-service/oauth/token?grant_type=iam_token", headers=headers)
if request.status_code != 200:
error_msg = f"Unable to fetch access token from IAM token."
#logging.error(error_msg)
raise ShiftLeftInlineException(error_msg)
token_response = request.json()
if token_response:
shift_left_inline_data.access_token = token_response.get('access_token')
logging.info(f"Successfully received the access token from IAM token.")
def format_prepped_request(prepped, encoding=None):
# prepped has .method, .path_url, .headers and .body attribute to view the request
encoding = encoding or requests.utils.get_encoding_from_headers(prepped.headers)
body = prepped.body.decode(encoding) if encoding else '<binary data>'
headers = '\n'.join(['{}: {}'.format(*hv) for hv in prepped.headers.items()])
return f"""{prepped.method} {prepped.path_url} HTTP/1.1 {headers} {body}"""
def submit_file_for_scan(file_name: str, shift_left_inline_data: Dict[str, Any]):
url = f'{shift_left_inline_data.environment}/neo/config-audit/devops/v1/scan?service={shift_left_inline_data.csp_name}'
while True:
try:
payload = {'filename': clean_filename(file_name)}
files = [
('templateFile',(file_name,open(os.path.join(shift_left_inline_data.clone_dir, file_name))))
]
headers = {
'x-access-token': shift_left_inline_data.access_token
}
session = requests.Session()
req = requests.Request('POST', url, headers=headers, data=payload, files=files)
prepped = session.prepare_request(req)
response = session.send(prepped, verify=True)
session.close
files_size = 0
for _, file_info in files:
_, file_object = file_info
file_object.seek(0, os.SEEK_END)
files_size += file_object.tell()
file_object.close()
except RequestException as e:
if isinstance(e, HTTPError) and e.response.status_code == 401:
update_access_token(shift_left_inline_data)
return submit_file_for_scan(file_name, shift_left_inline_data)
logging.error(f"Error while submitting {file_name} for scan due to : {str(e)}")
raise e
else:
if response.status_code == 429:
logging.info(f"Server is busy (reponse code 429), trying {file_name} again in 60 seconds.")
time.sleep(60)
else:
logging.info(f"Successfully submitted {file_name} ({files_size} bytes) for scan")
return {"status_code": response.status_code, "text": response.text}
if __name__ == "__main__":
perform_shift_left(sys.argv[1:])