openvidu-java-client: rollback to Apache HttpClient. Upgrade it to version 5

This commit is contained in:
pabloFuente 2023-01-31 20:35:33 +01:00
parent 047a23137a
commit 66bbe9f41f
6 changed files with 793 additions and 650 deletions

View File

@ -63,6 +63,11 @@
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>

View File

@ -1,25 +1,10 @@
package io.openvidu.java.client;
import java.net.http.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
/**
* @hidden
*/
public class Utils {
public static JsonObject httpResponseToJson(HttpResponse<String> response) throws OpenViduJavaClientException {
try {
JsonObject json = new Gson().fromJson(response.body(), JsonObject.class);
return json;
} catch (JsonSyntaxException e) {
throw new OpenViduJavaClientException(e.getMessage(), e.getCause());
}
}
public static boolean isAcceptableRecordingResolution(String stringResolution) {
// Matches every string with format "AxB", being A and B any number not starting
// with 0 and 3 digits long or 4 digits long if they start with 1

View File

@ -1,19 +1,28 @@
package io.openvidu.java.client.test;
import java.net.Authenticator;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.BasicHeader;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -31,40 +40,57 @@ public class OpenViduConstructorTest {
}
@Test
public void buildWithHttpClientWithoutAuthenticator() {
HttpClient.Builder builder = HttpClient.newBuilder();
builder.connectTimeout(Duration.ofMillis(10000));
ProxySelector proxy = ProxySelector.of(new InetSocketAddress("https://my.proxy.hostname/", 4444));
builder.proxy(proxy);
builder.followRedirects(Redirect.ALWAYS);
public void buildWithCustomHttpClient() {
HttpClientBuilder builder = HttpClients.custom();
// Custom header
BasicHeader header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
builder.setDefaultHeaders(List.of(header));
// Custom request timeout
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5, TimeUnit.SECONDS).build();
builder.setDefaultRequestConfig(requestConfig);
// Custom proxy to authenticate
HttpHost proxy = new HttpHost("https://localhost/", 8090);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(proxy),
new UsernamePasswordCredentials("username_admin", "secret_password".toCharArray()));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(proxy, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
builder.setRoutePlanner(routePlanner);
// Custom SSLContext
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
} catch (Exception e) {
}
builder.sslContext(sslContext);
builder.executor(Executors.newFixedThreadPool(1));
builder.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER));
OpenVidu OV = new OpenVidu("https://localhost:4443/", "MY_SECRET", builder.build());
Assertions.assertEquals(30000, OV.getRequestTimeout());
Assertions.assertTrue(OV.getRequestHeaders().isEmpty());
OV.setRequestTimeout(5000);
OV.setRequestHeaders(Map.of("header1", "value1", "header2", "value2"));
Assertions.assertEquals(5000, OV.getRequestTimeout());
Assertions.assertEquals(2, OV.getRequestHeaders().size());
}
final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
.setSslContext(sslContext).build();
final HttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(sslSocketFactory).build();
builder.setConnectionManager(connectionManager);
@Test
public void buildWithHttpClientWithAuthenticator() {
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("OPENVIDUAPP", "secret".toCharArray());
}
};
HttpClient.Builder builder = HttpClient.newBuilder().authenticator(authenticator);
new OpenVidu("https://localhost:4443/", "MY_SECRET", builder.build());
// Custom CredentialsProvider
final BasicCredentialsProvider customCredentialsProvider = new BasicCredentialsProvider();
customCredentialsProvider.setCredentials(new AuthScope(null, -1),
new UsernamePasswordCredentials("OPENVIDUAPP", "MY_SECRET".toCharArray()));
builder.setDefaultCredentialsProvider(customCredentialsProvider);
new OpenVidu("https://localhost", "MY_SECRET");
new OpenVidu("https://localhost", "MY_SECRET", builder);
new OpenVidu("https://localhost", builder);
}
}

View File

