如何輪替現有 Amazon SES SMTP IAM 使用者的存取金鑰?

2 分的閱讀內容
0

我想在 AWS 身分和存取管理 (IAM) 中輪替我的 Amazon Simple Email Service (Amazon SES) 簡易郵件傳輸協定 (SMTP) 憑證。如何建立與 Amazon SES 相容的使用者名稱和密碼?

解決方法

當客戶連線到 SES API 端點但無法使用 Amazon SES SMTP 介面時,您在 IAM 主控台中為 SMTP 使用者建立的存取金鑰有用。IAM 主控台中產生的金鑰格式與 Amazon SES SMTP 伺服器所需憑證所需的格式不同。

最佳做法是建立新的 Amazon SES SMTP 憑證,而不是轉換現有的秘密存取金鑰。

若要為 Amazon SES SMTP 界面設定憑證,請執行下列其中一個動作:

建立新的 Amazon SES SMTP 憑證 (建議)

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

2.    取得新憑證後,](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_manage.html#id_users_deleting_console)如果您不需要這些憑證,可以[刪除 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 年前