h-otterの備忘録

インフラを中心にコンピュータ関連をいいかんじにやっていきます

GitLabをhttpsアクセスできるようにする

今回はWindows Serverの証明機関を利用した、オレオレ認証のhttpsでやっていきます。

環境

GitLabのサーバー

  • FQDN: gitlab.domain.local
  • ciのDNS: ci.domain.local
  • Ubuntu 14.04.3 LTS
  • GitLab community edition 7.13.3

証明機関

GitLabをhttps化する

証明書の作成

まずはGitLabに設定されているデフォルトのディレクトリに証明書を作成します。証明書のファイル名は必ずexternal urlに設定したurlにする必要があります。今回の場合はdomain.localがそれに当たります。

$ sudo openssl genrsa -des3 -out /etc/gitlab/ssl/gitlab.domain.local.key 2048
Generating RSA private key, 2048 bit long modulus
...........................................................................................+++
..................................+++
e is 65537 (0x10001)
Enter pass phrase for /etc/gitlab/ssl/gitlab.domain.local.key:  // とりあえず設定します
Verifying - Enter pass phrase for /etc/gitlab/ssl/gitlab.domain.local.key:

$ sudo openssl rsa -in /etc/gitlab/ssl/gitlab.domain.local.key -out /etc/gitlab/ssl/gitlab.domain.local.key
Enter pass phrase for /etc/gitlab/ssl/gitlab.domain.local.key:  // とりあえず設定したパスフレーズを入力します
writing RSA key

$ sudo openssl req -new -key /etc/gitlab/ssl/gitlab.domain.local.key -out /etc/gitlab/ssl/gitlab.domain.local.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:gitlab.domain.local  // 入力必須です
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

Windows Serverで作成したcsrファイルをもとに証明書を発行します。Web発行サービスを利用する場合、Windows認証が使われているので証明機関の管理者権限のあるユーザーでアクセスしないとWebサーバーの証明書を発行できないことに注意が必要です。自分は何時間も悩んだ経験があります。OTZ

  1. "http://cert.domain.local/certsrv/"にアクセスする
  2. "証明書を要求する"
  3. "証明書の要求の詳細設定"
  4. "Base 64 エンコード CMC または PKCS #10 ファイルを使用して証明書の要求を送信するか、または Base 64 エンコード PKCS #7 ファイルを使用して更新の要求を送信する。" 5.作成したcsrファイルの中身をコピペし、証明書テンプレートを"Webサーバー"にします
  5. 発行した証明書をSCPなどで/etc/gitlab/ssl/にコピーし、拡張子を".cer"から".crt"に変更します。(めちゃくちゃ重要)

証明書をGitLabに適用する

設定ファイルを書き換えます。

/etc/gitlab/gitlab.rb

# 一行目から
## Url on which GitLab will be reachable.
## For more details on configuring external_url see:
## https://gitlab.com/gitlab-org/omnibus-gitlab/blob/629def0a7a26e7c2326566f0758d4a27857b52a3/README.md#configurin$
external_url 'https://gitlab.domain.local/'



# 約319行目から
################
# GitLab Nginx #
################
## see: https://gitlab.com/gitlab-org/omnibus-gitlab/tree/629def0a7a26e7c2326566f0758d4a27857b52a3/doc/settings/ng$

# nginx['enable'] = true
# nginx['client_max_body_size'] = '250m'
nginx['redirect_http_to_https'] = true



# 約406行目から
#################################
# application.yml configuration #
#################################

gitlab_ci['gitlab_server'] = { "url" => 'https://gitlab.domain.local', $

CIをhttps化する

GitLabの時と同様に証明書を作成し、反映させます。この時ファイル名と証明書を作成するときのFQDNをちゃんと"ci.domain.local"のようにCIのドメインをさすようにしましょう。

/etc/gitlab/gitlab.rb

# 約389行目から

############################################
# Url on which GitLab CI will be reachable #
############################################
## see https://gitlab.com/gitlab-org/omnibus-gitlab/tree/629def0a7a26e7c2326566f0758d4a27857b52a3/doc/gitlab-ci/RE$

ci_external_url 'https://ci.domain.local'