Importing reports via AWS Lambda Function within AWS Security Hub
To send scanning data to AWS Security Hub on AppSec Portal
Step 1: Integration Preparation
Step 2: Creating AWS Lambda Function
import json
import boto3
import urllib.request
import urllib3
def lambda_handler(event, context):
# Fetch AppSec Portal API key from AWS Secrets Manager
client_sm = boto3.client('secretsmanager')
appsec_portal_secret_raw = client_sm.get_secret_value(
SecretId="<secret_name>"
)
appsec_portal_api_json = json.loads(appsec_portal_secret_raw["SecretString"])
appsec_portal_api_token = "Token " + appsec_portal_api_json['key']
while True:
try:
url = 'https://<portal_address>/api/v1/scan/import/'
body = {
"file": ("event.json", json.dumps(event)),
"product_name": "AWS",
"product_type": "AWS",
"scanner_name": "AWS Security Hub Scan"
}
data, header = urllib3.encode_multipart_formdata(body)
r = urllib.request.Request(url, data=data)
r.add_header('Authorization', appsec_portal_api_token)
r.add_header('Content-Type', header)
response = urllib.request.urlopen(r)
print(response.getcode())
except Exception as e:
raise e
break
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}Step 3: Running the Function
Last updated