Skip to content

Commit 0d7f872

Browse files
authored
Store gsutil credentials in .boto file when storing gcloud credentials (#892)
The PR adds a call to _write_gsutil_credentials_file which would store the refresh token from gcloud credentials to ~/.boto file used by gsutil. https://cloud.google.com/storage/docs/boto-gsutil http://b/171747843
1 parent c0f7053 commit 0d7f872

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

patches/kaggle_secrets.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def get_gcloud_credential(self) -> str:
8383

8484
def set_gcloud_credentials(self, project=None, account=None):
8585
"""Set user credentials attached to the current kernel and optionally the project & account name to the `gcloud` CLI.
86-
86+
8787
Example usage:
8888
client = UserSecretsClient()
8989
client.set_gcloud_credentials(project="my-gcp-project", account="[email protected]")
@@ -92,9 +92,10 @@ def set_gcloud_credentials(self, project=None, account=None):
9292
"""
9393
creds = self.get_gcloud_credential()
9494
creds_path = self._write_credentials_file(creds)
95+
self._write_gsutil_credentials_file(creds)
9596

9697
subprocess.run(['gcloud', 'config', 'set', 'auth/credential_file_override', creds_path])
97-
98+
9899
if project:
99100
os.environ['GOOGLE_CLOUD_PROJECT'] = project
100101
subprocess.run(['gcloud', 'config', 'set', 'project', project])
@@ -127,7 +128,7 @@ def get_bigquery_access_token(self) -> Tuple[str, Optional[datetime]]:
127128
token, expiry = client.get_bigquery_access_token()
128129
"""
129130
return self._get_access_token(GcpTarget.BIGQUERY)
130-
131+
131132
def _write_credentials_file(self, credentials) -> str:
132133
adc_path = os.path.join(os.environ.get('HOME', '/'), 'gcloud_credential.json')
133134
with open(adc_path, 'w') as f:
@@ -136,6 +137,17 @@ def _write_credentials_file(self, credentials) -> str:
136137

137138
return adc_path
138139

140+
def _write_gsutil_credentials_file(self, credentials) -> str:
141+
import json
142+
creds_dict = json.loads(credentials)
143+
boto_path = os.path.join(os.environ.get('HOME', '/'), '.boto')
144+
with open(boto_path, 'w') as f:
145+
f.write('[Credentials]\n')
146+
f.write(' gs_oauth2_refresh_token = ')
147+
f.write(creds_dict['refresh_token'])
148+
149+
return boto_path
150+
139151
def _get_gcs_access_token(self) -> Tuple[str, Optional[datetime]]:
140152
return self._get_access_token(GcpTarget.GCS)
141153

tests/test_user_secrets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def call_get_secret():
136136
success=False)
137137

138138
def test_set_gcloud_credentials_succeeds(self):
139-
secret = '{"client_id":"gcloud","type":"authorized_user"}'
139+
secret = '{"client_id":"gcloud","type":"authorized_user","refresh_token":"refresh_token"}'
140140
project = 'foo'
141141
account = 'bar'
142142

0 commit comments

Comments
 (0)