如何为现有 Amazon SES SMTP IAM 用户轮换访问密钥?

2 分钟阅读
0

我想要在 AWS Identity and Access Management (IAM) 中轮换我的 Amazon Simple Email Service (Amazon SES) 简单邮件传输协议 (SMTP) 凭证。如何创建与 Amazon SES 兼容的用户名和密码?

解决方法

当客户连接到 SES API 端点时,您在 IAM 控制台中为 SMTP 用户创建的访问密钥有效,但不适用于 Amazon SES SMTP 接口。在 IAM 控制台中生成的密钥的格式与 Amazon SES SMTP 服务器所需的凭证所需的格式不同。

最佳做法是创建新的 Amazon SES SMTP 凭证,而不是转换现有的秘密访问密钥。

要为 Amazon SES SMTP 接口设置凭证,请执行以下操作之一:

创建新的 Amazon SES SMTP 凭证(推荐)

1.    使用 Amazon SES 控制台创建新的 Amazon SES SMTP 凭证

2.    获得新凭证后,如果不需要 IAM 中的现有 Amazon SES 凭证,则可以删除这些凭证

将您现有的秘密访问密钥转换为 Amazon SES SMTP 格式

**注意:**您必须使用 Python 3 或者更高版本并执行以下步骤。

1.    更新现有 IAM 用户的策略,以至少授予 ses:SendRawEmail 的权限

2.    将以下 Python 代码粘贴到文本编辑器中,然后将文件另存为 seskey.py

#!/usr/bin/env python3

import hmac
import hashlib
import base64
import argparse

SMTP_REGIONS = [
    'us-east-2',       # US East (Ohio)
    'us-east-1',       # US East (N. Virginia)
    'us-west-2',       # US West (Oregon)
    'ap-south-1',      # Asia Pacific (Mumbai)
    'ap-northeast-2',  # Asia Pacific (Seoul)
    'ap-southeast-1',  # Asia Pacific (Singapore)
    'ap-southeast-2',  # Asia Pacific (Sydney)
    'ap-northeast-1',  # Asia Pacific (Tokyo)
    'ca-central-1',    # Canada (Central)
    'eu-central-1',    # Europe (Frankfurt)
    'eu-west-1',       # Europe (Ireland)
    'eu-west-2',       # Europe (London)
    'sa-east-1',       # South America (Sao Paulo)
    'us-gov-west-1',   # AWS GovCloud (US)
]

# These values are required to calculate the signature. Do not change them.
DATE = "11111111"
SERVICE = "ses"
MESSAGE = "SendRawEmail"
TERMINAL = "aws4_request"
VERSION = 0x04


def sign(key, msg):
    return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()


def calculate_key(secret_access_key, region):
    if region not in SMTP_REGIONS:
        raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.")

    signature = sign(("AWS4" + secret_access_key).encode('utf-8'), DATE)
    signature = sign(signature, region)
    signature = sign(signature, SERVICE)
    signature = sign(signature, TERMINAL)
    signature = sign(signature, MESSAGE)
    signature_and_version = bytes([VERSION]) + signature
    smtp_password = base64.b64encode(signature_and_version)
    return smtp_password.decode('utf-8')


def main():
    parser = argparse.ArgumentParser(
        description='Convert a Secret Access Key for an IAM user to an SMTP password.')
    parser.add_argument(
        'secret', help='The Secret Access Key to convert.')
    parser.add_argument(
        'region',
        help='The AWS Region where the SMTP password will be used.',
        choices=SMTP_REGIONS)
    args = parser.parse_args()
    print(calculate_key(args.secret, args.region))


if __name__ == '__main__':
    main()

3.    要运行 Python 脚本,请输入您现有的秘密访问密钥。然后,输入空格和您使用 SMTP 密码的 AWS 区域。请使用以下命令:

python3 seskey.py YOURKEYrrpg/JHpyvtStUVcAV9177EAKKmDP37P us-east-1

重要事项: 请务必输入您的凭证并在安全可信的计算机上运行此脚本。

4.    该脚本将输出一个新的秘密访问密钥,您可以将其用于 Amazon SES。将生成的 SMTP 凭证存储在您的应用程序中,然后使用这些凭证连接到 SES SMTP 端点


AWS 官方
AWS 官方已更新 2 年前