

# CloudFront Connection Function を関連付ける
<a name="connection-functions"></a>

CloudFront Connection Functions により、TLS ハンドシェイク中にカスタム証明書検証ロジックを実装し、組み込みの mTLS 認証機能を拡張できます。

## Connection Functions とは
<a name="what-are-connection-functions"></a>

Connection Functions は、クライアント証明書の検証後、TLS ハンドシェイク中に実行される JavaScript 関数です。検証されたクライアント証明書は Connection Function に渡され、その時点で Connection Function はアクセスを許可するかどうかをさらに判断します。Connection Functions の詳細については、「[CloudFront Functions を使用してエッジでカスタマイズする](cloudfront-functions.md)」を参照してください。

## Connection Functions と mTLS の連携方法
<a name="how-connection-functions-work"></a>

クライアントが CloudFront ディストリビューションへの mTLS 接続を確立しようとすると、次のシーケンスが発生します。

1. クライアントが CloudFront エッジロケーションで TLS ハンドシェイクを開始します。

1. CloudFront はクライアント証明書をリクエストして受信します。

1. CloudFront は、トラストストアに対して標準証明書検証を実行します。

1. 証明書が標準検証に合格すると、CloudFront は Connection Function を呼び出します。**ViewerMtlsConfig** 内で **IgnoreCertificateExpiry** が有効になっている場合、期限切れ (ただし、他は有効) の証明書も Connection Function に渡されます。クライアント証明書が無効の場合、Connection Functions は呼び出されません。

1. Connection Function は、解析された証明書情報と接続の詳細を受け取ります。

1. 関数は、カスタムロジックに基づいて許可/拒否の決定を行います。

1. CloudFront は、お客様の決定に基づいて TLS 接続を完了または終了します。

Connection Functions は、検証モードとオプションモード (クライアントが証明書を提示する場合) の両方で呼び出されます。

## Connection Function を作成する
<a name="create-connection-function"></a>

CloudFront コンソールまたは CLI AWS を使用して Connection Functions を作成できます。

### Connection Function を作成するには (コンソール)
<a name="create-connection-function-console"></a>

