コンテンツにスキップ

短命の証明書

Cloudflare Accessは、ユーザーのAccessログインによって生成されたトークンに基づいて発行された短命の証明書で、従来のSSHキーのモデルを置き換えることができます。従来のモデルでは、ユーザーはキーペアを生成し、その公開鍵をSaltのようなインフラ管理ツールにコミットするか、管理者にアップロードします。これらの鍵は数ヶ月または数年の間変更されないことがあります。

Cloudflare Accessは、エンドユーザーが鍵を生成する負担を取り除き、短命の証明書を使用してインフラへのアクセスのセキュリティを向上させます。

1. Cloudflare Accessの背後にサーバーを保護する

Cloudflare Accessの短命の証明書は、Accessの背後にあるかどうかに関わらず、任意の最新のSSHサーバーで機能します。ただし、追加のセキュリティや監査機能、ブラウザベースのターミナルなどの機能を考慮して、サーバーをAccessの背後に置くことをお勧めします。

Cloudflare Accessの背後にサーバーを保護するには:

  1. サーバーをパブリックホスト名ルートに接続します。
  2. サーバー用のセルフホスト型Accessアプリケーションを作成します。

2. Unixユーザー名がユーザーのSSOアイデンティティと一致することを確認する

Cloudflare Accessは、トークンからアイデンティティを取得し、短命の証明書を使用してターゲットインフラでユーザーを認証します。

The simplest setup is one where a user’s Unix username matches their email address prefix. Issued short-lived certificates will be valid for the user’s email address prefix. For example, if a user in your Okta or GSuite organization is registered as jdoe@example.com, they would log in to the SSH server as jdoe.

For testing purposes, you can run the following command to generate a Unix user on the machine:

Terminal window
sudo adduser jdoe

Advanced setup: Differing usernames

SSH certificates include one or more principals in their signature which indicate the Unix usernames the certificate is allowed to log in as. Cloudflare Access will always set the principal to the user’s email address prefix. For example, when jdoe@example.com tries to connect, Access issues a short-lived certificate authorized for the principal jdoe.

By default, SSH servers authenticate the Unix username against the principals listed in the user’s certificate. You can configure your SSH server to accept principals that do not match the Unix username.

Username matches a different email

To allow jdoe@example.com to log in as the user johndoe, add the following to the server’s /etc/ssh/sshd_config:

Match user johndoe
AuthorizedPrincipalsCommand /bin/echo 'jdoe'
AuthorizedPrincipalsCommandUser nobody

This tells the SSH server that, when someone tries to authenticate as the user johndoe, check their certificate for the principal jdoe. This would allow the user jdoe@example.com to sign into the server with a command such as:

Terminal window
ssh johndoe@server

Username matches multiple emails

To allow multiple email addresses to log in as vmuser, add the following to the server’s /etc/ssh/sshd_config:

Match user vmuser
AuthorizedPrincipalsFile /etc/ssh/vmusers-list.txt

This tells the SSH server to load a list of principles from a file. Then, in /etc/ssh/vmusers-list.txt, list the email prefixes that can log in as vmuser, one per line:

jdoe
bwayne
robin

Username matches all users

To allow any Access user to log in as vmuser, add the following command to the server’s /etc/ssh/sshd_config:

Match user vmuser
AuthorizedPrincipalsCommand /bin/bash -c "echo '%t %k' | ssh-keygen -L -f - | grep -A1 Principals"
AuthorizedPrincipalsCommandUser nobody

This command takes the certificate presented by the user and authorizes whatever principal is listed on it.

Allow all users

To allow any Access user to log in with any username, add the following to the server’s /etc/ssh/sshd_config:

AuthorizedPrincipalsCommand /bin/bash -c "echo '%t %k' | ssh-keygen -L -f - | grep -A1 Principals"
AuthorizedPrincipalsCommandUser nobody

Since this will put the security of your server entirely dependent on your Access configuration, make sure your Access policies are correctly configured.

