LoginSignup
2
1

More than 1 year has passed since last update.

Cloudflare Zero Trust で IdP 登録をテストする (SimpleSAMLphp)

Last updated at Posted at 2022-09-12

概要

Cloudflare Zero Trust でテスト用の IdP を連携したいことがあります。

その場合は、Okta や OneLogin の開発者用無料アカウントを作成して IDaaS を利用することもできますが、自前のディレクトリサービスと連携して多様な認証方式を試すことができる SimpleSAMLphp も選択肢になります。

SP としても振る舞える一方で IdP としても活用可能なため、今回は IdP としての機能を利用します。

SP としての活用例はこちら:Cloudflare Zero Trust で SaaS アプリ登録をテストする (SimpleSAMLphp) - Qiita

SimpleSAMLphp インストール

CentOS を使います。

$ cat /etc/redhat-release 
CentOS Linux release 8.4.2105

事前に必要なパッケージをインストールし、サービスを起動します。

sudo dnf install -y httpd php php-xml php-json php-mbstring jq wget
sudo systemctl --now enable httpd
sudo systemctl --now enable php-fpm

SimpleSAMLphp をダウンロードします。

curl -s https://api.github.com/repos/simplesamlphp/simplesamlphp/releases/latest | jq '.assets[] | select(.name|match(".tar.gz$")) | .browser_download_url' | tr -d \" |  wget -qi -

ダウンロードしたファイルを確認します。

$ ls
simplesamlphp-1.19.6.tar.gz

ファイルを解凍し、中身を /var/www/simplesamlphp にコピーします。

tar xvzf simplesamlphp-1.19.6.tar.gz
sudo mv simplesamlphp-1.19.6/ /var/www/simplesamlphp

httpd に SimpleSAMLphp 用の設定ファイルを追加します。

cat << EOS | sudo tee /etc/httpd/conf.d/simplesamlphp.conf
Alias /simplesaml /var/www/simplesamlphp/www

<Directory /var/www/simplesamlphp/www>
   Require all granted
</Directory>
EOS

httpd を再起動し、SELinux を一時的に無効化します。

sudo systemctl restart httpd
sudo setenforce 0

SimpleSAMLphp を IdP として構成

以下のように設定ファイルを変更します。

example.com は、自身でホストするドメインに変更してください。

enable.saml20-idp で IdP 機能を有効化し、exampleauthでユーザ名・パスワードの認証方式を許可します。

sed -i "
  s/'auth.adminpassword' => '123'/'auth.adminpassword' => 'secret'/;
  s/'enable.saml20-idp' => false/'enable.saml20-idp' => true/;
  s/'baseurlpath' => 'simplesaml\/'/'baseurlpath' => 'https:\/\/idp.example.com\/simplesaml\/'/;
  s/'language.default' => 'en'/'language.default' => 'ja'/;
  s/'exampleauth' => false/'exampleauth' => true/;
  " /var/www/simplesamlphp/config/config.php

/config/authsources.phpの設定を以下のように変更します。

# コメントアウト削除
sed -i -e '103d'  /var/www/simplesamlphp/config/authsources.php
sed -i -e '85d'  /var/www/simplesamlphp/config/authsources.php
# ユーザ情報追加
sed -i "
  s/'exampleauth:UserPass',/'exampleauth:UserPass',\\
        'user1:pass1' => [\\
            'uid' => ['user1'],\\
            'email' => ['user1@example.com'],\\
            'eduPersonAffiliation' => ['member', 'admin'],\\
        ],\\
        'user2:pass2' => [\\
            'uid' => ['user2'],\\
            'email' => ['user2@example.com'],\\
            'eduPersonAffiliation' => ['member', 'developer'],\\
        ],/;
  " /var/www/simplesamlphp/config/authsources.php

以下の部分について、コメントアウトがなくなり、user1user2 が追加されたことを確認します。

    'example-userpass' => [
        'exampleauth:UserPass',
        'user1:pass1' => [
            'uid' => ['user1'],
            'email' => ['user1@example.com'],
            'eduPersonAffiliation' => ['member', 'admin'],
        ],
        'user2:pass2' => [
            'uid' => ['user2'],
            'email' => ['user2@example.com'],
            'eduPersonAffiliation' => ['member', 'developer'],
        ],

        // Give the user an option to save their username for future login attempts
        // And when enabled, what should the default be, to save the username or not
        //'remember.username.enabled' => false,
        //'remember.username.checked' => false,

        'student:studentpass' => [
            'uid' => ['test'],
            'eduPersonAffiliation' => ['member', 'student'],
        ],
        'employee:employeepass' => [
            'uid' => ['employee'],
            'eduPersonAffiliation' => ['member', 'employee'],
        ],
    ],

最後に SAML アサーションに署名する際に必要な自己証明書を作成します。

openssl req -newkey rsa:3072 -new -x509 -days 3652 -nodes -subj /C=JP/ST=Tokyo/CN=MyIdP -out /var/www/simplesamlphp/cert/server.crt -keyout /var/www/simplesamlphp/cert/server.pem
sudo chmod 644 /var/www/simplesamlphp/cert/server.pem

IdP(認証プロバイダ)に関するメタデータ設定は以下のファイルで確認できます。

$ cat /var/www/simplesamlphp/metadata/saml20-idp-hosted.php
<?php

