跳至内容

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

2 分钟阅读
0

我想要在 AWS Identity and Access Management (IAM) 中轮换我的 Amazon Simple Email Service (Amazon SES) 简单邮件传输协议 (SMTP) 凭证。

解决方法

当您连接到 Amazon SES 端点时,您在 IAM 控制台中创建的访问密钥将生效。但是,这些访问密钥无法用于 SES SMTP 接口。这是因为这些密钥的格式与 SMTP 凭证所需的格式不同。

要使访问密钥可用于 SES SMTP 接口,您可以创建新的 SES SMTP 凭证。或者,您也可以将现有的秘密访问密钥转换为 SMTP 凭证。

重要事项: 最佳做法是创建新的 SES SMTP 凭证,而不是将现有的秘密访问密钥转换为 SMTP 凭证。

创建新的 SES SMTP 凭证(最佳实践)

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

**注意:**在获取新的 SES SMTP 凭证后,如果您不需要 IAM 用户,则可以删除或停用该 IAM 用户

将您现有的秘密访问密钥转换为 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 脚本:

    python3 seskey.py YOURKEYrrpg/JHpyvtStUVcAV9177EAKKmDP37P your-region
    

    **注意:**请将 YOURKEYrrpg 替换为您现有的秘密访问密钥,将 your-region 替换为您使用 SMTP 密码的 AWS 区域。
    在脚本输出中,有一个新的 SMTP 密码,您可以将其用于 SES SMTP 接口。

  4. 将新的 SMTP 凭证安全地保存在您的应用程序中,以便向 SES SMTP 端点进行身份验证。