翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
このセクションでは、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 では、クライアントとの接続を保護するために Transport Layer Security (TLS) を使用する必要があります。を使用して Amazon Keyspaces に接続するにはTLS、Amazon デジタル証明書をダウンロードし、 を使用するように Go ドライバーを設定する必要がありますTLS。
次のコマンドを使用して Starfield デジタル証明書をダウンロードし、sf-class2-root.crt
をローカルまたはホームディレクトリ内に保存します。
curl https://certs.secureserver.net/repository/sf-class2-root.crt -O
注記
Amazon デジタル証明書を使用して Amazon Keyspaces に接続することもできます。クライアントが 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 に接続する
次のコードサンプルで、Apache Cassandra 用オープンソース Go ドライバーの SigV4 認証プラグインを使用して、Amazon Keyspaces (Apache Cassandra 向け) にアクセスする方法を示します。
まだ作成していない場合は、「」の手順に従ってIAMプリンシパルの認証情報を作成しますAmazon Keyspaces の AWS 認証情報の作成と設定。アプリケーションが Lambda または Amazon EC2インスタンスで実行されている場合、アプリケーションは自動的にインスタンスの認証情報を使用します。このチュートリアルをローカルで実行するには、認証情報をローカル環境変数として保存します。
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 のサービスエンドポイント」を参照してください。