Skip to content

Commit fee0a15

Browse files
nfeltwchargin
authored andcommitted
Configure oauth for uploader client and make it required (#2836)
* Remove --auth_type argument and make use of OAuth creds unconditional * Add OAuth client configuration
1 parent b9ea81e commit fee0a15

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

tensorboard/uploader/auth.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,32 @@
4646
)
4747

4848

49+
# The client "secret" is public by design for installed apps. See
50+
# https://developers.google.com/identity/protocols/OAuth2?csw=1#installed
51+
OAUTH_CLIENT_CONFIG = b"""
52+
{
53+
"installed": {
54+
"client_id": "373649185512-8v619h5kft38l4456nm2dj4ubeqsrvh6.apps.googleusercontent.com",
55+
"project_id": "hosted-tensorboard-prod",
56+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
57+
"token_uri": "https://oauth2.googleapis.com/token",
58+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
59+
"client_secret": "pOyAuU2yq2arsM98Bw5hwYtr",
60+
"redirect_uris": [
61+
"urn:ietf:wg:oauth:2.0:oob",
62+
"http://localhost"
63+
]
64+
}
65+
}
66+
"""
67+
68+
4969
# Components of the relative path (within the user settings directory) at which
5070
# to store TensorBoard uploader credentials.
5171
TENSORBOARD_CREDENTIALS_FILEPATH_PARTS = [
5272
"tensorboard", "credentials", "uploader-creds.json"]
5373

5474

55-
def application_default_credentials():
56-
"""Returns the active Application Default Credentials.
57-
58-
Returns:
59-
google.auth.credentials.Credentials: the current credentials.
60-
61-
Raises:
62-
google.auth.exceptions.DefaultCredentialsError:
63-
If no credentials were found, or if the credentials found were invalid.
64-
"""
65-
credentials, project_id = google.auth.default(scopes=OPENID_CONNECT_SCOPES)
66-
del project_id # unused
67-
return credentials
68-
69-
7075
class CredentialsStore(object):
7176
"""Private file store for a `google.oauth2.credentials.Credentials`."""
7277

tensorboard/uploader/uploader_main.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ def _define_flags(parser):
101101
choices=('local', 'ssl', 'ssl_dev'),
102102
help='The type of credentials to use for the gRPC client')
103103

104-
parser.add_argument(
105-
'--auth_type',
106-
type=str,
107-
default='user',
108-
choices=('adc', 'user', 'none'),
109-
help='The type of auth credentials to obtain and add to requests.')
110-
111104
parser.add_argument(
112105
'--auth_force_console',
113106
action='store_true',
@@ -201,6 +194,15 @@ def _run(flags):
201194
sys.stderr.write('Logged out of uploader.\n')
202195
sys.stderr.flush()
203196
return
197+
# TODO(b/141723268): maybe reconfirm Google Account prior to reuse.
198+
credentials = store.read_credentials()
199+
if not credentials:
200+
_prompt_for_user_ack(intent)
201+
client_config = json.loads(auth.OAUTH_CLIENT_CONFIG)
202+
flow = auth.build_installed_app_flow(client_config)
203+
credentials = flow.run(force_console=flags.auth_force_console)
204+
sys.stderr.write('\n') # Extra newline after auth flow messages.
205+
store.write_credentials(credentials)
204206

205207
channel_options = None
206208
if flags.grpc_creds_type == 'local':
@@ -214,27 +216,13 @@ def _run(flags):
214216
msg = 'Invalid --grpc_creds_type %s' % flags.grpc_creds_type
215217
raise base_plugin.FlagsError(msg)
216218

217-
if flags.auth_type != 'none':
218-
if flags.auth_type == 'user':
219-
# TODO(b/141723268): determine if we should reconfirm the intended Google
220-
# Account used for uploading prior to reusing the stored credentials.
221-
credentials = store.read_credentials()
222-
if not credentials:
223-
_prompt_for_user_ack(intent)
224-
client_config = json.loads(dev_creds.DEV_OAUTH_CLIENT_CONFIG)
225-
flow = auth.build_installed_app_flow(client_config)
226-
credentials = flow.run(force_console=flags.auth_force_console)
227-
sys.stderr.write('\n') # Extra newline after auth flow messages.
228-
store.write_credentials(credentials)
229-
elif flags.auth_type == 'adc':
230-
credentials = auth.application_default_credentials()
231-
channel_creds = grpc.composite_channel_credentials(
232-
channel_creds, auth.id_token_call_credentials(credentials))
219+
composite_channel_creds = grpc.composite_channel_credentials(
220+
channel_creds, auth.id_token_call_credentials(credentials))
233221

234222
# TODO(@nfelt): In the `_UploadIntent` case, consider waiting until
235223
# logdir exists to open channel.
236224
channel = grpc.secure_channel(
237-
flags.endpoint, channel_creds, options=channel_options)
225+
flags.endpoint, composite_channel_creds, options=channel_options)
238226
with channel:
239227
intent.execute(channel)
240228

0 commit comments

Comments
 (0)