1-
21import json
32import urllib3
43import hashlib
76
87def handler (event , context ):
98 cspm_url = event .get ('ApiUrl' )
10- ac_url = event .get ('AutoConnectApiUrl' )
119 aqua_api_key = event .get ('AquaApiKey' )
1210 aqua_secret = event .get ('AquaSecretKey' )
1311 role_arn = event .get ('RoleArn' )
@@ -17,38 +15,59 @@ def handler(event, context):
1715 aws_account_id = context .invoked_function_arn .split (":" )[4 ]
1816
1917 try :
20- print ('creating a new cspm key' )
18+ cspm_key_id = get_cspm_key_id (aqua_api_key , aqua_secret , cspm_url , role_arn )
19+ is_already_cspm_client = True
20+ print (f'Existing CSPM key found: { cspm_key_id } ' )
21+ except Exception as key_not_found :
22+ print (f'No existing key found' )
23+ print ('Creating new CSPM key' )
2124 is_already_cspm_client = create_cspm_key (
22- cspm_url , ac_url , aqua_api_key , aqua_secret ,
25+ cspm_url , aqua_api_key , aqua_secret ,
2326 role_arn , external_id , group , account_id , aws_account_id
2427 )
25- return {"IsAlreadyCSPMClient" : is_already_cspm_client }
2628
27- except Exception as e :
28- print (f"error: { e } " )
29- return {"error" : e }
29+ return {"IsAlreadyCSPMClient" : is_already_cspm_client }
3030
3131
3232def get_signature (aqua_secret , tstmp , path , method , body ):
3333 enc = tstmp + method + path + body
34- print (f'enc: { enc } ' )
3534 enc_b = bytes (enc , 'utf-8' )
3635 secret = bytes (aqua_secret , 'utf-8' )
3736 sig = hmac .new (secret , enc_b , hashlib .sha256 ).hexdigest ()
3837 return sig
3938
39+
4040def http_request (url , headers , method , body = None ):
41- http = urllib3 .PoolManager (cert_reqs = 'CERT_REQUIRED' )
41+ if body is None :
42+ body = {}
43+
44+ http = urllib3 .PoolManager (cert_reqs = 'CERT_NONE' )
4245
4346 try :
4447 response = http .request (method , url , body = body , headers = headers )
45- data = json . loads ( response . data . decode ( 'utf-8' ))
48+ return response
4649 except Exception as e :
47- print (f'could not parse event data; { e } ' )
48- data = {}
49- return data
50+ print ('Failed to send http request; {}' .format (e ))
51+ return None
52+
53+
54+ def get_cspm_key_id (aqua_api_key , aqua_secret , cspm_url , role_arn ):
55+ tstmp = str (int (time .time () * 1000 ))
56+ sig = get_signature (aqua_secret , tstmp , "/v2/keys" , "GET" , '' )
57+ headers = {"X-API-Key" : aqua_api_key , "X-Signature" : sig , "X-Timestamp" : tstmp }
58+
59+ response = http_request (cspm_url + "/v2/keys" , headers , "GET" )
60+ json_object = json .loads (response .data )
61+ if response .status not in (200 , 201 ):
62+ raise ValueError (f"Failed to get cspm key id for { role_arn } : { response .message } " )
63+
64+ for key in json_object ['data' ]:
65+ if key ['role_arn' ] == role_arn :
66+ return key ['id' ]
67+ raise Exception ("key not found" )
68+
5069
51- def create_cspm_key (cspm_url , ac_url , aqua_api_key , aqua_secret , role_arn , external_id , group , account_id , aws_account_id ):
70+ def create_cspm_key (cspm_url , aqua_api_key , aqua_secret , role_arn , external_id , group , account_id , aws_account_id ):
5271 body = {
5372 "name" : account_id ,
5473 "cloud" : "aws" ,
@@ -58,7 +77,7 @@ def create_cspm_key(cspm_url, ac_url, aqua_api_key, aqua_secret, role_arn, exter
5877 "group_id" : group
5978 }
6079
61- print (f'body: { body } ' )
80+ print (f'CSPM body: { body } ' )
6281 tstmp = str (int (time .time () * 1000 ))
6382 jsonbody = json .dumps (body , separators = (',' , ':' ))
6483 sig = get_signature (aqua_secret , tstmp , "/v2/keys" , "POST" , jsonbody )
@@ -69,12 +88,12 @@ def create_cspm_key(cspm_url, ac_url, aqua_api_key, aqua_secret, role_arn, exter
6988 }
7089
7190 response = http_request (cspm_url + '/v2/keys' , headers , "POST" , jsonbody )
72- print (f'response: { response } ' )
73- if response .get ('status' , 0 ) != 200 and response .get ('status' , 0 ) != 201 :
74- raise Exception (response .get ('message' , "Internal server error" ))
91+ if response .status not in (200 , 201 ):
92+ raise Exception ("Failed to create cspm key id" , response .data .decode ("utf-8" ))
7593
94+ print (f'CSPM response: { response .data .decode ("utf-8" )} ' )
7695 is_already_cspm_client = False
77- if response .get ( ' status' , 0 ) == 200 :
96+ if response .status == 200 :
7897 is_already_cspm_client = True
7998
8099 return is_already_cspm_client
0 commit comments