Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## [Unreleased]
### Added
- HTTP logging support for OAuth 2.0 Password Grant authentication, by @HardNorth
### Fixed
- Explicitly disable proxy for OAuth authentication when `rp.oauth.use.proxy=false` to avoid issues when proxy is set through system properties, by @HardNorth

## [5.4.6]
### Added
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies {
implementation "org.aspectj:aspectjweaver:${project.aspectj_version}"

implementation "org.slf4j:slf4j-api:${slf4j_version}"
implementation 'org.apache.commons:commons-lang3:3.18.0'
implementation 'org.apache.commons:commons-lang3:3.19.0'

testImplementation "org.slf4j:jul-to-slf4j:${slf4j_version}"
testImplementation("org.junit.platform:junit-platform-runner:${project.junit_runner_version}") {
Expand All @@ -80,6 +80,7 @@ dependencies {
}
testImplementation 'commons-io:commons-io:2.17.0'
testImplementation 'com.epam.reportportal:agent-java-test-utils:0.1.0'
testImplementation "com.squareup.okhttp3:mockwebserver:${project.okhttp_version}"
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public ListenerParameters() {
this.convertImage = DEFAULT_CONVERT_IMAGE;
this.reportingTimeout = DEFAULT_REPORTING_TIMEOUT;
this.httpLogging = DEFAULT_HTTP_LOGGING;
this.oauthUseProxy = DEFAULT_OAUTH_USE_PROXY;

this.keystoreType = DEFAULT_KEYSTORE_TYPE;
this.truststoreType = DEFAULT_KEYSTORE_TYPE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* Copyright 2019 EPAM Systems
* Copyright 2025 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.service;

import jakarta.annotation.Nonnull;
Expand All @@ -27,16 +28,16 @@
*/
public class BearerAuthInterceptor implements Interceptor {

private final String apiKey;
private final String authHeaderValue;

public BearerAuthInterceptor(String apiKey) {
this.apiKey = apiKey;
this.authHeaderValue = "Bearer " + apiKey;
}

@Override
@Nonnull
public Response intercept(Chain chain) throws IOException {
Request rq = chain.request().newBuilder().addHeader("Authorization", "Bearer " + apiKey).build();
Request rq = chain.request().newBuilder().addHeader("Authorization", authHeaderValue).build();
return chain.proceed(rq);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -101,9 +102,13 @@ public OAuth2PasswordGrantAuthInterceptor(@Nonnull ListenerParameters parameters
URL tokenUrl = parseTokenUri(parameters);

OkHttpClient.Builder clientBuilder = ClientUtils.setupSsl(new OkHttpClient.Builder(), tokenUrl, parameters);
ClientUtils.setupHttpLoggingInterceptor(clientBuilder, parameters);

if (parameters.isOauthUseProxy()) {
ClientUtils.setupProxy(clientBuilder, parameters);
} else {
// Explicitly disable proxy to override system proxy settings
clientBuilder.proxy(Proxy.NO_PROXY);
}

ofNullable(parameters.getHttpConnectTimeout()).ifPresent(d -> clientBuilder.connectTimeout(d.toMillis(), TimeUnit.MILLISECONDS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.epam.reportportal.service;

import com.epam.reportportal.listeners.ListenerParameters;
import com.epam.reportportal.listeners.LogLevel;
import com.epam.reportportal.test.TestUtils;
import com.epam.reportportal.util.test.SocketUtils;
import com.epam.ta.reportportal.ws.model.FinishExecutionRQ;
import com.epam.ta.reportportal.ws.model.FinishTestItemRQ;
import com.epam.ta.reportportal.ws.model.StartTestItemRQ;
import com.epam.ta.reportportal.ws.model.launch.StartLaunchRQ;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import io.reactivex.Maybe;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -106,6 +108,14 @@ private static FinishExecutionRQ buildFinishLaunchRq(Comparable<? extends Compar
return rq;
}

private static SaveLogRQ buildSaveLogRq(Comparable<? extends Comparable<?>> logTime) {
SaveLogRQ rq = new SaveLogRQ();
rq.setLogTime(logTime);
rq.setLevel(LogLevel.INFO.name());
rq.setMessage("some message");
return rq;
}

private static Comparable<? extends Comparable<?>> dateOrInstant(boolean instant) {
Instant testInstant = Instant.ofEpochSecond(START_TIME_SECONDS_BASE, START_TIME_NANO_ADJUSTMENT);
if (!instant) {
Expand Down Expand Up @@ -163,7 +173,7 @@ private static SocketUtils.ServerCallable buildServerCallableForStartItem(Server

private static SocketUtils.ServerCallable buildServerCallableForFinishItem(ServerSocket ss, boolean micro) {
List<String> responses = new ArrayList<>();
responses.add(micro ? "files/responses/info_response_microseconds.txt" : "files/responses/info_response_no_microseconds.txt");
responses.add(micro ? "files/responses/info_response_microseconds.txt" : "files/responses/info_response_no_microseconds2.txt");
responses.add("files/responses/start_launch_response.txt");
responses.add("files/responses/simple_response.txt"); // finish item response
return new SocketUtils.ServerCallable(ss, Collections.emptyMap(), responses);
Expand All @@ -178,6 +188,14 @@ private static SocketUtils.ServerCallable buildServerCallableForFinishLaunch(Ser
return new SocketUtils.ServerCallable(ss, Collections.emptyMap(), responses);
}

private static SocketUtils.ServerCallable buildServerCallableForLog(ServerSocket ss, boolean micro) {
List<String> responses = new ArrayList<>();
responses.add(micro ? "files/responses/info_response_microseconds.txt" : "files/responses/info_response_no_microseconds2.txt");
responses.add("files/responses/start_launch_response.txt");
responses.add("files/responses/simple_response.txt"); // finish item response
return new SocketUtils.ServerCallable(ss, Collections.emptyMap(), responses);
}

/* ---------------------- Launch start ---------------------- */
@Test
public void start_launch_useMicroseconds_false_Date_sends_numeric_time() throws Exception {
Expand Down Expand Up @@ -426,4 +444,56 @@ private void finishLaunchTimeCase(boolean micro, boolean instant) throws Excepti
assertTimeNumeric(json, "endTime");
}
}

/* ---------------------- log ---------------------- */

@Test
public void log_useMicroseconds_false_Date_sends_numeric_time() throws Exception {
logTimeCase(false, false);
}

@Test
public void log_launch_useMicroseconds_false_Instant_sends_numeric_time() throws Exception {
logTimeCase(false, true);
}

@Test
public void log_launch_useMicroseconds_true_Instant_sends_iso_micro_time() throws Exception {
logTimeCase(true, true);
}

@Test
public void log_launch_useMicroseconds_true_Date_sends_numeric_time() throws Exception {
logTimeCase(true, false);
}

private void logTimeCase(boolean micro, boolean instant) throws Exception {
ServerSocket ss = SocketUtils.getServerSocketOnFreePort();
String baseUrl = "http://localhost:" + ss.getLocalPort();
ListenerParameters parameters = baseParameters(baseUrl);
parameters.setBatchLogsSize(1);

ReportPortalClient rpClient = Objects.requireNonNull(ReportPortal.builder()
.buildClient(ReportPortalClient.class, parameters, clientExecutor));
StartLaunchRQ launchRq = buildStartLaunchRq(new Date());
ReportPortal rp = ReportPortal.create(rpClient, parameters, clientExecutor);
Launch launch = rp.newLaunch(launchRq);
SaveLogRQ rq = buildSaveLogRq(dateOrInstant(instant));

SocketUtils.ServerCallable serverCallable = buildServerCallableForLog(ss, micro);

Pair<List<String>, ?> result = executeWithClosing(
ss, serverCallable, () -> {
launch.log(rq);
return Boolean.TRUE;
}
);

String json = findLastJsonWithKey(result, "time");
if (expectString(micro, instant)) {
assertTimeIsoMicro(json, "time");
} else {
assertTimeNumeric(json, "time");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.epam.reportportal.service;

import com.epam.reportportal.listeners.ListenerParameters;
import jakarta.annotation.Nonnull;
import okhttp3.*;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -547,4 +549,70 @@ public void test401And403ThrottledSimultaneously() throws Exception {
assertEquals(3, capturedRequests.size(), "Token refresh should have been attempted after throttling period");
assertEquals(6, apiRequestCount.size(), "API should have been called twice (initial + retry after refresh)");
}

@Nonnull
private static OAuth2PasswordGrantAuthInterceptor getOAuth2PasswordGrantAuthInterceptor() {
ListenerParameters params = new ListenerParameters();
params.setOauthTokenUri("https://oauth.example.com/token");
params.setOauthUsername("test-user");
params.setOauthPassword("test-password");
params.setOauthClientId("test-client-id");
params.setOauthClientSecret("test-client-secret");
params.setOauthScope("test-scope");
params.setOauthUseProxy(false); // This is the key setting

// Create interceptor using constructor that creates its own client
// This will respect the oauthUseProxy setting
return new OAuth2PasswordGrantAuthInterceptor(params);
}

@Test
public void testOAuthAvoidsProxyWhenOauthUseProxyIsFalse() throws Exception {
// Save original system properties
String originalHttpsProxyHost = System.getProperty("https.proxyHost");
String originalHttpsProxyPort = System.getProperty("https.proxyPort");

try (MockWebServer mockProxyServer = new MockWebServer()) {
// Start mock proxy server on localhost
mockProxyServer.start();

// Set system proxy properties to point to our mock proxy
System.setProperty("https.proxyHost", "localhost");
System.setProperty("https.proxyPort", String.valueOf(mockProxyServer.getPort()));

// Configure parameters with oauthUseProxy = false and a non-localhost token URL
OAuth2PasswordGrantAuthInterceptor interceptor = getOAuth2PasswordGrantAuthInterceptor();

// Create API client with OAuth interceptor
OkHttpClient apiClient = createMockApiClient(interceptor);

// Execute API request - this will trigger OAuth token request
Request apiRequest = new Request.Builder().url("http://localhost/api/test").get().build();

try (Response response = apiClient.newCall(apiRequest).execute()) {
// Verify API response is successful (from our mock API client)
assertTrue(response.isSuccessful());
assertEquals(200, response.code());

// Wait a bit to ensure no request comes to proxy
Thread.sleep(100);

// Verify that NO requests were made to the proxy server
// because oauthUseProxy=false means OAuth client should bypass proxy
assertEquals(0, mockProxyServer.getRequestCount(), "Proxy server should NOT receive any requests when oauthUseProxy=false");
}
} finally {
// Restore original system properties
if (originalHttpsProxyHost != null) {
System.setProperty("https.proxyHost", originalHttpsProxyHost);
} else {
System.clearProperty("https.proxyHost");
}
if (originalHttpsProxyPort != null) {
System.setProperty("https.proxyPort", originalHttpsProxyPort);
} else {
System.clearProperty("https.proxyPort");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json
Expires: 0
Pragma: no-cache
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Xss-Protection: 1; mode=block
Content-Length: 30

{"build":{"version":"5.12.0"}}