Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve hostname verification issue with localhost in self registration flow. #4794

Merged
merged 3 commits into from
Jul 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -200,6 +204,7 @@
org.wso2.carbon.identity.base;resolution:=optional;version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.user.core.*; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.utils.multitenancy; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}",
com.google.gson; version="${com.google.code.gson.osgi.version.range}"
</Import-Package>
</instructions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.owasp.encoder.Encode;
import org.wso2.carbon.context.PrivilegedCarbonContext;
Expand All @@ -41,6 +40,7 @@
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.user.core.UserCoreConstants;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.carbon.utils.HTTPClientUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;

import java.io.BufferedReader;
Expand Down Expand Up @@ -386,7 +386,7 @@ private static String buildAbsoluteURL(String contextPath) throws URLBuilderExce
public static String sendGetRequest(String backendURL) {

StringBuilder responseString = new StringBuilder();
try (CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {

HttpGet httpGet = new HttpGet(backendURL);
setAuthorizationHeader(httpGet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@
<artifactId>jettison</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -309,6 +313,7 @@
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.user.core.*; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.utils.multitenancy; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}",

org.wso2.carbon.identity.application.common.model;
version="${carbon.identity.package.import.version.range}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.json.JSONObject;
import org.json.JSONTokener;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointUtil;
import org.wso2.carbon.utils.HTTPClientUtils;

import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -53,7 +54,7 @@ public class AdminAdvisoryDataRetrievalClient {
*/
public JSONObject getAdminAdvisoryBannerData(String tenant) throws AdminAdvisoryDataRetrievalClientException {

try (CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {

String uri = getAdminAdvisoryBannerEndpoint(tenant);
HttpGet request = new HttpGet(uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointUtil;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementServiceUtil;
import org.wso2.carbon.utils.HTTPClientUtils;

import java.io.IOException;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -295,7 +294,7 @@ public Optional<String> getPropertyValue(String tenant, String governanceDomain,
String propertyName)
throws PreferenceRetrievalClientException {

try (CloseableHttpClient httpclient = HttpClientBuilder.create().useSystemProperties().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {
String endpoint = getUserGovernanceEndpoint(tenant);
HttpGet get = new HttpGet(endpoint);
setAuthorizationHeader(get);
Expand Down Expand Up @@ -372,8 +371,7 @@ public Optional<String> getPropertyValue(String tenant, String governanceDomain,
public boolean checkPreference(String tenant, String connectorName, String propertyName, boolean defaultValue)
throws PreferenceRetrievalClientException {

X509HostnameVerifier hv = new SelfRegistrationHostnameVerifier();
try (CloseableHttpClient httpclient = createHttpClientBuilderWithHV().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {
JSONArray main = new JSONArray();
JSONObject preference = new JSONObject();
preference.put(CONNECTOR_NAME, connectorName);
Expand Down Expand Up @@ -428,7 +426,7 @@ public boolean checkPreference(String tenant, String connectorName, String prope
public boolean checkMultiplePreference(String tenant, String connectorName, List<String> propertyNames)
throws PreferenceRetrievalClientException {

try (CloseableHttpClient httpclient = createHttpClientBuilderWithHV().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {
JSONArray requestBody = new JSONArray();
JSONObject preference = new JSONObject();
preference.put(CONNECTOR_NAME, connectorName);
Expand Down Expand Up @@ -499,14 +497,4 @@ private void setAuthorizationHeader(HttpRequestBase httpMethod) {
String authHeader = new String(encoding, Charset.defaultCharset());
httpMethod.addHeader(HTTPConstants.HEADER_AUTHORIZATION, CLIENT + authHeader);
}

private HttpClientBuilder createHttpClientBuilderWithHV() {

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
if (DEFAULT_AND_LOCALHOST.equals(System.getProperty(HOST_NAME_VERIFIER))) {
X509HostnameVerifier hv = new SelfRegistrationHostnameVerifier();
httpClientBuilder.setHostnameVerifier(hv);
}
return httpClientBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointConstants;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementEndpointUtil;
import org.wso2.carbon.identity.mgt.endpoint.util.IdentityManagementServiceUtil;
import org.wso2.carbon.identity.mgt.endpoint.util.client.model.User;
import org.wso2.carbon.utils.HTTPClientUtils;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -160,7 +159,7 @@ private String getEndpoint(String tenantDomain, String context) throws SelfRegis
private String executeGet(String url) throws SelfRegistrationMgtClientException, IOException {

boolean isDebugEnabled = log.isDebugEnabled();
try (CloseableHttpClient httpclient = createHttpClientBuilderWithHV().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {

HttpGet httpGet = new HttpGet(url);
setAuthorizationHeader(httpGet);
Expand Down Expand Up @@ -258,7 +257,7 @@ private JSONObject checkUserNameValidityInternal(User user, boolean skipSignUpCh
+ ". SkipSignUpCheck flag is set to " + skipSignUpCheck);
}

try (CloseableHttpClient httpclient = createHttpClientBuilderWithHV().build()) {
try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) {
JSONObject userObject = new JSONObject();
userObject.put(USERNAME, user.getUsername());

Expand Down Expand Up @@ -391,13 +390,4 @@ private boolean hasPIICategories(JSONObject purpose) {
JSONArray piiCategories = (JSONArray) purpose.get(PII_CATEGORIES);
return piiCategories.length() > 0;
}

private HttpClientBuilder createHttpClientBuilderWithHV() {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().useSystemProperties();
if (DEFAULT_AND_LOCALHOST.equals(System.getProperty(HOST_NAME_VERIFIER))) {
X509HostnameVerifier hv = new SelfRegistrationHostnameVerifier();
httpClientBuilder.setHostnameVerifier(hv);
}
return httpClientBuilder;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Carbon kernel version -->
<carbon.kernel.version>4.9.8</carbon.kernel.version>
<carbon.kernel.version>4.9.9</carbon.kernel.version>
<carbon.kernel.feature.version>4.7.0</carbon.kernel.feature.version>
<carbon.kernel.package.import.version.range>[4.5.0, 5.0.0)</carbon.kernel.package.import.version.range>
<carbon.kernel.registry.imp.pkg.version>[1.0.1, 2.0.0)</carbon.kernel.registry.imp.pkg.version>
Expand Down
Loading