Skip to content

Commit 718ff6c

Browse files
authored
Changes deprecated oauth2client for google-auth. (#1038)
Oauth2client was previously used in the mfg-inspector output callback for authentication, but the library is now deprecated and replaced by the google-auth library. This removes oauth2client and httplib2 from openhtf's output callbacks and replaces them with google-auth's oauth2 implementation using the Requests transport. PiperOrigin-RevId: 474878614
1 parent a60148d commit 718ff6c

File tree

3 files changed

+25
-49
lines changed

3 files changed

+25
-49
lines changed

openhtf/output/callbacks/mfg_inspector.py

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
"""Output and/or upload a TestRun or MfgEvent proto for mfg-inspector.com.
2-
"""
1+
"""Output and/or upload a TestRun or MfgEvent proto for mfg-inspector.com."""
32

4-
import json
53
import logging
6-
import threading
74
import time
85
import zlib
96

10-
import httplib2
11-
import oauth2client.client
7+
from google.auth.transport import requests
8+
from google.oauth2 import service_account
129

1310
from openhtf.output import callbacks
1411
from openhtf.output.proto import guzzle_pb2
@@ -26,26 +23,24 @@ class InvalidTestRunError(Exception):
2623
def _send_mfg_inspector_request(envelope_data, credentials, destination_url):
2724
"""Send upload http request. Intended to be run in retry loop."""
2825
logging.info('Uploading result...')
29-
http = httplib2.Http()
3026

31-
if credentials.access_token_expired:
32-
credentials.refresh(http)
33-
credentials.authorize(http)
34-
35-
resp, content = http.request(destination_url, 'POST', envelope_data)
27+
with requests.AuthorizedSession(credentials) as authed_session:
28+
response = authed_session.request(
29+
'POST', destination_url, data=envelope_data)
3630

3731
try:
38-
result = json.loads(content)
32+
result = response.json()
3933
except Exception:
40-
logging.warning('Upload failed with response %s: %s', resp, content)
41-
raise UploadFailedError(resp, content)
34+
logging.warning('Upload failed with response %s: %s', response,
35+
response.text)
36+
raise UploadFailedError(response, response.text)
4237

43-
if resp.status == 200:
38+
if response.status_code == 200:
4439
return result
4540

4641
message = '%s: %s' % (result.get('error',
4742
'UNKNOWN_ERROR'), result.get('message'))
48-
if resp.status == 400:
43+
if response.status_code == 400:
4944
raise InvalidTestRunError(message)
5045
else:
5146
raise UploadFailedError(message)
@@ -73,26 +68,6 @@ def send_mfg_inspector_data(inspector_proto, credentials, destination_url,
7368
return {}
7469

7570

76-
class _MemStorage(oauth2client.client.Storage):
77-
"""Helper Storage class that keeps credentials in memory."""
78-
79-
def __init__(self):
80-
self._lock = threading.Lock()
81-
self._credentials = None
82-
83-
def acquire_lock(self):
84-
self._lock.acquire(True)
85-
86-
def release_lock(self):
87-
self._lock.release()
88-
89-
def locked_get(self):
90-
return self._credentials
91-
92-
def locked_put(self, credentials):
93-
self._credentials = credentials
94-
95-
9671
class MfgInspector(object):
9772
"""Interface to convert a TestRun to a mfg-inspector compatible proto.
9873
@@ -146,14 +121,14 @@ def __init__(self,
146121
self.destination_url = destination_url
147122

148123
if user and keydata:
149-
self.credentials = oauth2client.client.SignedJwtAssertionCredentials(
150-
service_account_name=self.user,
151-
private_key=(self.keydata.encode()
152-
if isinstance(self.keydata, str) else self.keydata),
153-
scope=self.SCOPE_CODE_URI,
154-
user_agent='OpenHTF Guzzle Upload Client',
155-
token_uri=self.token_uri)
156-
self.credentials.set_store(_MemStorage())
124+
self.credentials = service_account.Credentials.from_service_account_info(
125+
{
126+
'client_email': self.user,
127+
'token_uri': self.token_uri,
128+
'private_key': self.keydata,
129+
'user_agent': 'OpenHTF Guzzle Upload Client',
130+
},
131+
scopes=[self.SCOPE_CODE_URI])
157132
else:
158133
self.credentials = None
159134

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ def run(self):
132132
'attrs>=19.3.0',
133133
'colorama>=0.3.9,<1.0',
134134
'contextlib2>=0.5.1,<1.0',
135+
'google-auth>=1.34.0',
135136
'inflection',
136137
'mutablerecords>=0.4.1,<2.0',
137-
'oauth2client>=1.5.2,<2.0',
138138
'protobuf>=3.6.0,<4.0',
139139
'PyYAML>=3.13',
140140
'pyOpenSSL>=17.1.0,<23',
141+
'requests>=2.27.1',
141142
'sockjs-tornado>=1.0.3,<2.0',
142143
'tornado>=4.3,<5.0',
143144
'typing-extensions',

test/output/callbacks/mfg_inspector_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class TestMfgInspector(test.TestCase):
4848

4949
def setUp(self):
5050
super(TestMfgInspector, self).setUp()
51-
self.mock_credentials = mock.patch(
52-
'oauth2client.client.SignedJwtAssertionCredentials').start(
53-
).return_value
51+
self.mock_credentials = mock.patch.object(
52+
mfg_inspector.service_account.Credentials,
53+
'from_service_account_info').start().return_value
5454

5555
self.mock_send_mfg_inspector_data = mock.patch.object(
5656
mfg_inspector, 'send_mfg_inspector_data').start()

0 commit comments

Comments
 (0)