翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Cassandra Go クライアントドライバーを使用した Amazon Keyspaces へのプログラムアクセス
このセクションでは、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) を使用する必要があります。TLS を使用して Amazon Keyspaces に接続するには、Amazon デジタル証明書をダウンロードし、TLS を使用するように Go ドライバーを設定する必要があります。
次のデジタル証明書をダウンロードし、ローカルまたはホームディレクトリにファイルを保存します。
AmazonRootCA1
AmazonRootCA2
AmazonRootCA3
AmazonRootCA4
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 ルート CAs で発行された証明書 AWS リージョン にすべて移行しています。この移行中に、Amazon ルート CAs 1~4 と Starfield ルートの両方を信頼するようにクライアントを設定し、すべてのリージョンで互換性を確保します。
ダウンロードしたすべての証明書を、この例の keyspaces-bundle.pem という名前の 1 つのpemファイルに結合します。そのためには、以下の コマンドを実行します。ファイルへのパスを書き留めます。これは後で必要になります。
cat AmazonRootCA1.pem \ AmazonRootCA2.pem \ AmazonRootCA3.pem \ AmazonRootCA4.pem \ sf-class2-root.crt \ >keyspaces-bundle.pem
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 thekeyspaces-bundle.pemcluster.SslOpts = &gocql.SslOptions{ CaPath: "path_to_file/keyspaces-bundle.pem", 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/keyspaces-bundle.pem"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 インスタンスで実行されているアプリケーションは、そのインスタンスの認証情報を自動的に使用します。このチュートリアルをローカルで実行するには、認証情報をローカル環境変数として保存します。
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/keyspaces-bundle.pem", 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/keyspaces-bundle.pem"-
このサンプルをローカルで実行するには、次の変数を環境変数として定義する必要があります。
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_DEFAULT_REGION
アクセスキーをコード外に保存するには、プログラムによるアクセス用のアクセスキーを保存する のベストプラクティスを参照してください。
利用可能なエンドポイントのリストについては、「Amazon Keyspaces のサービスエンドポイント」を参照してください。