获取凭据值示例

更新时间:2024-12-25 05:39:10

初始化KMS实例SDK客户端后,您可以通过客户端调用GetSecretValue接口获取凭据值。本文介绍获取凭据值的代码示例。

完整代码示例

调用GetSecretValue接口获取凭据值。

Python 2版本源码github地址:get_secret_value_sample.py

Python 3版本源码github地址: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()
# 连接协议请设置为"https"。KMS实例服务仅允许通过HTTPS协议访问。
config.protocol = "https"
# Client Key。
config.client_key_file = "<your-client-key-file>"
# Client Key解密口令。
config.password = os.getenv('CLIENT_KEY_PASSWORD')
# 设置endpoint为<your KMS Instance Id>.cryptoservice.kms.aliyuncs.com。
config.endpoint = "<your-endpoint>"
client = Client(config)


def get_secret_value(secret_name):
    request = GetSecretValueRequest()
    request.secret_name = secret_name
    runtime_options = RuntimeOptions()
    # 忽略服务端证书
    # runtime_options.ignore_ssl = True
    # verify表示实例CA证书的路径
    runtime_options.verify = "<your-ca-certificate-file-path>"
    resp = client.get_secret_value_with_options(request, runtime_options)
    print(resp)


secret_name = "<your-secret-name>"
get_secret_value(secret_name)

代码示例解析

初始化客户端

关于初始化客户端的详细介绍,请参见初始化客户端

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

config = Config()
# 连接协议请设置为"https"。KMS实例服务仅允许通过HTTPS协议访问。
config.protocol = "https"

# Client Key。
config.client_key_file = "<your-client-key-file>"

# Client Key解密口令。
config.password = os.getenv('CLIENT_KEY_PASSWORD')

# 设置endpoint为<your KMS Instance Id>.cryptoservice.kms.aliyuncs.com。
config.endpoint = "<your-endpoint>"
client = Client(config)

调用GetSecretValue接口获取凭据值

def get_secret_value(secret_name):
    request = GetSecretValueRequest()
    request.secret_name = secret_name
    runtime_options = RuntimeOptions()
    # 忽略服务端证书
    # runtime_options.ignore_ssl = True
    # verify表示实例CA证书的路径
    runtime_options.verify = "<your-ca-certificate-file-path>"
    resp = client.get_secret_value_with_options(request, runtime_options)
    print(resp)

  • 本页导读 (1)
  • 完整代码示例
  • 代码示例解析
  • 初始化客户端
  • 调用GetSecretValue接口获取凭据值