すべてのプロダクト
Search
ドキュメントセンター

Key Management Service:Python専用のKMS SDK

最終更新日:Jan 20, 2025

専用KMS SDK for Pythonを使用すると、専用キー管理サービス (KMS) のAPI操作を便利な方法で呼び出すことができます。 専用のKMS SDK for Pythonは、データ暗号化、データ復号化、署名生成、署名検証、秘密値クエリなどのビジネスシナリオに適しています。

背景情報

コメントを共有するか、サンプルコードを提供することを歓迎します。

前提条件

  • 専用KMSインスタンスを購入し、ハードウェアセキュリティモジュール (HSM) クラスターに接続します。 インスタンスの顧客マスターキー (CMK) とアプリケーションアクセスエンドポイント (AAP) が作成され、インスタンスのクライアントキーと認証機関 (CA) 証明書が保存されます。 詳細については、「アプリケーションをStandardエディションの専用KMSインスタンスに接続する」をご参照ください。

    説明

    ダウンロードしたCA証明書の名前はPrivateKmsCA_kst-******.pem形式で、ダウンロードしたクライアント鍵ファイルの名前はClientKey_******.json形式です。

  • 専用KMSインスタンスへのアクセスに使用される仮想プライベートクラウド (VPC) アドレスが取得されます。 VPCアドレスは次の要件を満たす必要があります。

    • VPCアドレスは、HSMクラスターの有効化時に指定されます。

    • VPCアドレスはコンピューターからアクセスできます。

    詳細については、「Standardエディションの専用KMSインスタンスの照会」をご参照ください。

専用KMS SDKのインストール

  • 専用KMS SDK for Python 3を使用してalibabacloud-dkms-gcsモジュールをインストールする場合は、次のコマンドを実行します。
    pip install alibabacloud-dkms-gcs
  • 専用KMS SDK for Python 2を使用してalibabacloud-dkms-gcs-python2モジュールをインストールする場合は、次のコマンドを実行します。
    pip install alibabacloud-dkms-gcs-python2

専用KMS SDKの初期化

