question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use Emulator Credentials when using Firestore Emulator

See original GitHub issue

describe the problem When trying to run CI/CD against a Firestore emulator instance (e.g. in GitHub Actions), tests were failing due to not finding the GOOGLE_APPLICATION_CREDENTIALS location. This was unexpected since we’d configured emulator=true, and it ofc wasn’t caught because we all have this already set on our local machines.

spring.cloud.gcp.firestore.emulator.enabled=true
spring.cloud.gcp.firestore.host-port=localhost:1234
spring.cloud.gcp.project-id=emulator

So, the workaround was to override the CredentialsProvider to explicitly use FirestoreOptions.EmulatorCredentials():

import com.google.api.gax.core.CredentialsProvider;
import com.google.auth.Credentials;
import com.google.cloud.firestore.FirestoreOptions;
//

@Configuration
public class SomeConfiguration {

  @Profile("integration-test")
  @Primary
  @Bean
  CredentialsProvider credentialsProvider() {
    return new CredentialsProvider() {
      @Override
      public Credentials getCredentials() throws IOException {
        return new FirestoreOptions.EmulatorCredentials();
      }
    };
  }
}

Describe the solution you’d like It’d be nice if this worked without further configuration; e.g. if CredentialsProvider used emulator credentials when emulator is enabled.

Describe alternatives you’ve considered I see in the source that CredentialsProvider always pulls from the location on the Credentials object, which makes sense since a lot of the GCP components don’t have emulator support. Might be tricky, if you don’t want to add some lines to check typeof Credentials.

Additional context None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
meltsufincommented, Jul 26, 2021

Great point about impacting other services! You’re welcome to work on it. I just realized that we have already solved this problem in Pub/Sub. So, we don’t have to reinvent the solution. In Pub/Sub, we made GcpPubSubAutoConfiguration aware of the emulator setting. See this snippet:

	public GcpPubSubAutoConfiguration(GcpPubSubProperties gcpPubSubProperties,
			GcpProjectIdProvider gcpProjectIdProvider,
			CredentialsProvider credentialsProvider) throws IOException {
		this.gcpPubSubProperties = gcpPubSubProperties;
		this.finalProjectIdProvider = (gcpPubSubProperties.getProjectId() != null)
				? gcpPubSubProperties::getProjectId
				: gcpProjectIdProvider;

		if (gcpPubSubProperties.getEmulatorHost() == null
				|| "false".equals(gcpPubSubProperties.getEmulatorHost())) {
			this.finalCredentialsProvider = gcpPubSubProperties.getCredentials().hasKey()
					? new DefaultCredentialsProvider(gcpPubSubProperties)
					: credentialsProvider;
		}
		else {
			// Since we cannot create a general NoCredentialsProvider if the emulator host is enabled
			// (because it would also be used for the other components), we have to create one here
			// for this particular case.
			this.finalCredentialsProvider = NoCredentialsProvider.create();
		}
	}

A similar approach should work in the Firestore module. WDYT?

1reaction
garywg04commented, Jul 27, 2021

Cool, look good to me. Let me try to follow the GcpPubSubAutoConfiguration and add the same handling to Firestore emulator.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect your app to the Authentication Emulator - Firebase
What can I do with the Authentication emulator? · Create, update and manage emulated user accounts for testing email/password, phone number/SMS, SMS multi-factor ......
Read more >
python - Firestore emulator with anonymous credentials
I am trying to get Firestore working in emulator-mode with Python on my local Linux PC. Is it possible to use anonymous credentials...
Read more >
Firestore SDK still requires credentials even if the target is ...
I think the issue is that I use Firestore from firebase-admin because I also ... now using the firestore emulator from the cloud...
Read more >
The Full Guide on how to use the Firebase Emulator ...
First, we'll go through the process of installing the emulator and the different APIs we'll use, like, Authentication, Firestore, and Functions. Second, we'll ......
Read more >
Disable Firebase emulators when in production
When we open the application, you can see both in the console and a big warning here, "Running in emulator mode. Do not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found