Skip to content
Permalink
Browse files
fix: throw SigningException as documented (#316)
* 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 a1ab97c0da43956a3ca0c112c3eba858be14084f
@@ -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 {
@@ -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 {
@@ -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,
@@ -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();

0 comments on commit a1ab97c

Please sign in to comment.