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

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

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

이 섹션에서는 Go 클라이언트 드라이버를 사용하여 Amazon Keyspaces에 접속하는 방법을 소개합니다. Amazon Keyspaces 리소스에 프로그래밍 방식으로 액세스할 수 있는 보안 인증 정보를 사용자와 애플리케이션에 제공하려면 다음 중 하나를 수행할 수 있습니다.

  • 특정 AWS Identity and Access Management (IAM) 사용자와 연결된 서비스별 보안 인증 정보를 생성합니다.

  • 보안 강화를 위해 모든 서비스에서 사용되는 IAM 사용자 및 역할에 대한 IAM 액세스 키를 생성하는 것이 좋습니다. AWS Cassandra 클라이언트 드라이버용 Amazon Keyspaces SigV4 인증 플러그인을 사용하면 사용자 이름 및 암호 대신 IAM 액세스 키를 사용하여 Amazon Keyspaces에 대한 호출을 인증할 수 있습니다. 자세한 정보는 Amazon Keyspace에 AWS 자격 증명을 생성하고 구성하는 방법을 참조하세요.

시작하기 전 준비 사항

시작하기 전에 다음 작업을 수행해야 합니다.

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

다음 명령을 사용하여 Starfield 디지털 인증서를 다운로드하고 sf-class2-root.crt을 로컬 또는 홈 디렉터리에 저장합니다.

curl https://certs.secureserver.net/repository/sf-class2-root.crt -O
참고

또한 Amazon 디지털 인증서를 사용하여 Amazon Keyspaces에 접속할 수 있으며 클라이언트가 Amazon Keyspaces에 성공적으로 접속하면 계속 접속할 수 있습니다. Starfield 인증서는 이전 인증 기관을 사용하는 클라이언트에게 추가적인 이전 버전과의 호환성을 제공합니다.

curl https://certs.secureserver.net/repository/sf-class2-root.crt -O

Apache Cassandra용 Gocql 드라이버와 서비스별 보안 인증 정보를 사용하여 Amazon Keyspaces에 접속합니다.

  1. 애플리케이션용 디렉터리를 생성합니다.

    mkdir ./gocqlexample
  2. 새 디렉터리로 이동합니다.

    cd gocqlexample
  3. 애플리케이션에 대한 파일을 생성합니다.

    touch cqlapp.go
  4. Go 드라이버를 다운로드합니다.

    go get github.com/gocql/gocql
  5. 다음 샘플 코드를 cqlapp.go 파일에 추가합니다.

    package main import ( "fmt" "github.com/gocql/gocql" "log" ) func main() { // add the Amazon Keyspaces service endpoint cluster := gocql.NewCluster("cassandra.us-east-2.amazonaws.com") cluster.Port=9142 // add your service specific credentials cluster.Authenticator = gocql.PasswordAuthenticator{ Username: "ServiceUserName", Password: "ServicePassword"} // provide the path to the sf-class2-root.crt cluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file/sf-class2-root.crt", EnableHostVerification: false, } // Override default Consistency to LocalQuorum cluster.Consistency = gocql.LocalQuorum cluster.DisableInitialHostLookup = false session, err := cluster.CreateSession() if err != nil { fmt.Println("err>", err) } defer session.Close() // run a sample query from the system keyspace var text string iter := session.Query("SELECT keyspace_name FROM system_schema.tables;").Iter() for iter.Scan(&text) { fmt.Println("keyspace_name:", text) } if err := iter.Close(); err != nil { log.Fatal(err) } session.Close() }

    사용 노트:

    1. "path_to_file/sf-class2-root.crt"을 첫 번째 단계에서 저장한 인증서 경로로 바꿉니다.

    2. 의 단계에 따라 ServiceUserName및 가 서비스별 자격 증명을 생성할 때 얻은 사용자 이름 및 암호와 ServicePassword일치하는지 확인하십시오. 서비스별 보안 인증 정보 생성

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

  6. 프로그램을 빌드합니다.

    go build cqlapp.go
  7. 프로그램을 실행합니다.

    ./cqlapp

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

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

아직 수행하지 않은 경우 Amazon Keyspace에 AWS 자격 증명을 생성하고 구성하는 방법의 단계에 따라 IAM 사용자 또는 역할의 보안 인증 정보를 생성합니다.

리포지토리에서 Go SigV4 인증 플러그인을 애플리케이션에 추가합니다. GitHub 플러그인은 카산드라용 오픈 소스 Go 드라이버 버전 1.2.x를 지원하며 Go용 SDK에 따라 다릅니다. AWS

$ go mod init $ go get github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin

이 코드 샘플에서 Amazon Keyspaces 엔드포인트는 Cluster 클래스로 표현됩니다. 클러스터의 인증 속성에 대한 AwsAuthenticator을 통해 보안 인증 정보가 생성됩니다.

package main import ( "fmt" "github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin/sigv4" "github.com/gocql/gocql" "log" ) func main() { // configuring the cluster options cluster := gocql.NewCluster("cassandra.us-west-2.amazonaws.com") cluster.Port=9142 var auth sigv4.AwsAuthenticator = sigv4.NewAwsAuthenticator() auth.Region = "us-west-2" auth.AccessKeyId = "AKIAIOSFODNN7EXAMPLE" auth.SecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" cluster.Authenticator = auth cluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file/sf-class2-root.crt", EnableHostVerification: false, } cluster.Consistency = gocql.LocalQuorum cluster.DisableInitialHostLookup = false session, err := cluster.CreateSession() if err != nil { fmt.Println("err>", err) return } defer session.Close() // doing the query var text string iter := session.Query("SELECT keyspace_name FROM system_schema.tables;").Iter() for iter.Scan(&text) { fmt.Println("keyspace_name:", text) } if err := iter.Close(); err != nil { log.Fatal(err) } }

사용 노트:

  1. "path_to_file/sf-class2-root.crt"을 첫 번째 단계에서 저장한 인증서 경로로 바꿉니다.

  2. 를 사용하여 얻은 액세스 키 AccessKeyId및 비밀 액세스 키와 AN이 SecretAccessKey일치하는지 확인하세요. AwsAuthenticator 자세한 내용은 AWS SDK for GoGo용 AWS SDK 구성을 참조하세요.

  3. 액세스 키를 코드 외부에 저장하는 경우의 모범 사례는 IAM 사용자의 액세스 키 관리 방법 섹션을 참조하세요.

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