専用KMS SDK for Goを使用すると、専用キー管理サービス (KMS) のAPI操作を便利な方法で呼び出すことができます。 専用KMS SDK for Goは、データの暗号化、データの復号化、署名の生成、署名の検証、秘密値のクエリなどのビジネスシナリオに適しています。
背景情報
オープンソースコードリポジトリにアクセスして、Dedicated KMS SDKのソースコードとサンプルコードを表示できます。 コメントを共有するか、サンプルコードを提供することを歓迎します。
前提条件
専用KMSインスタンスを購入し、ハードウェアセキュリティモジュール (HSM) クラスターに接続します。 インスタンスの顧客マスターキー (CMK) とアプリケーションアクセスエンドポイント (AAP) が作成され、インスタンスのクライアントキーと認証機関 (CA) 証明書が保存されます。 詳細については、「アプリケーションをStandardエディションの専用KMSインスタンスに接続する」をご参照ください。
説明ダウンロードしたCA証明書の名前はPrivateKmsCA_kst-******.pem形式で、ダウンロードしたクライアント鍵ファイルの名前はClientKey_******.json形式です。
専用KMSインスタンスへのアクセスに使用される仮想プライベートクラウド (VPC) アドレスが取得されます。 VPCアドレスは次の要件を満たす必要があります。
VPCアドレスは、HSMクラスターの有効化時に指定されます。
VPCアドレスはコンピューターからアクセスできます。
詳細については、「Standardエディションの専用KMSインスタンスの照会」をご参照ください。
専用KMS SDKのインストール
方法1:
go.mo dファイルを使用して依存関係を管理します。次のコンテンツを
go.mo dファイルに追加して、依存関係パッケージをインストールします。require ( github.com/aliyun/alibabacloud-dkms-gcs-go-sdk SDK version )説明Dedicated KMS SDKの最新バージョンの詳細については、オープンソースコードリポジトリをご参照ください。
方法2:
go getコマンドを実行して、リモートコードパッケージを取得します。$ go get -u github.com/aliyun/alibabacloud-dkms-gcs-go-sdk
専用KMS SDKの初期化
Standardエディションの専用KMSインスタンス用のGoクライアントを作成して、専用KMSインスタンスによって管理されているリソースを呼び出すことができます。 たとえば、クライアントを使用して、インスタンスによって管理されているキーを呼び出すことができます。 専用KMS SDK for Goを使用してAPIリクエストを開始する前に、クライアントを作成し、ビジネス要件に基づいてConfigのデフォルト設定を変更する必要があります。
認証機関 (CA) 証明書を設定します。
本番環境で安全な通信を確保するには、信頼できる証明書を設定する必要があります。
RuntimeOptionsのverifyフィールドをCA証明書の内容に設定します。 サンプルコード:import ( dedicatedkmsopenapiutil "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi-util" "github.com/alibabacloud-go/tea/tea" "io/ioutil" ) // Verify the certificate. ca, err := ioutil.ReadFile("path/to/caCert.pem") if err != nil { panic(err) } runtimeOptions := &dedicatedkmsopenapiutil.RuntimeOptions{ Verify: tea.String(string(ca)), } ... encryptResponse, err := client.EncryptWithOptions(encryptRequest, runtimeOptions)開発環境では、RuntimeOptionsの
ignoreSSLフィールドを使用して、信頼できる証明書の検証を一時的に無視できます。 サンプルコード:import ( dedicatedkmsopenapiutil "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi-util" "github.com/alibabacloud-go/tea/tea" ) // Ignore the certificate. runtimeOptions := &dedicatedkmsopenapiutil.RuntimeOptions{ IgnoreSSL: tea.Bool(true), } ... encryptResponse, err := client.EncryptWithOptions(encryptRequest, runtimeOptions)Standardエディションの専用KMSインスタンス用のクライアントを作成します。
Standardエディションの専用KMSインスタンスのクライアントを作成する場合、インスタンスのエンドポイントを指定する必要があります。 エンドポイントはスキームhttpsを除外します。 Standardエディションの専用KMSインスタンスのエンドポイントの詳細については、「Standardエディションの専用KMSインスタンスの照会」をご参照ください。
import ( dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi" dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk" "github.com/alibabacloud-go/tea/tea" ) config := &dedicatedkmsopenapi.Config{ // The connection protocol. Set the value to https. Protocol: tea.String("https"), // The client key of the dedicated KMS instance of the Standard edition. ClientKeyContent: tea.String("<your client key content>"), // The password that is used to decrypt the client key of the dedicated KMS instance of the Standard edition. Password: tea.String("<your client key password>"), // The endpoint of the dedicated KMS instance of the Standard edition. The endpoint excludes the scheme https. Endpoint: tea.String("<service_id>.cryptoservice.kms.aliyuncs.com"), } client, err := dedicatedkmssdk.NewClient(config)
例
Standardエディションの専用KMSインスタンスのクライアントを使用して、対称CMKを使用してデータを暗号化するEncrypt操作を呼び出します。
サンプルコードの詳細については、「ソースコード」をご参照ください。
import ( dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi" dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk" "github.com/alibabacloud-go/tea/tea" ) // The data that you want to encrypt. plaintext := []byte("encrypt plaintext") // The ID or alias of the CMK of the dedicated KMS instance of the Standard edition. keyId := "<your cipher key id>" encryptRequest := &dedicatedkmssdk.EncryptRequest{ KeyId: tea.String(keyId), Plaintext: plaintext, } encryptResponse, err := client.EncryptWithOptions(encryptRequest, runtimeOptions) if err != nil { panic(err) } // The ciphertext. cipher := encryptResponse.CiphertextBlob // The initial vector of Cipher that is used to decrypt data. iv := encryptResponse.Iv // The ID of the request. requestId := tea.StringValue(encryptResponse.RequestId)Standardエディションの専用KMSインスタンスのクライアントを使用して、Decrypt操作を呼び出し、対称CMKを使用して暗号文を復号します。
サンプルコードの詳細については、「ソースコード」をご参照ください。
import ( dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi" dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk" "github.com/alibabacloud-go/tea/tea" ) // The ID or alias of the CMK of the dedicated KMS instance of the Standard edition. keyId := "<your cipher key id>" // The ciphertext that you want to decrypt. ciphertextBlob := []byte("<your cipher data to decrypt>") // The initial vector of Cipher. The initial vector must be the same as the initial vector that is specified for encryption. iv := []byte("<IV value>") decryptRequest := &dedicatedkmssdk.DecryptRequest{ KeyId: tea.String(keyId), CiphertextBlob: ciphertextBlob, Iv: iv, } decryptResponse, err := client.DecryptWithOptions(decryptRequest, runtimeOptions) if err != nil { panic(err) } // The plaintext. plaintext := decryptResponse.Plaintext // The ID of the request. requestId := tea.StringValue(decryptResponse.RequestId)Standardエディションの専用KMSインスタンスのクライアントを使用してSign操作を呼び出し、非対称CMKを使用して署名を生成します。
サンプルコードの詳細については、「ソースコード」をご参照ください。
import ( dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi" dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk" "github.com/alibabacloud-go/tea/tea" ) // The ID or alias of the CMK of the dedicated KMS instance of the Standard edition. signerKeyId := "<the signer key id>" // The data to sign. message := []byte("<the data to sign>") signRequest := &dedicatedkmssdk.SignRequest{ KeyId: tea.String(signerKeyId), Message: message, MessageType: tea.String(messageType), } signResponse, err := client.SignWithOptions(signRequest, runtimeOptions) if err != nil { panic(err) } // The signature value. signature := signResponse.Signature // The ID of the request. requestId := tea.StringValue(signResponse.RequestId)Standardエディションの専用KMSインスタンスのクライアントを使用して、Verify操作を呼び出し、非対称CMKを使用して署名を検証します。
サンプルコードの詳細については、「ソースコード」をご参照ください。
import ( dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi" dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk" "github.com/alibabacloud-go/tea/tea" ) // The ID or alias of the CMK of the dedicated KMS instance of the Standard edition. signerKeyId := "<the signer key id>" // The data for which you want to verify the signature. message := []byte("<the data to sign>") // The signature value that you want to verify. signature := []byte("<the signature>") verifyRequest := &dedicatedkmssdk.VerifyRequest{ KeyId: tea.String(signerKeyId), Message: message, MessageType: tea.String(messageType), Signature: signature, } verifyResponse, err := client.VerifyWithOptions(verifyRequest, runtimeOptions) if err != nil { panic(err) } // The verification result. value := tea.BoolValue(verifyResponse.Value) // The ID of the request. requestId := tea.StringValue(verifyResponse.RequestId)Standardエディションの専用KMSインスタンスのクライアントを使用して、GetSecretValue操作を呼び出し、シークレット値を照会します。
サンプルコードの詳細については、「ソースコード」をご参照ください。
重要Go V0.2.1以降の専用KMS SDKは、GetSecretValue操作をサポートしています。
import ( dedicatedkmsopenapi "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/openapi" dedicatedkmssdk "github.com/aliyun/alibabacloud-dkms-gcs-go-sdk/sdk" "github.com/alibabacloud-go/tea/tea" ) // The secret name. secretName := "<your-dkms-secret-name>" getSecretValueRequest := &dedicatedkmssdk.GetSecretValueRequest{ SecretName: tea.String(secretName), } // The call process. response, err := client.GetSecretValueWithOptions(getSecretValueRequest, runtimeOptions) if err != nil { panic(err) } // The secret value. _secretData := tea.StringValue(response.SecretData) // The ID of the request. _RequestId := tea.StringValue(response.RequestId)