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):
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'])

View File

@ -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 '