Skip to content

Commit

Permalink
fix: throw SigningException as documented (#316)
Browse files Browse the repository at this point in the history
* throw SigningException as promised

* fix up inner class

* format

* add todo comment

* spot the diff

* restore test
  • Loading branch information
elharo authored and chingor13 committed Jul 31, 2019
1 parent c524252 commit a1ab97c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ public static Builder newBuilder() {
return new Builder();
}

/**
* Returns the email address associated with the GCE default service account.
*
* @throws RuntimeException if the default service account cannot be read
*/
@Override
// todo(#314) getAccount should not throw a RuntimeException
public String getAccount() {
if (serviceAccountEmail == null) {
try {
Expand All @@ -304,12 +310,15 @@ public String getAccount() {
*/
@Override
public byte[] sign(byte[] toSign) {
return IamUtils.sign(
getAccount(),
this,
transportFactory.create(),
toSign,
Collections.<String, Object>emptyMap());
try {
String account = getAccount();
return IamUtils.sign(
account, this, transportFactory.create(), toSign, Collections.<String, Object>emptyMap());
} catch (SigningException ex) {
throw ex;
} catch (RuntimeException ex) {
throw new SigningException("Signing failed", ex);
}
}

private String getDefaultServiceAccount() throws IOException {
Expand Down
1 change: 1 addition & 0 deletions oauth2_http/java/com/google/auth/oauth2/IamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class IamUtils {
* @param toSign bytes to sign
* @param additionalFields additional fields to send in the IAM call
* @return signed bytes
* @throws ServiceAccountSigner.SigningException if signing fails
*/
static byte[] sign(
String serviceAccountEmail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,26 @@ public void sign_sameAs() throws IOException {
assertArrayEquals(expectedSignature, credentials.sign(expectedSignature));
}

@Test
public void sign_getAccountFails() throws IOException {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2";
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};

transportFactory.transport.setAccessToken(accessToken);
transportFactory.transport.setSignature(expectedSignature);
ComputeEngineCredentials credentials =
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();

try {
credentials.sign(expectedSignature);
fail();
} catch (SigningException ex) {
assertNotNull(ex.getMessage());
assertNotNull(ex.getCause());
}
}

@Test
public void sign_accessDenied_throws() {
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
Expand Down

0 comments on commit a1ab97c

Please sign in to comment.