Skip to content

reference code for handle the scan result#1

Open
Hundao wants to merge 6 commits intomasterfrom
publish-malware-to-sns
Open

reference code for handle the scan result#1
Hundao wants to merge 6 commits intomasterfrom
publish-malware-to-sns

Conversation

@Hundao
Copy link
Owner

@Hundao Hundao commented Jul 24, 2022

TITLE

Change Summary

PR Checklist

  • I've read and followed the Contributing Guide.
  • Documents/Readmes
    • Updated accordingly
    • Not required
  • Plugins that have versioning

Other Notes

@Hundao Hundao changed the title lambda for handle the scan result reference code for handle the scan result Jul 27, 2022
Copy link

@trend-jack-c-tang trend-jack-c-tang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding the output format in the doc.

Comment on lines +4 to +10
Metadata:
AWS::ServerlessRepo::Application:
Name: cloudone-filestorage-plugin-publish-malware-to-sns
Description: >-
According to the scan result from the scanner, publish the malware information
to the target SNS topic.
Author: Trend Micro Cloud One File Storage Security

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not going to publish this to AWS serverless repository, so this is not required.

Copy link
Owner Author

@Hundao Hundao Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove in next commit.

Comment on lines +13 to +15
TargetSnsTopicName:
Type: String
Description: The name for the SNS topic to which malware information publish.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The customer may already have their own SNS topic. Could you figure out a way for using the existing SNS topic?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add 1 more parameter to let user fill in their customer SNS Topic ARN, and create a new one SNS topic if the value is empty string.

Comment on lines +16 to +24
PublishMalwareLambdaName:
Type: String
Description: The name for the lambda function publishes the malware information to SNS topic.
PublishMalwarePolicyName:
Type: String
Description: The name for the policy includes sns and account authority.
PublishMalwareRoleName:
Type: String
Description: The name for the role to execute the lambda function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For internal use resources, we usually don't let the customer choose the names. There might be problems, like name already existed, that would block customer from deploying the template. Any purpose for these parameters?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove these and using the default name (stack name).

Handler: handler.lambda_handler
FunctionName: !Ref PublishMalwareLambdaName
Runtime: python3.8
Timeout: 500

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you choose the timeout for this Lambda?

Properties:
FunctionName: !GetAtt PublishMalwareLambda.Arn
Action: lambda:InvokeFunction
Principal: events.amazonaws.com

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the endpoint of AWS eventbridge. Should be sns.amazonaws.com?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm very confused for why I can't receive the notification from SNS.

for i, scan_result in enumerate(scan_results)
]

response = sns_client.publish_batch(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SNS batch publish supports up to 10 messages, so be careful if the received events count would be more than 10. And you may need to handle partial failure cases.

Comment on lines +36 to +37
--runtime python3.9 \
--timeout 30 \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not matched with ones in the template. Why?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will align them in the next commit.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime is not aligned still.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the lost, fixed already.


scan_results.append({
'bucket_name': get_bucket_name(message['file_url']),
'account_name': fetch_account_name(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the account name be cached?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, move out from the for loop.

Ryan_c_wu and others added 2 commits July 27, 2022 13:49
Comment on lines +36 to +37
--runtime python3.9 \
--timeout 30 \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runtime is not aligned still.


if fails:
print(fails)
raise ValueError('fail to publish sns.')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unhandled error will make the Lambda retry the whole batch again. Some messages will be duplicated. In this case, I think we can just log them and let the execution finish successfully.

- Arn
Environment:
Variables:
TARGET_SNS_ARN: !If [NeedCreateNewSnsTopic, !Ref TargetSnsTopic, !Ref TargetSnsTopicArn, ]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TARGET_SNS_ARN: !If [NeedCreateNewSnsTopic, !Ref TargetSnsTopic, !Ref TargetSnsTopicArn, ]
TARGET_SNS_ARN: !If [ NeedCreateNewSnsTopic, !Ref TargetSnsTopic, !Ref TargetSnsTopicArn ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants