凭据管家客户端(SecretsManager Client)基于KMS凭据管家API封装了业务逻辑、最佳实践和设计模式,更易于开发者在业务系统中集成。主要适用于应用中动态使用托管在凭据管家中的凭据,告别对敏感信息的硬编码。

功能特性

凭据管家客户端具有以下功能特性:

  • 支持开发者在应用中快速集成凭据管家能力,一行代码读取凭据信息。
  • 封装凭据在应用中缓存和刷新的功能。
  • 封装API错误的重试机制,智能处理服务端错误。
  • 开放插件式设计模式,支持开发者自定义扩展缓存、错误重试等功能模块。

凭据管家Java客户端

安装SDK

凭据管家客户端支持Java语言,您可以访问SecretsManager Client for Java开源代码仓库了解更多代码信息。

您可以通过Maven在项目中使用凭据管家Java客户端,需要添加的依赖信息如下:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alibabacloud-secretsmanager-client</artifactId>
    <version>x.x.x</version>
</dependency>
说明 凭据管家Java客户端具体版本,请参见alibabacloud-secretsmanager-client-java

示例代码

  • 通过配置文件(secretsmanager.properties)构建客户端

    建议您采用基于Client Key的应用接入点,通过凭据管家Java SDK使用Client Key。关于如何创建Client Key,请参见为AAP绑定Client Key

    凭据管家Java客户端在1.1.8及以上版本支持基于Client Key应用接入点访问凭据管家,需要配置以下配置文件:

    ## 配置访问方式。
    credentials_type=client_key
    
    ## 读取Client Key的解密密码:支持从环境变量或者文件读取。
    client_key_password_from_env_variable=#your client key private key password environment variable name#
    client_key_password_from_file_path=#your client key private key password file path#
    
    ## 获取Client Key的私钥文件。
    client_key_private_key_path=#your client key private key file path#
    
    ## 配置关联的KMS地域。
    cache_client_region_id=[{"regionId":"#regionId#"}]

    通过配置文件(secretsmanager.properties)构建客户端的示例代码如下:

    import com.aliyuncs.kms.secretsmanager.client.SecretCacheClient;
    import com.aliyuncs.kms.secretsmanager.client.SecretCacheClientBuilder;
    import com.aliyuncs.kms.secretsmanager.client.exception.CacheSecretException;
    import com.aliyuncs.kms.secretsmanager.client.model.SecretInfo;
    
    public class CacheClientEnvironmentSample {
    
        public static void main(String[] args) {
            try {
                SecretCacheClient client = SecretCacheClientBuilder.newClient();
                SecretInfo secretInfo = client.getSecretInfo("#secretName#");
                System.out.println(secretInfo);
            } catch (CacheSecretException e) {
                e.printStackTrace();
            }
        }
    }
  • 通过指定参数(Access Key、Access Secret、RegionID等)构建客户端
    import com.aliyuncs.kms.secretsmanager.client.SecretCacheClient;
    import com.aliyuncs.kms.secretsmanager.client.SecretCacheClientBuilder;
    import com.aliyuncs.kms.secretsmanager.client.exception.CacheSecretException;
    import com.aliyuncs.kms.secretsmanager.client.model.SecretInfo;
    import com.aliyuncs.kms.secretsmanager.client.service.BaseSecretManagerClientBuilder;
    import com.aliyuncs.kms.secretsmanager.client.utils.CredentialsProviderUtils;
    
    public class CacheClientSimpleParametersSample {
    
        public static void main(String[] args) {
            try {
                SecretCacheClient client = SecretCacheClientBuilder.newCacheClientBuilder(
                        BaseSecretManagerClientBuilder.standard().withCredentialsProvider(CredentialsProviderUtils
                                .withAccessKey("#accessKeyId#", "#accessKeySecret#")).withRegion("#regionId#").build()).build();
                SecretInfo secretInfo = client.getSecretInfo("#secretName#");
                System.out.println(secretInfo);
            } catch (CacheSecretException e) {
                e.printStackTrace();
            }
        }
    }
  • 通过自定义参数构建客户端
    import com.aliyuncs.kms.secretsmanager.client.SecretCacheClient;
    import com.aliyuncs.kms.secretsmanager.client.SecretCacheClientBuilder;
    import com.aliyuncs.kms.secretsmanager.client.cache.FileCacheSecretStoreStrategy;
    import com.aliyuncs.kms.secretsmanager.client.exception.CacheSecretException;
    import com.aliyuncs.kms.secretsmanager.client.model.SecretInfo;
    import com.aliyuncs.kms.secretsmanager.client.service.BaseSecretManagerClientBuilder;
    import com.aliyuncs.kms.secretsmanager.client.service.DefaultRefreshSecretStrategy;
    import com.aliyuncs.kms.secretsmanager.client.service.FullJitterBackoffStrategy;
    import com.aliyuncs.kms.secretsmanager.client.utils.CredentialsProviderUtils;
    
    public class CacheClientDetailParametersSample {
    
        public static void main(String[] args) {
            try {
                SecretCacheClient client = SecretCacheClientBuilder.newCacheClientBuilder(BaseSecretManagerClientBuilder.standard()
                        .withCredentialsProvider(CredentialsProviderUtils.withAccessKey("#accessKeyId#", "#accessKeySecret#"))
                        .withRegion("#regionId#")
                        .withBackoffStrategy(new FullJitterBackoffStrategy(3, 2000, 10000)).build())
                        .withCacheSecretStrategy(new FileCacheSecretStoreStrategy("#cacheSecretPath#", true, "#salt#"))
                        .withRefreshSecretStrategy(new DefaultRefreshSecretStrategy("#ttlName#"))
                        .withCacheStage("#stage#")
                        .withSecretTTL("#secretName#", 1 * 60 * 1000l)
                        .withSecretTTL("#secretName1#", 2 * 60 * 1000l).build();
                SecretInfo secretInfo = client.getSecretInfo("#secretName#");
                System.out.println(secretInfo);
            } catch (CacheSecretException e) {
                e.printStackTrace();
            }
        }
    }

