Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NoCredentials in case the emulator host is being connected to #705

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/modules/ROOT/pages/pubsub.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ you need to set it instead of relying on the `quarkus.google.cloud.service-accou
Another solution, is to inject a `CredentialsProvider` provided by the extension, and to use it inside the various PubSub
builders and settings objects when, instantiating PubSub components.

In case you are connecting to the emulator (and thus `quarkus.google.cloud.pubsub.emulator-host` is set), the
extension will automatically use the `NoCredentialsProvider` for authentication. This behaviour can be disabled
by setting the `quarkus.google.cloud.pubsub.use-emulator-credentials` to `false`.

== Some example

This is an example usage of the extension: we create a REST resource with a single endpoint that sends a message to the `test-topic` topic when hit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.google.cloud.pubsub")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
Expand All @@ -14,4 +15,14 @@ public interface PubSubConfiguration {
*/
Optional<String> emulatorHost();

/**
* Forces the usage of emulator credentials. The logic automatically uses emulator credentials in case
* the emulatorHost is set.
* <ul>
* <li>If true: force usage of emulator credentials</li>
* <li>If false: force not using emulator credentials</li>
* </ul>
*/
@WithDefault("true")
boolean useEmulatorCredentials();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import java.util.Optional;
import java.util.stream.StreamSupport;

import com.google.api.gax.core.NoCredentialsProvider;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;

import com.google.api.gax.core.CredentialsProvider;
Expand All @@ -23,7 +25,7 @@
@ApplicationScoped
public class QuarkusPubSub {
@Inject
CredentialsProvider credentialsProvider;
Instance<CredentialsProvider> credentialsProvider;

@Inject
GcpConfigHolder gcpConfigHolder;
Expand Down Expand Up @@ -64,7 +66,7 @@ public Subscriber subscriber(String subscription, MessageReceiver receiver) {
public Subscriber subscriber(String subscription, String projectId, MessageReceiver receiver) {
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscription);
var builder = Subscriber.newBuilder(subscriptionName, receiver)
.setCredentialsProvider(credentialsProvider);
.setCredentialsProvider(credentialsProvider());
channelProvider.ifPresent(builder::setChannelProvider);
return builder.build();

Expand All @@ -83,7 +85,7 @@ public Publisher publisher(String topic) throws IOException {
public Publisher publisher(String topic, String projectId) throws IOException {
TopicName topicName = TopicName.of(projectId, topic);
var builder = Publisher.newBuilder(topicName)
.setCredentialsProvider(credentialsProvider);
.setCredentialsProvider(credentialsProvider());
channelProvider.ifPresent(builder::setChannelProvider);
return builder.build();
}
Expand All @@ -93,7 +95,7 @@ public Publisher publisher(String topic, String projectId) throws IOException {
*/
public SubscriptionAdminSettings subscriptionAdminSettings() throws IOException {
var builder = SubscriptionAdminSettings.newBuilder()
.setCredentialsProvider(credentialsProvider);
.setCredentialsProvider(credentialsProvider());
channelProvider.ifPresent(builder::setTransportChannelProvider);
return builder.build();
}
Expand All @@ -103,7 +105,7 @@ public SubscriptionAdminSettings subscriptionAdminSettings() throws IOException
*/
public TopicAdminSettings topicAdminSettings() throws IOException {
var builder = TopicAdminSettings.newBuilder()
.setCredentialsProvider(credentialsProvider);
.setCredentialsProvider(credentialsProvider());
channelProvider.ifPresent(builder::setTransportChannelProvider);
return builder.build();
}
Expand Down Expand Up @@ -146,4 +148,12 @@ public Subscription createSubscription(String topic, String subscription) throws
PushConfig.getDefaultInstance(), 0));
}
}

private CredentialsProvider credentialsProvider() {
if (pubSubConfiguration.emulatorHost().isPresent() && pubSubConfiguration.useEmulatorCredentials()) {
return new NoCredentialsProvider();
} else {
return credentialsProvider.get();
}
}
}
Loading