Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
Expand Down Expand Up @@ -135,11 +135,7 @@ void createScoped_clonesWithScopes() throws IOException {
.setAppIdentityService(appIdentity)
.build();
assertTrue(credentials.createScopedRequired());
try {
credentials.getRequestMetadata(CALL_URI);
fail("Should not be able to use credential without scopes.");
} catch (Exception expected) {
}
assertThrows(IOException.class, () -> credentials.getRequestMetadata(CALL_URI));
assertEquals(0, appIdentity.getGetAccessTokenCallCount());

GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
Expand Down
10 changes: 3 additions & 7 deletions oauth2_http/javatests/com/google/auth/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -90,15 +90,11 @@ private static boolean hasBearerToken(Map<String, List<String>> metadata, String
public static InputStream jsonToInputStream(GenericJson json) throws IOException {
json.setFactory(JSON_FACTORY);
String text = json.toPrettyString();
return new ByteArrayInputStream(text.getBytes("UTF-8"));
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
}

public static InputStream stringToInputStream(String text) {
try {
return new ByteArrayInputStream(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unexpected encoding exception", e);
}
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
}

public static Map<String, String> parseQuery(String query) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void initialize_populatesOAuth2Credentials() throws IOException {

HttpHeaders requestHeaders = request.getHeaders();
String authorizationHeader = requestHeaders.getAuthorization();
assertEquals(authorizationHeader, expectedAuthorization);
assertEquals(expectedAuthorization, authorizationHeader);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;
import java.util.List;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -70,7 +69,7 @@ public InputStream getErrorStream() {
}

@Override
public int waitFor() throws InterruptedException {
public int waitFor() {
return 0;
}

Expand All @@ -83,7 +82,9 @@ public int exitValue() {
}

@Override
public void destroy() {}
public void destroy() {
// Nothing was initialized and nothing needs to be destroyed
}
}

static class TestProcessProvider implements SecureConnectProvider.ProcessProvider {
Expand All @@ -102,19 +103,22 @@ public Process createProcess(InputStream metadata) throws IOException {

@Test
void testGetKeyStoreNonZeroExitCode() {
InputStream metadata =
try (InputStream metadata =
this.getClass()
.getClassLoader()
.getResourceAsStream("com/google/api/gax/rpc/mtls/mtlsCertAndKey.pem");
IOException actual =
assertThrows(
IOException.class,
() -> SecureConnectProvider.getKeyStore(metadata, new TestProcessProvider(1)));
assertTrue(
actual
.getMessage()
.contains("SecureConnect: Cert provider command failed with exit code: 1"),
"expected to fail with nonzero exit code");
.getResourceAsStream("com/google/api/gax/rpc/mtls/mtlsCertAndKey.pem")) {
IOException actual =
assertThrows(
IOException.class,
() -> SecureConnectProvider.getKeyStore(metadata, new TestProcessProvider(1)));
assertTrue(
actual
.getMessage()
.contains("SecureConnect: Cert provider command failed with exit code: 1"),
"expected to fail with nonzero exit code");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test
Expand Down Expand Up @@ -147,8 +151,7 @@ void testRunCertificateProviderCommandTimeout() {
}

@Test
void testGetKeyStore_FileNotFoundException()
throws IOException, GeneralSecurityException, InterruptedException {
void testGetKeyStore_FileNotFoundException() {
SecureConnectProvider provider =
new SecureConnectProvider(new TestProcessProvider(0), "/invalid/metadata/path.json");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,71 +58,77 @@ void workloadCertificateConfig_fromStream_Succeeds() throws IOException {
void workloadCertificateConfig_fromStreamMissingCertPath_Fails() throws IOException {
String certPath = "";
String privateKeyPath = "key.crt";
InputStream configStream = writeWorkloadCertificateConfigStream(certPath, privateKeyPath);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
assertTrue(
exception
.getMessage()
.contains(
"The cert_path field must be provided in the workload certificate configuration."));
try (InputStream configStream =
writeWorkloadCertificateConfigStream(certPath, privateKeyPath)) {
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
configStream));
assertTrue(
exception
.getMessage()
.contains(
"The cert_path field must be provided in the workload certificate configuration."));
}
}

@Test
void workloadCertificateConfig_fromStreamMissingPrivateKeyPath_Fails() throws IOException {
String certPath = "cert.crt";
String privateKeyPath = "";
InputStream configStream = writeWorkloadCertificateConfigStream(certPath, privateKeyPath);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
assertTrue(
exception
.getMessage()
.contains(
"The key_path field must be provided in the workload certificate configuration."));
try (InputStream configStream =
writeWorkloadCertificateConfigStream(certPath, privateKeyPath)) {
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
configStream));
assertTrue(
exception
.getMessage()
.contains(
"The key_path field must be provided in the workload certificate configuration."));
}
}

@Test
void workloadCertificateConfig_fromStreamMissingWorkload_Fails() throws IOException {
GenericJson json = new GenericJson();
json.put("cert_configs", new GenericJson());
InputStream configStream = TestUtils.jsonToInputStream(json);

CertificateSourceUnavailableException exception =
assertThrows(
CertificateSourceUnavailableException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
assertTrue(
exception
.getMessage()
.contains(
"A workload certificate configuration must be provided in the cert_configs object."));
try (InputStream configStream = TestUtils.jsonToInputStream(json)) {
CertificateSourceUnavailableException exception =
assertThrows(
CertificateSourceUnavailableException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
configStream));
assertTrue(
exception
.getMessage()
.contains(
"A workload certificate configuration must be provided in the cert_configs object."));
}
}

@Test
void workloadCertificateConfig_fromStreamMissingCertConfig_Fails() throws IOException {
GenericJson json = new GenericJson();
InputStream configStream = TestUtils.jsonToInputStream(json);

IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(configStream));
assertTrue(
exception
.getMessage()
.contains(
"The cert_configs object must be provided in the certificate configuration file."));
try (InputStream configStream = TestUtils.jsonToInputStream(json)) {
IllegalArgumentException exception =
assertThrows(
IllegalArgumentException.class,
() ->
WorkloadCertificateConfiguration.fromCertificateConfigurationStream(
configStream));
assertTrue(
exception
.getMessage()
.contains(
"The cert_configs object must be provided in the certificate configuration file."));
}
}

static InputStream writeWorkloadCertificateConfigStream(String certPath, String privateKeyPath)
Expand Down
72 changes: 36 additions & 36 deletions oauth2_http/javatests/com/google/auth/mtls/X509ProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,24 @@ void x509Provider_succeeds_withEnvVariable()
String certConfigPath = "certConfig.txt";
String certPath = "cert.crt";
String keyPath = "key.crt";
InputStream certConfigStream =
try (InputStream certConfigStream =
WorkloadCertificateConfigurationTest.writeWorkloadCertificateConfigStream(
certPath, keyPath);

TestX509Provider testProvider = new TestX509Provider();
testProvider.setEnv("GOOGLE_API_CERTIFICATE_CONFIG", certConfigPath);
testProvider.addFile(certConfigPath, certConfigStream);
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));

CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate expectedCert =
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));

// Assert that the store has the expected certificate and only the expected certificate.
KeyStore store = testProvider.getKeyStore();
assertEquals(1, store.size());
assertNotNull(store.getCertificateAlias(expectedCert));
certPath, keyPath)) {
TestX509Provider testProvider = new TestX509Provider();
testProvider.setEnv(certConfigPath);
testProvider.addFile(certConfigPath, certConfigStream);
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));

CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate expectedCert =
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));

// Assert that the store has the expected certificate and only the expected certificate.
KeyStore store = testProvider.getKeyStore();
assertEquals(1, store.size());
assertNotNull(store.getCertificateAlias(expectedCert));
}
}

@Test
Expand All @@ -166,24 +166,24 @@ void x509Provider_succeeds_withWellKnownPath()
String certConfigPath = "certConfig.txt";
String certPath = "cert.crt";
String keyPath = "key.crt";
InputStream certConfigStream =
try (InputStream certConfigStream =
WorkloadCertificateConfigurationTest.writeWorkloadCertificateConfigStream(
certPath, keyPath);

TestX509Provider testProvider = new TestX509Provider();
testProvider.setEnv("GOOGLE_API_CERTIFICATE_CONFIG", certConfigPath);
testProvider.addFile(certConfigPath, certConfigStream);
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));

CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate expectedCert =
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));

// Assert that the store has the expected certificate and only the expected certificate.
KeyStore store = testProvider.getKeyStore();
assertEquals(1, store.size());
assertNotNull(store.getCertificateAlias(expectedCert));
certPath, keyPath)) {
TestX509Provider testProvider = new TestX509Provider();
testProvider.setEnv(certConfigPath);
testProvider.addFile(certConfigPath, certConfigStream);
testProvider.addFile(certPath, new ByteArrayInputStream(TEST_CERT.getBytes()));
testProvider.addFile(keyPath, new ByteArrayInputStream(TEST_PRIVATE_KEY.getBytes()));

CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate expectedCert =
cf.generateCertificate(new ByteArrayInputStream(TEST_CERT.getBytes()));

// Assert that the store has the expected certificate and only the expected certificate.
KeyStore store = testProvider.getKeyStore();
assertEquals(1, store.size());
assertNotNull(store.getCertificateAlias(expectedCert));
}
}

static class TestX509Provider extends X509Provider {
Expand Down Expand Up @@ -211,8 +211,8 @@ String getEnv(String name) {
return variables.get(name);
}

void setEnv(String name, String value) {
variables.put(name, value);
void setEnv(String value) {
variables.put("GOOGLE_API_CERTIFICATE_CONFIG", value);
}

@Override
Expand Down
Loading
Loading