From ccec7d1fb79282f45f9633259f7bbd6a04ac62fc Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 15:43:12 +0200 Subject: [PATCH 1/7] URL Validation --- .../agents/http/DelegationServiceImpl.java | 66 ++++++++++++++----- .../agents/service/DataManagement.java | 50 ++++++++++---- 2 files changed, 88 insertions(+), 28 deletions(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index b02fb2e0..d8345954 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -49,6 +49,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.net.URI; /** * A service that may delegate an incoming @@ -129,6 +130,26 @@ public DelegationResponse executeQueryRemote(String remoteUrl, String skill, Str } + /** + * URL Validation + * + * @param url URL string for validation + * @return Returns true if the URL is valid, false otherwise. + */ + public static boolean isValid(String url) + { + // Try creating a valid URL + try { + new URI(url).toURL(); + return true; + } + + // If there was an Exception while creating URL object + catch (Exception e) { + return false; + } + } + /** * route a get request * @@ -142,16 +163,22 @@ public DelegationResponse sendGetRequest(EndpointDataReference dataReference, St monitor.debug(String.format("About to delegate GET %s", url)); - var requestBuilder = new okhttp3.Request.Builder() - .url(url); + if(isValid(url)){ - if (dataReference.getAuthKey() != null) { - requestBuilder = requestBuilder.addHeader(dataReference.getAuthKey(), Objects.requireNonNull(dataReference.getAuthCode())); - } + var requestBuilder = new okhttp3.Request.Builder() + .url(url); - var newRequest = requestBuilder.build(); + if (dataReference.getAuthKey() != null) { + requestBuilder = requestBuilder.addHeader(dataReference.getAuthKey(), Objects.requireNonNull(dataReference.getAuthCode())); + } + + var newRequest = requestBuilder.build(); - return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); + return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); + } else { + monitor.warning(format("Invalid URL", url)); + return null; + } } /** @@ -170,19 +197,26 @@ public DelegationResponse sendPostRequest(EndpointDataReference dataReference, S monitor.debug(String.format("About to delegate POST %s with content type %s", url, contentType)); - var requestBuilder = new okhttp3.Request.Builder() - .url(url) - .addHeader("Content-Type", contentType); + if(isValid(url)){ - if (dataReference.getAuthKey() != null) { - requestBuilder = requestBuilder.addHeader(dataReference.getAuthKey(), Objects.requireNonNull(dataReference.getAuthCode())); - } + var requestBuilder = new okhttp3.Request.Builder() + .url(url) + .addHeader("Content-Type", contentType); + + if (dataReference.getAuthKey() != null) { + requestBuilder = requestBuilder.addHeader(dataReference.getAuthKey(), Objects.requireNonNull(dataReference.getAuthCode())); + } - requestBuilder.post(okhttp3.RequestBody.create(request.getInputStream().readAllBytes(), parsedContentType)); + requestBuilder.post(okhttp3.RequestBody.create(request.getInputStream().readAllBytes(), parsedContentType)); - var newRequest = requestBuilder.build(); + var newRequest = requestBuilder.build(); - return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); + return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); + + } else { + monitor.warning(format("Invalid URL", url)); + return null; + } } protected static final Pattern PARAMETER_KEY_ALLOW = Pattern.compile("^(?(?!asset$)[^&?=]+)$"); diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java index e2db94b0..5f7f1735 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java @@ -42,6 +42,7 @@ import java.io.IOException; import java.net.URLEncoder; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.UUID; @@ -425,6 +426,26 @@ public IdResponse createOrUpdateGraph(String assetId, String name, String descri return createOrUpdateAsset(assetId, assetSpec); } + /** + * URL Validation + * + * @param url URL string for validation + * @return Returns true if the URL is valid, false otherwise. + */ + public static boolean isValid(String url) + { + // Try creating a valid URL + try { + new URI(url).toURL(); + return true; + } + + // If there was an Exception while creating URL object + catch (Exception e) { + return false; + } + } + /** * deletes an existing aseet * @@ -435,19 +456,24 @@ public IdResponse createOrUpdateGraph(String assetId, String name, String descri public IdResponse deleteAsset(String assetId) throws IOException { String version = "/v3"; var url = String.format(ASSET_UPDATE_CALL, config.getControlPlaneManagementProviderUrl(), version, assetId); - var request = new Request.Builder().url(url).delete(); - config.getControlPlaneManagementHeaders().forEach(request::addHeader); - try (var response = httpClient.newCall(request.build()).execute()) { - ResponseBody body = response.body(); - if (response.isSuccessful() && body != null) { - return JsonLd.processIdResponse(body.string()); - } else { - monitor.warning(format("Failure in calling the control plane at %s. Ignoring", url)); - return null; + if(isValid(url)){ + var request = new Request.Builder().url(url).delete(); + config.getControlPlaneManagementHeaders().forEach(request::addHeader); + try (var response = httpClient.newCall(request.build()).execute()) { + ResponseBody body = response.body(); + if (response.isSuccessful() && body != null) { + return JsonLd.processIdResponse(body.string()); + } else { + monitor.warning(format("Failure in calling the control plane at %s. Ignoring", url)); + return null; + } + } catch (Exception e) { + monitor.severe(format("Error in calling the control plane at %s", url), e); + throw e; } - } catch (Exception e) { - monitor.severe(format("Error in calling the control plane at %s", url), e); - throw e; + } else { + monitor.warning(format("Invalid URL", url)); + return null; } } From 38111640afae6ae1a64126b8b7f2a2bc1db5db76 Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 16:03:51 +0200 Subject: [PATCH 2/7] Codestyle customizations --- .../tractusx/agents/http/DelegationServiceImpl.java | 12 +++++------- .../tractusx/agents/service/DataManagement.java | 13 +++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index d8345954..1f2240b3 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -41,6 +41,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.URI; import java.nio.charset.Charset; import java.util.List; import java.util.Map; @@ -49,7 +50,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import java.net.URI; + /** * A service that may delegate an incoming @@ -142,10 +143,7 @@ public static boolean isValid(String url) try { new URI(url).toURL(); return true; - } - - // If there was an Exception while creating URL object - catch (Exception e) { + } catch (Exception e) { // If there was an Exception while creating URL object return false; } } @@ -163,7 +161,7 @@ public DelegationResponse sendGetRequest(EndpointDataReference dataReference, St monitor.debug(String.format("About to delegate GET %s", url)); - if(isValid(url)){ + if (isValid(url)) { var requestBuilder = new okhttp3.Request.Builder() .url(url); @@ -197,7 +195,7 @@ public DelegationResponse sendPostRequest(EndpointDataReference dataReference, S monitor.debug(String.format("About to delegate POST %s with content type %s", url, contentType)); - if(isValid(url)){ + if (isValid(url)) { var requestBuilder = new okhttp3.Request.Builder() .url(url) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java index 5f7f1735..546ef7ab 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/service/DataManagement.java @@ -41,8 +41,8 @@ import org.eclipse.tractusx.agents.utils.TypeManager; import java.io.IOException; -import java.net.URLEncoder; import java.net.URI; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.UUID; @@ -432,16 +432,12 @@ public IdResponse createOrUpdateGraph(String assetId, String name, String descri * @param url URL string for validation * @return Returns true if the URL is valid, false otherwise. */ - public static boolean isValid(String url) - { + public static boolean isValid(String url) { // Try creating a valid URL try { new URI(url).toURL(); return true; - } - - // If there was an Exception while creating URL object - catch (Exception e) { + } catch (Exception e) { // If there was an Exception while creating URL object return false; } } @@ -456,7 +452,8 @@ public static boolean isValid(String url) public IdResponse deleteAsset(String assetId) throws IOException { String version = "/v3"; var url = String.format(ASSET_UPDATE_CALL, config.getControlPlaneManagementProviderUrl(), version, assetId); - if(isValid(url)){ + + if (isValid(url)) { var request = new Request.Builder().url(url).delete(); config.getControlPlaneManagementHeaders().forEach(request::addHeader); try (var response = httpClient.newCall(request.build()).execute()) { From 69ccf40efc02bbd78520c420fc19b5cf7166686a Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 16:14:22 +0200 Subject: [PATCH 3/7] Codestyle adjustments --- .../eclipse/tractusx/agents/http/DelegationServiceImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index 1f2240b3..c6b6a755 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -131,14 +131,13 @@ public DelegationResponse executeQueryRemote(String remoteUrl, String skill, Str } - /** + /** * URL Validation * * @param url URL string for validation * @return Returns true if the URL is valid, false otherwise. */ - public static boolean isValid(String url) - { + public static boolean isValid(String url) { // Try creating a valid URL try { new URI(url).toURL(); From 7dbae370bb116803ad27da997d14721e31c56ef6 Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 16:18:01 +0200 Subject: [PATCH 4/7] Codestyle Adjustments --- .../org/eclipse/tractusx/agents/http/DelegationServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index c6b6a755..d5f02310 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -131,7 +131,7 @@ public DelegationResponse executeQueryRemote(String remoteUrl, String skill, Str } - /** + /** * URL Validation * * @param url URL string for validation From e9af61b70e07aa6731c6cc352b12b32d156625d3 Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 16:27:27 +0200 Subject: [PATCH 5/7] Bugfix --- .../eclipse/tractusx/agents/http/DelegationServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index d5f02310..4f6f0356 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -160,7 +160,7 @@ public DelegationResponse sendGetRequest(EndpointDataReference dataReference, St monitor.debug(String.format("About to delegate GET %s", url)); - if (isValid(url)) { + if (isValid(url.toString())) { var requestBuilder = new okhttp3.Request.Builder() .url(url); @@ -194,7 +194,7 @@ public DelegationResponse sendPostRequest(EndpointDataReference dataReference, S monitor.debug(String.format("About to delegate POST %s with content type %s", url, contentType)); - if (isValid(url)) { + if (isValid(url.toString())) { var requestBuilder = new okhttp3.Request.Builder() .url(url) From 8e9c1b050a4ca2443116989d010c8aefa8ebb5ac Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 16:32:14 +0200 Subject: [PATCH 6/7] Bugfix --- .../eclipse/tractusx/agents/http/DelegationServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index 4f6f0356..8c3557a1 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -173,7 +173,7 @@ public DelegationResponse sendGetRequest(EndpointDataReference dataReference, St return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); } else { - monitor.warning(format("Invalid URL", url)); + monitor.warning(format("Invalid URL", url.toString())); return null; } } @@ -211,7 +211,7 @@ public DelegationResponse sendPostRequest(EndpointDataReference dataReference, S return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); } else { - monitor.warning(format("Invalid URL", url)); + monitor.warning(format("Invalid URL", url.toString())); return null; } } From 72f2b6666a4afb82c64e3d12038e5877b13128bb Mon Sep 17 00:00:00 2001 From: Oguzhan Balandi Date: Thu, 23 May 2024 16:36:30 +0200 Subject: [PATCH 7/7] Bugfix --- .../org/eclipse/tractusx/agents/http/DelegationServiceImpl.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java index 8c3557a1..b9775984 100644 --- a/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java +++ b/matchmaking/src/main/java/org/eclipse/tractusx/agents/http/DelegationServiceImpl.java @@ -173,7 +173,6 @@ public DelegationResponse sendGetRequest(EndpointDataReference dataReference, St return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); } else { - monitor.warning(format("Invalid URL", url.toString())); return null; } } @@ -211,7 +210,6 @@ public DelegationResponse sendPostRequest(EndpointDataReference dataReference, S return new DelegationResponse(sendRequest(newRequest, response), Response.status(response.getStatus()).build()); } else { - monitor.warning(format("Invalid URL", url.toString())); return null; } }