Skip to content

Commit a267bbb

Browse files
committed
Make integration tests environment agnostic
1 parent 2cd373e commit a267bbb

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

javaagent-extensions/gcp-auth/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ test {
103103
dependsOn 'copyAgent'
104104
useJUnitPlatform()
105105

106-
environment("GOOGLE_CLOUD_QUOTA_PROJECT", "test-project-id")
106+
def fakeCredsFilePath = project.file("src/test/resources/fakecreds.json").getAbsolutePath()
107+
108+
environment("GOOGLE_CLOUD_QUOTA_PROJECT", "quota-project-id")
109+
environment("GOOGLE_APPLICATION_CREDENTIALS", fakeCredsFilePath.toString())
107110
jvmArgs = [
108111
"-javaagent:${javaAgentJarPath}",
109112
"-Dotel.javaagent.extensions=${authExtensionJarPath}",

javaagent-extensions/gcp-auth/src/test/java/com/google/cloud/opentelemetry/extension/auth/GcpAuthExtensionSmokeTest.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@
3131
import io.opentelemetry.proto.common.v1.KeyValue;
3232
import io.opentelemetry.proto.trace.v1.ResourceSpans;
3333
import java.net.URI;
34+
import java.security.KeyManagementException;
35+
import java.security.NoSuchAlgorithmException;
36+
import java.security.cert.X509Certificate;
3437
import java.util.Arrays;
3538
import java.util.List;
3639
import java.util.Optional;
3740
import java.util.concurrent.TimeUnit;
3841
import java.util.stream.Collectors;
42+
import javax.net.ssl.HttpsURLConnection;
43+
import javax.net.ssl.SSLContext;
44+
import javax.net.ssl.TrustManager;
45+
import javax.net.ssl.X509TrustManager;
3946
import org.junit.jupiter.api.AfterAll;
4047
import org.junit.jupiter.api.BeforeAll;
4148
import org.junit.jupiter.api.Test;
@@ -61,7 +68,7 @@ public class GcpAuthExtensionSmokeTest {
6168

6269
@Autowired private TestRestTemplate template;
6370

64-
// The port at which the backend server will recieve telemetry
71+
// The port at which the backend server will receive telemetry
6572
private static final int EXPORTER_ENDPOINT_PORT = 4318;
6673
// The port at which the mock GCP metadata server will run
6774
private static final int MOCK_GCP_METADATA_PORT = 8090;
@@ -78,12 +85,19 @@ public class GcpAuthExtensionSmokeTest {
7885
private static final String DUMMY_GCP_PROJECT = System.getProperty("google.cloud.project");
7986

8087
@BeforeAll
81-
public static void setup() {
88+
public static void setup() throws NoSuchAlgorithmException, KeyManagementException {
8289
// Set up the mock server to always respond with 200
8390
// Setup proxy host
8491
System.setProperty("http.proxyHost", "localhost");
8592
System.setProperty("http.proxyPort", MOCK_GCP_METADATA_PORT + "");
93+
System.setProperty("https.proxyHost", "localhost");
94+
System.setProperty("https.proxyPort", MOCK_GCP_METADATA_PORT + "");
8695
System.setProperty("http.nonProxyHost", "localhost");
96+
System.setProperty("https.nonProxyHost", "localhost");
97+
98+
// Disable SSL validation for integration test
99+
// The OAuth2 token validation requires SSL validation
100+
disableSSLValidation();
87101

88102
// Set up mock OTLP backend server to which traces will be exported
89103
backendServer = ClientAndServer.startClientAndServer(EXPORTER_ENDPOINT_PORT);
@@ -93,7 +107,19 @@ public static void setup() {
93107
String accessTokenResponse =
94108
"{\"access_token\": \"fake.access_token\",\"expires_in\": 3600, \"token_type\": \"Bearer\"}";
95109
mockGcpMetadataServer = ClientAndServer.startClientAndServer(MOCK_GCP_METADATA_PORT);
96-
MockServerClient mockServerClient = new MockServerClient("localhost", MOCK_GCP_METADATA_PORT);
110+
111+
MockServerClient mockServerClient =
112+
new MockServerClient("localhost", MOCK_GCP_METADATA_PORT).withSecure(true);
113+
114+
// mock the token refresh
115+
mockServerClient
116+
.when(request().withMethod("POST").withPath("/token"))
117+
.respond(
118+
response()
119+
.withStatusCode(200)
120+
.withHeader("Content-Type", "application/json")
121+
.withBody(new JsonBody(accessTokenResponse)));
122+
// mock the gcp metadata server
97123
mockServerClient
98124
.when(
99125
request()
@@ -138,6 +164,33 @@ public void authExtensionSmokeTest() {
138164

139165
// Helper methods
140166

167+
private static void disableSSLValidation()
168+
throws NoSuchAlgorithmException, KeyManagementException {
169+
TrustManager[] trustAllCerts =
170+
new TrustManager[] {
171+
new X509TrustManager() {
172+
@Override
173+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
174+
System.out.println("Reached checkClientTrusted");
175+
}
176+
177+
@Override
178+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
179+
System.out.println("Reached checkServerTrusted");
180+
}
181+
182+
@Override
183+
public X509Certificate[] getAcceptedIssuers() {
184+
System.out.println("No acceptedIssuers");
185+
return null;
186+
}
187+
}
188+
};
189+
SSLContext sc = SSLContext.getInstance("SSL");
190+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
191+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
192+
}
193+
141194
private void verifyResourceAttributes(List<ResourceSpans> extractedResourceSpans) {
142195
extractedResourceSpans.forEach(
143196
resourceSpan ->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "quota-project-id",
4+
"private_key_id": "aljmafmlamlmmasma",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKNwapOQ6rQJHetP\nHRlJBIh1OsOsUBiXb3rXXE3xpWAxAha0MH+UPRblOko+5T2JqIb+xKf9Vi3oTM3t\nKvffaOPtzKXZauscjq6NGzA3LgeiMy6q19pvkUUOlGYK6+Xfl+B7Xw6+hBMkQuGE\nnUS8nkpR5mK4ne7djIyfHFfMu4ptAgMBAAECgYA+s0PPtMq1osG9oi4xoxeAGikf\nJB3eMUptP+2DYW7mRibc+ueYKhB9lhcUoKhlQUhL8bUUFVZYakP8xD21thmQqnC4\nf63asad0ycteJMLb3r+z26LHuCyOdPg1pyLk3oQ32lVQHBCYathRMcVznxOG16VK\nI8BFfstJTaJu0lK/wQJBANYFGusBiZsJQ3utrQMVPpKmloO2++4q1v6ZR4puDQHx\nTjLjAIgrkYfwTJBLBRZxec0E7TmuVQ9uJ+wMu/+7zaUCQQDDf2xMnQqYknJoKGq+\noAnyC66UqWC5xAnQS32mlnJ632JXA0pf9pb1SXAYExB1p9Dfqd3VAwQDwBsDDgP6\nHD8pAkEA0lscNQZC2TaGtKZk2hXkdcH1SKru/g3vWTkRHxfCAznJUaza1fx0wzdG\nGcES1Bdez0tbW4llI5By/skZc2eE3QJAFl6fOskBbGHde3Oce0F+wdZ6XIJhEgCP\niukIcKZoZQzoiMJUoVRrA5gqnmaYDI5uRRl/y57zt6YksR3KcLUIuQJAd242M/WF\n6YAZat3q/wEeETeQq1wrooew+8lHl05/Nt0cCpV48RGEhJ83pzBm3mnwHf8lTBJH\nx6XroMXsmbnsEw==\n-----END PRIVATE KEY-----\n",
6+
"client_email": "[email protected]",
7+
"client_id": "100000000000000000221",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://oauth2.googleapis.com/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/quota-project-id%40appspot.gserviceaccount.com",
12+
"universe_domain": "googleapis.com"
13+
}

0 commit comments

Comments
 (0)