

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Cassandra Go クライアントドライバーを使用した Amazon Keyspaces へのプログラムアクセス
<a name="using_go_driver"></a>

このセクションでは、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 認証情報の作成と設定](access.credentials.md)」を参照してください。

**Topics**
+ [[開始する前に]](#using_go_driver.BeforeYouBegin)
+ [Apache Cassandra 用の Gocql ドライバーとサービス固有の認証情報を使用して Amazon Keyspaces に接続する](#go_ssc)
+ [Apache Cassandra 用の Go ドライバーと SigV4 認証プラグインを使用して Amazon Keyspaces に接続する](#go_SigV4)

## [開始する前に]
<a name="using_go_driver.BeforeYouBegin"></a>

開始する前に、次のタスクを完了する必要があります。

Amazon Keyspaces では、クライアントとの安全な接続を確保するために Transport Layer Security (TLS) を使用する必要があります。TLS を使用して Amazon Keyspaces に接続するには、Amazon デジタル証明書をダウンロードし、TLS を使用するように Go ドライバーを設定する必要があります。

 次のデジタル証明書をダウンロードし、ローカルまたはホームディレクトリにファイルを保存します。

1. AmazonRootCA1

1. AmazonRootCA2

1. AmazonRootCA3

1. AmazonRootCA4

1. 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 クラス 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 に接続する
<a name="go_ssc"></a>

1. アプリケーション用の新しいディレクトリを作成します。

   ```
   mkdir ./gocqlexample
   ```

1. 新しいディレクトリに移動します。

   ```
   cd gocqlexample
   ```

1. アプリケーション用のファイルを作成します。

   ```
   touch cqlapp.go
   ```

1. Go ドライバーをダウンロードします。

   ```
   go get github.com/gocql/gocql
   ```

1. 次のサンプルコードを 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 keyspaces-bundle.pem
       cluster.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()
   }
   ```

   使用に関する注意事項:

   1. を、最初のステップで保存した結合証明書ファイルへのパス`"path_to_file/keyspaces-bundle.pem"`に置き換えます。

   1. *ServiceUserName* と *ServicePassword* が、[Amazon Keyspaces にプログラムによってアクセスするためのサービス固有の認証情報を作成する](programmatic.credentials.ssc.md) の手順に従ってサービス固有の認証情報を生成したときに取得したユーザー名とパスワードと一致していることを確認してください。

   1. 利用可能なエンドポイントのリストについては、「[Amazon Keyspaces のサービスエンドポイント](programmatic.endpoints.md)」を参照してください。

1. プログラムを構築します。

   ```
   go build cqlapp.go
   ```

1. プログラムを実行します。

   ```
   ./cqlapp
   ```

## Apache Cassandra 用の Go ドライバーと SigV4 認証プラグインを使用して Amazon Keyspaces に接続する
<a name="go_SigV4"></a>

次のコードサンプルで、Apache Cassandra 用オープンソース Go ドライバーの SigV4 認証プラグインを使用して、Amazon Keyspaces (Apache Cassandra 向け) にアクセスする方法を示します。

IAM プリンシパル用の認証情報をまだ作成していない場合は、「[Amazon Keyspaces の AWS 認証情報の作成と設定](access.credentials.md)」の手順に従って作成します。Lambda または Amazon EC2 インスタンスで実行されているアプリケーションは、そのインスタンスの認証情報を自動的に使用します。このチュートリアルをローカルで実行するには、認証情報をローカル環境変数として保存します。

Go SigV4 認証プラグインを [GitHub リポジトリ](https://github.com/aws/aws-sigv4-auth-cassandra-gocql-driver-plugin)からアプリケーションに追加します。プラグインは、Cassandra 用のオープンソース Go ドライバーのバージョン 1.2.x をサポートし、 AWS SDK for Go に依存します。

```
$ 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)
    }
}
```

使用に関する注意事項:

1. を、最初のステップで保存した証明書ファイルへのパス`"path_to_file/keyspaces-bundle.pem"`に置き換えます。

1. このサンプルをローカルで実行するには、次の変数を環境変数として定義する必要があります。
   + `AWS_ACCESS_KEY_ID`
   + `AWS_SECRET_ACCESS_KEY`
   + `AWS_DEFAULT_REGION`

1. アクセスキーをコード外に保存するには、[プログラムによるアクセス用のアクセスキーを保存する](aws.credentials.manage.md) のベストプラクティスを参照してください。

1. 利用可能なエンドポイントのリストについては、「[Amazon Keyspaces のサービスエンドポイント](programmatic.endpoints.md)」を参照してください。