Grafanaセキュリティ強化
概要
このマニュアルではGrafanaのリモートアクセスセキュリティを強化するためHTTPS化とリバースプロキシの導入設定を行います。
事前確認
以下の手順は、監視ツール設定で設定したGrafana導入サーバーで実施してください。
~作業の流れ~
- 独自ドメイン(サブドメイン)を取得
※無料ドメイン取得手順も掲載 - WEBサーバーnginxのインストール・リバースプロキシ設定
- certbotをインストール
- 無料SSL証明書(Let’s Encrypt)を取得
- nginx設定変更
- SSL証明書自動更新を設定
作業前の注意事項
以下の文章をよく読みながら進めてください。コマンドの実行忘れ・コマンドコピーミスなどに十分注意してください。
1. ドメイン(サブドメイン)の取得
確認
ホームページなどの運用ですでに独自ドメインを取得済みの場合はそちらを使用出来ます。独自ドメインをお持ちでない方は無料ダイナミックDNSでドメインを取得できます。
独自ドメインをお持ちの方は、登録レジストラのDNS設定でGrafana用サブドメインを設定してください。
参考)
ホスト名:任意のサブドメイン名
TYPE:A
VALUE(値):Grafana導入サーバーIPアドレス
DDNS Nowで無料のドメインを取得します。 DDNS Nowは完全無料・定期通知不要・サービス保障稼働率100%など、使いやすいDDNSサービスです。ただしサブドメインなどは設定できません。1アカウント1ドメイン
ドメイン(アカウント)の取得
ここで取得したサブドメインがGrafanaのURLになります。
例)grafana-abc.f5.si
詳細設定からDNSのAレコードを設定
「Aレコード(IPv4アドレス)」欄にGrafanaを導入しているサーバーのIPアドレスを入力して「設定を変更する」をクリックしてください
登録情報確認
2. nginxのインストール
インストール
sudo apt update && sudo apt install nginx -yステータス確認
sudo systemctl --no-pager status nginx● nginx.service - A high performance web server and a reverse proxy server
~~
Active: active (running) HTTP/HTTPS用ポートを開放
sudo ufw allow 80/tcpsudo ufw allow 443/tcp開放中のGrafana用3000ポートを削除
numbered で表示された3000ポートの番号を指定して削除してください。
sudo ufw status numberedsudo ufw delete 番号ufwをリロード
sudo ufw reload確認
ローカルブラウザから http://Grafana導入サーバーのIPアドレス にアクセスしてNginx Welcomeページが表示されれば問題ありません。
xxxx.bbb.comを1. ドメイン(サブドメイン)の取得で取得したドメイン(サブドメイン)に置き換えて実行します。
domain=xxxx.bbb.com仮想ホストファイルの作成
cat > "$HOME/$domain.conf" << EOF
server {
listen 80;
server_name $domain;
location / {
proxy_set_header Host \$http_host;
proxy_pass http://127.0.0.1:3000;
}
}
EOFファイル移動
sudo mv $HOME/$domain.conf /etc/nginx/sites-available/シンボリックリンク作成
sudo ln -s /etc/nginx/sites-available/$domain.conf /etc/nginx/sites-enabled/$domain.conf構文チェック
sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulnginxの再読み込み
sudo systemctl reload nginx確認
ローカルブラウザからhttp://ドメイン名でアクセスし、Grafanaログイン画面が表示されればリバースプロキシ設定に成功しました!
デフォルトのホストファイルリンクを削除
sudo unlink /etc/nginx/sites-enabled/default構文チェック
sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulnginxの再読み込み
sudo systemctl reload nginx3. certbotをインストール
snapが利用可能か確認
snap version
snapとsnapdのバージョン情報が表示されればsnapが利用可能です。
sudo snap install --classic certbotcertbot from Certbot Project installed のように表示されればインストール完了です。
certbot のシンボリックリンクを作成
sudo ln -sf /snap/bin/certbot /usr/bin/certbot4. 無料SSL証明書(Let’s Encrypt)を取得
sudo certbot --nginx設定
-
任意のメールアドレスの登録
ここで登録したメールアドレスにSSL証明書更新前・期限切れ情報の通知が届きます。
-
Let's Encryptサービス利用規約への同意確認
yを入力して、Enter -
キャンペーンメール登録確認(不要ならN)
nを入力して、Enter -
SSL証明書登録ドメインの確認
1:にご自身のドメインが表示されていることを確認し、1を入力して、Enter
-
SSL証明書取得確認
This certificate expires onは取得したSSL証明書の有効期限です。
Congratulations!が表示されていれば署名書取得成功です。
確認
ローカルブラウザからhttps://ドメイン名でアクセスし、Grafanaログイン画面が表示されればHTTPS化に成功しています。
5. nginx設定変更
1. websocketの有効化
echo $domain戻り値が設定したドメインであること。
そうでなければ、変数domainに1. ドメイン(サブドメイン)の取得で取得したドメイン(サブドメイン)を代入してください。
仮想ホストファイルの編集
sudo sed -i '/location \//,/}/c\
location / {\
proxy_set_header Host $http_host;\
proxy_pass http://127.0.0.1:3000;\
}\
\
location /api/live {\
proxy_http_version 1.1;\
proxy_set_header Upgrade $http_upgrade;\
proxy_set_header Connection $connection_upgrade;\
proxy_set_header Host $http_host;\
proxy_pass http://127.0.0.1:3000;\
}' /etc/nginx/sites-enabled/$domain.conf2. nginx 構成ファイルの強化
事前バックアップ
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak.$(date +%Y%m%d-%H%M%S)ssl_protocols を TLSv1.2 TLSv1.3 に変更
sudo sed -i -E 's/^[[:space:]]*ssl_protocols.*/ ssl_protocols TLSv1.2 TLSv1.3;/' /etc/nginx/nginx.conf以下の設定を追記
grep -q 'limit_conn_zone \$binary_remote_addr zone=addr:5m;' /etc/nginx/nginx.conf || \
sudo sed -i '/include \/etc\/nginx\/sites-enabled\/\*;/a\
\
## Start: Size Limits \& Buffer Overflows ##\
client_body_buffer_size 3K;\
client_header_buffer_size 3k;\
client_max_body_size 80k;\
large_client_header_buffers 2 10k;\
## END: Size Limits \& Buffer Overflows ##\
\
### Directive describes the zone, in which the session states are stored i.e. store in slimits. ###\
### 1m can handle 32000 sessions with 32 bytes/session, set to 5m x 32000 session ###\
limit_conn_zone $binary_remote_addr zone=addr:5m;\
\
### Control maximum number of simultaneous connections for one session i.e. ###\
### restricts the amount of connections from a single ip address ###\
limit_conn addr 10;\
\
map $http_upgrade $connection_upgrade {\
default upgrade;\
'\'''\'' close;\
}' /etc/nginx/nginx.confnginxエラーチェック
sudo nginx -t戻り値
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfulエラー箇所を修正してください。
nginx: [emerg] unexpected "}" in /etc/nginx/sites-enabled/******.conf:8
nginx: configuration file /etc/nginx/nginx.conf test failednginxの再読み込み
sudo systemctl reload nginx状態確認
sudo systemctl --no-pager status nginx● nginx.service - A high performance web server and a reverse proxy server
~~
Active: active (running) 6. SSL証明書自動更新
1.SSL証明書自動更新タイマー確認
このプログラムでSSL証明書が自動更新されます。
systemctl status snap.certbot.renew.timer● snap.certbot.renew.timer - Timer renew for snap application certbot.renew
~~
Active: active (waiting)7. nginx自動リロード設定
SSL証明書更新後に nginx を再読み込みする deploy hook を作成します。
sudo tee /etc/letsencrypt/renewal-hooks/deploy/nginx-reload.sh > /dev/null << 'EOF'
#!/bin/bash
systemctl reload nginx
EOF実行権限を付与
sudo chmod 755 /etc/letsencrypt/renewal-hooks/deploy/nginx-reload.sh動作確認
sudo certbot renew --dry-runSaving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/***.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simulating renewal of an existing certificate for ***
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/***/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -SSL証明書自動更新タイマー確認
systemctl status snap.certbot.renew.timer● snap.certbot.renew.timer - Timer renew for snap application certbot.renew
~~
Active: active (waiting)Last updated on