4
5

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 5 years have passed since last update.

定期的にLet's Encrypt証明書を更新してSlackでアナウンス

Posted at

Let's Encrypt

公開サイトでは常時SSL(AOSSL)の対応が必須になってきていまして、
Let's Encryptは証明書発行が無料で簡単で最近数年間、人気を呼んでいます。
また、2018年1月からワイルドカード証明書の発行を開始 しています。
Let's Encryptの証明書有効期間は基本3ヶ月で、
certbotという簡単な証明書発行クライアントを利用します。
image.png

定期的にLet's Encrypt証明書を更新

有効期間が短いので、自分はcron設定で定期的に更新されるようにしておきました。

0 4 1 1,3,5,7,9,11 * root /usr/bin/systemctl stop httpd.service && /usr/bin/certbot renew --force-renew --quiet --post-hook "/usr/bin/systemctl start httpd.service"

定期的にLet's Encrypt証明書を更新してSlackでアナウンス

その状況をSlackにアナウンスするために以下のようにShellScriptでカスタマイズしました。

ShellScript

ssl_certificate_renew.sh
# !/bin/bash

# WebHookのURL
WEBHOOK_URL='https://hooks.slack.com/services/***************************'

# メッセージを一時保存する場所
MESSAGEFILE=$(mktemp -t ssl-certificate-notice-XXXX)

# 送信先のチャンネル
CHANNEL=${CHANNEL:-'#lee_test'}

# 終了時に削除
trap "rm ${MESSAGEFILE}" 0


send_notice_to_slack_renew_start () {
    # 見出し
    hd=${HEAD:-"start to ssl certificate renew.\n"}

    # json形式に整形
    payload="payload={
        \"channel\": \"${CHANNEL}\",
        \"text\": \"${hd}\"
    }"

    curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null
}

send_notice_to_slack_renew_success () {
    # 見出し
    hd=${HEAD:-"ssl certificate renew success.\n"}
    # 絵文字
    emoji=${EMOJI:-':carlton:'}

    # json形式に整形
    payload="payload={
        \"channel\": \"${CHANNEL}\",
        \"icon_emoji\": \"${emoji}\",
        \"text\": \"${hd}\"
    }"

    curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null
}

send_notice_to_slack_renew_fail () {
    # 改行処理
    cat ${MESSAGEFILE} | tr '\n' '\\' | sed 's/\\/\\n/g' > ${MESSAGEFILE}

    # 絵文字
    emoji=${EMOJI:-':aaw_yeah:'}
    # 見出し
    hd=${HEAD:-"<!here>ssl certificate renew fail.\n"}
    # メッセージをシンタックスハイライト付きで取得
    msg='```'`cat ${MESSAGEFILE}`'```'

    # json形式に整形
    payload="payload={
        \"channel\": \"${CHANNEL}\",
        \"icon_emoji\": \"${emoji}\",
        \"text\": \"${hd}${msg}\"
    }"

    curl -s -S -X POST --data-urlencode "${payload}" ${WEBHOOK_URL} > /dev/null
}

send_notice_to_slack_renew_start

/usr/bin/systemctl stop httpd.service
/usr/bin/certbot renew --force-renew --quiet 2> ${MESSAGEFILE}
RENEW_RESULT=$?
/usr/bin/systemctl start httpd.service

if [ ${RENEW_RESULT} -eq 0 ]; then
  send_notice_to_slack_renew_success
else
  send_notice_to_slack_renew_fail
fi

そして、ShellScriptを定期的に呼び出します。

cron

0 4 1 1,3,5,7,9,11 * root /bin/sh /run/ssl_certificate_renew.sh

結果

証明書更新成功

image.png

証明書更新失敗

image.png

ありがとうございます。

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?