1. AWS マネジメントコンソールにサインインし、[https://console.aws.amazon.com/cloudfront/v4/home](https://console.aws.amazon.com/cloudfront/v4/home) で CloudFront コンソールを開きます。

1. ナビゲーションペインで、**[関数]** を選択します。

1. **[Connection Functions]** タブを選択し、**[Connection Functions を作成]** を選択します。

1. AWS アカウント内で一意の関数名を入力します。

1. [**続行**] をクリックしてください。

1. 関数エディタで、証明書検証用の JavaScript コードを記述します。関数ハンドラーは許可または拒否のいずれかを呼び出す必要があります。

1. オプション: KeyValue ストアを Connection Function に関連付けると、失効制御を実装できます。

1. **[変更の保存]** をクリックします。

### Connection Function を作成するには (AWS CLI)
<a name="create-connection-function-cli"></a>

次の例は、Connection Function の作成方法を示しています。

code.js など、関数コードを個別のファイルに記述します。

```
function connectionHandler(connection) {
  connection.allow();
}
```

```
aws cloudfront create-connection-function \
  --name "certificate-validator" \
  --connection-function-config '{
      "Comment": "Client certificate validation function",
      "Runtime": "cloudfront-js-2.0"
  }' \
  --connection-function-code fileb://code.js
```

## Connection Function コードの構造
<a name="connection-function-code-structure"></a>

Connection Functions は、証明書と接続情報を含む接続オブジェクトを受信する connectionHandler 関数を実装します。関数は、接続を決定するために `connection.allow()` または `connection.deny()` を使用する必要があります。

### 基本的な Connection Function の例
<a name="basic-connection-function-example"></a>

次の例は、クライアント証明書のサブジェクトフィールドを検証するシンプルな Connection Function を示しています。

```
function connectionHandler(connection) {
    // Only process if a certificate was presented
    if (!connection.clientCertificate) {
        console.log("No certificate presented");
        connection.deny();
    }
    
    // Check the subject field for specific organization
    const subject = connection.clientCertificate.certificates.leaf.subject;
    if (!subject.includes("O=ExampleCorp")) {
        console.log("Certificate not from authorized organization");
       connection.deny();
    } else {
        // All checks passed
        console.log("Certificate validation passed");
        connection.allow();
    }
}
```

接続オブジェクトで使用できるクライアント証明書プロパティの完全な仕様は、こちらで入手できます。

```
{
  "connectionId": "Fdb-Eb7L9gVn2cFakz7wWyBJIDAD4-oNO6g8r3vXDV132BtnIVtqDA==", // Unique identifier for this TLS connection
  "clientIp": "203.0.113.42", // IP address of the connecting client (IPv4 or IPv6)
  "clientCertificate": {
    "certificates": {
      "leaf": {
        "subject": "CN=client.example.com,O=Example Corp,C=US", // Distinguished Name (DN) of the certificate holder
        "issuer": "CN=Example Corp Intermediate CA,O=Example Corp,C=US", // Distinguished Name (DN) of the certificate authority that issued this certificate
        "serialNumber": "4a:3f:5c:92:d1:e8:7b:6c", // Unique serial number assigned by the issuing CA (hexadecimal)
        "validity": {
          "notBefore": "2024-01-15T00:00:00Z", // Certificate validity start date (ISO 8601 format)
          "notAfter": "2025-01-14T23:59:59Z"   // Certificate expiration date (ISO 8601 format)
        },
        "sha256Fingerprint": "a1b2c3d4e5f6...abc123def456", // SHA-256 hash of the certificate (64 hex characters)
      },
    },
  },
}
```

## Connection Function を関連付ける
<a name="associate-connection-function-section"></a>

Connection Function を作成したらライブステージに発行し、ディストリビューションに関連付ける必要があります。

### Connection Function を発行して関連付けるには (コンソール)
<a name="publish-associate-console"></a>

1. AWS マネジメントコンソールにサインインし、[https://console.aws.amazon.com/cloudfront/v4/home](https://console.aws.amazon.com/cloudfront/v4/home) で CloudFront コンソールを開きます。

1. ナビゲーションペインで、**[関数]** を選択する

1. **[Connection Functions]** タブを選択し、Connection Functions を選択します。

1. **[発行]** を選択して、ライブステージに移動させます。

1. 発行セクションの下にある関連付けられたディストリビューションテーブルの **[関連付けを追加]** を選択します。

1. 関連付けるビューワー mTLS が有効になっているディストリビューションを選択します。

または、発行された Connection Functions をディストリビューションの詳細ページから関連付けることもできます。

1. すべてのディストリビューションが一覧表示されているコンソールのホームページに移動します。

1. 関連付けるディストリビューションを選択します。

1. **[全般]** タブを選択します。

1. **[設定]** セクションで、**[編集]** を選択します。

1. **[接続]** セクションで、**ビューワー相互認証 (mTLS)** を検索します。

1. **[Connection Function]** で、関数を選択します。

1. **[変更の保存]** をクリックします。

### Connection Function を関連付けるには (AWS CLI)
<a name="associate-connection-function-cli"></a>

次の例は、Connection Function をディストリビューションに関連付ける方法を示します。

```
// DistributionConfig:
{
   ...other settings,
    "ConnectionFunctionAssociation": {
        "Id": "cf_30c2CV2elHwCoInb3LtcaUJkZeD"
    }
}
```

## Connection Functions のユースケース
<a name="connection-function-use-cases"></a>

Connection Functions は、いくつかの高度な mTLS ユースケースを有効にします。
+ **証明書属性の検証** - 組織単位の要件やサブジェクト代替名パターンなど、クライアント証明書の特定のフィールドを検証します。
+ **証明書失効チェック** - KeyValueStore を使用してカスタム証明書失効チェックを実装し、失効した証明書のシリアル番号を保存します。
+ **IP ベースの証明書ポリシー** - クライアントの IP アドレスまたは地理的制限に基づいて異なる証明書ポリシーを適用します。
+ **マルチテナント検証** - ホスト名または証明書属性に基づいて異なる証明書要件が適用されるテナント固有の検証ルールを実装します。

**注記**  
Connection Functions は、TLS ハンドシェイク中にクライアント接続ごとに 1 回実行されます。  
Connection Functions は接続の許可または拒否のみを行うことができ、HTTP リクエストとレスポンスを変更することはできません。  
ディストリビューションに関連付けることができるのは、ライブステージにある関数 (発行済み) のみです。  
各ディストリビューションに含めることができる Connection Function は、最大 1 つまでです。

## 次のステップ
<a name="connection-function-next-steps"></a>

Connection Function を CloudFront ディストリビューションに関連付けた後、オプションの設定で mTLS 実装の動作をカスタマイズできます。オプションのクライアント証明書検証モードなど、追加設定の詳細な手順については、「[その他の設定](configuring-additional-settings.md)」を参照してください。