From 9f0d22d67874a83306f634f6f18cca0c5077b544 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Fri, 19 Feb 2021 08:43:10 -0800 Subject: [PATCH] GCS logs: more robust Credential class Google keeps changing the with_scopes method signature. We need to override that method in order to keep our path attribute around when it re-instantiates the object. As long as we always call refresh after the object is created, we can drop our custom constructor and then change with_scopes to use generic arguments. This should keep up with any further minor API changes without needing further local changes. Also, a minor inconsistency in command line arguments (used only for local manual testing) is corrected. Change-Id: Id41dfc464eb86429771a78c1fefae006e0915cb9 --- .../test_zuul_google_storage_upload.py | 3 ++- .../library/zuul_google_storage_upload.py | 20 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/roles/upload-logs-base/library/test_zuul_google_storage_upload.py b/roles/upload-logs-base/library/test_zuul_google_storage_upload.py index 351e37203..d0ce605ef 100644 --- a/roles/upload-logs-base/library/test_zuul_google_storage_upload.py +++ b/roles/upload-logs-base/library/test_zuul_google_storage_upload.py @@ -33,6 +33,7 @@ class TestCredential(testtools.TestCase): def test_credential(self): path = os.path.join(FIXTURE_DIR, 'gcs', 'auth.json') headers = {} - c = Credentials(path) + c = Credentials() + c._set_path(path) c.before_request(None, None, None, headers) self.assertEqual("Bearer something", headers['authorization']) diff --git a/roles/upload-logs-base/library/zuul_google_storage_upload.py b/roles/upload-logs-base/library/zuul_google_storage_upload.py index 0536a9cc9..91bdf4e2f 100755 --- a/roles/upload-logs-base/library/zuul_google_storage_upload.py +++ b/roles/upload-logs-base/library/zuul_google_storage_upload.py @@ -66,8 +66,8 @@ MAX_UPLOAD_THREADS = 24 class Credentials(gce_cred.Credentials): - def __init__(self, path, *args, **kw): - super(Credentials, self).__init__(*args, **kw) + def _set_path(self, path): + """Call this after initialization""" self._path = path self.refresh(None) @@ -78,13 +78,10 @@ class Credentials(gce_cred.Credentials): self.expiry = (datetime.datetime.utcnow() + datetime.timedelta(seconds=data['expires_in'])) - def with_scopes(self, scopes): - return self.__class__( - path=self._path, - scopes=scopes, - service_account_email=self._service_account_email, - quota_project_id=self._quota_project_id, - ) + def with_scopes(self, *args, **kw): + ret = super(Credentials, self).with_scopes(*args, **kw) + ret._set_path(self._path) + return ret class Uploader(): @@ -206,7 +203,8 @@ def run(container, files, project=None): if credentials_file: - cred = Credentials(credentials_file) + cred = Credentials() + cred._set_path(credentials_file) client = storage.Client(credentials=cred, project=project) else: client = storage.Client() @@ -299,7 +297,7 @@ def cli_main(): parser.add_argument('--dry-run', action='store_true', help='do not attempt to create containers or upload, ' 'useful with --verbose for debugging') - parser.add_argument('--credentials_file', + parser.add_argument('--credentials-file', help='A file with Google Cloud credentials') parser.add_argument('--project', help='Name of the Google Cloud project (required for '