凭据管家Python客户端

安装SDK

凭据管家客户端支持Python语言,您可以访问SecretsManager Client for Python开源代码仓库了解更多代码信息。

您可以执行如下pip安装命令,在项目中使用凭据管家Python客户端。

pip install aliyun-secret-manager-client
说明 凭据管家Python客户端具体版本,请参见aliyun-secretsmanager-client-python

示例代码

  • 通过系统环境变量构建客户端
    from alibaba_cloud_secretsmanager_client.secret_manager_cache_client_builder import SecretManagerCacheClientBuilder
    
    if __name__ == '__main__':
        secret_cache_client = SecretManagerCacheClientBuilder.new_client()
        secret_info = secret_cache_client.get_secret_info("#secretName#")
        print(secret_info.__dict__)
  • 通过指定参数(Access Key、Access Secret、RegionID等)构建客户端
    from alibaba_cloud_secretsmanager_client.secret_manager_cache_client_builder import SecretManagerCacheClientBuilder
    from alibaba_cloud_secretsmanager_client.service.default_secret_manager_client_builder import DefaultSecretManagerClientBuilder
    
    if __name__ == '__main__':
        secret_cache_client = SecretManagerCacheClientBuilder.new_cache_client_builder(DefaultSecretManagerClientBuilder.standard() \
            .with_access_key("#accessKeyId#", "#accessKeySecret#") \
            .with_region("#regionId#").build()) \
        .build();
        secret_info = secret_cache_client.get_secret_info("#secretName#")
        print(secret_info.__dict__)
  • 通过自定义参数构建客户端
    from alibaba_cloud_secretsmanager_client.secret_manager_cache_client_builder import SecretManagerCacheClientBuilder
    from alibaba_cloud_secretsmanager_client.cache.file_cache_secret_store_strategy import FileCacheSecretStoreStrategy
    from alibaba_cloud_secretsmanager_client.service.default_secret_manager_client_builder import DefaultSecretManagerClientBuilder
    from alibaba_cloud_secretsmanager_client.service.default_refresh_secret_strategy import DefaultRefreshSecretStrategy
    from alibaba_cloud_secretsmanager_client.service.full_jitter_back_off_strategy import FullJitterBackoffStrategy
    
    if __name__ == '__main__':
        secret_cache_client = SecretManagerCacheClientBuilder \
        .new_cache_client_builder(DefaultSecretManagerClientBuilder.standard().with_access_key("#accessKeyId#", "#accessKeySecret#") \
             .with_back_off_strategy(FullJitterBackoffStrategy(3, 2000, 10000)) \
             .with_region("#regionId#").build()) \
         .with_cache_secret_strategy(FileCacheSecretStoreStrategy("#cacheSecretPath#", True,"#salt#")) \
         .with_refresh_secret_strategy(DefaultRefreshSecretStrategy("#ttlName#")) \
         .with_cache_stage("#stage#") \
         .with_secret_ttl("#secretName#", 1 * 60 * 1000l) \
         .build()
        secret_info = secret_cache_client.get_secret_info("#secretName#")
        print(secret_info.__dict__)

