2
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?

ALBに自己署名証明書を導入

Posted at

背景

システムとしてはAWSのPrivateCAを利用してプライベート認証機関を立て、証明書を発行して利用していました。しかし、AWSのPrivateCAは月額400ドルかかるため、コスト削減のために開発環境の一部ではPrivateCAの利用をやめ、自前でPrivateCAおよびサーバー証明書を発行することにしました。

更新作業を簡略化するため、証明書の有効期限は100年に設定しています。


証明書作成 & ACM登録シェル

以下は自己署名証明書を作成し、AWS ACMに登録するスクリプトの例です。

#!/bin/bash

domain=example.internal
cn=example-cn

# 各種鍵の保管場所作成
output_dir=output
mkdir -p ${output_dir}

# OpenSSLの設定ファイルを作成
openssl_ca_conf=${output_dir}/openssl_ca.cnf

cat <<EOF > ${openssl_ca_conf}
[ req ]
default_bits       = 2048
prompt             = no
default_md         = sha256
distinguished_name = dn
x509_extensions    = v3_ca

[ dn ]
C                  = JP
ST                 = Tokyo
L                  = Adachi
O                  = Example, Ltd.
OU                 = Example
CN                 = ${cn}

[ v3_ca ]
basicConstraints    = critical,CA:TRUE
subjectKeyIdentifier = hash
keyUsage            = critical,digitalSignature,keyCertSign,cRLSign
EOF

openssl_conf=${output_dir}/openssl.cnf
cat <<EOF > ${openssl_conf}
[ req ]
default_bits        = 2048
prompt              = no
default_md          = sha256
req_extensions      = req_ext
distinguished_name  = dn

[ dn ]
C                   = JP
ST                  = Tokyo
L                   = Adachi
O                   = Example, Ltd.
OU                  = Example
CN                  = *.${domain}

[ req_ext ]
subjectAltName      = @alt_names

[ alt_names ]
DNS.1               = *.${domain}
EOF

# プライベートルート証明書の作成
openssl genrsa -out ${output_dir}/rootca.key 2048
openssl req -new -x509 -key ${output_dir}/rootca.key -sha256 -days 36500 -out ${output_dir}/rootca.pem -config ${openssl_ca_conf} -extensions v3_ca

# サーバ証明書の作成
openssl genrsa -out ${output_dir}/server.key 2048
openssl req -new -key ${output_dir}/server.key -out ${output_dir}/server.csr -config ${openssl_conf}
openssl x509 -req -in ${output_dir}/server.csr -CA ${output_dir}/rootca.pem -CAkey ${output_dir}/rootca.key -set_serial 01 -days 36500 -out ${output_dir}/server.pem -extfile ${openssl_conf} -extensions req_ext

# CLIでAWS ACMへ証明書をインポート
aws acm import-certificate --certificate fileb://${output_dir}/server.pem --private-key fileb://${output_dir}/server.key --certificate-chain fileb://${output_dir}/rootca.pem

ALBへの登録

作成した証明書をALBに登録する手順は以下の通りです。

  1. HTTPSリスナーを開き、証明書タブから「デフォルトを変更」をクリックします。
  2. ACMにインポートした証明書を選択し、デフォルトとして設定します。

ルート証明書の信頼設定

ALBにアクセスする端末に対して、以下の設定を行います。

  1. rootca.pem を端末にインストールします。
  2. 各OSの手順に従ってルート証明書として信頼を追加します。

アプリ側のトラストストアの更新

アプリケーションからALBにリクエストを送信する場合、アプリケーションのトラストストアにルート証明書を登録する必要があります。
以下はトラストストア作成のコマンド例です。JKS形式にしたかったため、keytoolを使用しています。

keytool -import -alias example-truststore -keystore truststore.jks -file 
rootca.pem
2
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
2
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?