@ -20,13 +20,11 @@ package io.openvidu.test.e2e;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.Socket;
import java.net.http.HttpClient;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Base64;
@ -43,11 +41,22 @@ import java.util.function.BiFunction;
import java.util.stream.Collectors;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.TrustStrategy;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
@ -2440,127 +2449,167 @@ public class OpenViduTestAppE2eTest extends AbstractOpenViduTestappE2eTest {
gracefullyLeaveParticipants(user, 2);
}
private HttpClientBuilder getHttpClientBuilder() {
HttpClientBuilder builder = HttpClients.custom();
TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new RuntimeException(e);
}
final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create()
.setSslContext(sslContext).build();
final HttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(sslSocketFactory).build();
builder.setConnectionManager(connectionManager);
return builder;
}
@Test
@DisplayName("openvidu-java-client custom HttpClient test")
void openViduJavaClientCustomHttpClientTest() throws Exception {
log.info("openvidu-java-client custom HttpClient test");
// Test all possible combinations: custom Authenticator present and valid,
// present and wrong and no present; in combination with custom Authorization
// header present and valid, present and wrong and no present
HttpClient.Builder builder = HttpClient.newBuilder();
SSLContext sslContext;
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { new X509ExtendedTrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(final X509Certificate[] a_certificates, final String a_auth_type) {
}
public void checkServerTrusted(final X509Certificate[] a_certificates, final String a_auth_type) {
}
public void checkClientTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final Socket a_socket) {
}
public void checkServerTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final Socket a_socket) {
}
public void checkClientTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final SSLEngine a_engine) {
}
public void checkServerTrusted(final X509Certificate[] a_certificates, final String a_auth_type,
final SSLEngine a_engine) {
}
} }, null);
} catch (KeyManagementException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
builder.sslContext(sslContext);
final String BASIC_AUTH = "Basic "
+ Base64.getEncoder().encodeToString(("OPENVIDUAPP:" + OPENVIDU_SECRET).getBytes());
final String WRONG_SECRET = "WRONG_SECRET_" + RandomStringUtils.randomAlphanumeric(10);
// 1. No authenticator, no header, 200
OpenVidu customHttpClientOV1 = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, builder.build());
customHttpClientOV1.fetch();
final String VALID_BASIC_AUTH = "Basic "
+ Base64.getEncoder().encodeToString(("OPENVIDUAPP:" + OPENVIDU_SECRET).getBytes());
final String WRONG_BASIC_AUTH = "Basic "
+ Base64.getEncoder().encodeToString(("OPENVIDUAPP:" + WRONG_SECRET).getBytes());
// 2. No authenticator, wrong header, 401
customHttpClientOV1.setRequestHeaders(Map.of("Authorization", "WRONG_AUTH_HEADER"));
final Collection<Header> VALID_AUTH_HEADER = List
.of(new BasicHeader(HttpHeaders.AUTHORIZATION, VALID_BASIC_AUTH));
final Collection<Header> WRONG_AUTH_HEADER = List
.of(new BasicHeader(HttpHeaders.AUTHORIZATION, WRONG_BASIC_AUTH));
// 1. No valid certificate with no forgiving SSLContext
OpenVidu[] customOV = { new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, HttpClients.custom()) };
Assertions.assertThrows(OpenViduJavaClientException.class, () -> {
customOV[0].fetch();
});
// 2. No CredentialsProvider, no Authorization header, no secret, 401
customOV[0] = new OpenVidu(OPENVIDU_URL, getHttpClientBuilder());
OpenViduHttpException thrown = Assertions.assertThrows(OpenViduHttpException.class, () -> {
customHttpClientOV1.fetch();
customOV[0].fetch();
});
Assertions.assertEquals(401, thrown.getStatus());
// 3. No authenticator and valid header, 200
customHttpClientOV1.setRequestHeaders(Map.of("Authorization", BASIC_AUTH));
customHttpClientOV1.fetch();
// 3. No CredentialsProvider, no Authorization header, valid secret, 200
customOV[0] = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, getHttpClientBuilder());
customOV[0].fetch();
// 4. Wrong authenticator and no header, 401
builder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("OPENVIDUAPP", WRONG_SECRET.toCharArray());
}
// 4. No CredentialsProvider, no Authorization header, wrong secret, 401
customOV[0] = new OpenVidu(OPENVIDU_URL, WRONG_SECRET, getHttpClientBuilder());
thrown = Assertions.assertThrows(OpenViduHttpException.class, () -> {
customOV[0].fetch();
});
OpenVidu customHttpClientOV2 = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, builder.build());
OpenViduJavaClientException thrown2 = Assertions.assertThrows(OpenViduJavaClientException.class, () -> {
customHttpClientOV2.fetch();
Assertions.assertEquals(401, thrown.getStatus());
// 5. No CredentialsProvider, wrong Authorization header, no secret, 401
HttpClientBuilder builder = getHttpClientBuilder();
builder.setDefaultHeaders(WRONG_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
thrown = Assertions.assertThrows(OpenViduHttpException.class, () -> {
customOV[0].fetch();
});
Assertions.assertTrue(thrown2.getMessage().contains("too many authentication attempts"));
Assertions.assertEquals(401, thrown.getStatus());
// 5. Wrong authenticator and wrong header, 401
customHttpClientOV2.setRequestHeaders(Map.of("Authorization", "WRONG_AUTH_HEADER"));
thrown2 = Assertions.assertThrows(OpenViduJavaClientException.class, () -> {
customHttpClientOV2.fetch();
// 6. No CredentialsProvider, wrong Authorization header, valid secret, 200
builder = getHttpClientBuilder();
builder.setDefaultHeaders(WRONG_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, builder);
customOV[0].fetch();
// 7. No CredentialsProvider, wrong Authorization header, wrong secret, 401
builder = getHttpClientBuilder();
builder.setDefaultHeaders(WRONG_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, WRONG_SECRET, builder);
thrown = Assertions.assertThrows(OpenViduHttpException.class, () -> {
customOV[0].fetch();
});
Assertions.assertTrue(thrown2.getMessage().contains("too many authentication attempts"));
Assertions.assertEquals(401, thrown.getStatus());
// 6. Wrong authenticator and valid header, 401
customHttpClientOV2.setRequestHeaders(Map.of("Authorization", BASIC_AUTH));
thrown2 = Assertions.assertThrows(OpenViduJavaClientException.class, () -> {
customHttpClientOV2.fetch();
// 8. No CredentialsProvider, valid Authorization header, no secret, 200
builder = getHttpClientBuilder();
builder.setDefaultHeaders(VALID_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
customOV[0].fetch();
// 9. No CredentialsProvider, valid Authorization header, valid secret, 200
builder = getHttpClientBuilder();
builder.setDefaultHeaders(VALID_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, builder);
customOV[0].fetch();
// 10. No CredentialsProvider, valid Authorization header, wrong secret, 200
builder = getHttpClientBuilder();
builder.setDefaultHeaders(VALID_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, WRONG_SECRET, builder);
customOV[0].fetch();
// 11. Valid CredentialsProvider, no Authorization header, no secret, 200
final BasicCredentialsProvider validCredentialsProvider = new BasicCredentialsProvider();
validCredentialsProvider.setCredentials(new AuthScope(null, -1),
new UsernamePasswordCredentials("OPENVIDUAPP", OPENVIDU_SECRET.toCharArray()));
builder = getHttpClientBuilder();
builder.setDefaultCredentialsProvider(validCredentialsProvider);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
customOV[0].fetch();
// 12. Valid CredentialsProvider, valid Authorization header, no secret, 200
builder = getHttpClientBuilder();
builder.setDefaultCredentialsProvider(validCredentialsProvider);
builder.setDefaultHeaders(VALID_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
customOV[0].fetch();
// 13. Valid CredentialsProvider, wrong Authorization header, no secret, 200
builder = getHttpClientBuilder();
builder.setDefaultCredentialsProvider(validCredentialsProvider);
builder.setDefaultHeaders(WRONG_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
customOV[0].fetch();
// 14. Wrong CredentialsProvider, no Authorization header, no secret, 401
final BasicCredentialsProvider wrongCredentialsProvider = new BasicCredentialsProvider();
validCredentialsProvider.setCredentials(new AuthScope(null, -1),
new UsernamePasswordCredentials("OPENVIDUAPP", WRONG_SECRET.toCharArray()));
builder = getHttpClientBuilder();
builder.setDefaultCredentialsProvider(wrongCredentialsProvider);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
thrown = Assertions.assertThrows(OpenViduHttpException.class, () -> {
customOV[0].fetch();
});
Assertions.assertTrue(thrown2.getMessage().contains("too many authentication attempts"));
Assertions.assertEquals(401, thrown.getStatus());
// 7. Valid authenticator and no header, 200
builder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("OPENVIDUAPP", OPENVIDU_SECRET.toCharArray());
}
// 15. Wrong CredentialsProvider, valid Authorization header, no secret, 200
builder = getHttpClientBuilder();
builder.setDefaultCredentialsProvider(wrongCredentialsProvider);
builder.setDefaultHeaders(VALID_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
customOV[0].fetch();
// 16. Wrong CredentialsProvider, wrong Authorization header, no secret
builder = getHttpClientBuilder();
builder.setDefaultCredentialsProvider(wrongCredentialsProvider);
builder.setDefaultHeaders(WRONG_AUTH_HEADER);
customOV[0] = new OpenVidu(OPENVIDU_URL, builder);
thrown = Assertions.assertThrows(OpenViduHttpException.class, () -> {
customOV[0].fetch();
});
OpenVidu customHttpClientOV3 = new OpenVidu(OPENVIDU_URL, OPENVIDU_SECRET, builder.build());
customHttpClientOV3.fetch();
// 8. Valid authenticator and wrong header, 200
customHttpClientOV3.setRequestHeaders(Map.of("Authorization", "WRONG_AUTH_HEADER"));
customHttpClientOV3.fetch();
// 9. Valid authenticator and valid header, 200
customHttpClientOV3.setRequestHeaders(Map.of("Authorization", BASIC_AUTH));
customHttpClientOV3.fetch();
// 10. Wrong secret, valid authenticator, no header, 200
OpenVidu customHttpClientOV4 = new OpenVidu(OPENVIDU_URL, WRONG_SECRET, builder.build());
customHttpClientOV4.fetch();
// 11. Wrong secret, valid authenticator, wrong header, 200
customHttpClientOV4.setRequestHeaders(Map.of("Authorization", "WRONG_AUTH_HEADER"));
customHttpClientOV4.fetch();
// 12. Wrong secret, no authenticator, valid header, 200
builder = HttpClient.newBuilder().sslContext(sslContext);
customHttpClientOV4 = new OpenVidu(OPENVIDU_URL, WRONG_SECRET, builder.build());
customHttpClientOV4.setRequestHeaders(Map.of("Authorization", BASIC_AUTH));
customHttpClientOV4.fetch();
Assertions.assertEquals(401, thrown.getStatus());
}
@Test