

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 設定 IAM 存取控制的用戶端
<a name="configure-clients-for-iam-access-control"></a>

若要讓用戶端能夠與使用 IAM 存取控制的 MSK 叢集通訊，您可以使用下列其中一種機制：
+ 使用 SASL\$1OAUTHBEARER 機制的非 Java 用戶端組態
+ 使用 SASL\$1OAUTHBEARER 機制或 AWS\$1MSK\$1IAM 機制的 Java 用戶端組態

## 使用 SASL\$1OAUTHBEARER 機制來設定 IAM
<a name="configure-clients-for-iam-access-control-sasl-oauthbearer"></a>

1. 使用下列 Python Kafka 用戶端範例編輯 client.properties 組態檔案。其他語言的組態變更也與其類似。

   ```
   from kafka import KafkaProducer
   from kafka.errors import KafkaError
   from kafka.sasl.oauth import AbstractTokenProvider
   import socket
   import time
   from aws_msk_iam_sasl_signer import MSKAuthTokenProvider
   
   class MSKTokenProvider():
       def token(self):
           token, _ = MSKAuthTokenProvider.generate_auth_token('<my AWS 區域>')
           return token
   
   tp = MSKTokenProvider()
   
   producer = KafkaProducer(
       bootstrap_servers='<myBootstrapString>',
       security_protocol='SASL_SSL',
       sasl_mechanism='OAUTHBEARER',
       sasl_oauth_token_provider=tp,
       client_id=socket.gethostname(),
   )
   
   topic = "<my-topic>"
   while True:
       try:
           inp=input(">")
           producer.send(topic, inp.encode())
           producer.flush()
           print("Produced!")
       except Exception:
           print("Failed to send message:", e)
   
   producer.close()
   ```

1. 下載所選組態語言的協助程式庫，並遵循該語言程式庫首頁*入門*一節中的指示。
   + JavaScript：[https://github.com/aws/aws-msk-iam-sasl-signer-js\$1getting-started](https://github.com/aws/aws-msk-iam-sasl-signer-js#getting-started)
   + Python：[https://github.com/aws/aws-msk-iam-sasl-signer-python\$1get-started](https://github.com/aws/aws-msk-iam-sasl-signer-python#get-started)
   + Go：[https://github.com/aws/aws-msk-iam-sasl-signer-go\$1getting-started](https://github.com/aws/aws-msk-iam-sasl-signer-go#getting-started)
   + .NET：[https://github.com/aws/aws-msk-iam-sasl-signer-net\$1getting-started](https://github.com/aws/aws-msk-iam-sasl-signer-net#getting-started)
   + JAVA：可透過 [https://github.com/aws/aws-msk-iam-auth/releases](https://github.com/aws/aws-msk-iam-auth/releases) jar 檔案獲得 Java 的 SASL\$1OAUTHBEARE 支援

## 使用 MSK 自訂 AWS\$1MSK\$1IAM 機制來設定 IAM
<a name="configure-clients-for-iam-access-control-msk-iam"></a>

1. 將以下內容新增到 `client.properties` 檔案。使用用戶端信任存放區檔案的完整路徑取代 *<PATH\$1TO\$1TRUST\$1STORE\$1FILE>*。
**注意**  
如果您不想使用特定憑證，可以從 `client.properties` 檔案中移除 `ssl.truststore.location=<PATH_TO_TRUST_STORE_FILE>`。如果您沒有為 `ssl.truststore.location` 指定值，Java 程序會使用預設憑證。

   ```
   ssl.truststore.location=<PATH_TO_TRUST_STORE_FILE>
   security.protocol=SASL_SSL
   sasl.mechanism=AWS_MSK_IAM
   sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required;
   sasl.client.callback.handler.class=software.amazon.msk.auth.iam.IAMClientCallbackHandler
   ```

   若要使用您為 AWS 登入資料建立的具名設定檔，`awsProfileName="your profile name";`請在用戶端組態檔案中包含 。如需具名設定檔的相關資訊，請參閱 AWS CLI 文件中的[具名設定檔](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)。

1. 下載最新的穩定版 [aws-msk-iam-auth](https://github.com/aws/aws-msk-iam-auth/releases) JAR 文件，並將其放置在類路徑中。如果您使用 Maven，請新增以下依賴項，根據需要調整版本號：

   ```
   <dependency>
       <groupId>software.amazon.msk</groupId>
       <artifactId>aws-msk-iam-auth</artifactId>
       <version>1.0.0</version>
   </dependency>
   ```

Amazon MSK 用戶端外掛程式已在 Apache 2.0 授權下開放原始碼。