# Chef で Device Protection をデプロイする

Chef クックブックを使用して、Chef Infra Client で既に管理している Linux マシンに Device Protection をインストールします。以下のレシピは、プラットフォームごとに適切なパッケージを選択し、ユーザーグループトークンを使ってインストールし、毎回のコンバージでサービスが実行され続けるようにします。

まず [Install Device Protection for Linux](https://help.aikido.dev/docs/docs-ja/aikido-device-protection/deploying-aikido-endpoint/device-protection-mdm-guides/linux/install-device-protection-for-linux) を読み、すべての方法で使用するトークン、パッケージ、インストールオプションを確認してください。残りの内容、つまりトークンの取り扱い、再起動、繰り返し実行、デバイス ID については [Linux Rollout Reference](https://help.aikido.dev/docs/docs-ja/aikido-device-protection/deploying-aikido-endpoint/device-protection-mdm-guides/linux/linux-rollout-reference) で説明しています。

## クックブックの構成

```
cookbooks/aikido_device_protection/
├── attributes/default.rb
├── metadata.rb
└── recipes/default.rb
```

## クックブックをセットアップする

{% stepper %}
{% step %}
**属性を追加する**

{% code title="cookbooks/aikido_device_protection/attributes/default.rb" %}
```ruby
default['aikido']['release_url'] =
  'https://github.com/AikidoSec/safechain-internals/releases/latest/download'

# Reboot after installing. Off by default, see step 3.
default['aikido']['reboot'] = false
```
{% endcode %}
{% endstep %}

{% step %}
**トークンを暗号化されたデータバッグに保存する**

これらのノードが属するユーザーグループのトークンを保持するデータバッグアイテムを作成します:

{% code overflow="wrap" %}
```bash
knife data bag create aikido device_protection --secret-file /path/to/encrypted_data_bag_secret
```
{% endcode %}

`data_bag_item` は、`Chef::Config[:encrypted_data_bag_secret]` にあるシークレットを使用してノード上で復号します。代わりに [Chef Vault](https://github.com/chef/chef-vault) を使用する場合は、参照を `chef_vault_item('aikido', 'device_protection')['token']` に置き換えてください。
{% endstep %}

{% step %}
**レシピを追加する**

{% code title="cookbooks/aikido_device_protection/recipes/default.rb" %}
```ruby
arch = node['kernel']['machine'] == 'aarch64' ? 'arm64' : 'amd64'

case node['platform_family']
when 'debian'
  package_file    = "EndpointProtection-#{arch}.deb"
  install_command = 'apt-get install -y'
  installed_check = "dpkg-query -W -f='${db:Status-Status}' aikido-endpoint-protection 2>/dev/null | grep -qx installed"
when 'rhel'
  package_file    = "EndpointProtection-#{arch}.el#{node['platform_version'].to_i}.rpm"
  install_command = 'dnf install -y'
  installed_check = 'rpm -q aikido-endpoint-protection'
else
  raise "Aikido Device Protection does not support #{node['platform_family']}"
end

package_path = "/var/cache/aikido/#{package_file}"

directory '/var/cache/aikido' do
  owner 'root'
  group 'root'
  mode '0700'
end

remote_file package_path do
  source "#{node['aikido']['release_url']}/#{package_file}"
  owner 'root'
  mode '0600'
  not_if installed_check
end

execute 'install aikido-endpoint-protection' do
  command "#{install_command} #{package_path}"
  environment 'AIKIDO_TOKEN' => data_bag_item('aikido', 'device_protection')['token']
  sensitive true
  not_if installed_check
  notifies :request_reboot, 'reboot[activate aikido-endpoint-protection]', :delayed
end

service 'aikido-endpoint-protection' do
  action [:enable, :start]
end

reboot 'activate aikido-endpoint-protection' do
  action :nothing
  reason 'Activate Aikido Device Protection'
  delay_mins 5
  only_if { node['aikido']['reboot'] }
end
```
{% endcode %}

このレシピには、知っておくべき点が 3 つあります:

* Chef の `package` リソースでは[インストールに必要な `AIKIDO_TOKEN`](https://help.aikido.dev/docs/docs-ja/aikido-device-protection/deploying-aikido-endpoint/device-protection-mdm-guides/linux/linux-rollout-reference#how-the-token-reaches-the-package) を渡せないため、インストールは `execute` リソースで行っています。
* [`not_if installed_check`](https://help.aikido.dev/docs/docs-ja/aikido-device-protection/deploying-aikido-endpoint/device-protection-mdm-guides/linux/linux-rollout-reference#make-repeat-runs-cheap) はダウンロードとインストールを保護しますが、これはアップグレードも阻止することを意味します。フリートを更新したい場合は、これを削除するか、`release_url` をミラーしたパッケージに向けてください。
* `node['aikido']['reboot']` はデフォルトでオフになっているため、コンバージによって誰かが作業中のマシンが再起動されることはありません。無人のノードではオンにしてください。[When protection becomes active](https://help.aikido.dev/docs/docs-ja/aikido-device-protection/deploying-aikido-endpoint/device-protection-mdm-guides/linux/linux-rollout-reference#when-protection-becomes-active) を参照してください。
{% endstep %}

{% step %}
**ラン・リストに追加する**

ロール、環境、または Policyfile を通じて、保護したいノードのラン・リストにレシピを追加します:

```ruby
run_list 'recipe[aikido_device_protection]'
```

各ノードで次回のコンバージ時にエージェントがインストールされます。段階的にロールアウトするには、まず 1 つのロールにレシピを追加してください。
{% endstep %}
{% endstepper %}

## ロールアウトを確認する

コンバージ済みのノードで次を実行します:

{% code overflow="wrap" %}
```bash
systemctl is-active aikido-endpoint-protection
aikidoctl version
```
{% endcode %}

その後、デバイスが **Active** ステータスで[デバイス一覧](https://app.aikido.dev/endpoint-protection/devices)に表示されます。

## トラブルシューティング


| 問題                                                                                  | 対処法                                                                                                                                                                                                                                             |
| ---------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| パッケージはインストールされるが、デバイスがダッシュボードに表示されない                       | インストールコマンドに `AIKIDO_TOKEN` が設定されていませんでした。構成管理ツールはローカル環境をホストに転送しないため、タスク自体に変数を設定してから、パッケージを再度インストールしてください                                    |
| `apt` または `dnf` がファイルをインストールせず、リポジトリ内でパッケージを探してしまう | 名前ではなくパスを指定してください: `apt install ./EndpointProtection-amd64.deb`。先頭の `./` によって、パッケージマネージャーはそれをローカルファイルとして扱います                                                                                              |
| Red Hat 系のホストでインストールが失敗する                                               | ビルドをメジャーバージョンに合わせてください。バージョン 9 では `el9` パッケージを、バージョン 10 では `el10` パッケージを使用します                                                                                                                                   |
| サービスが実行されていない                                                               | `systemctl status aikido-endpoint-protection` を実行し、次に `sudo aikidoctl diagnostics` を実行して詳細を送信してください                                                                                                                                 |
| Node.js または uv が依然として Aikido 証明書を拒否する                                        | インストール時に `NODE_EXTRA_CA_CERTS` と `UV_SYSTEM_CERTS` がシステム全体に設定されますが、実行中のシェルにはこれらが反映されません。新しいログインシェルを開くか、デバイスを再起動してください                                                                                    |
| トークンが実行出力やログに表示される                                                 | ツールのマスキング機能を使用してください: Ansible では `no_log`、Chef では `sensitive true`、Puppet では `Sensitive()`                                                                                                                                               |
| 複数のマシンがデバイス一覧で 1 つのエントリを共有している                                      | それらのマシンは同じ `/etc/machine-id` で起動しています。Linux のデバイス ID はこのファイルに基づくため、クローン元のイメージでこのファイルをクリアし、初回起動時に systemd に新しいものを書き込ませてください                                                                |
| トレイアイコンが表示されない                                                                     | Ubuntu 以外の GNOME では想定どおりの動作であり、見た目上の問題にすぎません。[Tray Icon Support on Linux](https://help.aikido.dev/docs/docs-ja/aikido-device-protection/deploying-aikido-endpoint/device-protection-mdm-guides/linux/install-device-protection-for-linux/tray-icon-support-on-linux) を参照してください |
