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

Key Management Service:シークレット値の取得例

最終更新日:Jan 20, 2025

KMSインスタンスのSDKクライアントが初期化されると、そのクライアントを使用してGetSecretValueインターフェイスを呼び出し、シークレット値を取得できます。 このトピックでは、このプロセスのコード例を示します。

完全なコード例

GetSecretValueインターフェイスを呼び出して、シークレット値を取得します。

GitHubのPython 2ソースコード: get_secret_value_sample.py

GitHubのPython 3ソースコード: get_secret_value_sample.py

このトピックで提供される例は、Python 3バージョンに基づいています。

シークレット値を取得するための完全なコード例

# -*- coding: utf-8 -*-
import os

from openapi.models import Config
from openapi_util.models import RuntimeOptions
from sdk.client import Client
from sdk.models import GetSecretValueRequest

config = Config()
# Set the connection protocol to "https". KMS instance services only allow access through the HTTPS protocol.
config.protocol = "https"
# Client Key.
config.client_key_file = "<CLIENT_KEY_FILE>"
# Client Key decryption password.
config.password = os.getenv('CLIENT_KEY_PASSWORD')
# Set the endpoint to <your KMS Instance Id>.cryptoservice.kms.aliyuncs.com.
config.endpoint = "<ENDPOINT>"
client = Client(config)


def get_secret_value(secret_name):
    request = GetSecretValueRequest()
    request.secret_name = secret_name
    runtime_options = RuntimeOptions()
    # Ignore server-side certificate
    # runtime_options.ignore_ssl = True
    # verify indicates the path of the instance CA certificate
    runtime_options.verify = "<CA_CERTIFICATE_FILE_PATH>"
    resp = client.get_secret_value_with_options(request, runtime_options)
    print(resp)


secret_name = "<SECRET_NAME>"
get_secret_value(secret_name)

コード例の分析

クライアントの初期化

クライアントの初期化の詳細については、「クライアントの初期化」をご参照ください。

# -*- coding: utf-8 -*-
from openapi.models import Config
from sdk.client import Client

config = Config()
# The connection protocol. Set the value to https. The KMS instance service only allows access through the HTTPS protocol.
config.protocol = "https"

# Client Key.
config.client_key_file = "<CLIENT_KEY_FILE>"

# Client Key decryption password.
config.password = os.getenv('CLIENT_KEY_PASSWORD')

# The endpoint of your KMS instance. Set the value in the following format: <ID of your KMS instance>.cryptoservice.kms.aliyuncs.com.
config.endpoint = "<ENDPOINT>"
client = Client(config)

GetSecretValueインターフェイスを呼び出して、シークレット値を取得します。

def get_secret_value(secret_name):
    request = GetSecretValueRequest()
    request.secret_name = secret_name
    runtime_options = RuntimeOptions()
    # Ignore server-side certificate
    # runtime_options.ignore_ssl = True
    # verify indicates the path of the instance CA certificate
    runtime_options.verify = "<CA_CERTIFICATE_FILE_PATH>"
    resp = client.get_secret_value_with_options(request, runtime_options)
    print(resp)