Skip to content

Commit a1ab97c

Browse files
elharochingor13
authored andcommitted
fix: throw SigningException as documented (#316)
* throw SigningException as promised * fix up inner class * format * add todo comment * spot the diff * restore test
1 parent c524252 commit a1ab97c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,13 @@ public static Builder newBuilder() {
277277
return new Builder();
278278
}
279279

280+
/**
281+
* Returns the email address associated with the GCE default service account.
282+
*
283+
* @throws RuntimeException if the default service account cannot be read
284+
*/
280285
@Override
286+
// todo(#314) getAccount should not throw a RuntimeException
281287
public String getAccount() {
282288
if (serviceAccountEmail == null) {
283289
try {
@@ -304,12 +310,15 @@ public String getAccount() {
304310
*/
305311
@Override
306312
public byte[] sign(byte[] toSign) {
307-
return IamUtils.sign(
308-
getAccount(),
309-
this,
310-
transportFactory.create(),
311-
toSign,
312-
Collections.<String, Object>emptyMap());
313+
try {
314+
String account = getAccount();
315+
return IamUtils.sign(
316+
account, this, transportFactory.create(), toSign, Collections.<String, Object>emptyMap());
317+
} catch (SigningException ex) {
318+
throw ex;
319+
} catch (RuntimeException ex) {
320+
throw new SigningException("Signing failed", ex);
321+
}
313322
}
314323

315324
private String getDefaultServiceAccount() throws IOException {

oauth2_http/java/com/google/auth/oauth2/IamUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class IamUtils {
6666
* @param toSign bytes to sign
6767
* @param additionalFields additional fields to send in the IAM call
6868
* @return signed bytes
69+
* @throws ServiceAccountSigner.SigningException if signing fails
6970
*/
7071
static byte[] sign(
7172
String serviceAccountEmail,

oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,26 @@ public void sign_sameAs() throws IOException {
299299
assertArrayEquals(expectedSignature, credentials.sign(expectedSignature));
300300
}
301301

302+
@Test
303+
public void sign_getAccountFails() throws IOException {
304+
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
305+
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
306+
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
307+
308+
transportFactory.transport.setAccessToken(accessToken);
309+
transportFactory.transport.setSignature(expectedSignature);
310+
ComputeEngineCredentials credentials =
311+
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
312+
313+
try {
314+
credentials.sign(expectedSignature);
315+
fail();
316+
} catch (SigningException ex) {
317+
assertNotNull(ex.getMessage());
318+
assertNotNull(ex.getCause());
319+
}
320+
}
321+
302322
@Test
303323
public void sign_accessDenied_throws() {
304324
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();

0 commit comments

Comments
 (0)