Skip to content

Commit

Permalink
Merge branch 'main' into release-please--branches--main
Browse files Browse the repository at this point in the history
  • Loading branch information
arithmetic1728 authored Mar 2, 2023
2 parents 6e863cd + c078a13 commit e54dea5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
22 changes: 9 additions & 13 deletions google/auth/_cloud_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

"""Helpers for reading the Google Cloud SDK's configuration."""

import json
import os
import subprocess

import six

from google.auth import _helpers
from google.auth import environment_vars
from google.auth import exceptions

Expand All @@ -35,7 +35,7 @@
_CLOUD_SDK_POSIX_COMMAND = "gcloud"
_CLOUD_SDK_WINDOWS_COMMAND = "gcloud.cmd"
# The command to get the Cloud SDK configuration
_CLOUD_SDK_CONFIG_COMMAND = ("config", "config-helper", "--format", "json")
_CLOUD_SDK_CONFIG_GET_PROJECT_COMMAND = ("config", "get", "project")
# The command to get google user access token
_CLOUD_SDK_USER_ACCESS_TOKEN_COMMAND = ("auth", "print-access-token")
# Cloud SDK's application-default client ID
Expand Down Expand Up @@ -105,18 +105,14 @@ def get_project_id():
try:
# Ignore the stderr coming from gcloud, so it won't be mixed into the output.
# https://github.com/googleapis/google-auth-library-python/issues/673
output = _run_subprocess_ignore_stderr((command,) + _CLOUD_SDK_CONFIG_COMMAND)
except (subprocess.CalledProcessError, OSError, IOError):
return None

try:
configuration = json.loads(output.decode("utf-8"))
except ValueError:
return None
project = _run_subprocess_ignore_stderr(
(command,) + _CLOUD_SDK_CONFIG_GET_PROJECT_COMMAND
)

try:
return configuration["configuration"]["properties"]["core"]["project"]
except KeyError:
# Turn bytes into a string and remove "\n"
project = _helpers.from_bytes(project).strip()
return project if project else None
except (subprocess.CalledProcessError, OSError, IOError):
return None


Expand Down
19 changes: 0 additions & 19 deletions tests/data/cloud_sdk_config.json

This file was deleted.

13 changes: 2 additions & 11 deletions tests/test__cloud_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@
with io.open(SERVICE_ACCOUNT_FILE, "rb") as fh:
SERVICE_ACCOUNT_FILE_DATA = json.load(fh)

with io.open(os.path.join(DATA_DIR, "cloud_sdk_config.json"), "rb") as fh:
CLOUD_SDK_CONFIG_FILE_DATA = fh.read()


@pytest.mark.parametrize(
"data, expected_project_id",
[
(CLOUD_SDK_CONFIG_FILE_DATA, "example-project"),
(b"I am some bad json", None),
(b"{}", None),
],
[(b"example-project\n", "example-project"), (b"", None)],
)
def test_get_project_id(data, expected_project_id):
check_output_patch = mock.patch(
Expand Down Expand Up @@ -94,9 +87,7 @@ def test__run_subprocess_ignore_stderr():
@mock.patch("os.name", new="nt")
def test_get_project_id_windows():
check_output_patch = mock.patch(
"subprocess.check_output",
autospec=True,
return_value=CLOUD_SDK_CONFIG_FILE_DATA,
"subprocess.check_output", autospec=True, return_value=b"example-project\n"
)

with check_output_patch as check_output:
Expand Down

0 comments on commit e54dea5

Please sign in to comment.