Skip to content

Commit

Permalink
Merge branch 'wso2:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
chamikasudusinghe committed Jun 21, 2023
2 parents 6896e84 + d3386ee commit 87a89a7
Show file tree
Hide file tree
Showing 98 changed files with 387 additions and 266 deletions.
2 changes: 1 addition & 1 deletion components/apimgt/org.wso2.carbon.apimgt.api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public enum ExceptionCodes implements ErrorHandler {
400, "Name of the gateway is read only"),
GATEWAY_ENVIRONMENT_VHOST_NOT_PROVIDED(900511, "Gateway Environment virtual hosts name not provided",
400, "Gateway Environment VHOST name not provided"),
INVALID_VHOST(900512, "Invalid virtual host name provided",
400, "Virtual host with provided vhost name does not exist"),

// Workflow related codes
WORKFLOW_EXCEPTION(900550, "Workflow error", 500,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
* This class represent an Virtual Host
*/
public class VHost {
// host name from the http endpoint
private String host;
private String httpContext = "";
private Integer httpPort = -1;
private Integer httpsPort = -1;
private Integer wsPort = DEFAULT_WS_PORT;
private String wsHost;
private Integer wssPort = DEFAULT_WSS_PORT;
private String wssHost;
private Integer websubHttpPort = DEFAULT_WEBSUB_HTTP_PORT;
private Integer websubHttpsPort = DEFAULT_WEBSUB_HTTPS_PORT;

Expand Down Expand Up @@ -95,6 +98,14 @@ public void setWsPort(Integer wsPort) {
this.wsPort = wsPort;
}

public String getWsHost() {
return wsHost;
}

public void setWsHost(String wsHost) {
this.wsHost = wsHost;
}

public Integer getWssPort() {
return wssPort;
}
Expand All @@ -103,6 +114,14 @@ public void setWssPort(Integer wssPort) {
this.wssPort = wssPort;
}

public String getWssHost() {
return wssHost;
}

public void setWssHost(String wssHost) {
this.wssHost = wssHost;
}

public Integer getWebsubHttpPort() {
return websubHttpPort;
}
Expand All @@ -120,27 +139,27 @@ public void setWebsubHttpsPort(Integer websubHttpsPort) {
}

public String getHttpUrl() {
return getUrl("http", httpPort == DEFAULT_HTTP_PORT ? "" : ":" + httpPort, httpContext);
return getUrl("http", host, httpPort == DEFAULT_HTTP_PORT ? "" : ":" + httpPort, httpContext);
}

public String getHttpsUrl() {
return getUrl("https", httpsPort == DEFAULT_HTTPS_PORT ? "" : ":" + httpsPort, httpContext);
return getUrl("https", host, httpsPort == DEFAULT_HTTPS_PORT ? "" : ":" + httpsPort, httpContext);
}

public String getWsUrl() {
return getUrl("ws", wsPort == DEFAULT_HTTP_PORT ? "" : ":" + wsPort, "");
return getUrl("ws", wsHost, wsPort == DEFAULT_HTTP_PORT ? "" : ":" + wsPort, "");
}

public String getWssUrl() {
return getUrl("wss", wssPort == DEFAULT_HTTPS_PORT ? "" : ":" + wssPort, "");
return getUrl("wss", wssHost, wssPort == DEFAULT_HTTPS_PORT ? "" : ":" + wssPort, "");
}

private String getUrl(String protocol, String port, String context) {
private String getUrl(String protocol, String hostName, String port, String context) {
// {protocol}://{host}{port}{context}
if (StringUtils.isNotEmpty(context) && !context.startsWith("/")) {
context = "/" + context;
}
return String.format("%s://%s%s%s", protocol, host, port, context);
return String.format("%s://%s%s%s", protocol, hostName, port, context);
}

public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementException {
Expand Down Expand Up @@ -178,11 +197,13 @@ public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementExc
// URL is not parsing for wss protocols, hence change to https
url = new URL(HTTPS_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
vhost.setWssPort(url.getPort() < 0 ? DEFAULT_WSS_PORT : url.getPort());
vhost.setWssHost(url.getHost());
break;
case WS_PROTOCOL:
// URL is not parsing for ws protocols, hence change to http
url = new URL(HTTP_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
vhost.setWsPort(url.getPort() < 0 ? DEFAULT_WS_PORT : url.getPort());
vhost.setWsHost(url.getHost());
break;
case WEBSUB_HTTP_PROTOCOL:
url = new URL(HTTP_PROTOCOL + PROTOCOL_SEPARATOR + elem[1]);
Expand All @@ -203,6 +224,14 @@ public static VHost fromEndpointUrls(String[] endpoints) throws APIManagementExc
throw new APIManagementException("Error while building VHost, missing required HTTP or HTTPS endpoint");
}

// If WebSocket host name of Vhost is empty, use HTTP/HTTPS endpoint hostname
if ((vhost.getWsHost() == null) || (StringUtils.isEmpty(vhost.getWsHost()))) {
vhost.setWsHost(vhost.getHost());
}
if ((vhost.getWssHost() == null) || (StringUtils.isEmpty(vhost.getWssHost()))) {
vhost.setWssHost(vhost.getHost());
}

return vhost;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>apimgt</artifactId>
<groupId>org.wso2.carbon.apimgt</groupId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<artifactId>apimgt</artifactId>
<groupId>org.wso2.carbon.apimgt</groupId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion components/apimgt/org.wso2.carbon.apimgt.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>apimgt</artifactId>
<groupId>org.wso2.carbon.apimgt</groupId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion components/apimgt/org.wso2.carbon.apimgt.eventing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>apimgt</artifactId>
<groupId>org.wso2.carbon.apimgt</groupId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion components/apimgt/org.wso2.carbon.apimgt.gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>apimgt</artifactId>
<version>9.28.152-SNAPSHOT</version>
<version>9.28.154-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
Expand All @@ -49,8 +48,6 @@ public class APILoggerManager {
private Map<Map<String, String>, String> logProperties = new HashMap<>();
private static final APILoggerManager apiLoggerManager = new APILoggerManager();
private final EventHubConfigurationDto eventHubConfigurationDto;
public static final int RETRIEVAL_RETRIES = 15;
public static final int RETRIEVAL_TIMEOUT_IN_SECONDS = 15;
public static final String UTF8 = "UTF-8";

public void initializeAPILoggerList() {
Expand Down Expand Up @@ -124,35 +121,13 @@ private String invokeService(String path, String tenantDomain) throws IOExceptio
method.setHeader(APIConstants.HEADER_TENANT, tenantDomain);
}
HttpClient httpClient = APIUtil.getHttpClient(servicePort, serviceProtocol);

HttpResponse httpResponse = null;
int retryCount = 0;
boolean retry;
do {
try {
httpResponse = httpClient.execute(method);
retry = false;
} catch (IOException ex) {
retryCount++;
if (retryCount < RETRIEVAL_RETRIES) {
retry = true;
log.warn("Failed retrieving " + path + " from remote endpoint: " + ex.getMessage()
+ ". Retrying after " + RETRIEVAL_TIMEOUT_IN_SECONDS +
" seconds.");
try {
Thread.sleep(RETRIEVAL_TIMEOUT_IN_SECONDS * 1000L);
} catch (InterruptedException e) {
// Ignore
}
} else {
throw new APIManagementException("Error while calling internal service", ex);
}
}
} while (retry);
if (HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()) {
log.error("Could not retrieve subscriptions for tenantDomain : " + tenantDomain);
throw new APIManagementException("Error while retrieving subscription from " + path);
try (CloseableHttpResponse httpResponse = APIUtil.executeHTTPRequestWithRetries(method, httpClient)){
return EntityUtils.toString(httpResponse.getEntity(), UTF8);
} catch (APIManagementException e) {
throw new APIManagementException("Error while calling internal service", e);
}
return EntityUtils.toString(httpResponse.getEntity(), UTF8);



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private CloseableHttpResponse invokeService(String endpoint, String tenantDomain

HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
try {
return APIUtil.executeHTTPRequest(method, httpClient);
return APIUtil.executeHTTPRequestWithRetries(method, httpClient);
} catch (APIManagementException e) {
throw new ArtifactSynchronizerException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private CloseableHttpResponse invokeService(String endpoint, String tenantDomain

HttpClient httpClient = APIUtil.getHttpClient(port, protocol);
try {
return APIUtil.executeHTTPRequest(method, httpClient);
return APIUtil.executeHTTPRequestWithRetries(method, httpClient);
} catch (APIManagementException e) {
throw new ArtifactSynchronizerException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static void logAPI(String flow, MessageContext messageContext) {

private static void addBasicProperties(JSONObject logMessage, MessageContext messageContext, String flow) {
logMessage.put("apiTo", messageContext.getProperty(API_TO));
logMessage.put("correlationId", messageContext.getProperty(APIConstants.CORRELATION_ID));
logMessage.put("correlationId", messageContext.getProperty("correlation_id"));
logMessage.put("flow", flow);
String verb = (String) ((Axis2MessageContext) messageContext).getAxis2MessageContext()
.getProperty(APIConstants.DigestAuthConstants.HTTP_METHOD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private String[] extractBasicAuthCredentials(String basicAuthHeader) throws APIS
String basicAuthKey = new String(Base64.decode(
basicAuthHeader.substring(basicAuthKeyHeaderSegment.length() + 1).trim()));
if (basicAuthKey.contains(":")) {
return basicAuthKey.split(":");
return basicAuthKey.split(":", 2);
} else {
log.error("Basic Authentication: Invalid Basic Auth token");
throw new APISecurityException(APISecurityConstants.API_AUTH_INVALID_CREDENTIALS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.gateway.dto.RevokedJWTTokenDTO;
import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.dto.EventHubConfigurationDto;
import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.DataLoadingException;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;

import java.io.IOException;
Expand All @@ -38,14 +40,14 @@
import java.util.Timer;
import java.util.TimerTask;

import static org.wso2.carbon.apimgt.impl.APIConstants.DigestAuthConstants.CHARSET;

/**
* Class which is responsible to fetch the revoked JWT signatures via webservice database during startup
*/
public class RevokedJWTTokensRetriever extends TimerTask {

private static final Log log = LogFactory.getLog(RevokedJWTTokensRetriever.class);
private static final int revokedJWTTokensRetrievalTimeoutInSeconds = 15;
private static final int revokedJWTTokensRetrievalRetries = 15;

@Override
public void run() {
Expand Down Expand Up @@ -73,32 +75,17 @@ private RevokedJWTTokenDTO[] retrieveRevokedJWTTokensData() {
int keyMgtPort = keyMgtURL.getPort();
String keyMgtProtocol = keyMgtURL.getProtocol();
HttpClient httpClient = APIUtil.getHttpClient(keyMgtPort, keyMgtProtocol);
HttpResponse httpResponse = null;
int retryCount = 0;
boolean retry;
do {
try {
httpResponse = httpClient.execute(method);
retry = false;
} catch (IOException ex) {
retryCount++;
if (retryCount < revokedJWTTokensRetrievalRetries) {
retry = true;
log.warn("Failed retrieving revoked JWT token signatures from remote endpoint: " +
ex.getMessage() + ". Retrying after " + revokedJWTTokensRetrievalTimeoutInSeconds +
" seconds...");
Thread.sleep(revokedJWTTokensRetrievalTimeoutInSeconds * 1000);
} else {
throw ex;
}
}
} while (retry);
String responseString;
try (CloseableHttpResponse httpResponse = APIUtil.executeHTTPRequestWithRetries(method, httpClient)) {
responseString = EntityUtils.toString(httpResponse.getEntity(), CHARSET);
} catch (APIManagementException e) {
throw new DataLoadingException("Error while retrieving revoked JWT tokens", e);
}

String responseString = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
if (responseString != null && !responseString.isEmpty()) {
return new Gson().fromJson(responseString, RevokedJWTTokenDTO[].class);
}
} catch (IOException | InterruptedException e) {
} catch (IOException | DataLoadingException e) {
log.error("Exception when retrieving revoked JWT tokens from remote endpoint ", e);
}
return null;
Expand Down
Loading

0 comments on commit 87a89a7

Please sign in to comment.