前回 の続き。 HTTPS 通信できるようにしていく。
環境
- OS: Windows 10 Pro
- Docker version 18.03.1-ce, build 9ee9f40
- docker-compose version 1.21.1, build 7641a569
- 使用イメージ
- gitlab & gitlab-runner
- gitlab: gitlab/gitlab-ce:11.0.1-ce.0
- gitlab-runner: gitlab/gitlab-runner:v11.0.0
- 証明書作成関係
- gitlab & gitlab-runner
証明書の準備
証明書作成環境構築
PowerShell 上での作業
$WORKING_DIR="PATH_TO_WORKING_DIR"
mkdir $WORKING_DIR\work
cd $WORKING_DIR\work
curl https://raw.githubusercontent.com/mikoto2000/docker-images/324bc3d5f81ad602dc9c35280f9cfe6f1dd0484a/openssl/docker-compose.yml -OutFile .\docker-compose.yml
mkdir usr_lib_ssl
curl https://raw.githubusercontent.com/mikoto2000/docker-images/324bc3d5f81ad602dc9c35280f9cfe6f1dd0484a/openssl/usr_lib_ssl/openssl.cnf -OutFile .\usr_lib_ssl\openssl.cnf
curl https://raw.githubusercontent.com/mikoto2000/docker-images/324bc3d5f81ad602dc9c35280f9cfe6f1dd0484a/openssl/usr_lib_ssl/v3_ca.txt -OutFile .\usr_lib_ssl\v3_ca.txt
docker-compose run --rm openssl /bin/bash
CA 構築
openssl コンテナ内での作業
### 各種情報定義
CA_KEY_FILE="/ca/private/cakey.pem"
CA_CSR_FILE="/ca/ca.csr"
CA_CRT_FILE="/ca/cacert.pem"
CA_COUNTRY="JP"
CA_STATE="Tokyo"
CA_LOCALITY="foo-ku"
CA_ORGANIZATION="bar Inc."
CA_ORGANIZATIONAL_UNIT="baz"
CA_COMMON="gitlab.example.com"
CA_EMAIL="mikoto2000@gmail.com"
### CA 初期化開始
cd /ca
# 各種ディレクトリ作成
mkdir cert private crl newcerts
chmod 700 private
# シリアル初期化
echo "01" | tee -a serial
# crlnumber 初期化
echo "01" | tee -a crlnumber
# インデックス初期化
touch index.txt
### CA 鍵と CA 証明書作成開始
# CA 用秘密鍵作成
openssl genrsa -out ${CA_KEY_FILE} 2048
# CA 証明書要求作成
CA_SUBJECT="/C=${CA_COUNTRY}/ST=${CA_STATE}/L=${CA_LOCALITY}/O=${CA_ORGANIZATION}/OU=${CA_ORGANIZATIONAL_UNIT}/CN=${CA_COMMON}"
openssl req -new -key ${CA_KEY_FILE} -out ${CA_CSR_FILE} -subj "${CA_SUBJECT}"
# CA 証明書要求確認
openssl req -noout -text -in $CA_CSR_FILE
# CA 証明書作成
openssl x509 -req -in $CA_CSR_FILE -signkey $CA_KEY_FILE -out $CA_CRT_FILE -extfile /usr/lib/ssl/v3_ca.txt
# 証明書コピー
cp $CA_CRT_FILE /client/ca.crt
サーバー証明書作成
openssl 環境での作業
CLIENT_COMMON="gitlab.example.com"
CLIENT_KEY_FILE="/client/${CLIENT_COMMON}.key"
CLIENT_CSR_FILE="/client/${CLIENT_COMMON}.csr"
CLIENT_CRT_FILE="/client/${CLIENT_COMMON}.crt"
CLIENT_COUNTRY="JP"
CLIENT_STATE="Tokyo"
CLIENT_LOCALITY="foo-ku"
CLIENT_ORGANIZATION="bar Inc."
CLIENT_ORGANIZATIONAL_UNIT="baz"
CLIENT_EMAIL="mikoto2000@gmail.com"
# クライアント秘密鍵作成
openssl genrsa -out ${CLIENT_KEY_FILE} 2048
# クライアント証明書要求作成
CLIENT_SUBJECT="/C=${CLIENT_COUNTRY}/ST=${CLIENT_STATE}/L=${CLIENT_LOCALITY}/O=${CLIENT_ORGANIZATION}/OU=${CLIENT_ORGANIZATIONAL_UNIT}/CN=${CLIENT_COMMON}"
openssl req -new -key ${CLIENT_KEY_FILE} -out ${CLIENT_CSR_FILE} -subj "${CLIENT_SUBJECT}"
# クライアント証明書要求確認
openssl req -noout -text -in ${CLIENT_CSR_FILE}
### クライアント証明書作成
cd /client
yes | openssl ca -out ${CLIENT_CRT_FILE} -infiles ${CLIENT_CSR_FILE}
サーバー設定
docker-compose.yml
version: '3'
services:
gitlab:
image: gitlab/gitlab-ce:11.0.1-ce.0
restart: always
hostname: gitlab.example.com
container_name: gitlab.example.com
volumes:
- gitlab_etc:/etc/gitlab
- gitlab_log:/var/log/gitlab
- gitlab_opt:/var/opt/gitlab
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'https://gitlab.example.com/'
gitlab_rails['gitlab_shell_ssh_port'] = 8022
nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 80
ports:
- "80:80"
- "443:443"
- "8022:22"
networks:
gitlab_net:
ipv4_address: 172.16.238.2
gitlab-runner:
image: gitlab/gitlab-runner:v11.0.0
restart: always
hostname: gitlab-runner.example.com
container_name: gitlab-runner.example.com
volumes:
- gitlab-runner_config:/etc/gitlab-runner
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- gitlab
networks:
gitlab_net:
ipv4_address: 172.16.238.3
networks:
gitlab_net:
ipam:
config:
- subnet: 172.16.238.0/24
volumes:
gitlab_etc:
external: true
gitlab_log:
external: true
gitlab_opt:
external: true
gitlab-runner_config:
external: true
external_url
をhttps
に修正ports
に443:443
を追加GITLAB_OMNIBUS_CONFIG
に下記を追加nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 80
証明書の配置
PowerShell 上での作業
cd $WORKING_DIR
docker run -it --rm -v "$WORKING_DIR\work\client:/client" -v "gitlab-runner_config:/gitlab-runner_config" -v "gitlab_etc:/gitlab_etc" debian:stretch-slim /bin/bash
コンテナ内での作業
# gitlab にコピー
mkdir /gitlab_etc/ssl
chmod 700 /gitlab_etc/ssl
cp /client/gitlab.example.com.key /client/gitlab.example.com.crt /gitlab_etc/ssl
# gitlab-runner にコピー(※ CA の証明書をコピー)
cp /client/ca.crt /gitlab-runner_config/certs/gitlab.example.com.crt
# config.toml 内の `http` を `https` に修正(※ いくつか runner がある場合には、ちゃんとエディタで編集しましょう)
sed -i -e '{s/http:\/\//https:\/\//}' /gitlab-runner_config/config.toml
exit
動作確認
docker-compose up -d
のあと、前回の job を retry して動作確認。 ok.
0 件のコメント:
コメントを投稿