Skip to content

recommended solutions not working when trying to label URL in a python lambda function

0

I received the following solutions for labeling a URL in a python lambda function: To label or name a URL in a Python Lambda function used in an SNS message, you can use HTML formatting within your message string. Instead of directly including the URL, you can create a hyperlink with a custom label. Here's how you can modify your code:

First, import the html module:

import html

Then, modify your message formatting to include an HTML anchor tag:

message = "\n".join([ f"{f['Resources'][0].get('Details', {}).get('AwsEc2Instance', {}).get('KeyName', 'N/A')}: {f['Severity']['Label']} \n" f"{f['Title']}: {f['Description']} \n" f"{f['Remediation']['Recommendation']['Text']}: " f"<a href='{html.escape(f['Remediation']['Recommendation']['Url'])}'>Click here for remediation</a>" "\n" for f in findings ])

In this modified version, we're creating an HTML anchor tag with the URL as the href attribute and "Click here for remediation" as the link text. The html.escape() function is used to properly encode any special characters in the URL.

When sending this message through SNS, make sure to set the message format to HTML if your target supports it. For email notifications, you would set the "MessageStructure" parameter to "html" when publishing to the SNS topic.

Keep in mind that the ability to use HTML formatting depends on the SNS subscription type and the capabilities of the receiving end. For example, SMS subscriptions won't support HTML, but email subscriptions typically do.

If HTML isn't supported for your use case, you could alternatively use a more simple text-based format like this:

f"{f['Remediation']['Recommendation']['Text']}: Click here for remediation"

This format is often recognized by various messaging platforms as a clickable link with custom text. Sources Amazon SNS message attributes - Amazon Simple Notification Service

Well, neither approach recommended is working for me. When trying the html option, I get

equine-wrkld-demo-server-kp01: HIGH EC2.8 EC2 instances should use Instance Metadata Service Version 2 (IMDSv2): This control checks whether your Amazon Elastic Compute Cloud (Amazon EC2) instance metadata version is configured with Instance Metadata Service Version 2 (IMDSv2). The control passes if HttpTokens is set to required for IMDSv2. The control fails if HttpTokens is set to optional. For information on how to correct this issue, consult the AWS Security Hub controls documentation.: <a href='https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.aws.amazon.com_console_securityhub_EC2.8_remediation&d=DwICaQ&c=BSDicqBQBDjDI9RkVyTcHQ&r=4nrs1sW7KAflogEbAAfK-A&m=BfbZMRy15YC5DKbnI6YfBp3-GNArOwMyWEJlGyksuwvLqemiT_pIk0AGQTjrGSWE&s=pPGmqGpavz85kyxLcq0hGG50VVO_p-F7lqhVXHXbHyU&e= '>Click here for remediation</a>

when trying the second option with the clickable link with custom text, I get

equine-wrkld-demo-server-kp01: HIGH EC2.8 EC2 instances should use Instance Metadata Service Version 2 (IMDSv2): This control checks whether your Amazon Elastic Compute Cloud (Amazon EC2) instance metadata version is configured with Instance Metadata Service Version 2 (IMDSv2). The control passes if HttpTokens is set to required for IMDSv2. The control fails if HttpTokens is set to optional. For information on how to correct this issue, consult the AWS Security Hub controls documentation.: https://urldefense.proofpoint.com/v2/url?u=https-3A__docs.aws.amazon.com_console_securityhub_EC2.8_remediation&d=DwICaQ&c=BSDicqBQBDjDI9RkVyTcHQ&r=4nrs1sW7KAflogEbAAfK-A&m=1aYrvzzw1Frs-XDuC5rc6DsBln-TzEYBuHhL7Yc6d5cxbjGZV1oG7JbH2ogRR7Tk&s=J1POQPpMtzbRDlMtHrGuUy42WjjdWfyH_3pfL7zDx8Q&e= : Click here for remediation

In both cases, the "Click here for remediation is not a hyperlink that is clickable

any other suggestions? My SNS is set to use email, with subscribers using Outlook

2 Answers
1

Hello.

As far as I know, if you are using the email endpoint with Amazon SNS, you cannot format the message to HTML and send it.
If you want to format it as HTML and send an email, I think you need to use something like Amazon SES.
https://stackoverflow.com/questions/62807325/python-boto3-sns-email-formatting-with-html

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Given that you're using SNS with email subscriptions and Outlook as the email client, there are a few alternative approaches you can try:

  1. Plain text with URL description: Instead of using HTML, you can format your message using plain text with a description followed by the URL. For example:
message = f"{f['Remediation']['Recommendation']['Text']}: Click here for remediation ({f['Remediation']['Recommendation']['Url']})"

This approach provides both a description and the full URL, which Outlook should automatically convert to a clickable link.

  1. Use the displayText field: If you're using AWS User Notifications, you can utilize the displayText field for URL type text parts. This field allows you to provide a short, single-line description of the link, which will be hyperlinked with the URL itself. The maximum length for this field is 1024 characters.

  2. Ensure proper SNS message formatting: When sending the message through SNS, make sure you're setting the correct message format. For email notifications, you may need to set the "MessageStructure" parameter to "json" and include both a "default" and an "email" message format.

  3. Check SNS configuration: Verify that your SNS topic and subscription are correctly configured for email delivery. Ensure that the email protocol is selected and that the endpoint (email address) is correct.

  4. Investigate URL rewriting: The URL in your example appears to be rewritten by a security service (urldefense.proofpoint.com). This rewriting might be interfering with the link formatting. You may need to consult with your IT department to see if there's a way to bypass or modify this URL rewriting for internal AWS notifications.

If these suggestions don't resolve the issue, you may need to consult with your organization's email administrators or AWS support for further assistance, as there might be specific security policies or email configurations affecting the link rendering in Outlook.
Sources
how can I label a url in a lambda python function used in a SNS message? | AWS re:Post
TextPartValue - AWS User Notifications

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.