Skip to content

Commit b5d43c9

Browse files
committed
fix exception in ComputeEngineCredentials, add test. Edit exception tests to use assertThrows.
1 parent 424104b commit b5d43c9

File tree

3 files changed

+58
-55
lines changed

3 files changed

+58
-55
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,10 +609,8 @@ public byte[] sign(byte[] toSign) {
609609
throw ex;
610610
} catch (RuntimeException ex) {
611611
throw new SigningException("Signing failed", ex);
612-
} catch (IOException e) {
613-
// Throwing an IOException would be a breaking change, so wrap it here.
614-
// This should not happen for this credential type.
615-
throw new IllegalStateException("Failed to get universe domain", e);
612+
} catch (IOException ex) {
613+
throw new SigningException("Failed to sign: Error obtaining universe domain", ex);
616614
}
617615
}
618616

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ private static String getSignature(
114114
HttpRequestFactory factory)
115115
throws IOException {
116116
String signBlobUrl =
117-
String.format(OAuth2Utils.IAM_SIGN_BLOB_ENDPOINT_FORMAT, universeDomain, serviceAccountEmail);
117+
String.format(
118+
OAuth2Utils.IAM_SIGN_BLOB_ENDPOINT_FORMAT, universeDomain, serviceAccountEmail);
118119
GenericUrl genericUrl = new GenericUrl(signBlobUrl);
119120

120121
GenericData signRequest = new GenericData();

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

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import java.util.stream.Collectors;
6767
import java.util.stream.IntStream;
6868
import java.util.stream.Stream;
69+
import org.junit.Assert;
6970
import org.junit.Test;
7071
import org.junit.runner.RunWith;
7172
import org.junit.runners.JUnit4;
@@ -541,7 +542,7 @@ public LowLevelHttpResponse execute() throws IOException {
541542
}
542543

543544
@Test
544-
public void sign_sameAs() throws IOException {
545+
public void sign_sameAs() {
545546
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
546547
String defaultAccountEmail = "[email protected]";
547548
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
@@ -555,21 +556,36 @@ public void sign_sameAs() throws IOException {
555556
}
556557

557558
@Test
558-
public void sign_getAccountFails() throws IOException {
559+
public void sign_getUniverseException() {
560+
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
561+
562+
String defaultAccountEmail = "[email protected]";
563+
transportFactory.transport.setServiceAccountEmail(defaultAccountEmail);
564+
ComputeEngineCredentials credentials =
565+
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
566+
567+
transportFactory.transport.setRequestStatusCode(501);
568+
Assert.assertThrows(IOException.class, credentials::getUniverseDomain);
569+
570+
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
571+
SigningException signingException =
572+
Assert.assertThrows(SigningException.class, () -> credentials.sign(expectedSignature));
573+
assertEquals("Failed to sign: Error obtaining universe domain", signingException.getMessage());
574+
}
575+
576+
@Test
577+
public void sign_getAccountFails() {
559578
MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory();
560579
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
561580

562581
transportFactory.transport.setSignature(expectedSignature);
563582
ComputeEngineCredentials credentials =
564583
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
565584

566-
try {
567-
credentials.sign(expectedSignature);
568-
fail("Should not be able to use credential without exception.");
569-
} catch (SigningException ex) {
570-
assertNotNull(ex.getMessage());
571-
assertNotNull(ex.getCause());
572-
}
585+
SigningException exception =
586+
Assert.assertThrows(SigningException.class, () -> credentials.sign(expectedSignature));
587+
assertNotNull(exception.getMessage());
588+
assertNotNull(exception.getCause());
573589
}
574590

575591
@Test
@@ -601,15 +617,13 @@ public LowLevelHttpResponse execute() throws IOException {
601617
ComputeEngineCredentials credentials =
602618
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
603619

604-
try {
605-
byte[] bytes = {0xD, 0xE, 0xA, 0xD};
606-
credentials.sign(bytes);
607-
fail("Signing should have failed");
608-
} catch (SigningException e) {
609-
assertEquals("Failed to sign the provided bytes", e.getMessage());
610-
assertNotNull(e.getCause());
611-
assertTrue(e.getCause().getMessage().contains("403"));
612-
}
620+
byte[] bytes = {0xD, 0xE, 0xA, 0xD};
621+
622+
SigningException exception =
623+
Assert.assertThrows(SigningException.class, () -> credentials.sign(bytes));
624+
assertEquals("Failed to sign the provided bytes", exception.getMessage());
625+
assertNotNull(exception.getCause());
626+
assertTrue(exception.getCause().getMessage().contains("403"));
613627
}
614628

615629
@Test
@@ -641,15 +655,13 @@ public LowLevelHttpResponse execute() throws IOException {
641655
ComputeEngineCredentials credentials =
642656
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
643657

644-
try {
645-
byte[] bytes = {0xD, 0xE, 0xA, 0xD};
646-
credentials.sign(bytes);
647-
fail("Signing should have failed");
648-
} catch (SigningException e) {
649-
assertEquals("Failed to sign the provided bytes", e.getMessage());
650-
assertNotNull(e.getCause());
651-
assertTrue(e.getCause().getMessage().contains("500"));
652-
}
658+
byte[] bytes = {0xD, 0xE, 0xA, 0xD};
659+
660+
SigningException exception =
661+
Assert.assertThrows(SigningException.class, () -> credentials.sign(bytes));
662+
assertEquals("Failed to sign the provided bytes", exception.getMessage());
663+
assertNotNull(exception.getCause());
664+
assertTrue(exception.getCause().getMessage().contains("500"));
653665
}
654666

655667
@Test
@@ -674,14 +686,11 @@ public LowLevelHttpResponse execute() throws IOException {
674686
ComputeEngineCredentials credentials =
675687
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
676688

677-
try {
678-
credentials.refreshAccessToken();
679-
fail("Should have failed");
680-
} catch (IOException e) {
681-
assertTrue(e.getCause().getMessage().contains("503"));
682-
assertTrue(e instanceof GoogleAuthException);
683-
assertTrue(((GoogleAuthException) e).isRetryable());
684-
}
689+
IOException exception =
690+
Assert.assertThrows(IOException.class, () -> credentials.refreshAccessToken());
691+
assertTrue(exception.getCause().getMessage().contains("503"));
692+
assertTrue(exception instanceof GoogleAuthException);
693+
assertTrue(((GoogleAuthException) exception).isRetryable());
685694
}
686695

687696
@Test
@@ -714,12 +723,9 @@ public LowLevelHttpResponse execute() throws IOException {
714723
ComputeEngineCredentials credentials =
715724
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
716725

717-
try {
718-
credentials.refreshAccessToken();
719-
fail("Should have failed");
720-
} catch (IOException e) {
721-
assertFalse(e instanceof GoogleAuthException);
722-
}
726+
IOException exception =
727+
Assert.assertThrows(IOException.class, () -> credentials.refreshAccessToken());
728+
assertTrue(exception instanceof GoogleAuthException);
723729
}
724730
}
725731

@@ -889,15 +895,13 @@ public LowLevelHttpResponse execute() throws IOException {
889895
ComputeEngineCredentials credentials =
890896
ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
891897

892-
try {
893-
byte[] bytes = {0xD, 0xE, 0xA, 0xD};
894-
credentials.sign(bytes);
895-
fail("Signing should have failed");
896-
} catch (SigningException e) {
897-
assertEquals("Failed to sign the provided bytes", e.getMessage());
898-
assertNotNull(e.getCause());
899-
assertTrue(e.getCause().getMessage().contains("Empty content"));
900-
}
898+
byte[] bytes = {0xD, 0xE, 0xA, 0xD};
899+
900+
SigningException exception =
901+
Assert.assertThrows(SigningException.class, () -> credentials.sign(bytes));
902+
assertEquals("Failed to sign the provided bytes", exception.getMessage());
903+
assertNotNull(exception.getCause());
904+
assertTrue(exception.getCause().getMessage().contains("Empty content"));
901905
}
902906

903907
@Test

0 commit comments

Comments
 (0)