Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit c5c4c1a

Browse files
committed
Change service auth to use JSON files, since p12 keyfiles are not well supported in oauth2client any more.
Note that the integration test still uses the old code, because it runs against //third_party, and oauth2client.service_account didn't support ServiceAccountCredentials yet in that version. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=133660532
1 parent ea06e7e commit c5c4c1a

3 files changed

Lines changed: 58 additions & 21 deletions

File tree

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,16 @@ created in [Google Developers Console](https://console.developers.google.com).
122122
If your application runs on Google Compute Engine,
123123
[metadata service authentication](#Google_Compute_Engine) is an easier option.
124124

125-
The first step for this setup is to create the service account in .p12 format.
125+
The first step for this setup is to create the service account in .json format.
126126
Please see this [page](https://cloud.google.com/storage/docs/authentication?hl=en#generating-a-private-key)
127127
for detailed instructions. If you don't have a Google Cloud Platform project,
128128
you can create one for free on [Google Developers Console](https://console.developers.google.com).
129129

130130
Once you have the service account, please note the service account e-mail,
131131
[project ID and project number](https://developers.google.com/console/help/new/#projectnumber).
132-
Then copy the .p12 file to all the machines that run your application.
132+
Then copy the .json file to all the machines that run your application.
133133

134-
Then, enable the debugger agent in a similary way as described in
135-
the [previous](#Google_Compute_Engine) section:
134+
Then, enable the debugger agent using one of these two options:
136135

137136
_Option A_: add this code to the beginning of your `main()` function:
138137

@@ -144,8 +143,7 @@ try:
144143
enable_service_account_auth=True,
145144
project_id='my-gcp-project-id',
146145
project_number='123456789',
147-
service_account_email='123@developer.gserviceaccount.com',
148-
service_account_p12_file='/opt/cdbg/gcp.p12')
146+
service_account_json_file='/opt/cdbg/gcp.json')
149147
except ImportError:
150148
pass
151149
```
@@ -158,8 +156,7 @@ python \
158156
--enable_service_account_auth=1 \
159157
--project_id=<i>my-gcp-project-id</i> \
160158
--project_number=<i>123456789</i> \
161-
--service_account_email=<i>123@developer.gserviceaccount.com</i> \
162-
--service_account_p12_file=<i>/opt/cdbg/gcp.p12</i> \
159+
--service_account_json_file=<i>/opt/cdbg/gcp.json</i> \
163160
--</b> \
164161
myapp.py
165162
</pre>

src/googleclouddebugger/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,23 @@ def _StartDebugger():
6060
_breakpoints_manager.SetActiveBreakpoints)
6161
_hub_client.on_idle = _breakpoints_manager.CheckBreakpointsExpiration
6262
if _flags.get('enable_service_account_auth') in ('1', 'true', True):
63-
_hub_client.EnableServiceAccountAuth(
64-
_flags['project_id'],
65-
_flags['project_number'],
66-
_flags['service_account_email'],
67-
_flags['service_account_p12_file'])
63+
if _flags.get('service_account_p12_file'):
64+
try:
65+
_hub_client.EnableServiceAccountAuthP12(
66+
_flags['project_id'],
67+
_flags['project_number'],
68+
_flags['service_account_email'],
69+
_flags['service_account_p12_file'])
70+
except NotImplementedError as e:
71+
raise NotImplementedError(
72+
'{0}\nYou must specify project_id, project_number, and '
73+
'service_account_json_file in order to use service account '
74+
'authentication.'.format(e))
75+
else:
76+
_hub_client.EnableServiceAccountAuthJson(
77+
_flags['project_id'],
78+
_flags['project_number'],
79+
_flags['service_account_json_file'])
6880
else:
6981
_hub_client.EnableGceAuth()
7082
_hub_client.InitializeDebuggeeLabels(_flags)

src/googleclouddebugger/gcp_hub_client.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from backoff import Backoff
3434
import httplib2
3535
import oauth2client
36+
from oauth2client import service_account
3637
from oauth2client.contrib.gce import AppAssertionCredentials
3738

3839
import labels
@@ -165,19 +166,46 @@ def InitializeDebuggeeLabels(self, flags):
165166

166167
self._debuggee_labels['projectid'] = self._project_id()
167168

168-
def EnableServiceAccountAuth(self, project_id, project_number,
169-
email, p12_file):
170-
"""Selects to use the service account authentication.
169+
def EnableServiceAccountAuthP12(self, project_id, project_number,
170+
email, p12_file):
171+
"""Selects service account authentication with a p12 file.
171172
173+
Using this function is not recommended. Use EnableServiceAccountAuthJson
174+
for authentication, instead. The p12 file format is no longer recommended.
172175
Args:
173176
project_id: GCP project ID (e.g. myproject).
174177
project_number: numberic GCP project ID (e.g. 72386324623).
175-
email: service account identifier (...@developer.gserviceaccount.com).
176-
p12_file: path to the file with the private key.
178+
email: service account identifier for use with p12_file
179+
(...@developer.gserviceaccount.com).
180+
p12_file: (deprecated) path to an old-style p12 file with the
181+
private key.
182+
Raises:
183+
NotImplementedError indicates that the installed version of oauth2client
184+
does not support using a p12 file.
177185
"""
178-
with open(p12_file, 'rb') as f:
179-
self._credentials = oauth2client.client.SignedJwtAssertionCredentials(
180-
email, f.read(), scope=_CLOUD_PLATFORM_SCOPE)
186+
try:
187+
with open(p12_file, 'rb') as f:
188+
self._credentials = oauth2client.client.SignedJwtAssertionCredentials(
189+
email, f.read(), scope=_CLOUD_PLATFORM_SCOPE)
190+
except AttributeError:
191+
raise NotImplementedError(
192+
'P12 key files are no longer supported. Please use a JSON '
193+
'credentials file instead.')
194+
self._project_id = lambda: project_id
195+
self._project_number = lambda: project_number
196+
197+
def EnableServiceAccountAuthJson(self, project_id, project_number,
198+
auth_json_file):
199+
"""Selects service account authentication using Json credentials.
200+
201+
Args:
202+
project_id: GCP project ID (e.g. myproject).
203+
project_number: numberic GCP project ID (e.g. 72386324623).
204+
auth_json_file: the JSON keyfile
205+
"""
206+
self._credentials = (
207+
service_account.ServiceAccountCredentials
208+
.from_json_keyfile_name(auth_json_file, scopes=_CLOUD_PLATFORM_SCOPE))
181209
self._project_id = lambda: project_id
182210
self._project_number = lambda: project_number
183211

0 commit comments

Comments
 (0)