获取凭据值示例

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

完整代码示例

调用接口获取凭据值。

源码github地址:GetSecretValue.php

获取凭据值完整代码示例

<?php

if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}

use AlibabaCloud\Dkms\Gcs\OpenApi\Util\Models\RuntimeOptions;
use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;
use AlibabaCloud\Dkms\Gcs\Sdk\Models\GetSecretValueRequest;

// 填写您在KMS应用管理获取的ClientKey文件路径
// $clientKeyFile = '<your client key file path>';

// 或者,填写您在KMS应用管理获取的ClientKey文件内容
$clientKeyContent = '<your client key content>';

// 填写您在KMS应用管理创建ClientKey时输入的加密口令
$password = getenv('CLIENT_KEY_PASSWORD');

// 填写您的KMS实例VPC地址
$endpoint = '<your dkms instance service address>';

// 填写您在KMS创建的凭据名称
$secretName = '<your secret name>';

// KMS实例SDK Client对象
$client = getDkmsGcsSdkClient();
if (is_null($client)) exit(1);

// 获取KMS凭据示例
getSecretValueSample();

function getSecretValueSample(){
    global $client, $secretName;

    // 构建获取凭据请求
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    // 忽略服务端证书
    $runtimeOptions = new RuntimeOptions();
    //$runtimeOptions->ignoreSSL = true;

    try {
        // 调用获取凭据接口
        $getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);

        // 凭据名称
        $_secretName = $getSecretValueResponse->secretName;
        // 凭据值
        $_secretData = $getSecretValueResponse->secretData;

        var_dump($getSecretValueResponse->toMap());
    } catch (\Exception $error) {
        if ($error instanceof \AlibabaCloud\Tea\Exception\TeaError) {
            var_dump($error->getErrorInfo());
        }
        var_dump($error->getMessage());
        var_dump($error->getTraceAsString());
    }
}

/**
 * 构建KMS实例SDK Client对象
 * @return AlibabaCloudDkmsGcsSdkClient
 */
function getDkmsGcsSdkClient()
{
    global $clientKeyContent, $password, $endpoint;

    // 构建KMS实例SDK Client配置
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    //连接协议请设置为"https"。KMS实例服务仅允许通过HTTPS协议访问。
    $config->protocol = 'https';
    //Client Key。
    $config->clientKeyContent = $clientKeyContent;
    //Client Key口令。
    $config->password = $password;
    //设置endpoint为<your KMS Instance Id>.cryptoservice.kms.aliyuncs.com。
    $config->endpoint = $endpoint;
    // 实例CA证书
    $config->caFilePath = 'path/to/caCert.pem';

    // 构建KMS实例SDK Client对象
    return new AlibabaCloudDkmsGcsSdkClient($config);
}

代码示例解析

初始化客户端

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

<?php

use AlibabaCloud\Dkms\Gcs\Sdk\Client as AlibabaCloudDkmsGcsSdkClient;
use AlibabaCloud\Dkms\Gcs\OpenApi\Models\Config as AlibabaCloudDkmsGcsOpenApiConfig;


function getDkmsGcsSdkClient()
{
    global $clientKeyContent, $password, $endpoint;

    // 构建KMS实例SDK Client配置
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    //连接协议请设置为"https"。KMS实例服务仅允许通过HTTPS协议访问。
    $config->protocol = 'https';
    //Client Key。
    $config->clientKeyContent = $clientKeyContent;
    //Client Key口令。
    $config->password = $password;
    //设置endpoint为<your KMS Instance Id>.cryptoservice.kms.aliyuncs.com。
    $config->endpoint = $endpoint;
    // 实例CA证书
    $config->caFilePath = 'path/to/caCert.pem';

    // 构建KMS实例SDK Client对象
    return new AlibabaCloudDkmsGcsSdkClient($config);
}

调用GetSecretValue接口获取凭据值

function getSecretValueSample(){
    global $client, $secretName;

    // 构建获取凭据请求
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    // 忽略服务端证书
    $runtimeOptions = new RuntimeOptions();
    //$runtimeOptions->ignoreSSL = true;

    try {
        // 调用获取凭据接口
        $getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);

        // 凭据名称
        $_secretName = $getSecretValueResponse->secretName;
        // 凭据值
        $_secretData = $getSecretValueResponse->secretData;

        var_dump($getSecretValueResponse->toMap());
    } catch (\Exception $error) {
        if ($error instanceof \AlibabaCloud\Tea\Exception\TeaError) {
            var_dump($error->getErrorInfo());
        }
        var_dump($error->getMessage());
        var_dump($error->getTraceAsString());
    }
}