3131import io .opentelemetry .proto .common .v1 .KeyValue ;
3232import io .opentelemetry .proto .trace .v1 .ResourceSpans ;
3333import java .net .URI ;
34+ import java .security .KeyManagementException ;
35+ import java .security .NoSuchAlgorithmException ;
36+ import java .security .cert .X509Certificate ;
3437import java .util .Arrays ;
3538import java .util .List ;
3639import java .util .Optional ;
3740import java .util .concurrent .TimeUnit ;
3841import 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 ;
3946import org .junit .jupiter .api .AfterAll ;
4047import org .junit .jupiter .api .BeforeAll ;
4148import 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 ->
0 commit comments