기존 Amazon SES SMTP IAM 사용자의 액세스 키를 교체하려면 어떻게 해야 하나요?

3분 분량
0

AWS Identity and Access Management(IAM)에서 Amazon Simple Email Service(Amazon SES) 심플 메일 전송 프로토콜(SMTP) 보안 인증 정보를 교체하고 싶습니다. Amazon SES와 호환되는 사용자 이름과 암호를 만들려면 어떻게 해야 하나요?

​해결 방법

SMTP 사용자를 위해 IAM 콘솔에서 생성한 액세스 키는 고객이 SES API 엔드포인트에 연결할 때 작동하지만 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년 전