Cloudflare Zero Trust Root CA 証明書の期限切れに伴う更新

# Cloudflare Zero Trust Root CA 証明書の期限切れ
:::note warn
WARP ユーザへの通信影響を避けるために **日本時間 2025 年 2 月 3 日(月) 01:05 までに** 作業が必要です
:::

> [User-side certificates · Cloudflare Zero Trust docs](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/)
>
>デフォルトのグローバル Cloudflare ルート証明書の有効期限は 2025年2月3日です。2024年10月17日より前にデフォルトの Cloudflare 証明書をインストールした場合は、検査エラーを避けるために、新しい証明書を生成し、Zero Trust 組織用に有効化する必要があります。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/3bc2db89-5812-e538-c565-e1263ba92d2e.png)

# 更新手順
以下のような手順で Cloudflare Zero Trust Root CA 証明書の更新作業が必要です。

```mermaid:
%%{init: {'themeCSS': 'img {width: 48px; height:48px;}'}}%%
graph LR

subgraph Device
    OldCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>Old Root CA Cert]:::transparent
    NewCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>New Root CA Cert]:::transparent
end

subgraph Cloudflare Zero Trust
    subgraph Inactive
        InactiveNewCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>New Root CA Cert]:::transparent
    end
    subgraph Active
        ActivatedNewCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>New Root CA Cert]:::transparent
        ActivatedOldCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>Old Root CA Cert]:::transparent
    end
    subgraph Active and In-Use
    direction TB
        InUseNewCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>New Root CA Cert]:::transparent
        InUseOldCert[<img src='https://cf-icons.pages.dev/ssl.svg' width='48' height='48'/><br>Old Root CA Cert]:::transparent
%%        InUseNewCert ~~~ InUseOldCert
    end
end

API:::hidden --> |1.&nbsp;Generate|InactiveNewCert --> |2.&nbsp;Activate|ActivatedNewCert

ActivatedNewCert --> |4.&nbsp;Turn On|InUseNewCert
InUseOldCert -.-> |4.&nbsp;Automatically<br>Turned Off|ActivatedOldCert

OldCert ~~~ InUseOldCert <---- |"0.&nbsp;Install<br>(Download & Trust)"|OldCert
InUseOldCert ~~~ OldCert

NewCert ~~~ ActivatedNewCert ---> |"3.&nbsp;Install<br>(Download & Trust)"|NewCert
ActivatedNewCert ~~~ NewCert

classDef hidden display: none;
classDef transparent fill-opacity:0,stroke-opacity:0;
```

# 0. 既存証明書の確認

## GUI
「設定 ＞ リソース ＞ 証明書 ＞ 管理」から、現在「使用中」の証明書の有効期限が確認できます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/fd4306f7-ef68-7bf7-53f3-7fc6675518d6.png)


## API

`settings.certificate.id` が存在しなければ、デフォルトの Cloudflare Root CA が使用されます。

https://developers.cloudflare.com/api-next/resources/zero_trust/subresources/gateway/subresources/configurations/models/gateway_configuration_settings/#(schema)

>certificate?: {
>Certificate settings for Gateway TLS interception. If not specified, the Cloudflare Root CA will be used.
>
>id: string
>UUID of certificate to be used for interception. Certificate must be active on the edge. A nil UUID will indicate the Cloudflare Root CA should be used.
>
>}

```shell:
export EMAIL='YOUR_EMAIL'
export APIKEY='YOUR_APIKEY'
export ACCOUNT_ID='YOUR_ACCOUNT_ID'

curl -s https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/configuration -X GET \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" | jq '.result.settings.certificate'
```

# 1. 証明書の生成

## GUI
:::note warn
生成する証明書の有効期間は、1 日以上 30 年以内で指定する必要があります。
指定がない場合、証明書の有効期間はデフォルトで 5 年です。
:::

:::note warn
24 時間以内に最大 3 つの証明書を作成できます。必要に応じて証明書を削除してください。
:::

