跳至內容

如何為現有 SES SMTP IAM 使用者輪換存取金鑰?

2 分的閱讀內容
0

我想在 AWS Identity and Access Management (IAM) 中輪換 Amazon Simple Email Service (Amazon SES) 簡易郵件傳輸通訊協定 (SMTP) 憑證。

解決方法

您在 IAM 主控台建立的存取金鑰可以用來連線到 Amazon SES 端點。但是,存取金鑰不適用於 SES SMTP 介面。這是因為金鑰的格式與 SMTP 憑證所需的格式不同。

若要讓存取金鑰與 SES SMTP 介面一起使用,您可以建立新的 SES SMTP 憑證。或者,您也可以將現有的私密存取金鑰轉換為 SMTP 憑證。

重要: 最佳做法是建立新的 SES SMTP 憑證,而不是將現有的私密存取金鑰轉換為 SMTP 憑證。

建立新的 SES SMTP 憑證 (最佳做法)

使用 Amazon SES 主控台建立新的 SES SMTP 憑證

**注意:**取得新的 SES SMTP 憑證後,如果您不再需要該 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 端點進行驗證。