GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: avoid authentication when use emulator by HemangChothani · Pull Request #328 · googleapis/python-storage

This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Conversation

@HemangChothani

@HemangChothani HemangChothani commented Nov 28, 2020

Copy link
Copy Markdown
Contributor

Fixes #324

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@HemangChothani
feat: avoid authentication when use emulator
b5ea0b2
@HemangChothani
HemangChothani requested review from frankyn and tseaver November 28, 2020 14:46
@google-cla google-cla Bot added the cla: yes This human has signed the Contributor License Agreement. label Nov 28, 2020
@product-auto-label product-auto-label Bot added the api: storage Issues related to the googleapis/python-storage API. label Nov 28, 2020
tseaver
tseaver suggested changes Nov 30, 2020
View reviewed changes
Comment thread google/cloud/storage/client.py Outdated
project = os.getenv("GCLOUD_PROJECT")
if project is None:
raise ValueError(
"For emulator pass `project` as a argument or set `GCLOUD_PROJECT` via environment variable"

@tseaver tseaver Nov 30, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
"For emulator pass `project` as a argument or set `GCLOUD_PROJECT` via environment variable"
"To use the GCS emulator, pass 'project' as an argument "
"or set GCLOUD_PROJECT environment variable"

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/test_client.py Outdated
Comment on lines +222 to +232
def test_ctor_w_emulator_w_env_var_glcoud_project(self):
from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR

HOST = "https://api.example.com"
PROJECT = "test"

with mock.patch("os.environ", {STORAGE_EMULATOR_ENV_VAR: HOST}):
with mock.patch("os.getenv", side_effect=[HOST, PROJECT]):
client = self._make_one()
self.assertEqual(client.project, PROJECT)
self.assertEqual(client._connection.API_BASE_URL, HOST)

@tseaver tseaver Nov 30, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This test needs an assertion that the client's credentials is an instance of AnonymousCredentials.

Also, rather than patching os.getenv, this would be clearer if we just patched in the expected environment:

Suggested change
def test_ctor_w_emulator_w_env_var_glcoud_project(self):
from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR
HOST = "https://api.example.com"
PROJECT = "test"
with mock.patch("os.environ", {STORAGE_EMULATOR_ENV_VAR: HOST}):
with mock.patch("os.getenv", side_effect=[HOST, PROJECT]):
client = self._make_one()
self.assertEqual(client.project, PROJECT)
self.assertEqual(client._connection.API_BASE_URL, HOST)
def test_ctor_w_emulator_w_env_var_glcoud_project(self):
from google.auth.credentials import AnonymousCredentials
from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR
HOST = "https://api.example.com"
PROJECT = "test"
environ = {
STORAGE_EMULATOR_ENV_VAR: HOST,
"GCLOUD_PROJECT": PROJECT,
}
with mock.patch("os.environ", environ):
client = self._make_one()
self.assertEqual(client.project, PROJECT)
self.assertEqual(client._connection.API_BASE_URL, HOST)
self.assertIsInstance(client._credentials, AnonymousCredentials)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread tests/unit/test_client.py
Comment on lines +234 to +246
def test_ctor_w_emulator_w_project_argument(self):
HOST = "https://api.example.com"
PROJECT = "test"

with mock.patch("os.getenv", side_effect=[HOST]):
client = self._make_one(project=PROJECT)
self.assertEqual(client.project, PROJECT)

def test_ctor_w_emulator_missing_project(self):
HOST = "https://api.example.com"
with mock.patch("os.getenv", side_effect=[HOST, None]):
with self.assertRaises(ValueError):
self._make_one()

@tseaver tseaver Nov 30, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

We need to verify that the passed-in project overrides the one in the environment. Likewise for these tests, just patch os.environ:

Suggested change
def test_ctor_w_emulator_w_project_argument(self):
HOST = "https://api.example.com"
PROJECT = "test"
with mock.patch("os.getenv", side_effect=[HOST]):
client = self._make_one(project=PROJECT)
self.assertEqual(client.project, PROJECT)
def test_ctor_w_emulator_missing_project(self):
HOST = "https://api.example.com"
with mock.patch("os.getenv", side_effect=[HOST, None]):
with self.assertRaises(ValueError):
self._make_one()
def test_ctor_w_emulator_w_project_argument(self):
from google.auth.credentials import AnonymousCredentials
from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR
HOST = "https://api.example.com"
PROJECT = "test"
ENVIRON_PROJECT = "environ-project"
environ = {
STORAGE_EMULATOR_ENV_VAR: HOST,
"GCLOUD_PROJECT": ENVIRON_PROJECT,
}
with mock.patch("os.environ", environ):
client = self._make_one(project=PROJECT)
self.assertEqual(client.project, PROJECT)
self.assertEqual(client._connection.API_BASE_URL, HOST)
self.assertIsInstance(client._credentials, AnonymousCredentials)
def test_ctor_w_emulator_missing_project(self):
from google.cloud.storage._helpers import STORAGE_EMULATOR_ENV_VAR
HOST = "https://api.example.com"
environ = {STORAGE_EMULATOR_ENV_VAR: HOST}
with mock.patch("os.environ", environ):
with self.assertRaises(ValueError):
self._make_one()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
frankyn
frankyn suggested changes Nov 30, 2020
View reviewed changes
Comment thread google/cloud/storage/client.py

if os.getenv(STORAGE_EMULATOR_ENV_VAR) is not None:
credentials = AnonymousCredentials()
if project in [_marker, None]:

@frankyn frankyn Nov 30, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is project validation necessary given the super class will fall back to the expected environment variables?

https://googleapis.dev/python/google-cloud-core/latest/client.html#google.cloud.client.ClientWithProject

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@HemangChothani HemangChothani Dec 3, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, when we are passing credentials as a keyword argument and if not passing project then it found the project in fall back but then throw an error about Default credential from here https://github.com/googleapis/google-auth-library-python/blob/bc92abbce5e0a5e7b5de8157e7772db84349a6cb/google/auth/_default.py#L356 when called a method google.auth.default() method because method unable to find credential from the given four options .

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@HemangChothani HemangChothani Dec 14, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think i can remove this condition after merge of cloud-core-51 PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread google/cloud/storage/client.py
if project is _marker:
project = None

if os.getenv(STORAGE_EMULATOR_ENV_VAR) is not None:

@frankyn frankyn Nov 30, 2020

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It would be good to infer this from endpoint value in client_options.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@HemangChothani HemangChothani Dec 3, 2020

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry didn't get the point, few questions regarding that

  • what if client_options didn't pass

  • which value use to compare with kw_args["api_endpoint"]? _DEFAULT_STORAGE_HOST which is https://storage.googleapis.com?

  • Right now first we call the super class of client with credentials and then iterate the client_options and _get_storage_host() for an api_endpoint, should we need to change that order?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
HemangChothani added 2 commits December 3, 2020 17:03
@HemangChothani
feat: changes in unit tests to mock environ
e436dad
@HemangChothani
Merge branch 'master' of https://github.com/googleapis/python-storage …
6915bb4
…into storage_issue_324
@HemangChothani
HemangChothani requested a review from a team December 3, 2020 11:34
@HemangChothani
Merge branch 'master' of https://github.com/googleapis/python-storage …
2390499
…into storage_issue_324
@cojenco

cojenco commented Feb 10, 2022

Copy link
Copy Markdown
Contributor

Replaced by #679

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cojenco cojenco closed this Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

2 more reviewers
@tseaver tseaver tseaver requested changes
@frankyn frankyn frankyn requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

api: storage Issues related to the googleapis/python-storage API. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Avoid Authentication When Connecting to Storage Emulator

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@HemangChothani @cojenco @tseaver @frankyn
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.