<img width="50%" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/d4b87288-d4e4-018a-6575-ac288e31fdbc.png"><img width="50%" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/0a047509-9622-3720-19c2-6ae2a09a69db.png">


## API

`{"validity_period_days": 10950}` でカスタム日数指定することで、30 年先まで有効なルート証明書がアカウントごとに発行できます。

https://developers.cloudflare.com/api/resources/zero_trust/subresources/gateway/subresources/certificates/methods/create/

```shell:
curl -s https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/certificates -X POST \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     -d '{"validity_period_days": 10950}' | jq
```

```json:
{
  "result": {
    "in_use": false,
    "id": "xxx",
    "certificate": "-----BEGIN CERTIFICATE-----\nxxx\n-----END CERTIFICATE-----\n",
    "issuer_org": "Cloudflare, Inc.",
    "issuer_raw": "CN=Gateway CA - Cloudflare Managed G1 YOUR_ACCOUNT_ID,OU=www.cloudflare.com,O=Cloudflare\\, Inc.,L=San Francisco,ST=California,C=US",
    "fingerprint": "xxx",
    "binding_status": "inactive",
    "qs_pack_id": "00000000-0000-0000-0000-000000000000",
    "type": "gateway_managed",
    "updated_at": "0001-01-01T00:00:00Z",
    "uploaded_on": "0001-01-01T00:00:00Z",
    "created_at": "2024-10-18T06:44:40.154205Z",
    "expires_on": "2054-10-18T06:40:00Z"
  },
  "success": true,
  "errors": [],
  "messages": []
}     
```

# 2. 証明書のアクティブ化

## GUI
:::note info
ステータスが `AVAILABLE` の証明書は、「[Install certificate using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/)」の機能で、WARP を使用してユーザーのデバイスにインストールできます。
:::

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/59fb2b37-3f9f-12f8-e2ca-0bdb15566860.png)

## API
`inactive` な証明書をアクティベートし、 `pending_deployment` から `active` になることを確認します。

```shell:
export CERT_ID='YOUR_CERT_ID'

curl -s https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/certificates/$CERT_ID/activate \ -X POST \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     -d '{}' | jq
```

```json:pending.json
{
  "result": {
    "in_use": false,
    "id": "xxx",
    "certificate": "-----BEGIN CERTIFICATE-----\nxxx\n-----END CERTIFICATE-----\n",
    "issuer_org": "Cloudflare, Inc.",
    "issuer_raw": "CN=Gateway CA - Cloudflare Managed G1 YOUR_ACCOUNT_ID,OU=www.cloudflare.com,O=Cloudflare\\, Inc.,L=San Francisco,ST=California,C=US",
    "fingerprint": "xxx",
    "binding_status": "pending_deployment",
    "qs_pack_id": "00000000-0000-0000-0000-000000000000",
    "type": "gateway_managed",
    "updated_at": "0001-01-01T00:00:00Z",
    "uploaded_on": "0001-01-01T00:00:00Z",
    "created_at": "2024-10-18T06:44:40.154205Z",
    "expires_on": "2054-10-18T06:40:00Z"
  },
  "success": true,
  "errors": [],
  "messages": []
}
```

```shell:
curl -s https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/certificates/$CERT_ID -X GET \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" | jq
```

```json:active.json
{
  "result": {
    "in_use": false,
    "id": "xxx",
    "certificate": "-----BEGIN CERTIFICATE-----\nxxx\n-----END CERTIFICATE-----\n",
    "issuer_org": "Cloudflare, Inc.",
    "issuer_raw": "CN=Gateway CA - Cloudflare Managed G1 YOUR_ACCOUNT_ID,OU=www.cloudflare.com,O=Cloudflare\\, Inc.,L=San Francisco,ST=California,C=US",
    "fingerprint": "xxx",
    "binding_status": "active",
    "qs_pack_id": "00000000-0000-0000-0000-000000000000",
    "type": "gateway_managed",
    "updated_at": "0001-01-01T00:00:00Z",
    "uploaded_on": "0001-01-01T00:00:00Z",
    "created_at": "2024-10-18T06:44:40.154205Z",
    "expires_on": "2054-10-18T06:40:00Z"
  },
  "success": true,
  "errors": [],
  "messages": []
}
```

