기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
이 섹션에서는 Go Cassandra 클라이언트 드라이버를 사용하여 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)을 사용해야 합니다. 를 사용하여 Amazon Keyspaces에 연결하려면 Amazon 디지털 인증서를 다운로드하고를 사용하도록 Go 드라이버를 구성TLS해야 합니다TLS.
다음 명령을 사용하여 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에 접속합니다.
-
애플리케이션용 디렉터리를 생성합니다.
mkdir ./gocqlexample
-
새 디렉터리로 이동합니다.
cd gocqlexample
-
애플리케이션에 대한 파일을 생성합니다.
touch cqlapp.go
-
Go 드라이버를 다운로드합니다.
go get github.com/gocql/gocql
-
다음 샘플 코드를 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() }사용 노트:
"
을 첫 번째 단계에서 저장한 인증서 경로로 바꿉니다.path_to_file
/sf-class2-root.crt"의 단계에 따라
ServiceUserName
및가 서비스별 자격 증명을 생성할 때 얻은 사용자 이름과 암호와ServicePassword
일치하는지 확인합니다Amazon Keyspaces에 프로그래밍 방식으로 액세스하기 위한 서비스별 자격 증명 만들기.사용 가능한 엔드포인트 목록은 Amazon Keyspaces의 서비스 엔드포인트 섹션을 참조하세요.
프로그램을 빌드합니다.
go build cqlapp.go
프로그램을 실행합니다.
./cqlapp
Apache Cassandra용 Go 드라이버와 SigV4 인증 플러그인을 사용하여 Amazon Keyspaces에 접속
다음 코드 샘플에서는 오픈 소스 Go 드라이버를 위한 SigV4 인증 플러그인을 사용하여 Amazon Keyspaces(Apache Cassandra용) 에 액세스하는 방법을 설명합니다.
아직 생성하지 않은 경우의 단계에 따라 IAM 보안 주체의 자격 증명을 생성합니다Amazon Keyspaces에 대한 AWS 보안 인증 생성 및 구성. 애플리케이션이 Lambda 또는 Amazon EC2 인스턴스에서 실행 중인 경우 애플리케이션은 인스턴스의 자격 증명을 자동으로 사용합니다. 이 자습서를 로컬에서 실행하려면 자격 증명을 로컬 환경 변수로 저장할 수 있습니다.
Go SigV4 인증 플러그인을 GitHub리포지토리
$ 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
// the authenticator uses the default credential chain to find AWS credentials
cluster.Authenticator = sigv4.NewAwsAuthenticator()
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)
}
}
사용 노트:
"
을 첫 번째 단계에서 저장한 인증서 경로로 바꿉니다.path_to_file
/sf-class2-root.crt"-
이 예제를 로컬에서 실행하려면 다음 변수를 환경 변수로 정의해야 합니다.
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION
액세스 키를 코드 외부에 저장하는 경우의 모범 사례는 프로그래밍 방식 액세스를 위한 액세스 키 저장 섹션을 참조하세요.
사용 가능한 엔드포인트 목록은 Amazon Keyspaces의 서비스 엔드포인트 섹션을 참조하세요.