Skip to content

Commit 578ff7f

Browse files
Jon Wayne Parrottdhermes
authored andcommitted
Apply scopes to explicitly provided credentials if needed (googleapis#4594)
1 parent 235ac55 commit 578ff7f

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

api_core/google/api_core/grpc_helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from google.api_core import exceptions
2121
from google.api_core import general_helpers
2222
import google.auth
23+
import google.auth.credentials
2324
import google.auth.transport.grpc
2425
import google.auth.transport.requests
2526

@@ -127,6 +128,9 @@ def create_channel(target, credentials=None, scopes=None, **kwargs):
127128
"""
128129
if credentials is None:
129130
credentials, _ = google.auth.default(scopes=scopes)
131+
else:
132+
credentials = google.auth.credentials.with_scopes_if_required(
133+
credentials, scopes)
130134

131135
request = google.auth.transport.requests.Request()
132136

api_core/tests/unit/test_grpc_helpers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from google.api_core import exceptions
2020
from google.api_core import grpc_helpers
21+
import google.auth.credentials
2122

2223

2324
def test__patch_callable_name():
@@ -169,3 +170,19 @@ def test_create_channel_explicit(secure_authorized_channel):
169170
assert channel is secure_authorized_channel.return_value
170171
secure_authorized_channel.assert_called_once_with(
171172
mock.sentinel.credentials, mock.ANY, target)
173+
174+
175+
@mock.patch('google.auth.transport.grpc.secure_authorized_channel')
176+
def test_create_channel_explicit_scoped(unused_secure_authorized_channel):
177+
scopes = ['1', '2']
178+
179+
credentials = mock.create_autospec(
180+
google.auth.credentials.Scoped, instance=True)
181+
credentials.requires_scopes = True
182+
183+
grpc_helpers.create_channel(
184+
mock.sentinel.target,
185+
credentials=credentials,
186+
scopes=scopes)
187+
188+
credentials.with_scopes.assert_called_once_with(scopes)

0 commit comments

Comments
 (0)