Standardエディションの専用KMSインスタンス用のPythonクライアントを作成して、専用KMSインスタンスによって管理されるリソースを呼び出すことができます。 たとえば、クライアントを使用して、インスタンスによって管理されているキーを呼び出すことができます。 専用KMS SDK for Pythonを使用してAPIリクエストを開始する前に、クライアントを作成し、ビジネス要件に基づいてConfigのデフォルト設定を変更する必要があります。

  1. 認証機関 (CA) 証明書を設定します。

    本番環境で安全な通信を確保するには、信頼できる証明書を設定する必要があります。

    RuntimeOptionsオプションのverify filedをCA証明書のパスに設定します。
    # -*- coding: utf-8 -*-
    from openapi_util.models import RuntimeOptions
    
    runtime_options = RuntimeOptions()
    # Ignore SSL verification.
    # runtime_options.ignore_ssl = True
    # CA certificate path
    runtime_options.verify = "<your-ca-certificate-file-path>"
    ...
    response = client.encrypt_with_options(request, runtime_options)
                            
  2. Standardエディションの専用KMSインスタンス用のクライアントを作成します。

    Standardエディションの専用KMSインスタンスのクライアントを作成する場合、インスタンスのエンドポイントを指定する必要があります。 エンドポイントはスキームhttpsを除外します。 Standardエディションの専用KMSインスタンスのエンドポイントの詳細については、「Standardエディションの専用KMSインスタンスの照会」をご参照ください。

    # -*- coding: utf-8 -*-
    from openapi.models import Config
    from sdk.client import Client
    
    config = Config()
    # The connection protocol. Set the value to https.
    config.protocol = "https"
    # The client key of the dedicated KMS instance of the Standard edition.
    config.client_key_content = "<your-client-key-content>"
    # The password that is used to decrypt the client key of the dedicated KMS instance of the Standard edition.
    config.password = "<your-password>"
    # The endpoint of the dedicated KMS instance of the Standard edition. The value excludes the scheme https.
    config.endpoint = "<your-endpoint>"
    client = Client(config)

  • Standardエディションの専用KMSインスタンスのクライアントを使用して、対称CMKを使用してデータを暗号化するEncrypt操作を呼び出します。

    サンプルコードの詳細については、「Python 3ソースコード」または「Python 2ソースコード」をご参照ください。

    # -*- coding: utf-8 -*-
    
    from sdk.models import EncryptRequest
    
    request = EncryptRequest()
    # The data that you want to encrypt.
    request.plaintext = "<your-plaintext>".encode("utf-8")
    # The ID or alias of the CMK of the dedicated KMS instance of the Standard edition.
    request.key_id = "<your-key-id>"
    encrypt_response = client.encrypt_with_options(request, runtime_options)
    # The ciphertext.
    ciphertext_blob = encrypt_response.ciphertext_blob
    # The initial vector of Cipher, which is used to decrypt data.
    iv = encrypt_response.iv
    # The ID of the request.
    request_id = encrypt_response.request_id
  • Standardエディションの専用KMSインスタンスのクライアントを使用して、Decrypt操作を呼び出し、対称CMKを使用して暗号文を復号します。

    サンプルコードの詳細については、「Python 3ソースコード」または「Python 2ソースコード」をご参照ください。

    # -*- coding: utf-8 -*-
    
    from sdk.models import DecryptRequest
    
    request = DecryptRequest()
    # The ciphertext that you want to decrypt.
    request.ciphertext_blob = "<your-ciphertext-blob>"
    # The ID or alias of the CMK of the dedicated KMS instance of the Standard edition.
    request.key_id = "<your-key-id>"
    # The initial vector of Cipher. The initial vector must be the same as the initial vector that is specified for encryption.
    request.iv = "<your-iv>"
    decrypt_response = client.decrypt_with_options(request, runtime_options)
    # The plaintext.
    plaintext = decrypt_response.plaintext;
    # The ID of the request.
    request_id = decrypt_response.request_id;
  • Standardエディションの専用KMSインスタンスのクライアントを使用してSign操作を呼び出し、非対称CMKを使用して署名を生成します。

    サンプルコードの詳細については、「Python 3ソースコード」または「Python 2ソースコード」をご参照ください。

    # -*- coding: utf-8 -*-
    
    from sdk.models import SignRequest
    
    request = SignRequest()
    # The ID or alias of the CMK of the dedicated KMS instance of the Standard edition.
    request.key_id = "<your-key-id>"
    # The data to sign.
    request.message = "<your_raw_message>"
    # The signature algorithm.
    request.algorithm = "<your-algorithm>"
    sign_response = client.sign_with_options(request, runtime_options)
    # The signature value.
    signature = sign_response.signature
    # The ID of the request.
    request_id = sign_response.request_id
  • Standardエディションの専用KMSインスタンスのクライアントを使用して、Verify操作を呼び出し、非対称CMKを使用して署名を検証します。

    サンプルコードの詳細については、「Python 3ソースコード」または「Python 2ソースコード」をご参照ください。

    # -*- coding: utf-8 -*-
    
    from sdk.models import SignRequest
    
    request = VerifyRequest()
    # The ID or alias of the CMK of the dedicated KMS instance of the Standard edition. 
    request.key_id = "<your-key-id>"
    # The data for which you want to verify the signature.
    request.message = "<your_raw_message>"
    # The signature algorithm.
    request.algorithm = "<your-algorithm>"
    # The signature value.
    request.signature = "<your-signature>"
    verify_response = client.verify_with_options(request, runtime_options)
    # The verification result.
    valid = verify_response.valid
    # The ID of the request.
    request_id = verify_response.request_id
  • Standardエディションの専用KMSインスタンスのクライアントを使用して、GetSecretValue操作を呼び出し、シークレット値を照会します。

    サンプルコードの詳細については、「Python 3ソースコード」または「Python 2ソースコード」をご参照ください。

    重要
    • Python V0.0.3以降の専用KMS SDKは、GetSecretValue操作をサポートしています。
    • Python 2 V0.0.5以降の専用KMS SDKは、GetSecretValue操作をサポートしています。
    # -*- coding: utf-8 -*-
    
    from sdk.models import GetSecretValueRequest
    
    request = GetSecretValueRequest()
    # The secret name. 
    request.secret_name = "<your-secret-name>"
    response = client.get_secret_value_with_options(request, runtime_options)
    # The secret value. 
    secret_data = response.secret_data
    # Display the request ID. 
    request_id = response.request_id