Skip to content

Commit fc785ad

Browse files
committed
code changes to validate JSON input array
1 parent 6336cdb commit fc785ad

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

firehose-template.yaml

+62-7
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,60 @@ Resources:
222222
region = event_data['AWS_REGION']
223223
account_id = event_data['AWS_ACCOUNT_ID']
224224
awsRealm = event_data['AWS_REALM']
225+
common_attributes_str = event_data['CommonAttributes']
226+
isAttributesValid = True
227+
isLogGroupValid = True
228+
229+
try:
230+
attributes = json.loads(common_attributes_str)
231+
if not isinstance(attributes, list):
232+
raise ValueError('CommonAttributes must be a JSON array')
233+
for attribute in attributes:
234+
if not isinstance(attribute, dict):
235+
raise ValueError("Each element in CommonAttributes should be a JSON object.")
236+
if 'AttributeName' not in attribute or 'AttributeValue' not in attribute:
237+
raise ValueError("Each element in CommonAttributes should have 'AttributeName' and 'AttributeValue' keys.")
238+
if not attribute['AttributeName'] or not attribute['AttributeValue']:
239+
raise ValueError("Each element in CommonAttributes should have non-empty 'AttributeName' and 'AttributeValue' values.")
240+
241+
response['UserInputCommonAttributesErrorMessages'] = 'No Errors Found in User Input for setting up custom attributes.'
242+
except Exception as e:
243+
logger.error(f'CommonAttributes provided {common_attributes_str} is not a valid JSON, the error is: {str(e)}')
244+
isAttributesValid = False
245+
response['UserInputCommonAttributesErrorMessages'] = (
246+
'Validation Failed for Input Provided. The CommonAttributes provided : {} is not a valid JSON. '
247+
'Please provide a valid JSON for CommonAttributes.'.format(common_attributes_str)
248+
)
249+
250+
try:
251+
log_group_config = event_data['LogGroupConfig']
252+
log_group_config_json = json.loads(log_group_config)
253+
if not isinstance(log_group_config_json, list):
254+
raise ValueError('LogGroupConfig must be a JSON array')
255+
for log_group in log_group_config_json:
256+
if not isinstance(log_group, dict):
257+
raise ValueError("Each element in LogGroupConfig should be a JSON object.")
258+
if 'LogGroupName' not in log_group:
259+
raise ValueError("Each element in LogGroupConfig should have 'LogGroupName' key.")
260+
if not log_group['LogGroupName']:
261+
raise ValueError("Each element in LogGroupConfig should have non-empty 'LogGroupName' value.")
262+
263+
response['LogGroupErrorMessages'] = 'No Errors Found in User Input for Log Group'
264+
except Exception as e:
265+
logger.error(f'LogGroup provided {log_group_config} is not a valid JSON, the error is: {str(e)}')
266+
isLogGroupValid = False
267+
response['LogGroupErrorMessages'] = (
268+
'Validation Failed for Input Provided. The LogGroup provided : {} is not a valid JSON. '
269+
'Please provide a valid JSON for LogGroup.'.format(log_group_config)
270+
)
225271
272+
if(not isAttributesValid or not isLogGroupValid):
273+
response['LogGroupArns'] = ''
274+
response['InvalidLogGroups'] = ''
275+
response['CommonAttributes']= []
276+
cfnresponse.send(event, context, cfnresponse.SUCCESS, response)
277+
return
278+
226279
# these parameter are needed for entity synthesis
227280
additional_attributes = [
228281
{"AttributeName": "aws.accountId", "AttributeValue": account_id},
@@ -233,8 +286,7 @@ Resources:
233286
{"AttributeName": "aws.realm", "AttributeValue": awsRealm}
234287
]
235288
236-
237-
common_attributes_str = event_data['CommonAttributes']
289+
238290
# Convert the json to the correct json format
239291
if common_attributes_str.strip():
240292
common_attributes = json.loads(common_attributes_str)
@@ -273,9 +325,9 @@ Resources:
273325
response['CommonAttributes'] = common_attributes
274326
response['LogGroupArns'] = ','.join(log_group_arns)
275327
response['InvalidLogGroups'] = ','.join(invalid_log_groups)
276-
response['ErrorMessages'] = "No Errors Found in User Input"
328+
response['LogGroupErrorMessages'] = "No Errors Found in User Input for Log Group"
277329
if invalid_log_groups:
278-
response['ErrorMessages'] = (
330+
response['LogGroupErrorMessages'] = (
279331
'Validation Failed for Input Provided. These Log Groups: [{}] do not exist in your account.'
280332
'Please setup Cloudwatch to Data Firehose subscription manually for additional log groups including these failed ones to stream with the resource Logical ID: "LoggingFirehoseStreamToNewRelic".'
281333
'While setting up the subscription manuually you can use the IAM role with resource Logical ID: "CloudWatchFirehoseRole" created by this deployment.'.format(','.join(invalid_log_groups))
@@ -353,6 +405,9 @@ Outputs:
353405
NewRelicLogsLoggingFirehoseStreamArn:
354406
Description: The ARN of the Logging DataFirehose Stream.
355407
Value: !GetAtt NewRelicLogsFirehoseStreamToNewRelic.Arn
356-
NewRelicLogsUserInputErrors:
357-
Description: Contains Details about Errors in User Input.
358-
Value: !GetAtt NewRelicLogsResourceForUserInputParsing.ErrorMessages
408+
NewRelicLogsUserInputLogGroupErrors:
409+
Description: Contains Details about Errors in User Input for LogGroup.
410+
Value: !GetAtt NewRelicLogsResourceForUserInputParsing.LogGroupErrorMessages
411+
NewRelicLogsUserInputCommonAttributeErrors:
412+
Description: Contains Details about Errors in User Input for setting up Common Attributes in lambda.
413+
Value: !GetAtt NewRelicLogsResourceForUserInputParsing.UserInputCommonAttributesErrorMessages

0 commit comments

Comments
 (0)