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

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

最終更新日:Jan 20, 2025

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

完全なコード例

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

GitHubのソースコードアドレス: GetSecretValueSample.cs

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

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;


namespace AlibabaCloud.Dkms.Gcs.Sdk.Example
{
    public class GetSecretValueSample 
    {

        public static AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config CreateKmsInstanceConfig(string clientKeyFile, string password, string endpoint, string caFilePath)
        {
            AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config config = new AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config();
            config.ClientKeyFile = clientKeyFile;
            config.Password = password;
            config.Endpoint = endpoint;
            config.CaFilePath = caFilePath;
            return config;
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config> CreateKmsInstanceConfigAsync(string clientKeyFile, string password, string endpoint, string caFilePath)
        {
            AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config config = new AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config();
            config.ClientKeyFile = clientKeyFile;
            config.Password = password;
            config.Endpoint = endpoint;
            config.CaFilePath = caFilePath;
            return config;
        }

        public static AlibabaCloud.Dkms.Gcs.Sdk.Client CreateClient(AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig)
        {
            return new AlibabaCloud.Dkms.Gcs.Sdk.Client(kmsInstanceConfig);
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.Sdk.Client> CreateClientAsync(AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig)
        {
            return new AlibabaCloud.Dkms.Gcs.Sdk.Client(kmsInstanceConfig);
        }

        public static AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueResponse GetSecretValue(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string secretName, string versionStage, bool? fetchExtendedConfig)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest
            {
                SecretName = secretName,
                VersionStage = versionStage,
                FetchExtendedConfig = fetchExtendedConfig,
            };
            //Ignore CA certificate authentication
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return client.GetSecretValueWithOptions(request,runtime);
            return client.GetSecretValue(request);
        }

        public static async Task<AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueResponse> GetSecretValueAsync(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string secretName, string versionStage, bool? fetchExtendedConfig)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest
            {
                SecretName = secretName,
                VersionStage = versionStage,
                FetchExtendedConfig = fetchExtendedConfig,
            };
            //Ignore CA certificate authentication
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return await client.GetSecretValueWithOptionsAsync(request,runtime);
            return await client.GetSecretValueAsync(request);
        }

        public static void Main(string[] args)
        {
            string regionId = "REGION_ID";
            // CA certificate of the KMS instance
            string caFilePath = "CA_FILE_PATH";
            // Set the endpoint to <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com.
            string endpoint = "ENDPOINT";
            // Set the Client Key and Client Key security token.
            AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig = CreateKmsInstanceConfig(AlibabaCloud.DarabonbaEnv.Client.GetEnv("ClientKeyFile"), AlibabaCloud.DarabonbaEnv.Client.GetEnv("Password"), endpoint, caFilePath);
            AlibabaCloud.Dkms.Gcs.Sdk.Client client = CreateClient(kmsInstanceConfig);
            //getSecretValue
            string secretName = "SECRET_NAME";
            string versionStage = "VERSION_STAGE";
            bool? fetchExtendedConfig = true;
            AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueResponse getSecretValueRes = GetSecretValue(client, secretName, versionStage, fetchExtendedConfig);
            string getSecretValueResJson = AlibabaCloud.TeaUtil.Common.ToJSONString(AlibabaCloud.TeaUtil.Common.ToMap(getSecretValueRes));
            AlibabaCloud.TeaConsole.Client.Log("getSecretValueRes:" + getSecretValueResJson);
        }


    }
}

コード例分析

クライアントを初期化

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

using System;

string regionId = "your-regionId";

// The CA certificate of the KMS instance
string caFilePath = "your-caFilePath";

// Set the endpoint to <your KMS Instance Id>.cryptoservice.kms.aliyuncs.com.
string endpoint = "your-endpoint";

// Set the Client Key and the security token of the Client Key.
AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig = CreateKmsInstanceConfig(AlibabaCloud.DarabonbaEnv.Client.GetEnv("ClientKeyFile"), AlibabaCloud.DarabonbaEnv.Client.GetEnv("Password"), endpoint, caFilePath);
AlibabaCloud.Dkms.Gcs.Sdk.Client client = CreateClient(kmsInstanceConfig);

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

  public static AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueResponse GetSecretValue(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string secretName, string versionStage, bool? fetchExtendedConfig)
        {
            AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest
            {
                SecretName = secretName,
                VersionStage = versionStage,
                FetchExtendedConfig = fetchExtendedConfig,
            };
            //Ignore CA certificate authentication
            //AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
            //runtime.IgnoreSSL = true;
            //return client.GetSecretValueWithOptions(request,runtime);
            return client.GetSecretValue(request);
        }