Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active June 30, 2016 01:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/a36a194f8a80ce5fa49bf950e092c604 to your computer and use it in GitHub Desktop.
Save mamemomonga/a36a194f8a80ce5fa49bf950e092c604 to your computer and use it in GitHub Desktop.
アクセス制限のかかったGitLabにLet's Encryptをつかってhttps化する

アクセス制限のかかったGitLabをLet's Encryptをつかってhttps化する

  • GitLab-ceが設置済みで稼働中とする
  • Let's Encryptはhttpをつかって外側サーバから認証用の接続が必要なので、Port80は解放する必要がある。
  • httpはLet's Encrypt認証専用とし、GitLabでは使わないようにする。

参考URL

GitLabの設定

  • IPアドレスの制限を行う

  • まずは自己署名でhttps接続できるようにする。

GitLabの設定

$ sudo vim /etc/gitlab/gitlab.rb

-- 編集・追記 ここから --

external_url 'https://gitlab.example.com'
nginx['custom_nginx_config']         = "include /etc/gitlab/custom_nginx_config.conf;"
nginx['custom_gitlab_server_config'] = "include /etc/gitlab/custom_gitlab_server_config.conf;"

-- 編集・追記 ここまで --

nginx http領域の設定

sudo sh -c 'cat > /etc/gitlab/custom_nginx_config.conf' << 'EOS'
server {
	listen 80;
	server_name _;
	access_log  /var/log/gitlab/nginx/access.log;
	error_log   /var/log/gitlab/nginx/error.log;
	location / {
		return 404;
	}
	location /.well-known {
		alias /var/letsencrypt/.well-known;
	}
}
EOS

許可するIPアドレスの設定、この例ではローカルアドレスだが実際はグローバルアドレスだと思う。

sudo sh -c 'cat > /etc/gitlab/custom_gitlab_server_config.conf' << 'EOS'
allow 192.168.0.0/24;
deny all;
EOS

空のディレクトリを作成

sudo mkdir /var/letsencrypt

GitLabへ変更を適用

sudo gitlab-ctl reconfigure

チェック

curl -k https://gitlab.example.com/users/sign_in/
curl http://gitlab.example.com/
  • httpsはfirewallで制限してしまってもいいかもしれない。

Let's Encryptのセットアップ

sudo mkdir /usr/local/letsencrypt
sudo chown `whoami` /usr/local/letsencrypt
cd /usr/local/letsencrypt
git clone https://github.com/letsencrypt/letsencrypt .
sudo ./letsencrypt-auto --help

letsencrypt-auto の実行

sudo ./letsencrypt-auto certonly --webroot --webroot-path /var/letsencrypt -d gitlab.example.com

GitLabの設定

sudo vim /etc/gitlab/gitlab.rb

-- 編集・追記 ここから --

nginx['redirect_http_to_https']      = false
nginx['ssl_certificate']             = "/etc/letsencrypt/live/gitlab.example.com/fullchain.pem"
nginx['ssl_certificate_key']         = "/etc/letsencrypt/live/gitlab.example.com/privkey.pem"

-- 編集・追記 ここまで --

適用

sudo gitlab-ctl reconfigure

これで、許可されたIPアドレスからhttpsでアクセスし、正しい証明書になっているか確認する。

証明書の強制更新

90日ごとに失効するらしい

更新

sudo /usr/local/letsencrypt/letsencrypt-auto renew

強制更新

sudo /usr/local/letsencrypt/letsencrypt-auto renew --force-renew

定時更新

sudo crontab -e -u root
00 05 01 * * /usr/local/letsencrypt/letsencrypt-auto renew --force-renew && gitlab-ctl restart

時間はずらしたほうがいいとおもいます。

GitLabメモ

GitLabのIP制限をlocation領域で設定する場合

Let's Encryptの認証はhttpのみを使うようなので、この方法は使わないことにした。

location / 部分だけ制限して、後から追加するLEの GET /.well-known/ へはどこからでも接続できるようにしておく。

vim /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx-gitlab-http.conf.erb

以下のように追加する。だいたいこのへん

107  <% path = @relative_url ? @relative_url : "/" %>
106   location <%= path %> {
      include /etc/gitlab/location_append.conf;
107     ## If you use HTTPS make sure you disable gzip compression

許可するIPアドレスリストを設定する

cat > /etc/gitlab/location_append.conf << 'EOS'
allow 192.168.0.0/24;
deny all;
EOS

GitLab再設定

sudo gitlab-ctl reconfigure
  • 特定のIPアドレスからのみ接続できるか確認する
  • GitLabを更新したら上の書き換えが消える可能性が高いので要注意
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment