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
証明機関
- DNS: cert.domain.local
- Windows Server 2012 R2
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
- "http://cert.domain.local/certsrv/"にアクセスする
- "証明書を要求する"
- "証明書の要求の詳細設定"
- "Base 64 エンコード CMC または PKCS #10 ファイルを使用して証明書の要求を送信するか、または Base 64 エンコード PKCS #7 ファイルを使用して更新の要求を送信する。" 5.作成したcsrファイルの中身をコピペし、証明書テンプレートを"Webサーバー"にします
- 発行した証明書を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'
GitLabにLDAPでログインする
GitLabにはLDAP、つまりADの認証を利用してログインすることができます。認証機関を統一できると管理が楽なのでおすすめです。
環境
ADサーバー:
- ホスト名 ad.domain.local
- ドメイン名 domain.local
ADにユーザーを作成
セキュリティのために権限を最小限に抑えたアカウントを作りましょう。
適当に名前を付けます。
アカウントオプションにはGitLabとの連携が切れないように、"ユーザーはパスワードを変更できない"・"パスワードを無制限にする"にチェックをつけるとよいでしょう。
LDAPの検索ができればよいのでグループには"Domain Guests"に参加するだけで十分です。
GitLabでLDAPの設定
LDAPの基本的な知識が必要になりますが、比較的簡単なので頑張りましょう。DNはGet-ADUser
を使えば簡単に取得することができます。baseはログインできるアカウントが最小限になるようにOUを作成するとよいでしょう。
約38行目から
/etc/gitlab/gitlab.rb
gitlab_rails['ldap_enabled'] = true gitlab_rails['ldap_servers'] = YAML.load <<-'EOS' # remember to close this block with 'EOS' below main: # 'main' is the GitLab 'provider ID' of this LDAP server label: 'domain.local' host: 'ad.domain.local' port: 389 uid: 'sAMAccountName' method: 'plain' # "tls" or "ssl" or "plain" bind_dn: 'CN=gitlab,CN=Users,DC=domain,DC=local' password: '[password]' active_directory: true base: 'DC=domain,DC=local' # allow_username_or_email_login: true # block_auto_created_users: false # user_filter: '' # ## EE only # group_base: '' # admin_group: '' # sync_ssh_keys: false EOS
最後に設定を反映させれば終わりです。
$ sudo gitlab-ctl reconfigure $ sudo gitlab-ctl restart
無償版の機能の制限
GitLabには無償版(Community Edition: CE)と有償版(Enterprise Edition: EE)によって機能の差があり、LDAPにも無償版には制限がかかっています。具体的には以下の項目になります。
- セカンダリサーバーの指定
- グループの取得
EEの意味が分かれば"gitlab.rb"に丁寧な説明が書いてあるのでわかってもらえるかと思います。英語が堪能ではない自分にはセカンダリサーバーの指定ができない理由が全く分からず、ずいぶん長い間悩むことになりました…
GitLabのメール通知をGmailで送る
GitLabはGitHubを参考にして作られたサービスであるため、似たような機能を数多く備えています。今回はGitHubにもあるメール通知をGmailのSMTPサーバーから送信する方法を書いていきます。
Gmailのアカウント設定
GitLabからGmailのSMTPサーバーにログインするにはセキュリティレベルを下げる必要があります。
ログインした状態で[安全性の低いアプリを許可]にアクセスし、"安全性の低いアプリのアクセス"をオンにします。文字通りセキュリティは弱くなるのでGitLab用のメールアカウントを用意するなどの対策をするとよいでしょう。
GitLabのメール設定
現状のバージョン(7.12.2)のOmnibusをインストールすると、"/etc/gitlab/gitlab.rb"を編集するだけですべて設定することができます。公式サイトの説明が一番わかりやすかったです。
/etc/gitlab/gitlab.rb
13行目から
# gitlab_rails['gitlab_ssh_host'] = 'ssh.host_example.com' # gitlab_rails['time_zone'] = 'UTC' gitlab_rails['gitlab_email_enabled'] = true gitlab_rails['gitlab_email_from'] = '[YourMailAddress]@gmail.com' gitlab_rails['gitlab_email_display_name'] = '[好きなように決めてください]' gitlab_rails['gitlab_email_reply_to'] = '[YourMailAddress]@gmail.com' # gitlab_rails['gitlab_default_can_create_group'] = true # gitlab_rails['gitlab_username_changing_enabled'] = true
187行目から
gitlab_rails['smtp_enable'] = true gitlab_rails['smtp_address'] = "smtp.gmail.com" gitlab_rails['smtp_port'] = 587 gitlab_rails['smtp_user_name'] = "[YourMailAddress]@gmail.com" gitlab_rails['smtp_password'] = "[YourPassword]" gitlab_rails['smtp_domain'] = "smtp.gmail.com" gitlab_rails['smtp_authentication'] = "login" gitlab_rails['smtp_enable_starttls_auto'] = true gitlab_rails['smtp_tls'] = false gitlab_rails['smtp_openssl_verify_mode'] = 'peer' # Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert',$ # gitlab_rails['smtp_ca_path'] = "/etc/ssl/certs" # gitlab_rails['smtp_ca_file'] = "/etc/ssl/certs/ca-certificates.crt"
以上で設定できたので反映させます。
$ sudo gitlab-ctl reconfigure $ sudo gitlab-ctl restart
これで設定できたはずですが、テストメールを送る機能を見つけられていないので検証する際はIssueを自分で作成して発言するなどが良いかと思います。
GitLab CIのメール設定
前述したGitLabの設定を行っていれば、GitLab CIの設定は簡単です。
/etc/gitlab/gitlab.rb
412行目から
gitlab_ci['gitlab_ci_email_from'] = '[YourMailAddress]@gmail.com' gitlab_ci['gitlab_ci_support_email'] = '[YourMailAddress]@gmail.com' # gitlab_ci['gitlab_ci_all_broken_builds'] = true # gitlab_ci['gitlab_ci_add_pusher'] = true
同様に反映させて終了です。
$ sudo gitlab-ctl reconfigure $ sudo gitlab-ctl restart
自分は友人との開発用にGitLabのサーバーをVPNにおいているのですが、外出先でも即時通知してくれるメール機能は重宝しています。皆さんもぜひ使ってみてください。
参考URL
Active Directoryで作成したホームディレクトリをUbuntuでマウントする方法
Active Directoryの認証を利用した共有フォルダをUbuntuのホームディレクトリに設定する方法を書きます。今回はWinbindでドメインに参加し、Ubuntuでホームディレクトリに対象のフォルダをマウントしていきます。
環境
ドメインサーバー
ホスト名 : ad.domain.local
レルム : DOMAIN.LOCAL
レルムは認証をするネットワークの範囲を示します。大文字入力が一般的です。わかりやすく言えばログインするときにユーザー名に"username@domain.local"のように入力した場合、@以降に入るものがレルムと等価であると考えていいはずです。
ファイルサーバー
ホスト名 : file.domain.local
共有先 : UserShare
※ 共有先に各ユーザーに各ユーザー名のフォルダ名を付けたディレクトリを作成しておきます(詳しい方法は別記事で)
クライアント
ホスト名 : client
OS : ubuntu server 14.04 LTS
ネットワーク : DNSにADサーバーをさすようにしておいてください(詳しい方法は別記事で)
具体的な設定
1. Winbind
必要なパッケージのインストール
$ sudo aptitude -y install winbind libpam-winbind libnss-winbind krb5-config
"Configuring Kerberos Authentication"が出てきた場合
自分は出てくるときと出てこない時があるんですけど何が条件なのでしょう?誰か教えてください。
それぞれの項目に対して以下のように入力してください。
Default Kerberos version 5 realm: DOMAIN.LOCAL
Kerberos servers for your realm: ad.domain.local
Administrative server for your Kerberos realm: ad.domain.local
sambaの設定
/etc/samba/smb.conf
29行目から修正と追記
workgroup = DOMAIN password server = ad.domain.local realm = DOMAIN.LOCAL security = ads idmap config * : range = 16777216-33554431 template homedir = /home/%U template shell = /bin/bash winbind use default domain = true winbind offline logon = false
pamなどのログインの設定
/etc/nsswitch.conf
7行目から追記
passwd:compat winbind group:compat winbind shadow:compat winbind
/etc/pam.d/common-session
最終行に追記
session optional pam_mkhomedir.so skel=/etc/skel umask=077
ADに参加
ADに参加します。Windowsがドメインに参加するときと同様に"Domain Admins"に参加しているユーザーが必要になります。
$ sudo net ads join -U Administrator Enter Administrator's password: Using short domain name -- DOMAIN Joined 'client' to dns domain 'domain.local' No DNS domain configured for client. Unable to perform DNS Update. DNS update failed: NT_STATUS_INVALID_PARAMETER $ sudo initctl restart winbind winbind start/running, process * $ sudo initctl restart smbd smbd start/running, process * $ sudo initctl restart nmbd nmbd start/running, process *
これでADに参加することができました。SSHでもログインすることができるようになっていると思います。DNSの更新ができないと怒られますが支障はありません。まだ検証不足ですが、前もって動的更新をオンにした入れ子をDNSに作っておき、クライアントの"/etc/network/interfaces"正しく"dns-search"を設定したうえで参加するとうまく行ったりします。
Sudoersの設定
Winbindの最後に"Domain Admins"に管理者権限を与えます。
/etc/sudoers
22行目以降
# Members of the admin group may gain root privileges %admin ALL=(ALL) ALL %Domain\ Admins ALL=(ALL) ALL
空白などの特殊文字の前には"\"を入れます。もちろん"Domain Admins"の部分を変えればほかのグループにも管理者権限を与えることは可能ですし、複数の行を作成して複数のグループに管理者権限を与えることも可能です。
2. ホームディレクトリにマウント
ホームディレクトリにマウントするために"libpam-mount"などをインストールします。
$ sudo apt-get install libpam-mount smbclient cifs-utils
/etc/security/pam_mount.conf.xml
16行目から追記と<mkmountpoint ...>
タグの修正
<volume user="*" server="file.domain.local" path="UserShares" mountpoint="home" fstype="cifs" /> <cifsmount>mount -t cifs //%(SERVER)/%(VOLUME)/%(USER) %(MNTPT)/%(USER) -o "user=%(USER),uid=%(USERUID),gid=%(USERGID)%(before=\",\" OPTIONS)"</cifsmount> <umount>umount %(MNTPT)/%(USER)</umount> <mkmountpoint enable="1" remove="false" />
以上でログイン時ににADのsmbサーバーのホームディレクトリをマウントするようなりました。GPOと組み合わせるとLinuxとWindowsの連携がとても取りやすくなるのでお勧めです。ぜひやってみてください。
参考URL
以下のサイトを参考にしました。ありがとうございます。
Samba Winbind : Server World
How to authenticate Linux accounts against an Active Directory and mount a Windows share on login? : Server Fault
このブログについて
自己紹介
自分は情報系の大学生です。小さなころからPCが大好きでいろいろいじっていましたが、プログラミングや自作PC、サーバー構築など実用的なことを始めたのは高校生になってからです。資格は基本情報技術者を高校生の時にとりました。これから応用情報やネットワークスペシャリストをとるか迷っています。未熟な点も多いですが、挑戦していこうと思うのでよろしくお願いします。
このブログの目的
このブログは自分がやったことを忘れないように書いていくことを主旨としています。もちろんそれだけではありません。もし、このブログが自分と同じ問題に直面した人などに役立つのなら幸いです。また、皆さんのコメントなども楽しみです。よく本にはほかの人とのつながりが成長の第一歩であるといった内容が記載されています。このブログをその一歩にしたいと思います。