/**
 * SAML 2.0 IdP configuration for SimpleSAMLphp.
 *
 * See: https://simplesamlphp.org/docs/stable/simplesamlphp-reference-idp-hosted
 */

$metadata['__DYNAMIC:1__'] = [
    /*
     * The hostname of the server (VHOST) that will use this SAML entity.
     *
     * Can be '__DEFAULT__', to use this entry by default.
     */
    'host' => '__DEFAULT__',

    // X.509 key and certificate. Relative to the cert directory.
    'privatekey' => 'server.pem',
    'certificate' => 'server.crt',

    /*
     * Authentication source to use. Must be one that is configured in
     * 'config/authsources.php'.
     */
    'auth' => 'example-userpass',

    /* Uncomment the following to use the uri NameFormat on attributes. */
    /*
    'attributes.NameFormat' => 'urn:oasis:names:tc:SAML:2.0:attrname-format:uri',
    'authproc' => [
        // Convert LDAP names to oids.
        100 => ['class' => 'core:AttributeMap', 'name2oid'],
    ],
    */

    /*
     * Uncomment the following to specify the registration information in the
     * exported metadata. Refer to:
     * http://docs.oasis-open.org/security/saml/Post2.0/saml-metadata-rpi/v1.0/cs01/saml-metadata-rpi-v1.0-cs01.html
     * for more information.
     */
    /*
    'RegistrationInfo' => [
        'authority' => 'urn:mace:example.org',
        'instant' => '2008-01-17T11:28:03Z',
        'policies' => [
            'en' => 'http://example.org/policy',
            'es' => 'http://example.org/politica',
        ],
    ],
    */
];

Cloudflare Tunnel インストール

以下のドキュメントを参考にセットアップします。

Set up your first tunnel · Cloudflare Zero Trust docsimage-20220722005638629

セットアップが完了すると、https://idp.example.com/simplesaml にアクセスできます。

example.com が Cloudflare でホストされている必要があります。)

image-20220603230741235

example-userpass のログイン確認

この段階で user1 でログインできることを確認しておきます。

image-20220913001813617

image-20220913001851330

image-20220913001922371

image-20220913001947170

SimpleSAMLphp の IdP メタデータを取得

以下の要領で IdP として構成できた metadata.xml をダウンロードします。

image-20220912004129440

image-20220912004220176

Cloudflare Zero Trust に IdP (SimpleSAMLphp) を登録

Settings > Authentication > Login methods > Add new > Add a login method > Select an identity provider

から Generic な SAML プロバイダとして登録します。

image-20220913002406547

先ほどダウンロードした metadata.xml をアップロードすると、必須項目が自動で入力されます。

image-20220912004656363

Email / SAML attribute を以下のように構成して、Save します。

image-20220913002731755

Cloudflare Access を SimpleSAMLphp に SP として登録

SimpleSAMLphp を IdP としたときに、SimpleSAMLphp からみると、Cloudflare は連携先の SP と見えるので、そのように登録します。

上記登録時のガイドにもある https://<your-team-name>.cloudflareaccess.com/cdn-cgi/access/saml-metadata から取得できる情報を以下のコマンドで反映できます。

sed -i "
  s/<?php/<?php\\
\$metadata['https:\/\/<your-team-name>.cloudflareaccess.com\/cdn-cgi\/access\/callback'] = [\\
    'AssertionConsumerService' => 'https:\/\/<your-team-name>.cloudflareaccess.com\/cdn-cgi\/access\/callback',\\
    'SingleLogoutService' => 'https:\/\/<your-team-name>.cloudflareaccess.com\/cdn-cgi\/access\/logout',\\
];/;
  " /var/www/simplesamlphp/metadata/saml20-sp-remote.php

認証テスト

正しく構成されたかをテストできます。

image-20220913003301986

image-20220913001922371

image-20220913003347586

まとめ

シンプルなユーザー名・パスワードでの認証プロバイダとして SimpleSAMLphp を Cloudflare Zero Trust に連携させることができました。

また、SimpleSAMLphp には exampleauth:UserPass 以外にもBasic認証やディレクトリサービスとの連携、データベース照合など、他にも利用可能な認証モジュールがあるため、様々なケースでのテストに便利に使えそうです。

SimpleSAMLphp Authentication module

authcrypt:Hash

Username & password authentication with hashed passwords.

authcrypt:Htpasswd

Username & password authentication against .htpasswd file.

authX509:authX509userCert

Authenticate against a LDAP database with a SSL client certificate.

exampleauth:UserPass

Authenticate against a list of usernames and passwords.

exampleauth:Static

Automatically log in as a user with a set of attributes.

ldap:LDAP

Authenticates an user to a LDAP server.

ldap:LDAPMulti

Authenticates an user to one of several LDAP server. The user can choose the LDAP server from a dropdown list.

sqlauth:SQL

Authenticate an user against a database.

radius:Radius

Authenticates an user to a Radius server.

multiauth:MultiAuth

Allow the user to select from a list of authentication sources.

saml:SP

Authenticate against a SAML IdP. Can be used for bridging.

authYubiKey:YubiKey

Authenticate with a YubiKey .

authtwitter:Twitter

Authenticate with your Twitter account using the Twitter OAuth API.

papi:PAPI

Authenticate by means of the PAPI protocol.

参考

2
1
1

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
1