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

fix: Allow users to set environment variable to connect to emulator running on docker #1164

Merged

Conversation

danieljbruce
Copy link
Contributor

@danieljbruce danieljbruce commented Sep 27, 2023

Fixes #1119

Summary:

Originally, https://github.com/googleapis/nodejs-datastore/pull/1101/files was merged to support users who wanted to pass in a custom regional endpoint to use a datastore service hosted at a different address than the default endpoint. Before PR 1101 the code assumed that if a custom endpoint was provided then the emulator was being used so insecure credentials should be set so authentication can be skipped, but for users using regional endpoints this does not work. Users using regional endpoints still need authentication so a change in PR 1101 was introduced to set insecure credentials and skip authentication only if the provided endpoint was localhost.

After the PR was merged, users were affected who were using the emulator on a docker service. This is because after PR 1101 was merged the code is checking to see if the custom endpoint provided is localhost, realizing that it is not and so is not setting insecure authentication credentials like it was before and is not skipping authentication. If users are using the emulator on docker, they should at least be able to set DATASTORE_EMULATOR_HOST to indicate that they want to use the emulator so that authentication can be skipped and they can use the emulator.

Changes:

This PR allows the code to assume that the user is using the emulator if the endpoint provided is localhost or the DATASTORE_EMULATOR_HOST environment variable is set. The reason we cannot just check to see if the DATASTORE_EMULATOR_HOST variable is set is because the user might not be using this environment variable when using the emulator and instead might be passing in the emulator address in apiEndpoint as instructed by the following documentation:

* Set that environment variable and your localhost Datastore will
* automatically be used. You can also pass this address in manually with
* `apiEndpoint`.
. It is important to cover the case where the user is using the emulator, but passing the emulator address into the constructor.

Test cases failing before source code changes:

checking ssl credentials are set correctly with custom endpoints
with DATASTORE_EMULATOR_HOST environment variable set
with DATASTORE_EMULATOR_HOST set to remote host
should use insecure ssl credentials when ssl credentials are not provided:

Remaining limitations:

With this change there are still two cases where users will not have their preferred experience.

  1. When the user is running an emulator on a docker service and they are not setting the DATASTORE_EMULATOR_HOST environment variable and instead passing apiEndpoint into the constructor then their code will not skip authentication and will not connect to the emulator. This means users running the emulator on docker must connect to the emulator by setting DATASTORE_EMULATOR_HOST. There is no way to avoid this limitation because we want to support regional endpoints and there is no way to detect the difference between a regional endpoint passed in and an endpoint for a host running an emulator on docker. This limitation existed before the change in this PR, but not before PR 1101.
  2. This PR introduces a problem for users that set DATASTORE_EMULATOR_HOST and wish to use a regional endpoint. Users using a regional endpoint should normally not be setting DATASTORE_EMULATOR_HOST environment variable, but if they are then their calls to the regional endpoint will not work. Since we have no way to distinguish a regional endpoint from a docker endpoint running the emulator, we cannot make all users happy who supply a custom remote endpoint and provide the DATASTORE_EMULATOR_HOST environment variable. For this case, we either set insecure credentials and skip authentication in this case introducing this problem or we don’t skip authentication and don’t solve Unable to connect to emulator running on docker compose with client 7.5.1 #1119.

Instead of checking to see if the endpoint is local, we should check to see if the datastore emulator variable is set. This is because there is an edge case where the user is using an emulator, but the emulator is not listening from local host and in this case we want to use insecure credentials.
Add unit tests for evaluating value of ssl credentials. Tests evaluate what happens when the user uses remote custom endpoints or local custom endpoints in combination with different values for the DATASTORE_EMULATOR_HOST variable.
The tests should use a concise apiEndpoint variable and they should set the host for the remote case to be the same as the apiEndpoint variable.
More test cases are needed to describe what happens when the DATASTORE_EMULATOR_HOST variable is not set both in a remote and in a localhost environment because those behaviors should be different. Setting the apiEndpoint to localhost means the user wants to use the emulator without specifying the environment variable so insecure credentials should be provided. Setting the apiEndpoint to remote means the user likely wishes to use regional endpoints so should not be using insecure credentials to skip authentication.
If the user uses localhost as the endpoint or they provide DATASTORE_EMULATOR_HOST then it is assumed that the user is using the emulator and authentication is skipped. Otherwise, authentication is not skipped.
The old test title should be what it was before. setHost is now only needed in one place.
The comments need to be added to explain why the test is written the way that it is written so that we know the motivation behind each test.
@danieljbruce danieljbruce requested review from a team as code owners September 27, 2023 15:06
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: datastore Issues related to the googleapis/nodejs-datastore API. labels Sep 27, 2023
@danieljbruce danieljbruce added the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 27, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 27, 2023
…into 1119-unable-to-connect-to-emulator

# Conflicts:
#	test/index.ts
@danieljbruce danieljbruce added the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 29, 2023
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Sep 29, 2023
@danieljbruce danieljbruce merged commit a41741b into googleapis:main Sep 29, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the googleapis/nodejs-datastore API. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to connect to emulator running on docker compose with client 7.5.1
2 participants