1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

リモート作業に優しい Apache 認証設定

Last updated at Posted at 2020-05-24

以下の要件を満たす Apache conf が必要でした。

  • オフィスネットワークからのアクセスは認証なし
  • 外部からのアクセスでは Basic 認証
  • ロードバランサーからのヘルスチェックは認証なし
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_host_module modules/mod_authz_host.so

# 許可された IP アドレスからのアクセス条件
SetEnvIf Remote_Addr "^172\.16\." load_barancer # LB ネットワーク
SetEnvIf X-Forwarded-For "^xxx\.xxx\.xxx\.xxx(,| |$)" allowed_access # 社内 IP
SetEnvIf X-Forwarded-For "^$" internal_access # LB からの直アクセス

<Location />
  <RequireAny>
    # LBからの直接アクセス
    <RequireAll>
      Require ip 172.16.0.0/16
      Require env internal_access
    </RequireAll>

    # 許可された IP アドレス
    <RequireAll>
      Require env load_barancer
      Require env allowed_access
    </RequireAll>

    # それ以外のアクセスはBasic認証
    AuthType basic
    AuthName "Private Zone"
    AuthUserFile /var/www/html/.htpasswd
    Require valid-user
  </RequireAny>
</Location>

最後の条件、ヘルスチェックについては 401 を OK コードにするか、 /healthcheck エンドポイントを用意できればこんな面倒な記述は不要で、以下で OK です。

LoadModule setenvif_module modules/mod_setenvif.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_host_module modules/mod_authz_host.so

# 許可された IP アドレスからのアクセス条件
SetEnvIf Remote_Addr "^172\.16\." load_barancer # LB ネットワーク
SetEnvIf X-Forwarded-For "^xxx\.xxx\.xxx\.xxx(,| |$)" allowed_access # 社内 IP

<Location />
  <RequireAny>
    # 許可された IP アドレス
    <RequireAll>
      Require env load_barancer
      Require env allowed_access
    </RequireAll>

    # それ以外のアクセスはBasic認証
    AuthType basic
    AuthName "Private Zone"
    AuthUserFile /var/www/html/.htpasswd
    Require valid-user
  </RequireAny>
</Location>

<Location /healthcheck>
  Require ip 172.16.0.0/16
</Location>

内部ネットワークの prefix が 16 以外の場合は前方一致だと不完全なため、SetEnvIf Remote_Addr が複数必要になります。

参考サイト

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?