您在日常訪問(wèn)應(yīng)用程序的過(guò)程中數(shù)據(jù)庫(kù)內(nèi)找賬號(hào)密碼,通常會(huì)嵌入憑據(jù)直接訪問(wèn)程序。在需要更新憑據(jù)時(shí),您除了創(chuàng)建新的憑據(jù)以外,還需要執(zhí)行一些其他操作。您還需要花費(fèi)一些時(shí)間更新應(yīng)用程序以使用新的憑據(jù)。如果您有多個(gè)應(yīng)用程序同一憑據(jù),而您錯(cuò)過(guò)更新其中一個(gè)數(shù)據(jù)庫(kù)內(nèi)找賬號(hào)密碼,則該應(yīng)用程序就無(wú)法使用憑據(jù)登錄。
因此使用方便有效且安全性高的憑據(jù)管理工具十分關(guān)鍵。
華為云憑據(jù)管理服務(wù)(Cloud ,CSMS)可以幫助您擁有以下優(yōu)勢(shì):
使用憑據(jù)登錄數(shù)據(jù)庫(kù)
下文為您介紹如何創(chuàng)建憑據(jù),并且通過(guò)API調(diào)用憑據(jù)來(lái)登錄到您的數(shù)據(jù)庫(kù)。
您首先需要確保您的賬號(hào)擁有KMS 或者KMS 權(quán)限,詳情見(jiàn)DEW權(quán)限管理。
圖1 憑據(jù)登錄流程
流程說(shuō)明如下:
您首先需要在憑據(jù)管理服務(wù)中使用控制臺(tái)或者創(chuàng)建一個(gè)憑據(jù),用來(lái)存儲(chǔ)數(shù)據(jù)庫(kù)的相關(guān)信息(例如:數(shù)據(jù)庫(kù)地址、端口、密碼)。 當(dāng)您使用應(yīng)用程序訪問(wèn)數(shù)據(jù)庫(kù)時(shí),憑據(jù)管理服務(wù)會(huì)去查詢所創(chuàng)建的憑據(jù)存儲(chǔ)的內(nèi)容。 憑據(jù)管理服務(wù)檢索并解密憑據(jù)密文,將憑據(jù)中保存的信息通過(guò)憑據(jù)管理API安全地返回到應(yīng)用程序中。 應(yīng)用程序獲取到解密后的憑據(jù)明文信息,使用這些安全的信息訪問(wèn)數(shù)據(jù)庫(kù)。
創(chuàng)建憑據(jù)和查詢憑據(jù)API
您可以調(diào)用以下API,通過(guò)API創(chuàng)建憑據(jù)保存相關(guān)內(nèi)容并且查詢相關(guān)憑據(jù)信息。
API名稱
說(shuō)明
創(chuàng)建憑據(jù)
創(chuàng)建新的憑據(jù),并且將憑據(jù)值存入憑據(jù)的初始版本
查詢憑據(jù)
查詢指定憑據(jù)的信息
通過(guò)API接口創(chuàng)建憑據(jù)和查詢憑據(jù) 請(qǐng)準(zhǔn)備基礎(chǔ)認(rèn)證信息: 創(chuàng)建和查詢憑據(jù)信息:
憑據(jù)名稱:
憑據(jù)值:
憑據(jù)版本值:
憑據(jù)版本:
import com.huaweicloud.sdk.core.auth.BasicCredentials;import com.huaweicloud.sdk.csms.v1.CsmsClient; import com.huaweicloud.sdk.csms.v1.model.CreateSecretRequest; import com.huaweicloud.sdk.csms.v1.model.CreateSecretRequestBody; import com.huaweicloud.sdk.csms.v1.model.CreateSecretResponse; import com.huaweicloud.sdk.csms.v1.model.ShowSecretVersionRequest; import com.huaweicloud.sdk.csms.v1.model.ShowSecretVersionResponse; public class CsmsCreateSecretExample { /** * 基礎(chǔ)認(rèn)證信息: * - ACCESS_KEY: 華為云賬號(hào)Access Key * - SECRET_ACCESS_KEY: 華為云賬號(hào)Secret Access Key * - PROJECT_ID: 華為云局點(diǎn)項(xiàng)目ID 詳情見(jiàn)https://support.huaweicloud.com/productdesc-iam/iam_01_0023.html * - CSMS_ENDPOINT: 華為云CSMS服務(wù)訪問(wèn)終端地址 詳情見(jiàn)https://support.huaweicloud.com/api-dew/dew_02_0052.html */ private static final String ACCESS_KEY = "
"; private static final String SECRET_ACCESS_KEY = " "; private static final String PROJECT_ID = " "; private static final String CSMS_ENDPOINT = " "; // 用來(lái)查詢憑據(jù)最新版本詳情的版本Id private static final String LATEST_SECRET = "latest"; public static void main(String[] args) { String secretName = args[0]; String secretString = args[1]; // 創(chuàng)建憑據(jù) createSecret(secretName, secretString); // 通過(guò)憑據(jù)版本值“l(fā)atest”或者版本值“v1"查詢到新創(chuàng)建的憑據(jù)內(nèi)容 ShowSecretVersionResponse latestVersion = showSecretVersion(secretName, LATEST_SECRET); ShowSecretVersionResponse firstVersion = showSecretVersion(secretName, "v1"); assert latestVersion.equals(firstVersion); assert latestVersion.getVersion().getSecretString().equalsIgnoreCase(secretString); } /** * 創(chuàng)建憑據(jù) * @param secretName * @param secretString */ private static void createSecret(String secretName, String secretString) { CreateSecretRequest secret = new CreateSecretRequest().withBody( new CreateSecretRequestBody().withName(secretName).withSecretString(secretString)); CsmsClient csmsClient = getCsmsClient();
CreateSecretResponse createdSecret = csmsClient.createSecret(secret); System.out.printf("Created secret success, secret detail:%s", createdSecret); } /** * 根據(jù)憑據(jù)版本id查詢憑據(jù)版本詳情 * @param secretName * @param versionId * @return */ private static ShowSecretVersionResponse showSecretVersion(String secretName, String versionId) { ShowSecretVersionRequest showSecretVersionRequest = new ShowSecretVersionRequest().withSecretName(secretName) .withVersionId(versionId); CsmsClient csmsClient = getCsmsClient(); ShowSecretVersionResponse version = csmsClient.showSecretVersion(showSecretVersionRequest); System.out.printf("Query secret success. version id:%s", version.getVersion().getVersionMetadata().getId()); return version; }
/** * 獲取CSMS服務(wù)客戶端 * @return */ private static CsmsClient getCsmsClient() { BasicCredentials auth = new BasicCredentials() .withAk(ACCESS_KEY) .withSk(SECRET_ACCESS_KEY) .withProjectId(PROJECT_ID); return CsmsClient.newBuilder().withCredential(auth).withEndpoint(CSMS_ENDPOINT).build(); } }
使用應(yīng)用程序獲取數(shù)據(jù)庫(kù)賬號(hào)和密碼 獲取憑據(jù)管理服務(wù)的CSMS SDK的依賴聲明。
示例如下:
mysql mysql-connector-javaXXX com.google.code.gson gson2.8.9
com.huaweicloud.sdk huaweicloud-sdk-csms3.0.79
建立數(shù)據(jù)庫(kù)鏈接并且獲取賬號(hào)密碼:
示例代碼如下
import com.google.gson.Gson; import com.google.gson.JsonObject; import com.huaweicloud.sdk.csms.v1.model.ShowSecretVersionRequest; import com.huaweicloud.sdk.csms.v1.model.ShowSecretVersionResponse; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; // 通過(guò)憑據(jù)信息獲取指定的數(shù)據(jù)庫(kù)賬號(hào)和口令 public static Connection getMySQLConnectionBySecret(String secretName, String jdbcUrl) throws ClassNotFoundException, SQLException{ Class.forName(MYSQL_JDBC_DRIVER); ShowSecretVersionResponse latestVersionValue = getCsmsClient().showSecretVersion(new ShowSecretVersionRequest().withSecretName(secretName).withVersionId("latest")); String secretString = latestVersionValue.getVersion().getSecretString(); JsonObject jsonObject = new Gson().fromJson(secretString, JsonObject.class); return DriverManager.getConnection(jdbcUrl, jsonObject.get("username").getAsString(), jsonObject.get("password").getAsString()); }