# パッケージのインストール中に Device Protection を強制する

Aikido Device Protection が有効でないマシンでのパッケージのインストールをブロックします。これにより、ポリシーだけに頼らず、開発ワークフローの一部として保護を強制できます。

## これを使う理由

サプライチェーン攻撃は、依存関係を通じて侵入することがよくあります。Device Protection はインストール時に悪意のあるパッケージを遮断しますが、動作している場合に限ります。インストール前のチェックを追加することで、保護されていないマシンに誤ってパッケージをインストールすることを防げます。

## 仕組み

多くのパッケージマネージャーは、インストール前に実行されるライフサイクルフックやラッパースクリプトをサポートしています。チェックがゼロ以外のコードで終了すると、インストールは中止され、開発者には明確なメッセージが表示されます。

{% tabs %}
{% tab title="macOS" %}
{% code overflow="wrap" %}

```bash
/Applications/Aikido\ Endpoint\ Protection.app/Contents/Resources/scripts/healthy
```

{% endcode %}
{% endtab %}

{% tab title="Windows" %}
{% code overflow="wrap" %}

```powershell
& "C:\Program Files\AikidoSecurity\EndpointProtection\scripts\Healthy.ps1"
```

{% endcode %}
{% endtab %}
{% endtabs %}

## パッケージマネージャーの例

以下の例では macOS のパスを使用しています。Windows では、上記の PowerShell コマンドに置き換えてください。

{% tabs %}
{% tab title="npm" %}
を追加します。 `preinstall` 用のスクリプト `package.json`:

```json
{
  "scripts": {
    "preinstall": "'/Applications/Aikido Endpoint Protection.app/Contents/Resources/scripts/healthy'"
  }
}
```

{% endtab %}

{% tab title="yarn / pnpm" %}
yarn と pnpm はどちらも `preinstall` スクリプトを `package.json`:

```json
{
  "scripts": {
    "preinstall": "'/Applications/Aikido Endpoint Protection.app/Contents/Resources/scripts/healthy'"
  }
}
```

{% endtab %}

{% tab title="pip / uv" %}
pip と uv にはネイティブのインストール前フックがありません。代わりに `Makefile` インストールをラップするターゲットを使います:

```makefile
install: check-aikido
	pip install -r requirements.txt

check-aikido:
	/Applications/Aikido\ Endpoint\ Protection.app/Contents/Resources/scripts/healthy
```

実行します `make install` の代わりに `pip install` を直接使います。同じパターンは `uv sync` または `poetry install`.
{% endtab %}

{% tab title="Maven" %}
次の `exec-maven-plugin` に `validate` フェーズに `pom.xml`を追加します。これは依存関係が解決される前に実行されます:

```xml
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.1.0</version>
  <executions>
    <execution>
      <id>check-aikido</id>
      <phase>validate</phase>
      <goals><goal>exec</goal></goals>
      <configuration>
        <executable>bash</executable>
        <arguments>
          <argument>-c</argument>
          <argument>/Applications/Aikido\ Endpoint\ Protection.app/Contents/Resources/scripts/healthy</argument>
        </arguments>
      </configuration>
    </execution>
  </executions>
</plugin>
```

{% endtab %}

{% tab title="NuGet" %}
を追加します。 `.targets` ファイルをプロジェクトに追加し（例: `CheckAikido.targets`）、それを `.csproj`:

```xml
<!-- CheckAikido.targets -->
<Project>
  <Target Name="CheckAikido" BeforeTargets="Restore">
    <Exec Command="bash -c &quot;/Applications/Aikido\ Endpoint\ Protection.app/Contents/Resources/scripts/healthy&quot;" />
  </Target>
</Project>
```

```xml
<!-- YourProject.csproj -->
<Import Project="CheckAikido.targets" />
```

{% endtab %}
{% endtabs %}

## Git フック

Git フックはコミットやチェックアウトの前に実行され、パッケージのインストール前ではありませんが、保護されていないマシンでのすべての git 操作をブロックしたいチームにとっては、追加の実施手段として有用です。

チェックを追加します `.git/hooks/pre-commit`:

```bash
#!/bin/bash
/Applications/Aikido\ Endpoint\ Protection.app/Contents/Resources/scripts/healthy
```

実行可能にします:

```bash
chmod +x .git/hooks/pre-commit
```

{% hint style="info" %}
`.git/hooks` は git で追跡されません。チームとフックを共有するには、 [Lefthook](https://github.com/evilmartians/lefthook) または [Husky](https://typicode.github.io/husky/) のようなツールを使って、リポジトリの一部としてフックを管理およびコミットしてください。
{% endhint %}
