2
2

More than 3 years have passed since last update.

ApacheでOpenIDのリバースプロキシを作る with Docker

Posted at

概要

OpenIDのリバースプロキシをApacheで作る手順。docker-composeで環境を作る。
このページでは、prometheus, alertmanager, grafanaにOpenID認証をかける例を書く。

関連記事

ディレクトリ構造

.
├── docker-compose.yaml
├── .env
├── conf
│   └── openidc.conf
└── Dockerfile

docker-compose

prometheus, alertmanager, grafanaのコンテナは、127.0.0.1からのアクセスのみに制限する。

docker-compose.yaml
version: '3.2'

services:
  openid-rproxy:
    container_name: openid-rproxy
    build:
      context: ./
      dockerfile: Dockerfile
    env_file:
      - .env
    ports:
      - "80:80"
    restart: always

  prometheus:
    ports:
      - 127.0.0.1:9090:9090
    # 省略
  alertmanager:
    ports:
      - 127.0.0.1:9093:9093
    # 省略
  grafana:
    ports:
      - 127.0.0.1:3000:3000
    # 省略

prometheus, alertmanager, grafanaについては以下を参照。
参考:prometheus, alertmanager, grafanaのdocker-compose.yaml

Dockerfile

Apacheにmod-auth-openidcを入れて環境を作る。

Dockerfile
FROM httpd:2.4

RUN apt-get update -y
RUN mkdir /usr/local/apache2/conf/include/ &&  echo 'Include conf/include/*.conf ' >> /usr/local/apache2/conf/httpd.conf

RUN apt-get install -y libapache2-mod-auth-openidc

COPY ./conf/*.conf   /usr/local/apache2/conf/include/

.env

OpenIDの設定値を書く。

.env
# OIDCClientID
OPENID_CLIENT_ID=

# OIDCRedirectURI
OPENID_REDIRECT_URL=

# OIDCClientSecret
OPENID_CLIENT_SECRET=

# Require claim
OPENID_ROLE_NAME=

# OIDCCryptoPassphrase
# python -c 'import os,base64; print base64.urlsafe_b64encode(os.urandom(16))' などで発行する
OPENID_PASS_PHRASE=

conf/openidc.conf

Apacheのconfファイルを作る。

conf/openidc.conf
LoadModule auth_openidc_module /usr/lib/apache2/modules/mod_auth_openidc.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

<VirtualHost *:80>
  LogLevel info

  OIDCClientID ${OPENID_CLIENT_ID}
  OIDCClientSecret ${OPENID_CLIENT_SECRET}
  OIDCRedirectURI ${OPENID_REDIRECT_URL}
  OIDCCryptoPassphrase ${OPENID_PASS_PHRASE}
  # 適宜変更
  OIDCProviderMetadataURL https://accounts.google.com/.well-known/openid-configuration
  OIDCProviderTokenEndpointAuth client_secret_basic
  # 適宜変更
  OIDCScope "openid profile email"
  OIDCSSLValidateServer Off
  OIDCResponseType "code"
  # 適宜変更
  OIDCOAuthRemoteUserClaim username
  OIDCCookieSameSite On
  # セッション有効期間の秒数。デフォルトは28800秒(8時間)。
  # OIDCSessionMaxDuration 28800
  # 無操作タイムアウトの秒数。デフォルトは300秒。
  OIDCSessionInactivityTimeout 28800

  <Location />
    AuthType openid-connect
    Require valid-user
    Require claim ${OPENID_ROLE_NAME}
  </Location>

  ProxyRequests Off

  # proxyするホスト
  # prometheus, alertmanager, grafanaの3つを、サブディレクトリでアクセスできるようにする例
  ProxyPass /prometheus http://prometheus:9090/prometheus
  ProxyPassReverse /prometheus http://prometheus:9090/prometheus

  ProxyPass /alertmanager http://alertmanager:9093/alertmanager
  ProxyPassReverse /alertmanager http://alertmanager:9093/alertmanager

  # grafanaは末尾に/grafanaが不要
  ProxyPass /grafana http://grafana:3000
  ProxyPassReverse /grafana http://grafana:3000
</VirtualHost>

起動

$ docker-compose up -d --build

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2