凭据管家Go客户端

安装SDK

凭据管家客户端支持Python语言,您可以访问SecretsManager Client for Go开源代码仓库了解更多代码信息。

您可以执行如下安装命令,在项目中使用凭据管家Go客户端。

 go get -u github.com/aliyun/aliyun-secretsmanager-client-go
说明 凭据管家Go客户端具体版本,请参见aliyun-secretsmanager-client-go

示例代码

  • 通过配置文件(secretsmanager.properties)构建客户端

    建议您采用基于Client Key的应用接入点,通过凭据管家Go SDK使用Client Key。关于如何创建Client Key,请参见为AAP绑定Client Key

    凭据管家Go客户端在v1.0.1及以上版本支持基于Client Key应用接入点访问凭据管家,需要配置以下配置文件:

    ## 配置访问方式。
    credentials_type=client_key
    
    ## 读取Client Key的解密密码:支持从环境变量或者文件读取。
    client_key_password_from_env_variable=#your client key private key password environment variable name#
    client_key_password_from_file_path=#your client key private key password file path#
    
    ## 获取Client Key的私钥文件。
    client_key_private_key_path=#your client key private key file path#
    
    ## 配置关联的KMS地域。
    cache_client_region_id=[{"regionId":"#regionId#"}]

    通过配置文件(secretsmanager.properties)构建客户端的示例代码如下:

    package main
    
    import (
        "fmt"
        "github.com/aliyun/aliyun-secretsmanager-client-go/sdk"
    )
    
    func main() {
        client, err := sdk.NewClient()
        if err != nil {
            // Handle exceptions
            panic(err)
        }
        secretInfo, err := client.GetSecretInfo("#secretName#")
        if err != nil {
            // Handle exceptions
            panic(err)
        }
        fmt.Printf("SecretValue:%s\n",secretInfo.SecretValue)
    }
  • 通过指定参数(Access Key、Access Secret、RegionID等)构建客户端
    package main
    
    import (
        "fmt"
        "github.com/aliyun/aliyun-secretsmanager-client-go/sdk/service"
        "github.com/aliyun/aliyun-secretsmanager-client-go/sdk"
    )
    
    func main() {
        client, err := sdk.NewSecretCacheClientBuilder(service.NewDefaultSecretManagerClientBuilder().Standard().WithAccessKey("#accessKeyId#", "#accessKeySecret#").WithRegion("#regionId#").Build()).Build()
        if err != nil {
            // Handle exceptions
            panic(err)
        }
        secretInfo, err := client.GetSecretInfo("#secretName#")
        if err != nil {
            // Handle exceptions
            panic(err)
        }
        fmt.Printf("SecretValue:%s\n",secretInfo.SecretValue)
    }
  • 通过自定义参数构建客户端
    package main
    
    import (
        "fmt"
        "github.com/aliyun/aliyun-secretsmanager-client-go/sdk/service"
        "github.com/aliyun/aliyun-secretsmanager-client-go/sdk"
        "github.com/aliyun/aliyun-secretsmanager-client-go/sdk/cache"
    )
    
    func main() {
        client, err := sdk.NewSecretCacheClientBuilder(
            service.NewDefaultSecretManagerClientBuilder().Standard().WithAccessKey("#accessKeyId#", "#accessKeySecret#").WithRegion("#regionId#").WithBackoffStrategy(&service.FullJitterBackoffStrategy{RetryMaxAttempts: 3, RetryInitialIntervalMills: 2000, Capacity: 10000}).Build()).WithCacheSecretStrategy(cache.NewFileCacheSecretStoreStrategy("#cacheSecretPath#", true, "#salt#")).WithRefreshSecretStrategy(service.NewDefaultRefreshSecretStrategy("#jsonTTLPropertyName#")).WithCacheStage("ACSCurrent").WithSecretTTL("#secretName#", 1*60*1000).Build()
        if err != nil {
            // Handle exceptions
            panic(err)
        }
        secretInfo, err := client.GetSecretInfo("#secretName#")
        if err != nil {
            // Handle exceptions
            panic(err)
        }
        fmt.Printf("SecretValue:%s\n",secretInfo.SecretValue)
    }