1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[nodejs] AWS IAM ユーザの secret_access_key から SES 用の SMTP パスワード を生成する

Last updated at Posted at 2022-02-10

TL;DR

  • SES で生成した IAM ユーザ ses-smtp-user.YYYYMMDD にポリシーを足して、他の用途に流用しようと考えたが、どうも上手くいかない。
  • 原因は、secret_access_key と SMTP 用のパスワードが異なるためだった。
  • secret_access_key から SMTP 用のパスワードは生成できるようだ。逆はだめ。
  • 公式のドキュメントには python 版が記されている
    https://docs.aws.amazon.com/ses/latest/dg/smtp-credentials.html
  • nodejs 版を記しておく
const crypto = require('crypto');

const smtp_credentials_generate = (secret_access_key, region) => {
    const version = 0x04;
    const kMessage = [
        '11111111', // date
        region,
        'ses', // service
        'aws4_request', // terminal
        'SendRawEmail', // message
    ].reduce(
        (secret, text) => crypto.createHmac('sha256', secret).update(text).digest(),
        'AWS4' + secret_access_key,
    );
    const signatureAndVersion = Buffer.concat([Buffer.from([version]), kMessage]);
    const smtpPassword = signatureAndVersion.toString('base64');

    return smtpPassword;
};

使い方

  • ログインする SES の region を指定する
const secret_access_key = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const smtp_password = smtp_credentials_generate(secret_access_key, 'us-west-2');
console.log(smtp_password);

補足

  • ses が使えるようにポリシーを足しておくこと
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ses:SendRawEmail",
            "Resource": "*"
        }
    ]
}

テスト

  • テスト送信
#!/bin/bash

user='AKI... アクセスキーID'
pass='上記で生成したSMTP用のパスワード'

from='exabugs@hoge.com'
rcpt='exabugs@hoge.com'

cat > input.txt <<EOT
EHLO hoge.com
AUTH LOGIN
$(echo -n $user | openssl enc -base64)
$(echo -n $pass | openssl enc -base64)
MAIL FROM: $from
RCPT TO: $rcpt
DATA
From: $from
To: $rcpt
Subject: Amazon SES SMTP Test

This message was sent using the Amazon SES SMTP interface.
.
QUIT
EOT

openssl s_client -crlf -quiet -connect email-smtp.us-west-2.amazonaws.com:465 < input.txt
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?