全部产品
Search
文档中心

Key Management Service:Contoh kode untuk mengambil nilai rahasia

更新时间:Jul 02, 2025

Setelah menginisialisasi SDK client instance KMS, Anda dapat menggunakannya untuk memanggil API GetSecretValue guna mengambil nilai rahasia. Topik ini menyediakan contoh kode terkait.

Contoh lengkap

<?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;

// Tentukan jalur file ClientKey yang Anda peroleh di manajemen aplikasi KMS.
// $clientKeyFile = '<CLIENT_KEY_FILE_PATH>';

// Sebagai alternatif, tentukan konten file ClientKey yang Anda peroleh di manajemen aplikasi KMS.
$clientKeyContent = '<CLIENT_KEY_CONTENT>';

// Tentukan kata sandi enkripsi yang Anda masukkan saat membuat ClientKey di manajemen aplikasi KMS.
$password = getenv('CLIENT_KEY_PASSWORD');

// Tentukan alamat VPC instance KMS Anda.
$endpoint = '<DKMS_INSTANCE_SERVICE_ADDRESS>';

// Tentukan nama kredensial yang Anda buat di KMS.
$secretName = '<SECRET_NAME>';

// Objek SDK Client untuk instance KMS.
$client = getDkmsGcsSdkClient();
if (is_null($client)) exit(1);

// Contoh pengambilan nilai rahasia KMS.
getSecretValueSample();

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

    // Buat permintaan untuk mengambil nilai rahasia.
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    // Abaikan sertifikat server.
    $runtimeOptions = new RuntimeOptions();
    //$runtimeOptions->ignoreSSL = true;

    try {
        // Panggil operasi untuk mengambil nilai rahasia.
        $getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);

        // Nama rahasia.
        $_secretName = $getSecretValueResponse->secretName;
        // Nilai rahasia.
        $_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());
    }
}

/**
 * Buat objek SDK Client untuk instance KMS.
 * @return AlibabaCloudDkmsGcsSdkClient
 */
function getDkmsGcsSdkClient()
{
    global $clientKeyContent, $password, $endpoint;

    // Buat konfigurasi SDK Client untuk instance KMS.
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    // Setel protokol koneksi ke "https". Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
    $config->protocol = 'https';
    // Client Key.
    $config->clientKeyContent = $clientKeyContent;
    // Kata sandi Client Key.
    $config->password = $password;
    // Setel endpoint ke <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com.
    $config->endpoint = $endpoint;
    // Sertifikat CA instance.
    $config->caFilePath = 'path/to/caCert.pem';

    // Buat objek SDK Client untuk instance KMS.
    return new AlibabaCloudDkmsGcsSdkClient($config);
}

Penjelasan contoh

Inisialisasi client

<?php

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


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

    // Buat konfigurasi SDK client instance KMS.
    $config = new AlibabaCloudDkmsGcsOpenApiConfig();
    // Protokol koneksi. Setel nilainya ke https. Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
    $config->protocol = 'https';
    // Client Key.
    $config->clientKeyContent = $clientKeyContent;
    // Token keamanan Client Key.
    $config->password = $password;
    // Endpoint instance KMS Anda. Setel nilainya dalam format berikut: <ID instance KMS Anda>.cryptoservice.kms.aliyuncs.com.
    $config->endpoint = $endpoint;
    // Sertifikat CA instance.
    $config->caFilePath = 'path/to/caCert.pem';

    // Buat objek SDK client instance KMS.
    return new AlibabaCloudDkmsGcsSdkClient($config);
}

Panggil API GetSecretValue

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

    // Buat permintaan untuk mengambil nilai rahasia.
    $getSecretValueRequest = new GetSecretValueRequest([
        'secretName' => $secretName,
    ]);
    // Abaikan sertifikat server.
    $runtimeOptions = new RuntimeOptions();
    //$runtimeOptions->ignoreSSL = true;

    try {
        // Panggil API untuk mengambil nilai rahasia.
        $getSecretValueResponse = $client->getSecretValueWithOptions($getSecretValueRequest, $runtimeOptions);

        // Nama rahasia.
        $_secretName = $getSecretValueResponse->secretName;
        // Nilai rahasia.
        $_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());
    }
}