diff --git a/patches/kaggle_secrets.py b/patches/kaggle_secrets.py index 18bd8988..8f0d3eb3 100644 --- a/patches/kaggle_secrets.py +++ b/patches/kaggle_secrets.py @@ -83,7 +83,7 @@ def get_gcloud_credential(self) -> str: def set_gcloud_credentials(self, project=None, account=None): """Set user credentials attached to the current kernel and optionally the project & account name to the `gcloud` CLI. - + Example usage: client = UserSecretsClient() client.set_gcloud_credentials(project="my-gcp-project", account="me@my-org.com") @@ -92,9 +92,10 @@ def set_gcloud_credentials(self, project=None, account=None): """ creds = self.get_gcloud_credential() creds_path = self._write_credentials_file(creds) + self._write_gsutil_credentials_file(creds) subprocess.run(['gcloud', 'config', 'set', 'auth/credential_file_override', creds_path]) - + if project: os.environ['GOOGLE_CLOUD_PROJECT'] = project subprocess.run(['gcloud', 'config', 'set', 'project', project]) @@ -127,7 +128,7 @@ def get_bigquery_access_token(self) -> Tuple[str, Optional[datetime]]: token, expiry = client.get_bigquery_access_token() """ return self._get_access_token(GcpTarget.BIGQUERY) - + def _write_credentials_file(self, credentials) -> str: adc_path = os.path.join(os.environ.get('HOME', '/'), 'gcloud_credential.json') with open(adc_path, 'w') as f: @@ -136,6 +137,17 @@ def _write_credentials_file(self, credentials) -> str: return adc_path + def _write_gsutil_credentials_file(self, credentials) -> str: + import json + creds_dict = json.loads(credentials) + boto_path = os.path.join(os.environ.get('HOME', '/'), '.boto') + with open(boto_path, 'w') as f: + f.write('[Credentials]\n') + f.write(' gs_oauth2_refresh_token = ') + f.write(creds_dict['refresh_token']) + + return boto_path + def _get_gcs_access_token(self) -> Tuple[str, Optional[datetime]]: return self._get_access_token(GcpTarget.GCS) diff --git a/tests/test_user_secrets.py b/tests/test_user_secrets.py index 46dd6d8b..fdaa58b2 100644 --- a/tests/test_user_secrets.py +++ b/tests/test_user_secrets.py @@ -136,7 +136,7 @@ def call_get_secret(): success=False) def test_set_gcloud_credentials_succeeds(self): - secret = '{"client_id":"gcloud","type":"authorized_user"}' + secret = '{"client_id":"gcloud","type":"authorized_user","refresh_token":"refresh_token"}' project = 'foo' account = 'bar'