Pulling messages from Google PubSub not working - Permission denied

Viewed 1066

I am trying to pull messages from an existing subscription, but every time I try I get a PERMISSION_DENIED error. Testing permissions returns "true" for "pubsub.subscriptions.consume" though. Maybe it has to do with using the exiting subscription instead of creating a new one? I haven't found a way to do this though and every example online does it the same way.

TopicName topic = TopicName.create("cloud-project", "sn-1000");
SubscriptionName subscription = SubscriptionName.create("cloud-project", "sn-1000");

String accountFile = "ServiceAccount_sn-1000.json";
CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(
    ServiceAccountCredentials.fromStream(new FileInputStream(accountFile))
);

SubscriptionAdminSettings subscriptionAdminSettings =
    SubscriptionAdminSettings.defaultBuilder()
    .setCredentialsProvider(credentialsProvider)
    .build();

// THIS IS WHERE IT FAILS
SubscriptionAdminClient subscriptionAdminClient =
    SubscriptionAdminClient.create(subscriptionAdminSettings);
subscriptionAdminClient.createSubscription(subscription, topic, PushConfig.getDefaultInstance(), 0);

Subscriber subscriber = Subscriber.defaultBuilder(subscription, new MyMessageReceiver()).build();

subscriber.addListener(new Subscriber.Listener() {
    @Override
    public void failed(Subscriber.State from, Throwable failure) {
        // Handle failure. This is called when the Subscriber encountered a fatal error and is shutting down.
        System.err.println(failure);
    }
}, MoreExecutors.directExecutor());

subscriber.startAsync().awaitRunning();

if(subscriber.state().equals(ApiService.State.FAILED)) {
    subscriber.stopAsync().awaitTerminated();
    return;
}

//Printing messages written to a buffer in the custom MyMessageReceiver class, which implements MessageReceiver

subscriber.stopAsync().awaitTerminated();

And I receive this error when executing this code in the console:

com.google.api.gax.grpc.GrpcApiException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: User not authorized to perform this action.
at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:112)
at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:53)
at com.google.common.util.concurrent.Futures$4.run(Futures.java:1123)
at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:435)
at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:900)
at com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:811)
at com.google.common.util.concurrent.AbstractFuture.setException(AbstractFuture.java:675)
at io.grpc.stub.ClientCalls$GrpcFuture.setException(ClientCalls.java:466)
at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:442)
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:426)
at io.grpc.internal.ClientCallImpl.access$100(ClientCallImpl.java:76)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:512)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$700(ClientCallImpl.java:429)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:544)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:52)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:117)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: io.grpc.StatusRuntimeException: PERMISSION_DENIED: User not authorized to perform this action.
at io.grpc.Status.asRuntimeException(Status.java:543)
... 15 more
1 Answers

The line that is failing is one where you are trying to create a subscription, not where you are trying to consume messages. The "pubsub.subscriptions.consume" permission is not sufficient for creating a subscription. The access control page details the permissions needed to perform different actions. In order to create a subscription, the required permissions are "pubsub.subscriptions.create on the containing Cloud project, and pubsub.topics.attachSubscription on the requested topic. Note that for creating a subscription in Project A on a Topic T in Project B, the appropriate permissions must be granted on both Project A and on Topic T."

If the subscription already exists, then you do not need to call createSubscription again and instead can just create the subscriber object:

ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of("cloud-project", "sn-1000");
Subscriber subscriber = Subscriber.newBuilder(subscriptionName, new MyMessageReceiver()).build();
Related