Skip to content

Commit

Permalink
Merge pull request #579 from loicmathieu/use-credentials
Browse files Browse the repository at this point in the history
Use credentials
  • Loading branch information
loicmathieu authored Feb 27, 2024
2 parents ef2b41e + 9288b67 commit b4931fc
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +22,7 @@
public class BigQueryProducer {

@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
CredentialsProvider credentialsProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 10 additions & 11 deletions docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
----
Expand All @@ -86,24 +86,23 @@ 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;
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +19,7 @@
public class FirebaseAdminProducer {

@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,7 +23,7 @@
public class FirestoreProducer {

@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,7 +21,7 @@
public class LoggingProducer {

@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,7 +22,7 @@
public class SecretManagerProducer {

@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,7 +18,7 @@
@ApplicationScoped
public class SpannerProducer {
@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,7 +19,7 @@
public class StorageProducer {

@Inject
GoogleCredentials googleCredentials;
Credentials googleCredentials;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand Down

0 comments on commit b4931fc

Please sign in to comment.