首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Gooble Cloud KMS:调用kms客户端代码冻结

Gooble Cloud KMS:调用kms客户端代码冻结
EN

Stack Overflow用户
提问于 2019-10-02 18:36:18
回答 1查看 212关注 0票数 0

我想使用google cloud kms来加密和解密SON值,我正在使用下面的代码作为示例https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/kms/src/main/java/com/example/CryptFile.java

代码语言:javascript
复制
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {

  // The resource name of the cryptoKey
  String resourceName = CryptoKeyName.format(projectId, locationId, keyRingId, cryptoKeyId);

  // Encrypt the plaintext with Cloud KMS.
  EncryptResponse response = client.encrypt(resourceName, ByteString.copyFrom(plaintext));

  // Extract the ciphertext from the response.
  return response.getCiphertext().toByteArray();
}

当代码执行行client.encrypt(resourceName, ByteString.copyFrom(plaintext));时,它冻结了,并且我没有得到任何响应。

如果我使用gcloud命令加密/解密,它可以工作。

我在App Engine标准(运行时java8)上运行我的应用程序,我使用的依赖项是

代码语言:javascript
复制
   <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-kms</artifactId>
        <version>1.29.0</version>
    </dependency>

我在代码中做了一些更改以获取凭据:

代码语言:javascript
复制
    AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
    GoogleCredentials credentials = AppEngineCredentials.newBuilder().setScopes(Arrays.asList("https://www.googleapis.com/auth/cloudkms")).
            setAppIdentityService(appIdentityService).build();

    FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(credentials);
    KeyManagementServiceSettings kmsSettings = KeyManagementServiceSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();

    try (KeyManagementServiceClient client = KeyManagementServiceClient.create(kmsSettings)) {

但我总是得到“未认证:失败的计算凭据元数据”。

有什么帮助吗?如果我遗漏了什么,请告诉我。

问候

EN

回答 1

Stack Overflow用户

发布于 2020-11-12 07:48:31

同样的事情,运行示例中的代码会在调用encrypt时出错

Json身份验证文件设置为环境导出GOOGLE_APPLICATION_CREDENTIALS="../my.json“

根据文档对用户授予正确的权限云KMS CryptoKey加解密

已在调试器中验证所有4个参数是否正确: projectId、locationId、keyRingId、cryptoKeyId

此代码挂起

代码语言:javascript
复制
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {

            final String resourceName = CryptoKeyName.format(projectId, locationId, keyRingId, cryptoKeyId);

            // Always Hangs here!!!!
            final EncryptResponse response = client.encrypt(resourceName, ByteString.copyFromUtf8(data));

            return response.getCiphertext().toString();
        } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58199817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档