Quarkus 本机和 Google Cloud Storage:通过 Producer 注入存储时“未找到您的默认凭据”

问题描述 投票:0回答:1

在我的 Quarkus 服务项目中,我使用 Google Cloud Storage (GCS) 读取和写入 GCS 存储桶。最近,我重构了有关 GCS 访问的代码,以便能够通过 Google 的

LocalStorageHelper
的虚假实现对我的服务进行单元测试。

这是我的 CDI 生成器类,它为单元测试模式或其他所有模式生成接口

Storage
的实现:

@Slf4j
@ApplicationScoped
public class GcpStorageProducer {

    // Contains my custom Quarkus configuration items, used for GCP project ID here
    @Inject AppRuntime runtime;

    @Produces
    @ApplicationScoped
    @IfBuildProfile("test") // Enabled for tests
    public Storage testStorage() {
        log.info("Producing GCS service bean for unit test environment");
        return LocalStorageHelper.getOptions()
                                 .getService();
    }

    @Produces
    @DefaultBean
    @ApplicationScoped
    public Storage realStorage() throws IOException { // Default storage when not in tests
        log.info("Producing GCS service bean with GCP project {}", runtime.gcpProject());
        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
        if (credentials.createScopedRequired()) {
            credentials = credentials.createScoped("https://www.googleapis.com/auth/devstorage.read_write");
        }
        return StorageOptions.newBuilder()
                             .setProjectId(runtime.gcpProject())
                             .setCredentials(credentials)
                             .build()
                             .getService();
    }
}

注入点微不足道:

@Inject Storage storage;

这在本地和 Google Cloud Run 上的单元测试和 Quarkus JVM 模式下都按预期工作。 但是,在 Google Cloud Run 上使用 JDK 17 和 Mandrel 23.0.1.2-Final 编译的本机模式下,此解决方案会产生异常

java.io.IOException: Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.

我在没有明确的凭据设置的情况下进行了测试,但这导致了类似的结果。

令我惊讶的是,使用内联

Storage
服务实例化重构之前的代码在 Cloud Run 上以本机模式和 JVM 模式工作,它看起来如下,就像您在网上或 Google GCS 文档中找到的每个指南一样,但不可测试(至少我想):

// ... some code before
Storage storage = StorageOptions.newBuilder()
                                .setProjectId(runtime.gcpProject())
                                .build()
                                .getService();
Blob objectToLoad = storage.get(runtime.sourceBucketName(), filename);
// ... some code after

在 Cloud Run 中使用本机二进制文件进行注入时,如何未检测到“应用程序默认凭据”?

java google-cloud-storage cdi google-cloud-run quarkus-native
1个回答
0
投票

我已经能够让它工作了,似乎依赖关系

com.google.cloud:google-cloud-nio
在构建本机应用程序时混淆了一些东西。

我的解决方案是将此依赖项移至 Maven 测试范围。我还在编译范围中包含了 Quarkus Google Cloud Storage 扩展,而不是拥有自己的 CDI 生成器

Storage

对于带有

@QuarkusTest
的 Quarkus 单元测试,我现在在
@Mock
中使用 CDI 替代方案(带有 Quarkus 的
src/test/java
注释),它像以前一样使用
LocalStorageHelper

@ApplicationScoped
public class GcpStorageAlternative {

    @Mock
    @Produces
    @ApplicationScoped
    public Storage getTestStorage() {
        return LocalStorageHelper.customOptions(true)
                                 .getService();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.