Skip to content

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

3분 분량
0

AWS Identity and Access Management(IAM)에서 Amazon Simple Email Service(Amazon SES) SMTP(Simple Mail Transfer Protocol) 자격 증명을 교체하려고 합니다.

해결 방법

IAM 콘솔에서 생성한 액세스 키는 Amazon SES 엔드포인트에 연결할 때 작동합니다. 하지만 SES SMTP 인터페이스에서는 액세스 키가 작동하지 않습니다. 이는 키 형식이 SMTP 자격 증명에 필요한 키 형식과 다르기 때문입니다.

액세스 키가 SES SMTP 인터페이스에서 작동하도록 하려면 새 SES SMTP 자격 증명을 생성할 수 있습니다. 또는 기존 시크릿 액세스 키를 SMTP 자격 증명으로 변환할 수도 있습니다.

중요: 기존 시크릿 액세스 키를 SMTP 자격 증명으로 변환하는 것보다 새 SES 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 리전으로 바꾸십시오.
    스크립트 출력에는 SES SMTP 인터페이스에서 사용할 수 있는 새 SMTP 암호가 있습니다.

  4. 새 SMTP 자격 증명을 애플리케이션에 안전하게 저장하여 SES SMTP 엔드포인트에 인증합니다.

댓글 없음