# 3. 証明書のインストール
:::note info
Windows デバイスでは、[Install certificate using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/) によって、自動的に証明書をインストールし、**信頼します**。
:::

:::note warn
[Install certificate using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/) は、`AVAILABLE` に設定された証明書をダウンロードするようになります。新しく利用可能になった証明書がユーザーのデバイスにダウンロードされるまで、最大24時間かかる場合があります。

デバイスへのダウンロードを即時強制したい場合は、`warp-cli tunnel rotate-keys` によって、すぐに新しい証明書を WARP デバイスに強制配備できます。
:::

:::note warn
macOS Big Sur 以降では、[Install certificate using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/) によって、証明書を自動的に**信頼することができません**。
追加の対応として、個別に手動で信頼する、もしくは組織のすべてのデバイスで証明書が自動的に信頼されるように、モバイルデバイス管理（MDM）ツールを設定することが考えられます。
:::

|   | [Manually](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/manual-deployment/) | [With MDM](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/manual-deployment/#mobile-device-management-mdm-software) | [Using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/) |
|:-:|:-:|:-:|:-:|
| Windows | :white_check_mark: | :white_check_mark: | :white_check_mark:<br>Version 2024.12.554.0 or later<br>(Automatically installed and trusted) |
| macOS | :white_check_mark: | :white_check_mark: | :white_check_mark:<br>Version 2024.12.554.0 or later<br>(Need to trust manually) |
| Linux | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| iOS | :white_check_mark: | :white_check_mark: | :x: |
| Android | :white_check_mark: | :white_check_mark: | :x: |
| ChromeOS | :white_check_mark: | :white_check_mark: | :x: |

## Windows [using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/) Version 2024.12.554.0

- 「設定 ＞ WARP クライアント ＞ グローバル設定 ＞ [システム証明書ストアに CA をインストールする](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/warp/configure-warp/warp-settings/#install-ca-to-system-certificate-store)」を有効にします。
- [Cloudflare WARP Windows Release](https://install.appcenter.ms/orgs/cloudflare/apps/1.1.1.1-windows-1/distribution_groups/release) から Version 2024.12.554.0 をインストールします。

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Cloudflare WARP Windows Latest release<br>Version 2024.12.554.0<br>Dec 20, 2024 at 07:24<br>--<br>Adds support for installing all available custom gateway certificates from an account to the system store.<a href="https://t.co/NidgfsKUxQ">https://t.co/NidgfsKUxQ</a> <a href="https://t.co/1C5fzSJne6">pic.twitter.com/1C5fzSJne6</a></p>&mdash; kyhayama (@kyhayama) <a href="https://twitter.com/kyhayama/status/1869922200486326711?ref_src=twsrc%5Etfw">December 20, 2024</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

インストールされた証明書の一覧を確認するには、管理者として Command Prompt を実行した上で、以下のコマンドを実行します。

```bat:管理者として Command Prompt を実行
certlm.msc
```

「信頼されたルート証明機関 ＞ 証明書」に信頼済み証明書としてインストールされることが確認できます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/2043eb2a-a5f6-7e18-c3cc-edff9ff97c8f.png)

また、WARP クライアントによるデバイスポスチャで、以下のファイルチェックを設定することで、管理者側からデバイス全体に配布されたかどうかを確認できます。

- `%PROGRAMDATA%\Cloudflare\installed_certs\<CERT_ID>.pem`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/798a5afa-7d16-5cb9-08af-a685edd346cc.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/dcabba1d-3c5b-d1bb-152c-517bcd6e3e29.png)

## macOS [using WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/) Version 2024.12.554.0
以下の設定と WARP バージョンを使用します。

- 「設定 ＞ WARP クライアント ＞ グローバル設定 ＞ [システム証明書ストアに CA をインストールする](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/warp/configure-warp/warp-settings/#install-ca-to-system-certificate-store)」を有効にします。
- [Cloudflare WARP macOS Release](https://install.appcenter.ms/orgs/cloudflare/apps/1.1.1.1-macos-1/distribution_groups/release) から Version 2024.12.554.0 をインストールします。

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Cloudflare WARP macOS Latest release<br>Version 2024.12.554.0 (20241219.55)<br>Dec 20, 2024 at 07:33<br>---<br>Adds support for installing all available custom gateway certificates from an account to the system store.<a href="https://t.co/gYrVzTLjGf">https://t.co/gYrVzTLjGf</a> <a href="https://t.co/xMo95JlEvU">pic.twitter.com/xMo95JlEvU</a></p>&mdash; kyhayama (@kyhayama) <a href="https://twitter.com/kyhayama/status/1869922455923634626?ref_src=twsrc%5Etfw">December 20, 2024</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

証明書のインストールログは `warp-diag` の `daemon.log` から以下のような内容が確認できます。

macOS Sequoia を使用したため、証明書を自動的に信頼することに失敗している様子も確認できます。

https://forums.developer.apple.com/forums/thread/671582

```shell:
% cat Downloads/warp-debugging-info-20241223-160849/daemon.log | grep _ca
2024-12-23T03:10:30.990Z DEBUG actor_root_ca: Initializing root-ca actor
2024-12-23T03:10:30.990Z DEBUG actor_root_ca: Sleeping for 15s to allow main loop boot-up
2024-12-23T03:10:31.003Z DEBUG main_loop: warp::storage::registration: Error loading registration config, fetching a new one Error("missing field `install_root_ca`", line: 1, column: 24491)
2024-12-23T03:10:45.991Z DEBUG actor_root_ca::handler: Starting root-ca actor loop
2024-12-23T03:10:47.111Z  INFO actor_root_ca::handler: Applying account certificate(s) cert_ids=["6e3f82d0-9758-4680-9c5d-b629417b1a61", "c4c878ff-4c7b-47d4-b206-88f9f4265cd6"]
2024-12-23T03:10:47.310Z  INFO root_ca: Installing new root CA certificate total=2
2024-12-23T03:10:47.357Z  WARN root_ca: Failed to automatically trust certificates installed err=InstallFailed(Custom { kind: Other, error: "SecTrustSettingsSetTrustSettings: The authorization was denied since no user interaction was possible.\n" })
2024-12-23T03:10:47.357Z  INFO root_ca::utils: Successfully wrote certs to separate files destination="/Library/Application Support/Cloudflare/installed_certs"
2024-12-23T03:10:47.357Z DEBUG root_ca::store: Done installing certificate(s) total=2
2024-12-23T03:10:59.738Z  INFO actor_root_ca::handler: Applying account certificate(s) cert_ids=[]
2024-12-23T03:10:59.739Z  INFO root_ca: Removing cert from trust store fingerprint="E1804F9F3BE727C0FB9886098F1ED54E915E1229"
2024-12-23T03:10:59.770Z  INFO root_ca: Removing cert from trust store fingerprint="5E6A25FE165022165C58004C93E17A6C648043A1"
2024-12-23T03:10:59.793Z DEBUG root_ca::store: Done installing certificate(s) total=0
2024-12-23T03:11:05.163Z  INFO actor_root_ca::handler: Applying account certificate(s) cert_ids=[]
2024-12-23T03:11:05.164Z DEBUG root_ca::store: Done installing certificate(s) total=0
```

インストールされた証明書の一覧を取得するには、以下のコマンドを実行します。

```shell:
% security find-certificate -a /Library/Keychains/System.keychain | awk -F'"' '/alis/{print $4}' | grep Gateway   
Gateway CA - Cloudflare Managed G4 YOUR_ACCOUNT_ID
Gateway CA - Cloudflare Managed G1 YOUR_ACCOUNT_ID
Gateway CA - Cloudflare Managed G3 YOUR_ACCOUNT_ID
Gateway CA - Cloudflare Managed G2 YOUR_ACCOUNT_ID
```

これらの証明書を信頼するには、以下のコマンドを実行します。

```shell:
security find-certificate -a /Library/Keychains/System.keychain | awk -F'"' '/alis/{print $4}' | grep Gateway | while read line
do
  security -v find-certificate -c $line -p /Library/Keychains/System.keychain > $line.pem
  sudo security add-trusted-cert -d -r trustRoot -k "/Library/Keychains/System.keychain" $line.pem
done
```

もしくは Keychain Access のアプリを使って、証明書の確認と信頼ができます。

https://forums.developer.apple.com/forums/thread/756746

```shell:
open /System/Library/CoreServices/Applications/Keychain\ Access.app 
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/322c50c4-d3d5-208b-adda-72700e61eda4.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/34e46d29-7a8a-cf5c-a67a-2a239a497c7b.png)

また、WARP クライアントによるデバイスポスチャで、以下のファイルチェックを設定することで、管理者側からデバイス全体に配布されたかどうかを確認できます。

- `/Library/Application Support/Cloudflare/installed_certs/<CERT_ID>.pem`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/708dd6b1-5a0b-d0df-931d-0631e558db5b.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/908d9beb-93a1-f566-5df4-1d82c0f0343d.png)

# 4. 証明書の有効化（切り替え）

:::note warn
`使用中` のステータスに設定することができる証明書はアカウントごとに 1 つのみです。
アカウントに属するデバイス全体へ証明書が事前に配布されたことを確認してから切り替えるようにしてください。
:::

## GUI

各証明書の詳細から「証明書を確認して有効にする」ボタンを押すと、即時切り替わります。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/1a502451-7a2e-5f47-8ecc-9245cc287dfa.png)

## API
以下のコマンドを実行します。

```shell:
curl -s https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/configuration \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     -d '{"settings": {"certificate": {"id": "'$CERT_ID'"}} }' | jq
```

# 5. 接続確認
証明書の有効化（切り替え）後に WARP ON の状態でブラウジングすると、`TLS Decryption` に使用される証明書が切り替わっていることが確認できます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/c46371a6-2d3e-ae37-2b27-2a512f4f8c84.png)

# まとめ

アカウント全体に及ぶ設定の変更は、影響が大きいため、慎重におこなう必要があります。

とはいえ、既存の証明書の有効期限が迫っているため、期限前に切り替えを実施する必要があります。

各種連携ツールを駆使しながら、テスト用のアカウントを用意して手順を確認する等の対応を進める必要があります。

必要に応じてメンテナンスウインドウを設ける等して、業務への影響を最小限にできるよう、検討してもらえたらと思います。

# 参考 : デバイス一覧の取得
API からデバイス一覧を `csv` として取得したい場合には、以下のコマンドを実行します。

デバイスごとのバージョン情報を確認できます。

デバイス数が `per_page` の最大値 100 を超える場合、すべてのデバイスリストを取得するには `page` パラメータを増やして、複数回の API コールが必要です。

```shell:
export EMAIL='YOUR_EMAIL'
export APIKEY='YOUR_APIKEY'
export ACCOUNT_ID='YOUR_ACCOUNT_ID'

curl -sX GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices?page=1&per_page=100" \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     | jq -r '.result | ["id","user.id","user.email","user.name","key","device_type","name","version","updated","created","last_seen","model","mac_address","os_version","ip","gateway_device_id","serial_number"],(.[]|[.id,.user.id,.user.email,.user.name,.key,.device_type,.name,.version,.updated,.created,.last_seen,.model,.mac_address,.os_version,.ip,.gateway_device_id,.serial_number])|@csv' \
     > `date +%Y%m%d`_devices.csv
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/8e712e85-5557-3db3-37ba-48afc0421cf6.png)

# 参考：デバイスポスチャチェックも含めたデバイス一覧の取得
デバイスポスチャチェックの情報を個別に取得し、上記の内容にマージして `csv` として出力します。



```shell:
export EMAIL='YOUR_EMAIL'
export APIKEY='YOUR_APIKEY'
export ACCOUNT_ID='YOUR_ACCOUNT_ID'

# Download Device List as JSON
curl -sX GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices?page=1&per_page=100" \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     | jq -r '.result' > `date +%Y%m%d`_devices.json

---------------
--- Windows ---
---------------

# Specify the posuture rule id of Windows New Root CA Cert Installed Check
export WIN_POSTURE_ID=$(curl -sX GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices/posture?per_page=100" \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     | jq -r '.result[] | select(.name == "Windows New Root CA Cert Installed") | .id')
echo $WIN_POSTURE_ID

# Merge the posture check information into device list
echo '' > `date +%Y%m%d`_win_devices.json
cat `date +%Y%m%d`_devices.json | jq -r '.[].id' | while read line
do
  curl -sX GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices/$line" \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices/$line/posture/check" \
       -H "X-Auth-Email: $EMAIL" \
       -H "X-Auth-Key: $APIKEY" \
       -H "Content-Type: application/json" \
       | jq -s 'select(.[0].result.device_type == "windows") | .[0].result * {posture: .[1].result."'$WIN_POSTURE_ID'"}' >> `date +%Y%m%d`_win_devices.json
done

# Format JSON as CSV file
cat `date +%Y%m%d`_win_devices.json \
| jq -r -s '["id","user.id","user.email","user.name","key","device_type","name","version","updated","created","last_seen","model","mac_address","os_version","ip","gateway_device_id","serial_number","posture.rule_name","posture.timestamp","posture.success","posture.error"],(.[]|[.id,.user.id,.user.email,.user.name,.key,.device_type,.name,.version,.updated,.created,.last_seen,.model,.mac_address,.os_version,.ip,.gateway_device_id,.serial_number,.posture.rule_name,.posture.timestamp,.posture.success,.posture.error])|@csv' \
> `date +%Y%m%d`_win_devices.csv

-------------
--- macOS ---
-------------
# Specify the posuture rule id of macOS New Root CA Cert Installed Check
export MAC_POSTURE_ID=$(curl -sX GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices/posture?per_page=100" \
     -H "X-Auth-Email: $EMAIL" \
     -H "X-Auth-Key: $APIKEY" \
     -H "Content-Type: application/json" \
     | jq -r '.result[] | select(.name == "macOS New Root CA Cert Installed") | .id')
echo $MAC_POSTURE_ID

# Merge the posture check information into device list
echo '' > `date +%Y%m%d`_mac_devices.json
cat `date +%Y%m%d`_devices.json | jq -r '.[].id' | while read line
do
  curl -sX GET "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices/$line" \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/devices/$line/posture/check" \
       -H "X-Auth-Email: $EMAIL" \
       -H "X-Auth-Key: $APIKEY" \
       -H "Content-Type: application/json" \
       | jq -s 'select(.[0].result.device_type == "mac") | .[0].result * {posture: .[1].result."'$MAC_POSTURE_ID'"}' >> `date +%Y%m%d`_mac_devices.json
done

# Format JSON as CSV file
cat `date +%Y%m%d`_mac_devices.json \
| jq -r -s '["id","user.id","user.email","user.name","key","device_type","name","version","updated","created","last_seen","model","mac_address","os_version","ip","gateway_device_id","serial_number","posture.rule_name","posture.timestamp","posture.success","posture.error"],(.[]|[.id,.user.id,.user.email,.user.name,.key,.device_type,.name,.version,.updated,.created,.last_seen,.model,.mac_address,.os_version,.ip,.gateway_device_id,.serial_number,.posture.rule_name,.posture.timestamp,.posture.success,.posture.error])|@csv' \
> `date +%Y%m%d`_mac_devices.csv
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/cd7421e1-4422-95b9-e3b4-80c3e4ac6445.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/101361/5bb44eac-3570-e2f7-9f3d-ef16ee007e04.png)

# 参考 : Cloudflare Zero Trust Root CA 証明書のダウンロードページを Pages で作る

https://qiita.com/khayama/items/acbdfff809a061ef148c

# 参考 : Cloudflare からのメール通知

>- [Action Required] Reminder: Update Your Device Certificate Before Expiration on February 2, 2025
> 
> Zero Trust Account Name: My Enterprise Company, Inc.
>
> Hello Cloudflare Zero Trust Admin,
>
> This is a reminder that your **Cloudflare Zero Trust device certificate will expire on February 2, 2025, at 16:05:00 GMT**. You are receiving this email because we identified you as a Super Administrator or Administrator for an account that is still using the expiring certificate. Please update the device certificate before this expiration date so that **your users will not experience disruptions** while using Cloudflare’s Zero Trust protections.
>
>**What’s happening?**
>Cloudflare Zero Trust relies on [user-side certificates](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/) for functionality like [block pages](https://developers.cloudflare.com/cloudflare-one/policies/gateway/block-page/) for filtering (DNS and HTTP), [HTTPS traffic inspection](https://developers.cloudflare.com/cloudflare-one/policies/gateway/http-policies/tls-decryption/), [data loss prevention (DLP)](https://developers.cloudflare.com/cloudflare-one/policies/data-loss-prevention/), [anti-virus scanning](https://developers.cloudflare.com/cloudflare-one/policies/gateway/http-policies/antivirus-scanning/), [sandboxing](https://developers.cloudflare.com/cloudflare-one/policies/gateway/http-policies/file-sandboxing/), and [browser isolation](https://developers.cloudflare.com/cloudflare-one/policies/browser-isolation/). The current certificate is reaching its expiration date on February 2, 2025, requiring our customers to rotate certificates. If you are unable to update the device certificate before the expiry date your users will experience errors while trying to browse inspected domains and/or will not be able to view your block page. 
>
>**Action is required in the following cases:**
> - Your account has TLS decryption turned on in Zero Trust [network settings](https://one.dash.cloudflare.com/?to=/:account/settings/network).
> - Your account has Gateway DNS policies configured to display a [block page](https://developers.cloudflare.com/cloudflare-one/policies/gateway/block-page/#turn-on-the-block-page).
>
>**What do I need to do?**
>Please follow the instructions [here](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/) to generate a new account-specific Cloudflare Zero Trust root Certificate Authority (CA). Then, use the CA to issue and deploy your own .cert and .pem device certificates for all devices. The required steps are below:
>
>1. [Generate](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/#generate-a-cloudflare-root-certificate) a new certificate ([dashboard link](https://one.dash.cloudflare.com/?to=/:account/settings/resources/certificates)) and [activate](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/#activate-a-root-certificate) it in the dashboard.
>2. Download and trust the new certificates on all user devices through [WARP](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/#install-a-certificate-using-warp), [mobile device management (MDM) software](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/manual-deployment/#mobile-device-management-mdm-software), or [manually](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/manual-deployment/). 
>    1. Note: For customers updating certificates through WARP using the “[Install CA to system certificate store](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/warp/configure-warp/warp-settings/#install-ca-to-system-certificate-store)” option, the most recent client versions (2024.12.554.0) on Windows, Linux & MacOS support multiple AVAILABLE certificates on the client at once to reduce potential disruptions. MacOS and certain Linux distros will still require the user to [manually trust](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/automated-deployment/#macos) the downloaded certificate if deployed through WARP.
>    2. For mobile deployments, if you are not deploying certificates through an MDM, iOS and Android devices will require [manual installation](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/manual-deployment/#ios).
>3. Mark the newly generated certificate as IN-USE in the dashboard.
>
>New connections and block page instances will use this CA once you mark the certificate as IN-USE. 
>
>You can review [our documentation](https://developers.cloudflare.com/cloudflare-one/connections/connect-devices/user-side-certificates/) and reference this short [implementation demo](https://customer-2bofhtypwnbskqxy.cloudflarestream.com/6a9f04c84da8b33faa2d7b9f1801407b/watch) for more information.
>
>Need help?
We understand that this may be a large effort for some organizations. If any additional questions should arise, open a ticket in the [Cloudflare Support Portal](https://dash.cloudflare.com/?to=/:account/support) to contact our Support team.
>
>Best regards,
>
>Cloudflare Zero Trust Team