From e5173c593f9f03ceb6a754edf6e432fd1ead1a32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Mon, 12 Feb 2024 12:22:04 +0100 Subject: [PATCH 1/2] Use Credentials instead of GoogleCredentials So it can be replaced with another type of credentials (like emulator credentials) --- .../bigquery/runtime/BigQueryProducer.java | 4 ++-- .../common/GcpCredentialProducer.java | 3 ++- .../common/GcpCredentialProviderProducer.java | 4 ++-- docs/modules/ROOT/pages/index.adoc | 21 +++++++++---------- .../admin/runtime/FirebaseAdminProducer.java | 5 +++-- .../firestore/runtime/FirestoreProducer.java | 4 ++-- .../it/mock/ApplicationProducerMock.java | 7 ++++--- .../logging/runtime/cdi/LoggingProducer.java | 4 ++-- .../runtime/SecretManagerProducer.java | 4 ++-- .../spanner/runtime/SpannerProducer.java | 4 ++-- .../storage/runtime/StorageProducer.java | 4 ++-- 11 files changed, 33 insertions(+), 31 deletions(-) diff --git a/bigquery/runtime/src/main/java/io/quarkiverse/googlecloudservices/bigquery/runtime/BigQueryProducer.java b/bigquery/runtime/src/main/java/io/quarkiverse/googlecloudservices/bigquery/runtime/BigQueryProducer.java index cb4f2722..42e7ea1b 100644 --- a/bigquery/runtime/src/main/java/io/quarkiverse/googlecloudservices/bigquery/runtime/BigQueryProducer.java +++ b/bigquery/runtime/src/main/java/io/quarkiverse/googlecloudservices/bigquery/runtime/BigQueryProducer.java @@ -9,7 +9,7 @@ import jakarta.inject.Singleton; import com.google.api.gax.core.CredentialsProvider; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQueryOptions; import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient; @@ -22,7 +22,7 @@ public class BigQueryProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject CredentialsProvider credentialsProvider; diff --git a/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProducer.java b/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProducer.java index 2a58a297..392fd655 100644 --- a/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProducer.java +++ b/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProducer.java @@ -13,6 +13,7 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; +import com.google.auth.Credentials; import com.google.auth.oauth2.AccessToken; import com.google.auth.oauth2.GoogleCredentials; @@ -41,7 +42,7 @@ public void verifySecurityIdentity() { @Produces @Singleton @Default - public GoogleCredentials googleCredential() throws IOException { + public Credentials googleCredential() throws IOException { GcpBootstrapConfiguration gcpConfiguration = gcpConfigHolder.getBootstrapConfig(); if (gcpConfiguration.serviceAccountLocation().isPresent()) { try (FileInputStream is = new FileInputStream(gcpConfiguration.serviceAccountLocation().get())) { diff --git a/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProviderProducer.java b/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProviderProducer.java index 06da3b57..65132aa2 100644 --- a/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProviderProducer.java +++ b/common/runtime/src/main/java/io/quarkiverse/googlecloudservices/common/GcpCredentialProviderProducer.java @@ -7,13 +7,13 @@ import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.FixedCredentialsProvider; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; @Singleton public class GcpCredentialProviderProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Produces @Singleton diff --git a/docs/modules/ROOT/pages/index.adoc b/docs/modules/ROOT/pages/index.adoc index 07645f3a..1147b5dd 100644 --- a/docs/modules/ROOT/pages/index.adoc +++ b/docs/modules/ROOT/pages/index.adoc @@ -74,10 +74,10 @@ or use the provided `CredentialsProvider` when instantiating their objects.** == Using Google Cloud services emulators If you plan to use one of the Google Cloud services emulators (for running on localhost, or for testing purpose), on a non-authenticated environment, -you'll need to mock the Google Cloud authentication, and optionally the `CredentialsProvider` if you're using it (otherwise it will be removed by Quarkus CDI engine). +you'll need to mock the Google Cloud authentication credentials, and optionally the `CredentialsProvider` if you're using it (otherwise it will be removed by Quarkus CDI engine). For testing, this can be done by creating a CDI producer that will produce a mocked bean (with Quarkus mock support and Mockito) -to replace the `GoogleCloudCredentials` and the `CredentialsProvider`. +to replace the `Credentials` and the `CredentialsProvider` beans. [source, java] ---- @@ -86,11 +86,10 @@ import javax.enterprise.inject.Default; import javax.enterprise.inject.Produces; import javax.inject.Singleton; -import org.mockito.Mockito; - -import com.google.auth.oauth2.GoogleCredentials; import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.NoCredentialsProvider; +import com.google.auth.Credentials; +import com.google.cloud.NoCredentials; import io.quarkus.test.Mock; @@ -98,12 +97,12 @@ import io.quarkus.test.Mock; @ApplicationScoped public class GoogleCredentialsMockProducer { - @Produces - @Singleton - @Default - public GoogleCredentials googleCredential() { - return Mockito.mock(GoogleCredentials.class); - } + @Produces + @Singleton + @Default + public Credentials googleCredential() { + return NoCredentials.getInstance(); + } // only needed if you're injecting it inside one of your CDI beans @Produces diff --git a/firebase-admin/runtime/src/main/java/io/quarkiverse/googlecloudservices/firebase/admin/runtime/FirebaseAdminProducer.java b/firebase-admin/runtime/src/main/java/io/quarkiverse/googlecloudservices/firebase/admin/runtime/FirebaseAdminProducer.java index b897a630..bb95956d 100644 --- a/firebase-admin/runtime/src/main/java/io/quarkiverse/googlecloudservices/firebase/admin/runtime/FirebaseAdminProducer.java +++ b/firebase-admin/runtime/src/main/java/io/quarkiverse/googlecloudservices/firebase/admin/runtime/FirebaseAdminProducer.java @@ -6,6 +6,7 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; +import com.google.auth.Credentials; import com.google.auth.oauth2.GoogleCredentials; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; @@ -18,7 +19,7 @@ public class FirebaseAdminProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject GcpConfigHolder gcpConfigHolder; @@ -37,7 +38,7 @@ public FirebaseApp getFirebaseApp() { GcpBootstrapConfiguration gcpConfiguration = gcpConfigHolder.getBootstrapConfig(); FirebaseOptions firebaseOptions = FirebaseOptions.builder() - .setCredentials(googleCredentials) + .setCredentials((GoogleCredentials) googleCredentials) .setProjectId(gcpConfiguration.projectId().orElse(null)) .build(); diff --git a/firestore/runtime/src/main/java/io/quarkiverse/googlecloudservices/firestore/runtime/FirestoreProducer.java b/firestore/runtime/src/main/java/io/quarkiverse/googlecloudservices/firestore/runtime/FirestoreProducer.java index 383e3d4d..898c5a42 100644 --- a/firestore/runtime/src/main/java/io/quarkiverse/googlecloudservices/firestore/runtime/FirestoreProducer.java +++ b/firestore/runtime/src/main/java/io/quarkiverse/googlecloudservices/firestore/runtime/FirestoreProducer.java @@ -11,7 +11,7 @@ import org.threeten.bp.Duration; import com.google.api.gax.retrying.RetrySettings; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.FirestoreOptions; @@ -23,7 +23,7 @@ public class FirestoreProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject GcpConfigHolder gcpConfigHolder; diff --git a/integration-tests/main/src/test/java/io/quarkiverse/googlecloudservices/it/mock/ApplicationProducerMock.java b/integration-tests/main/src/test/java/io/quarkiverse/googlecloudservices/it/mock/ApplicationProducerMock.java index da5a6c96..592c8809 100644 --- a/integration-tests/main/src/test/java/io/quarkiverse/googlecloudservices/it/mock/ApplicationProducerMock.java +++ b/integration-tests/main/src/test/java/io/quarkiverse/googlecloudservices/it/mock/ApplicationProducerMock.java @@ -12,7 +12,8 @@ import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.NoCredentialsProvider; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; +import com.google.cloud.NoCredentials; import com.google.cloud.secretmanager.v1.AccessSecretVersionResponse; import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; import com.google.cloud.secretmanager.v1.SecretPayload; @@ -28,8 +29,8 @@ public class ApplicationProducerMock { @Produces @Singleton @Default - public GoogleCredentials googleCredential() { - return Mockito.mock(GoogleCredentials.class); + public Credentials googleCredential() { + return NoCredentials.getInstance(); } @Produces diff --git a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/cdi/LoggingProducer.java b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/cdi/LoggingProducer.java index 6717cb0b..06d3521d 100644 --- a/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/cdi/LoggingProducer.java +++ b/logging/runtime/src/main/java/io/quarkiverse/googlecloudservices/logging/runtime/cdi/LoggingProducer.java @@ -5,7 +5,7 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; import com.google.cloud.logging.Logging; import com.google.cloud.logging.LoggingOptions; @@ -21,7 +21,7 @@ public class LoggingProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject GcpConfigHolder gcpConfigHolder; diff --git a/secret-manager/runtime/src/main/java/io/quarkiverse/googlecloudservices/secretmanager/runtime/SecretManagerProducer.java b/secret-manager/runtime/src/main/java/io/quarkiverse/googlecloudservices/secretmanager/runtime/SecretManagerProducer.java index fa299b89..5d3e11ff 100644 --- a/secret-manager/runtime/src/main/java/io/quarkiverse/googlecloudservices/secretmanager/runtime/SecretManagerProducer.java +++ b/secret-manager/runtime/src/main/java/io/quarkiverse/googlecloudservices/secretmanager/runtime/SecretManagerProducer.java @@ -7,7 +7,7 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; import com.google.cloud.secretmanager.v1.SecretManagerServiceClient; import com.google.cloud.secretmanager.v1.SecretManagerServiceSettings; @@ -22,7 +22,7 @@ public class SecretManagerProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject GcpConfigHolder gcpConfigHolder; diff --git a/spanner/runtime/src/main/java/io/quarkiverse/googlecloudservices/spanner/runtime/SpannerProducer.java b/spanner/runtime/src/main/java/io/quarkiverse/googlecloudservices/spanner/runtime/SpannerProducer.java index 17a90dc4..387b0c2b 100644 --- a/spanner/runtime/src/main/java/io/quarkiverse/googlecloudservices/spanner/runtime/SpannerProducer.java +++ b/spanner/runtime/src/main/java/io/quarkiverse/googlecloudservices/spanner/runtime/SpannerProducer.java @@ -8,7 +8,7 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; import com.google.cloud.spanner.Spanner; import com.google.cloud.spanner.SpannerOptions; @@ -18,7 +18,7 @@ @ApplicationScoped public class SpannerProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject GcpConfigHolder gcpConfigHolder; diff --git a/storage/runtime/src/main/java/io/quarkiverse/googlecloudservices/storage/runtime/StorageProducer.java b/storage/runtime/src/main/java/io/quarkiverse/googlecloudservices/storage/runtime/StorageProducer.java index 87496c8d..e078b651 100644 --- a/storage/runtime/src/main/java/io/quarkiverse/googlecloudservices/storage/runtime/StorageProducer.java +++ b/storage/runtime/src/main/java/io/quarkiverse/googlecloudservices/storage/runtime/StorageProducer.java @@ -8,7 +8,7 @@ import jakarta.inject.Inject; import jakarta.inject.Singleton; -import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.Credentials; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; @@ -19,7 +19,7 @@ public class StorageProducer { @Inject - GoogleCredentials googleCredentials; + Credentials googleCredentials; @Inject GcpConfigHolder gcpConfigHolder; From 9288b6738af9b5b1539fee7d63d54038b65de467 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Mon, 12 Feb 2024 12:22:40 +0100 Subject: [PATCH 2/2] Close the transport channel in BigtableResource --- .../googlecloudservices/it/BigtableResource.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/integration-tests/main/src/main/java/io/quarkiverse/googlecloudservices/it/BigtableResource.java b/integration-tests/main/src/main/java/io/quarkiverse/googlecloudservices/it/BigtableResource.java index 4fef8997..aff28ae2 100644 --- a/integration-tests/main/src/main/java/io/quarkiverse/googlecloudservices/it/BigtableResource.java +++ b/integration-tests/main/src/main/java/io/quarkiverse/googlecloudservices/it/BigtableResource.java @@ -3,6 +3,7 @@ import java.io.IOException; import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; @@ -46,12 +47,14 @@ public class BigtableResource { @Inject CredentialsProvider credentialsProvider; + private TransportChannelProvider channelProvider; + @PostConstruct void initBigtable() throws IOException { if (emulatorHost != null) { ManagedChannel channel = ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build(); - TransportChannelProvider channelProvider = FixedTransportChannelProvider.create( + this.channelProvider = FixedTransportChannelProvider.create( GrpcTransportChannel.create(channel)); NoCredentialsProvider credentialsProvider = NoCredentialsProvider.create(); @@ -85,6 +88,13 @@ void initBigtable() throws IOException { } } + @PreDestroy + void closeChannel() throws Exception { + if (this.channelProvider != null) { + this.channelProvider.getTransportChannel().close(); + } + } + @GET public String bigtable() throws IOException { BigtableDataSettings.Builder settings = BigtableDataSettings.newBuilder()