3. 短命の証明書の公開鍵を生成する

  1. Zero Trustで、Access > Service Auth > SSHに移動します。

  2. Applicationドロップダウンで、SSHサーバーを表すAccessアプリケーションを選択します。

  3. Generate certificateを選択します。アプリケーションにスコープされた公開鍵を含む行が表示されます。

  4. 鍵を保存するか、サーバーの設定に便利な場所に保管します。 サービス認証ダッシュボードでこの公開鍵をコピーするために戻ることができます。

4. 公開鍵を保存する

  1. ステップ3でダッシュボードから生成された公開鍵をコピーします。
  1. Use the following command to change directories to the SSH configuration directory on the remote target machine:

    Terminal window
    cd /etc/ssh
  2. Once there, you can use the following command to both generate the file and open a text editor to input/paste the public key.

    Terminal window
    vim ca.pub
  3. In the ca.pub file, paste the public key without any modifications.

    The ca.pub file can hold multiple keys, listed one per line. Empty lines and comments starting with # are also allowed.

  4. Save the ca.pub file. In some systems, you may need to use the following command to force the file to save depending on your permissions:

    Terminal window
    :w !sudo tee %
    :q!

5. SSHD設定を変更する

The following procedure makes two changes to the sshd_config file on the remote target machine. The first change requires that you uncomment a field already set in most default configurations; the second change adds a new field.

  1. While staying within the /etc/ssh directory on the remote machine, open the sshd_config file.

    Terminal window
    vim /etc/ssh/sshd_config
  2. Go to the row named PubkeyAuthentication. In most default configurations, the row will appear commented out as follows:

    # PubkeyAuthentication yes
  3. Remove the # symbol to uncomment the line; keep the setting yes enabled.

  4. Next, add a new line below PubkeyAuthentication as follows:

    TrustedUserCAKeys /etc/ssh/ca.pub

    Save the file and quit the editor. You might need to use the following command again to save and exit.

    :w !sudo tee %
    :q!

6. SSHサーバーを再起動する

Once you have modified your SSHD configuration, restart the SSH service on the remote machine.

Debian/Ubuntu

For older Debian/Ubuntu versions:

Terminal window
sudo service ssh restart

For newer Debian/Ubuntu versions:

Terminal window
sudo systemctl restart ssh

CentOS/RHEL

For CentOS/RHEL 6 and older:

Terminal window
sudo service sshd restart

For CentOS/RHEL 7 and newer:

Terminal window
sudo systemctl restart sshd

7. ユーザーとして接続する

クライアントSSH設定を構成する

クライアント側では、デバイスを構成してCloudflare Accessを使用して保護されたマシンにアクセスします。短命の証明書を使用するには、SSH設定ファイル(~/.ssh/config)に以下の設定を含める必要があります。

時間を節約するために、以下のcloudflaredコマンドを使用して必要な設定コマンドを印刷できます:

Terminal window
cloudflared access ssh-config --hostname vm.example.com --short-lived-cert

手動で設定することを好む場合、生成されたSSH設定の例は次のとおりです:

Match host vm.example.com exec "/usr/local/bin/cloudflared access ssh-gen --hostname %h"
HostName vm.example.com
ProxyCommand /usr/local/bin/cloudflared access ssh --hostname %h
IdentityFile ~/.cloudflared/vm.example.com-cf_key
CertificateFile ~/.cloudflared/vm.example.com-cf_key-cert.pub

ブラウザベースのターミナルを介して接続する

エンドユーザーは、Cloudflareのブラウザベースのターミナルを使用して、設定なしでSSHセッションに接続できます。ユーザーはアプリケーションのURLにアクセスし、Cloudflareのターミナルが短命の証明書のフローを処理します。これを有効にするには、ブラウザレンダリングを有効にするを参照してください。


あなたのSSHサーバーは現在Cloudflare Accessの背後で保護されています — ユーザーは接続する前にアイデンティティプロバイダーで認証を求められます。また、Gateway Audit SSHポリシーを設定することで、SSHコマンドのログ記録を有効にすることもできます。