Gunakan Alibaba Cloud SDK untuk memanggil GetSecretValue dan ambil nilai rahasia melalui gateway bersama atau gateway khusus. Perbedaan antara kedua gateway hanya terletak pada inisialisasi client; kode permintaan dan penanganan eksepsi tetap identik.
Pilih gateway
| Shared gateway | Dedicated gateway | |
|---|---|---|
| Kapan digunakan | Beban kerja umum yang mengakses KMS melalui jaringan publik atau VPC | Beban kerja yang memerlukan instans KMS khusus dengan isolasi yang lebih tinggi |
| Endpoint format | Jaringan publik: kms.<REGION_ID>.aliyuncs.com / VPC: kms-vpc.<REGION_ID>.aliyuncs.com | <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com |
| Sertifikat CA | Tidak diperlukan | SDK V2.0: diperlukan / SDK V1.0: tidak didukung — atur HTTPSInsecure ke true melalui client.SetHTTPSInsecure(true) |
Prasyarat
Sebelum memulai, pastikan Anda telah memiliki:
Akun Alibaba Cloud dengan rahasia yang telah dibuat di KMS
ALIBABA_CLOUD_ACCESS_KEY_IDdanALIBABA_CLOUD_ACCESS_KEY_SECRETyang telah disetel sebagai variabel lingkungan
Menyimpan Pasangan Kunci Akses di kode sumber berisiko menyebabkan kebocoran kredensial. Gunakan Token Layanan Keamanan (STS) untuk beban kerja produksi.
Dapatkan nilai rahasia melalui shared gateway
Contoh lengkap
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Gunakan ID AccessKey dan AccessKey Secret Anda untuk menginisialisasi client.
* @return Client
* @throws Exception
*/
public static com.aliyun.kms20160120.Client createClient() throws Exception {
// Jika kode proyek bocor, Pasangan Kunci Akses mungkin ikut bocor dan sumber daya dalam akun Anda menjadi tidak aman. Kode berikut hanya untuk referensi.
// Kami menyarankan Anda menggunakan Token Layanan Keamanan (STS), yang memberikan keamanan lebih tinggi. Untuk informasi selengkapnya tentang metode autentikasi, lihat https://www.alibabacloud.com/help/en/sdk/developer-reference/v2-manage-access-credentials.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah dikonfigurasi di lingkungan runtime kode.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi di lingkungan runtime kode.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Untuk informasi selengkapnya tentang endpoint, lihat https://api.alibabacloud.com/product/Kms.
config.endpoint = "kms.ap-southeast-1.aliyuncs.com";
return new com.aliyun.kms20160120.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.kms20160120.Client client = Sample.createClient();
com.aliyun.kms20160120.models.GetSecretValueRequest getSecretValueRequest = new com.aliyun.kms20160120.models.GetSecretValueRequest()
.setSecretName("test****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Jika Anda menyalin dan menjalankan contoh kode ini, tulis kode Anda sendiri untuk menampilkan respons operasi API jika diperlukan.
client.getSecretValueWithOptions(getSecretValueRequest, runtime);
} catch (TeaException error) {
// Tangani eksepsi dengan hati-hati sesuai skenario bisnis aktual Anda dan jangan abaikan eksepsi dalam proyek Anda. Pesan kesalahan yang ditampilkan dalam contoh ini hanya untuk referensi.
// Cetak pesan kesalahan
System.out.println(error.getMessage());
// Berikan URL yang digunakan untuk troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// Tangani eksepsi dengan hati-hati sesuai skenario bisnis aktual Anda dan jangan abaikan eksepsi dalam proyek Anda. Pesan kesalahan yang ditampilkan dalam contoh ini hanya untuk referensi.
// Cetak pesan kesalahan
System.out.println(error.getMessage());
// Berikan URL yang digunakan untuk troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}Analisis contoh
Inisialisasi client
Atur endpoint ke alamat gateway bersama untuk wilayah Anda. Contoh ini menggunakan endpoint jaringan publik Singapura (ap-southeast-1).
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Untuk informasi selengkapnya tentang endpoint, lihat https://api.alibabacloud.com/product/Kms.
config.endpoint = "kms.ap-southeast-1.aliyuncs.com";Panggil GetSecretValue
Ganti test**** dengan SecretName aktual Anda.
com.aliyun.kms20160120.models.GetSecretValueRequest getSecretValueRequest = new com.aliyun.kms20160120.models.GetSecretValueRequest()
.setSecretName("test****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
client.getSecretValueWithOptions(getSecretValueRequest, runtime);
} catch (TeaException error) {
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}Tanggapan berisi nilai rahasia dan metadata. Untuk daftar lengkap bidang tanggapan, lihat GetSecretValue.
Dapatkan nilai rahasia melalui dedicated gateway
Contoh lengkap
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Gunakan ID AccessKey dan AccessKey Secret Anda untuk menginisialisasi client.
* @return Client
* @throws Exception
*/
public static com.aliyun.kms20160120.Client createClient() throws Exception {
// Jika kode proyek bocor, Pasangan Kunci Akses mungkin ikut bocor dan sumber daya dalam akun Anda menjadi tidak aman. Kode berikut hanya untuk referensi.
// Kami menyarankan Anda menggunakan Token Layanan Keamanan (STS), yang memberikan keamanan lebih tinggi. Untuk informasi selengkapnya tentang metode autentikasi, lihat https://www.alibabacloud.com/help/en/sdk/developer-reference/v2-manage-access-credentials.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID telah dikonfigurasi di lingkungan runtime kode.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Wajib. Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi di lingkungan runtime kode.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// Endpoint gateway khusus
config.endpoint = "kst-hzz65f176a0ogplgq****.cryptoservice.kms.aliyuncs.com";
// Sertifikat CA instans KMS
config.ca = "-----BEGIN CERTIFICATE-----MIIDuzCCAqOgAwIBAgIJALTKwWAjvbMiMA0GCS****";
return new com.aliyun.kms20160120.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.kms20160120.Client client = Sample.createClient();
com.aliyun.kms20160120.models.GetSecretValueRequest getSecretValueRequest = new com.aliyun.kms20160120.models.GetSecretValueRequest()
.setSecretName("test****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Jika Anda menyalin dan menjalankan contoh kode ini, tulis kode Anda sendiri untuk menampilkan respons operasi API jika diperlukan.
client.getSecretValueWithOptions(getSecretValueRequest, runtime);
} catch (TeaException error) {
// Tangani eksepsi dengan hati-hati sesuai skenario bisnis aktual Anda dan jangan abaikan eksepsi dalam proyek Anda. Pesan kesalahan yang ditampilkan dalam contoh ini hanya untuk referensi.
// Cetak pesan kesalahan
System.out.println(error.getMessage());
// Berikan URL yang digunakan untuk troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// Tangani eksepsi dengan hati-hati sesuai skenario bisnis aktual Anda dan jangan abaikan eksepsi dalam proyek Anda. Pesan kesalahan yang ditampilkan dalam contoh ini hanya untuk referensi.
// Cetak pesan kesalahan
System.out.println(error.getMessage());
// Berikan URL yang digunakan untuk troubleshooting.
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}Analisis contoh
Inisialisasi client
Gateway khusus memerlukan dua bidang tambahan: endpoint instans KMS dan sertifikat CA.
// Endpoint gateway khusus
config.endpoint = "kst-hzz65f176a0ogplgq****.cryptoservice.kms.aliyuncs.com";
// Sertifikat CA instans KMS
config.ca = "-----BEGIN CERTIFICATE-----MIIDuzCCAqOgAwIBAgIJALTKwWAjvbMiMA0GCS****";Ganti kst-hzz65f176a0ogplgq**** dengan ID instans KMS Anda, dan ganti nilai sertifikat CA dengan sertifikat CA yang diunduh dari instans KMS Anda.
SDK V1.0 tidak mendukung sertifikat CA. Sebagai gantinya, atur HTTPSInsecure ke true: client.SetHTTPSInsecure(true).
Panggil GetSecretValue
Kode permintaan dan penanganan eksepsi identik dengan contoh gateway bersama. Ganti test**** dengan SecretName aktual Anda.
com.aliyun.kms20160120.models.GetSecretValueRequest getSecretValueRequest = new com.aliyun.kms20160120.models.GetSecretValueRequest()
.setSecretName("test****");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
client.getSecretValueWithOptions(getSecretValueRequest, runtime);
} catch (TeaException error) {
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
System.out.println(error.getMessage());
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}