Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Make 403 an allowable Pub/Sub UP status #2385

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 21 additions & 19 deletions docs/src/main/asciidoc/pubsub.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ dependencies {

This starter is also available from https://start.spring.io[Spring Initializr] through the `GCP Messaging` entry.

=== Spring Boot Actuator Support
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move this section up?
Our convention is to have configuration after deps in refdoc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll move it back. My thought was that configuration is really long for pubsub, and this needed to be more prominent, but maybe not.


==== Cloud Pub/Sub Health Indicator

If you are using Spring Boot Actuator, you can take advantage of the Cloud Pub/Sub health indicator called `pubsub`.
The health indicator will verify whether Cloud Pub/Sub is up and accessible by your application.
To enable it, all you need to do is add the https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready[Spring Boot Actuator] to your project.

The `pubsub` indicator will then roll up to the overall application status visible at http://localhost:8080/actuator/health (use the `management.endpoint.health.show-details` property to view per-indicator details).

[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
----

NOTE: If your application already has actuator and Cloud Pub/Sub starters, this health indicator is enabled by default.
To disable the Cloud Pub/Sub indicator, set `management.health.pubsub.enabled` to `false`.

[#pubsub-configuration]
=== Configuration

Expand Down Expand Up @@ -117,25 +138,6 @@ MaxRpcTimeout puts a limit on the value of the RPC timeout, so that the RpcTimeo
can't increase the RPC timeout higher than this amount. | No | 0
|===

=== Spring Boot Actuator Support

==== Cloud Pub/Sub Health Indicator

If you are using Spring Boot Actuator, you can take advantage of the Cloud Pub/Sub health indicator called `pubsub`.
The health indicator will verify whether Cloud Pub/Sub is up and accessible by your application.
To enable it, all you need to do is add the https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready[Spring Boot Actuator] to your project.
To disable the Cloud Pub/Sub indicator, set `management.health.pubsub.enabled` to `false`.

The `pubsub` indicator will then roll up to the overall application status visible at http://localhost:8080/actuator/health (use the `management.endpoint.health.show-details` property to view per-indicator details).

[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
----

=== Pub/Sub Operations & Template

`PubSubOperations` is an abstraction that allows Spring users to use Google Cloud Pub/Sub without depending on any Google Cloud Pub/Sub API semantics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.gcp.autoconfigure.pubsub.health;

import com.google.api.gax.rpc.StatusCode.Code;
import java.util.UUID;

import com.google.api.gax.rpc.ApiException;
Expand Down Expand Up @@ -53,7 +54,8 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
this.pubSubTemplate.pull("subscription-" + UUID.randomUUID().toString(), 1, true);
}
catch (ApiException aex) {
if (aex.getStatusCode().getCode() == StatusCode.Code.NOT_FOUND) {
Code errorCode = aex.getStatusCode().getCode();
if (errorCode == StatusCode.Code.NOT_FOUND || errorCode == Code.PERMISSION_DENIED) {
builder.up();
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@

package org.springframework.cloud.gcp.autoconfigure.pubsub;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import com.google.api.gax.grpc.GrpcStatusCode;
import com.google.api.gax.rpc.ApiException;
import io.grpc.Status.Code;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.gcp.autoconfigure.pubsub.health.PubSubHealthIndicator;
import org.springframework.cloud.gcp.autoconfigure.pubsub.health.PubSubHealthIndicatorAutoConfiguration;
import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

/**
* Tests for the PubSub Health Indicator.
*
Expand All @@ -48,13 +48,21 @@ public class PubSubHealthIndicatorTests {
private PubSubTemplate pubSubTemplate;

@Test
public void healthUp() throws Exception {
public void healthUpFor404() throws Exception {
when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())).thenThrow(new ApiException(
new IllegalStateException("Illegal State"), GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), false));
PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate);
assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.UP);
}

@Test
public void healthUpFor403() throws Exception {
when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())).thenThrow(new ApiException(
new IllegalStateException("Illegal State"), GrpcStatusCode.of(Code.PERMISSION_DENIED), false));
PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate);
assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.UP);
}

@Test
public void healthDown() {
when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean()))
Expand Down