Cassandra Python 클라이언트 드라이버를 사용하여 프로그래밍 방식으로 Amazon Keyspaces에 액세스 - Amazon Keyspaces(Apache Cassandra용)

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

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에서는 클라이언트와의 연결을 보호하는 데 도움이 되는 전송 계층 보안(TLS)을 사용해야 합니다. TLS를 사용하여 Amazon Keyspaces에 연결하려면 Amazon 디지털 인증서를 다운로드하고 TLS를 사용하도록 Python 드라이버를 구성해야 합니다.

다음 디지털 인증서를 다운로드하고 파일을 로컬 또는 홈 디렉터리에 저장합니다.

  1. AmazonRootCA1

  2. AmazonRootCA2

  3. AmazonRootCA3

  4. AmazonRootCA4

  5. Starfield 클래스 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 Class 2 CA에 고정된 TLS 인증서를 사용했습니다. AWS 는 Amazon Trust Services(Amazon Root CA 1~4)에 따라 발급된 인증서 AWS 리전 로 모두 마이그레이션하고 있습니다. CAs 이 전환 중에 Amazon Root 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)

사용 노트:

  1. 를 첫 번째 단계에서 저장된 결합된 인증서 파일의 경로"path_to_file/keyspaces-bundle.pem"로 바꿉니다.

  2. ServiceUserNameServicePasswordAmazon Keyspaces에 프로그래밍 방식으로 액세스하기 위한 서비스별 자격 증명 만들기에 대한 단계에 따라 서비스별 자격 증명을 생성할 때 얻은 사용자 이름 및 암호와 일치해야 합니다.

  3. 사용 가능한 엔드포인트 목록은 Amazon Keyspaces의 서비스 엔드포인트 섹션을 참조하세요.

Apache Cassandra용 DataStax Python 드라이버와 SigV4 인증 플러그인을 사용하여 Amazon Keyspaces에 접속

다음 섹션에서는 Apache Cassandra용 오픈 소스 DataStax Python 드라이버를 위한 SigV4 인증 플러그인을 사용하여 Amazon Keyspaces(Apache Cassandra용) 에 액세스하는 방법을 설명합니다.

아직 수행하지 않은 경우 Amazon Keyspaces에 대한 AWS 자격 증명 생성 및 구성의 단계에 따라 IAM 역할의 자격 증명을 생성합니다. 이 튜토리얼에서는 IAM 역할이 필요한 임시 자격 증명을 사용합니다. 임시 자격 증명에 대한 자세한 내용은 IAM 역할 및 SigV4 플러그인을 생성하여 임시 자격 증명으로 Amazon Keyspaces에 접속 섹션을 참조하세요.

그런 다음 GitHub 리포지토리에서 Python SigV4 인증 플러그인을 환경에 추가합니다.

pip install cassandra-sigv4

다음 코드 예제는 Cassandra용 오픈 소스 DataStax Python 드라이버와 SigV4 인증 플러그인을 사용하여 Amazon Keyspaces에 접속하는 방법을 보여줍니다. 플러그인은 AWS SDK for Python(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)

사용 노트:

  1. "path_to_file/keyspaces-bundle.pem"을 첫 번째 단계에서 저장한 인증서 경로로 바꿉니다.

  2. aws_access_key_id, aws_secret_access_key, aws_session_tokenboto3.session를 사용하여 얻은 Access Key, Secret Access Key, Session Token와 일치하는지 확인합니다. 자세한 내용은 AWS SDK for Python (Boto3)자격 증명을 참조하세요.

  3. 사용 가능한 엔드포인트 목록은 Amazon Keyspaces의 서비스 엔드포인트 섹션을 참조하세요.