Skip to content

Commit 038ae1b

Browse files
fix: migrate signBlob to iamcredentials.googleapis.com (#553)
* Migrate signBlob from iam.googleapis.com to iamcredentials.googleapis.com. This API is deprecated and will be shutdown in one year. This is used google.auth.iam.Signer. Added a system_test to sanity check the implementation.
1 parent 8c84d0f commit 038ae1b

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ pylintrc.test
3939
pytype_output/
4040

4141
.python-version
42+
.DS_Store
43+
cert_path
44+
key_path

google/auth/iam.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from google.auth import crypt
2929
from google.auth import exceptions
3030

31-
_IAM_API_ROOT_URI = "https://iam.googleapis.com/v1"
31+
_IAM_API_ROOT_URI = "https://iamcredentials.googleapis.com/v1"
3232
_SIGN_BLOB_URI = _IAM_API_ROOT_URI + "/projects/-/serviceAccounts/{}:signBlob?alt=json"
3333

3434

@@ -71,7 +71,7 @@ def _make_signing_request(self, message):
7171
url = _SIGN_BLOB_URI.format(self._service_account_email)
7272
headers = {}
7373
body = json.dumps(
74-
{"bytesToSign": base64.b64encode(message).decode("utf-8")}
74+
{"payload": base64.b64encode(message).decode("utf-8")}
7575
).encode("utf-8")
7676

7777
self._credentials.before_request(self._request, method, url, headers)
@@ -97,4 +97,4 @@ def key_id(self):
9797
@_helpers.copy_docstring(crypt.Signer)
9898
def sign(self, message):
9999
response = self._make_signing_request(message)
100-
return base64.b64decode(response["signature"])
100+
return base64.b64decode(response["signedBlob"])

system_tests/test_service_account.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from google.auth import _helpers
1818
from google.auth import exceptions
19+
from google.auth import iam
1920
from google.oauth2 import service_account
2021

2122

@@ -46,3 +47,19 @@ def test_refresh_success(http_request, credentials, token_info):
4647
"https://www.googleapis.com/auth/userinfo.profile",
4748
]
4849
)
50+
51+
def test_iam_signer(http_request, credentials):
52+
credentials = credentials.with_scopes(
53+
["https://www.googleapis.com/auth/iam"]
54+
)
55+
56+
# Verify iamcredentials signer.
57+
signer = iam.Signer(
58+
http_request,
59+
credentials,
60+
credentials.service_account_email
61+
)
62+
63+
signed_blob = signer.sign("message")
64+
65+
assert isinstance(signed_blob, bytes)

tests/compute_engine/test_credentials.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ def test_with_target_audience_integration(self):
354354
signature = base64.b64encode(b"some-signature").decode("utf-8")
355355
responses.add(
356356
responses.POST,
357-
"https://iam.googleapis.com/v1/projects/-/serviceAccounts/"
358-
"service-account@example.com:signBlob?alt=json",
357+
"https://iamcredentials.googleapis.com/v1/projects/-/"
358+
"serviceAccounts/service-account@example.com:signBlob?alt=json",
359359
status=200,
360360
content_type="application/json",
361-
json={"keyId": "some-key-id", "signature": signature},
361+
json={"keyId": "some-key-id", "signedBlob": signature},
362362
)
363363

364364
id_token = "{}.{}.{}".format(

tests/test_iam.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def test_key_id(self):
7878
def test_sign_bytes(self):
7979
signature = b"DEADBEEF"
8080
encoded_signature = base64.b64encode(signature).decode("utf-8")
81-
request = make_request(http_client.OK, data={"signature": encoded_signature})
81+
request = make_request(http_client.OK, data={"signedBlob": encoded_signature})
8282
credentials = make_credentials()
8383

8484
signer = iam.Signer(request, credentials, mock.sentinel.service_account_email)

0 commit comments

Comments
 (0)