本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Cassandra Python 用戶端驅動程式以程式設計方式存取 Amazon Keyspaces
在本節中,我們會示範如何使用 Python 用戶端驅動程式連線至 Amazon Keyspaces。若要為使用者和應用程式提供以程式設計方式存取 Amazon Keyspaces 資源的登入資料,您可以執行下列其中一項操作:
-
建立與特定 AWS Identity and Access Management (IAM) 使用者相關聯的服務特定登入資料。
-
為了增強安全性,我們建議為所有 AWS 服務中使用的 IAM 使用者或角色建立 IAM 存取金鑰。Cassandra 用戶端驅動程式的 Amazon Keyspaces SigV4 身分驗證外掛程式可讓您使用 IAM 存取金鑰來驗證對 Amazon Keyspaces 的呼叫,而不是使用者名稱和密碼。如需詳細資訊,請參閱建立和設定 Amazon Keyspaces 的 AWS 登入資料。
主題
開始之前
您需要先完成下列任務,才能開始。
Amazon Keyspaces 需要使用 Transport Layer Security (TLS) 來協助保護與用戶端的連線。若要使用 TLS 連線至 Amazon Keyspaces,您需要下載 Amazon 數位憑證,並將 Python 驅動程式設定為使用 TLS。
下載下列數位憑證,並將檔案儲存在本機或主目錄中。
AmazonRootCA1
AmazonRootCA2
AmazonRootCA3
AmazonRootCA4
Starfield Class 2 根目錄 (選用 – 用於回溯相容性)
若要下載憑證,您可以使用下列命令。
curl -O https://www.amazontrust.com/repository/AmazonRootCA1.pem curl -O https://www.amazontrust.com/repository/AmazonRootCA2.pem curl -O https://www.amazontrust.com/repository/AmazonRootCA3.pem curl -O https://www.amazontrust.com/repository/AmazonRootCA4.pem curl -O https://certs.secureserver.net/repository/sf-class2-root.crt
注意
Amazon Keyspaces 先前使用錨定至 Starfield 類別 2 CA 的 TLS 憑證。 AWS 正在將所有 遷移 AWS 區域 至根據 Amazon Trust Services (Amazon 根 CAs 發行的憑證。在此轉換期間,請將用戶端設定為信任 Amazon 根 CAs1–4 和 Starfield 根,以確保所有區域的相容性。
使用範例中的名稱 keyspaces-bundle.pem,將所有下載的憑證合併成單一pem檔案。您可以執行下列 命令,即可進行新增:請記下 檔案的路徑,您稍後會需要此項目。
cat AmazonRootCA1.pem \ AmazonRootCA2.pem \ AmazonRootCA3.pem \ AmazonRootCA4.pem \ sf-class2-root.crt \ >keyspaces-bundle.pem
使用適用於 Apache Cassandra 的 Python 驅動程式和服務特定憑證連線至 Amazon Keyspaces
下列程式碼範例示範如何使用 Python 用戶端驅動程式和服務特定的登入資料連線到 Amazon Keyspaces。
from cassandra.cluster import Cluster from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED from cassandra.auth import PlainTextAuthProvider ssl_context = SSLContext(PROTOCOL_TLSv1_2 ) ssl_context.load_verify_locations('path_to_file/keyspaces-bundle.pem') ssl_context.verify_mode = CERT_REQUIRED auth_provider = PlainTextAuthProvider(username='ServiceUserName', password='ServicePassword') cluster = Cluster(['cassandra.us-east-2.amazonaws.com'], ssl_context=ssl_context, auth_provider=auth_provider, port=9142) session = cluster.connect() r = session.execute('select * from system_schema.keyspaces') print(r.current_rows)
用量備註:
"以第一個步驟中儲存的合併憑證檔案路徑取代 。path_to_file/keyspaces-bundle.pem"依照 的步驟,確保
ServiceUserName和ServicePassword與您產生服務特定登入資料時取得的使用者名稱和密碼相符建立服務特定的登入資料,以程式設計方式存取 Amazon Keyspaces。如需可用端點的清單,請參閱 Amazon Keyspaces 的服務端點。
使用適用於 Apache Cassandra 的 DataStax Python 驅動程式和 SigV4 身分驗證外掛程式連線至 Amazon Keyspaces
下一節說明如何使用開放原始碼 DataStax Python 驅動程式的 SigV4 身分驗證外掛程式,讓 Apache Cassandra 存取 Amazon Keyspaces (適用於 Apache Cassandra)。
如果您尚未這麼做,請先遵循 中的步驟,為您的 IAM 角色建立登入資料建立和設定 Amazon Keyspaces 的 AWS 登入資料。本教學課程使用臨時登入資料,需要 IAM 角色。如需暫時登入資料的詳細資訊,請參閱 使用 IAM 角色和 SigV4 外掛程式建立臨時登入資料以連線至 Amazon Keyspaces。
然後,從 GitHub 儲存庫
pip install cassandra-sigv4
下列程式碼範例示範如何使用適用於 Cassandra 的開放原始碼 DataStax Python 驅動程式和 SigV4 身分驗證外掛程式連線至 Amazon Keyspaces。外掛程式取決於適用於 Python 的 AWS SDK (Boto3)。它使用 boto3.session 來取得臨時登入資料。
from cassandra.cluster import Cluster from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED from cassandra.auth import PlainTextAuthProvider import boto3 from cassandra_sigv4.auth import SigV4AuthProvider ssl_context = SSLContext(PROTOCOL_TLSv1_2) ssl_context.load_verify_locations('path_to_file/keyspaces-bundle.pem') ssl_context.verify_mode = CERT_REQUIRED # use this if you want to use Boto to set the session parameters. boto_session = boto3.Session(aws_access_key_id="AKIAIOSFODNN7EXAMPLE", aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", aws_session_token="AQoDYXdzEJr...<remainder of token>", region_name="us-east-2") auth_provider = SigV4AuthProvider(boto_session) # Use this instead of the above line if you want to use the Default Credentials and not bother with a session. # auth_provider = SigV4AuthProvider() cluster = Cluster(['cassandra.us-east-2.amazonaws.com'], ssl_context=ssl_context, auth_provider=auth_provider, port=9142) session = cluster.connect() r = session.execute('select * from system_schema.keyspaces') print(r.current_rows)
用量備註:
"將 取代為第一個步驟中儲存的憑證路徑。path_to_file/keyspaces-bundle.pem"請確定
aws_access_key_id、aws_secret_access_key和aws_session_token符合Session TokenAccess Key、Secret Access Key以及您使用 取得的boto3.session。如需詳細資訊,請參閱 中的登入資料適用於 Python (Boto3) 的 AWS SDK。 如需可用端點的清單,請參閱 Amazon Keyspaces 的服務端點。