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
This commit is contained in:
James E. Blair 2021-02-19 08:43:10 -08:00
parent 4170cedf60
commit 9f0d22d678
2 changed files with 11 additions and 12 deletions

View File

@ -33,6 +33,7 @@ class TestCredential(testtools.TestCase):
def test_credential(self): def test_credential(self):
path = os.path.join(FIXTURE_DIR, 'gcs', 'auth.json') path = os.path.join(FIXTURE_DIR, 'gcs', 'auth.json')
headers = {} headers = {}
c = Credentials(path) c = Credentials()
c._set_path(path)
c.before_request(None, None, None, headers) c.before_request(None, None, None, headers)
self.assertEqual("Bearer something", headers['authorization']) self.assertEqual("Bearer something", headers['authorization'])

View File

@ -66,8 +66,8 @@ MAX_UPLOAD_THREADS = 24
class Credentials(gce_cred.Credentials): class Credentials(gce_cred.Credentials):
def __init__(self, path, *args, **kw): def _set_path(self, path):
super(Credentials, self).__init__(*args, **kw) """Call this after initialization"""
self._path = path self._path = path
self.refresh(None) self.refresh(None)
@ -78,13 +78,10 @@ class Credentials(gce_cred.Credentials):
self.expiry = (datetime.datetime.utcnow() + self.expiry = (datetime.datetime.utcnow() +
datetime.timedelta(seconds=data['expires_in'])) datetime.timedelta(seconds=data['expires_in']))
def with_scopes(self, scopes): def with_scopes(self, *args, **kw):
return self.__class__( ret = super(Credentials, self).with_scopes(*args, **kw)
path=self._path, ret._set_path(self._path)
scopes=scopes, return ret
service_account_email=self._service_account_email,
quota_project_id=self._quota_project_id,
)
class Uploader(): class Uploader():
@ -206,7 +203,8 @@ def run(container, files,
project=None): project=None):
if credentials_file: if credentials_file:
cred = Credentials(credentials_file) cred = Credentials()
cred._set_path(credentials_file)
client = storage.Client(credentials=cred, project=project) client = storage.Client(credentials=cred, project=project)
else: else:
client = storage.Client() client = storage.Client()
@ -299,7 +297,7 @@ def cli_main():
parser.add_argument('--dry-run', action='store_true', parser.add_argument('--dry-run', action='store_true',
help='do not attempt to create containers or upload, ' help='do not attempt to create containers or upload, '
'useful with --verbose for debugging') 'useful with --verbose for debugging')
parser.add_argument('--credentials_file', parser.add_argument('--credentials-file',
help='A file with Google Cloud credentials') help='A file with Google Cloud credentials')
parser.add_argument('--project', parser.add_argument('--project',
help='Name of the Google Cloud project (required for ' help='Name of the Google Cloud project (required for '