オレオレ証明書を使ってGitLabをhttps化

ソースコードには企業秘密がつまっているので、GitLab との通信を HTTPS で暗号化することにしました。

環境

ConoHa 2GB の CentOS 6.5 で GitLab 7-1-stable を動かしていて、HTTPD には nginx ではなく Apache 2.2.15 を使っています。

オレオレ証明書の作成

まず、暗号化に必要な鍵と証明書を作成します。
「オレオレ証明書」とかでググればたくさんやり方が出てきますが、僕は以下を参考にしました。

Apache に mod_ssl をインストール

# yum install -y mod_ssl

インストールしたら、ssl.conf の SSLCertificateFileSSLCertificateKeyFile を修正。
先ほど作った鍵と証明書を指定します。

/etc/httpd/conf.d/ssl.conf L100-
#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
#SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
#SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key

Apache の VirtualHost の設定を変更

HTTPS だと VirtualHost が使えないという記事もありますが、使えました。
僕は、httpd.conf の一番下に以下のように書いています。

/etc/httpd/conf/httpd.conf
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
NameVirtualHost *:443
#
# NOTE: NameVirtualHost cannot be used without a port specifier~
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# Default Host
#

<VirtualHost *:80>
    ServerName www.yuuan.net
</VirtualHost>

#
# Virtual Hosts
#

Include conf.d/vhosts/*.conf

VirtualHost は、一番初めに定義したものがデフォルトのホストとなる仕様なので、初めにこのブログを定義しています。1
こうすることで、定義されていないアドレスにアクセスがあると、このブログが表示されるようになります。

さて、GitLab の VirtualHost の設定ですが、何も考えないでやったらログインできなくなってしまいました。

ここに対処方法が書かれているので、その通りにします。
ここで用意されている gitlab-ssl.conf に、少しだけ修正を加えたものを gist に置きました。

修正箇所について説明します。

<VirtualHost *:80>
    ServerName gitlab.example.com
    ServerSignature Off

#   DocumentRoot /home/git/gitlab/public
#   ProxyPass / http://localhost:8080/ timeout=300
#   ProxyPassReverse / http://localhost:8080/

    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [NE,R,L]

    ErrorLog /var/log/httpd/gitlab/error_log
    TransferLog /var/log/httpd/gitlab/access_log
</VirtualHost>

HTTP でアクセスしたときの設定です。
コメントアウトしてあるのは、今まで使っていた設定です。
HTTPS でのアクセスがうまくいかなかったときのために残しておきました。
ログの設定も同様に残しています。

<VirtualHost *:443>
    SSLEngine on
    #strong encryption ciphers only
    #see ciphers(1) http://www.openssl.org/docs/apps/ciphers.html
    SSLCipherSuite SSLv3:TLSv1:+HIGH:!SSLv2:!MD5:!MEDIUM:!LOW:!EXP:!ADH:!eNULL:!aNULL
    SSLCertificateFile    /etc/httpd/conf/ssl.crt/server.crt
    SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key
#   SSLCACertificateFile  /etc/httpd/ssl.crt/your-ca.crt

鍵と証明書を設定しています。

    # Ensure that encoded slashes are not decoded but left in their encoded state.
    # http://doc.gitlab.com/ce/api/projects.html#get-single-project
#   AllowEncodedSlashes NoDecode
    AllowEncodedSlashes On

AllowEncodedSlashesNoDecode は、2.2.18 以降じゃないと使えないので、代わりに On に設定しています。

GitLab の設定

GitLab 側にも修正が必要です。
まず、GitLab の設定を修正します。

gitlab/config/gitlab.yml L15-
  ## GitLab settings
  gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: gitlab.example.com
    port: 443
    https: true

ポートを 443 に設定し、httpstrue にします。
上の対策をしたので、これで大丈夫だと思いますが、もしログインできなかったりした場合は、httpsfalse にしてみてください。

httpsfalse にすると、ログイン後のリダイレクトで HTTP の方に飛ばされてしまったりします。
gitlab-ssl.conf で、HTTP へのアクセスは HTTPS に飛ばすようにしてあるので問題ないのですが、ハマった場合はこの辺を疑ってみてください。

次に、GitLab Shell の設定を修正します。

gitlab-shell/config.yml L4-
# Url to gitlab instance. Used for api calls. Should end with a slash.
gitlab_url: "https://gitlab.example.com/"

http_settings:
#  user: someone
#  password: somepass
  ca_file: /etc/httpd/conf/ssl.crt/server.crt
  ca_path: /etc/httpd/conf/ssl.crt
  self_signed_cert: true

URL の http を https に修正して、鍵と証明書を設定します。
オレオレ証明書を使っている場合は、self_signed_cert を true にします。

最後に

GitLab と Apache を再起動すれば完了です。

# service gitlab restart
# service httpd restart
  1. 443番ポートで初めに定義されているのは、ssl.conf の中の VirtualHost です。

1 thoughts on “オレオレ証明書を使ってGitLabをhttps化

  1. ピンバック: GitLab | フリーランスSEの仕事中に得た知識を体系化

コメントを残す