Search code examples
javaspring-cloud-gcp

How to programmatically authenticate Subscriber and pass in application credentials json for Spring Cloud


I have a Subscriber app in Java using spring-cloud-gcp-starter-pubsub, but I don't want to authenticate to Google Cloud using a JSON file on the filesystem; we are getting the file from a different secure location (not on disk) and want to programmatically authenticate the Subscriber.

Currently our app relies on the standard Spring Cloud magic that automatically authenticates based on the JSON file on disk; we create the Subscriber using the PubSubSubscriberTemplate created by default:

@Bean
public Subscriber subscriber(PubSubSubscriberTemplate template) {
    return template.subscribeAndConvert(...);
}

How can we programmatically authenticate the Subscriber, assuming the app has already retrieved the json credentials from a secure external source?


Solution

  • Provide a bean implementing the CredentialsProvider interface -- it will be evaluated lazily wherever credentials are needed.

    refdoc: https://googlecloudplatform.github.io/spring-cloud-gcp/3.2.1/reference/html/index.html#credentials