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

Issue #379: Add support for escaped macros #430

Merged
merged 19 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
58493bd
Issue #379: Add support for escaped macros
MedMaalej Sep 27, 2024
ba28710
Issue #379: Add support for escaped macros
MedMaalej Sep 30, 2024
3faf187
Issue #379: Add support for escaped macros
MedMaalej Sep 30, 2024
5f2a991
Issue #379: Add support for escaped macros
MedMaalej Oct 1, 2024
23ff624
Issue #379: Add support for escaped macros
MedMaalej Oct 2, 2024
fa436bd
Issue #379: Add support for escaped macros
MedMaalej Oct 3, 2024
3c614a9
Merge branch 'main' of github.com:sentrysoftware/metricshub into feat…
MedMaalej Oct 3, 2024
fb0c866
Issue #379: Add support for escaped macros
MedMaalej Oct 4, 2024
790de21
Issue #379: Add support for escaped macros
MedMaalej Oct 4, 2024
d1ba2ff
Issue #379: Add support for escaped macros
MedMaalej Oct 7, 2024
8664e94
Merge branch 'main' of github.com:sentrysoftware/metricshub into feat…
MedMaalej Oct 7, 2024
15fed92
Merge branch 'main' of github.com:sentrysoftware/metricshub into feat…
MedMaalej Oct 9, 2024
6ca4fd1
Issue #379: Add support for escaped macros
MedMaalej Oct 10, 2024
5f8bd29
Merge branch 'main' of github.com:sentrysoftware/metricshub into feat…
MedMaalej Oct 10, 2024
f649321
Issue #379: Add support for escaped macros
MedMaalej Oct 11, 2024
e706c6b
Issue #379: Add support for escaped macros
NassimBtk Oct 11, 2024
34dd864
Merge branch 'main' into feature/issue-379-Add-support-for-escaped-ch…
NassimBtk Oct 16, 2024
4d06839
Issue #379: Add support for escaped macros
NassimBtk Oct 22, 2024
de6a43d
Merge remote-tracking branch 'origin/main' into feature/issue-379-Add…
NassimBtk Oct 22, 2024
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.sentrysoftware.metricshub.engine.common.helpers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.sentrysoftware.metricshub.engine.common.helpers.MetricsHubConstants.HOSTNAME_MACRO;
import static org.sentrysoftware.metricshub.engine.common.helpers.MetricsHubConstants.PASSWORD_MACRO;
import static org.sentrysoftware.metricshub.engine.common.helpers.MetricsHubConstants.USERNAME_MACRO;

import org.junit.jupiter.api.Test;

class MacrosUpdaterTest {

@Test
void testUpdate() {
final String text = String.format(
"""
%s
%s
%s
%s
%s
%s
%s
%s
""",
HOSTNAME_MACRO,
PASSWORD_MACRO,
USERNAME_MACRO,
MacrosUpdater.BASIC_AUTH_BASE64_MACRO,
MacrosUpdater.SHA256_AUTH_MACRO,
MacrosUpdater.PASSWORD_BASE64_MACRO,
MacrosUpdater.USERNAME_ESC_JSON,
MacrosUpdater.PASSWORD_ESC_JSON
);

final String result = MacrosUpdater.update(text, "user", "pwd".toCharArray(), "token", "hostname");
final String expected =
"""
hostname
pwd
user
dXNlcjpwd2Q=
3c469e9d6c5875d37a43f353d4f88e61fcf812c66eee3457465a40b0da4153e0
cHdk
user
pwd
""";
assertEquals(expected, result);
}

@Test
void testEscapeJsonSpecialCharacters() {
assertEquals("test\\\"test", MacrosUpdater.escapeJsonSpecialCharacters("test\"test"));
assertEquals("test\\ttest", MacrosUpdater.escapeJsonSpecialCharacters("test\ttest"));
assertEquals("test\\ntest", MacrosUpdater.escapeJsonSpecialCharacters("test\ntest"));
assertEquals("test\\rtest", MacrosUpdater.escapeJsonSpecialCharacters("test\rtest"));
assertEquals("te\\\\st", MacrosUpdater.escapeJsonSpecialCharacters("te\\st"));
}

@Test
public void testEscapeUrlSpecialCharacters() {
assertEquals("Hello%20World", MacrosUpdater.escapeUrlSpecialCharacters("Hello World"));
assertEquals("test%40example.com", MacrosUpdater.escapeUrlSpecialCharacters("[email protected]"));
assertEquals("%24%26%2F%3F", MacrosUpdater.escapeUrlSpecialCharacters("$&/?"));
assertEquals("%7Bfoo%7D", MacrosUpdater.escapeUrlSpecialCharacters("{foo}"));
assertEquals("%7E%23", MacrosUpdater.escapeUrlSpecialCharacters("~#"));
}

@Test
public void testEscapeRegexSpecialCharacters() {
// Test escaping of common regex characters
assertEquals("\\Q.\\E", MacrosUpdater.escapeRegexSpecialCharacters("."));
assertEquals("\\Q^\\E", MacrosUpdater.escapeRegexSpecialCharacters("^"));
assertEquals("\\Q$\\E", MacrosUpdater.escapeRegexSpecialCharacters("$"));
assertEquals("\\Q*\\E", MacrosUpdater.escapeRegexSpecialCharacters("*"));
assertEquals("\\Q+\\E", MacrosUpdater.escapeRegexSpecialCharacters("+"));
assertEquals("\\Q?\\E", MacrosUpdater.escapeRegexSpecialCharacters("?"));
assertEquals("\\Q{\\E", MacrosUpdater.escapeRegexSpecialCharacters("{"));
assertEquals("\\Q}\\E", MacrosUpdater.escapeRegexSpecialCharacters("}"));
assertEquals("\\Q[\\E", MacrosUpdater.escapeRegexSpecialCharacters("["));
assertEquals("\\Q]\\E", MacrosUpdater.escapeRegexSpecialCharacters("]"));
assertEquals("\\Q(\\E", MacrosUpdater.escapeRegexSpecialCharacters("("));
assertEquals("\\Q)\\E", MacrosUpdater.escapeRegexSpecialCharacters(")"));
assertEquals("\\Q|\\E", MacrosUpdater.escapeRegexSpecialCharacters("|"));
assertEquals("\\Q\\\\E", MacrosUpdater.escapeRegexSpecialCharacters("\\"));
assertEquals("\\Q/\\E", MacrosUpdater.escapeRegexSpecialCharacters("/"));

// Test a combined string with multiple special characters
assertEquals("\\Q(a*b+c)\\E", MacrosUpdater.escapeRegexSpecialCharacters("(a*b+c)"));

// Test string with no special regex characters (should remain unchanged)
assertEquals("\\Qabc123\\E", MacrosUpdater.escapeRegexSpecialCharacters("abc123"));

// Test an empty string input (should remain unchanged)
assertEquals("\\Q\\E", MacrosUpdater.escapeRegexSpecialCharacters(""));

// Test complex string containing both escaped special characters and normal characters
assertEquals("\\Qabc(def)[ghi].\\E", MacrosUpdater.escapeRegexSpecialCharacters("abc(def)[ghi]."));
}

@Test
public void testEscapeXmlSpecialCharacters() {
assertEquals("&lt;tag&gt;", MacrosUpdater.escapeXmlSpecialCharacters("<tag>"));
assertEquals("Fish &amp; Chips", MacrosUpdater.escapeXmlSpecialCharacters("Fish & Chips"));
assertEquals("&quot;Hello&quot;", MacrosUpdater.escapeXmlSpecialCharacters("\"Hello\""));
assertEquals("It&apos;s mine", MacrosUpdater.escapeXmlSpecialCharacters("It's mine"));
}

@Test
public void testEscapeWindowsCmdSpecialCharacters() {
assertEquals("^&^|^<^>", MacrosUpdater.escapeWindowsCmdSpecialCharacters("&|<>"));
assertEquals("Hello ^^World", MacrosUpdater.escapeWindowsCmdSpecialCharacters("Hello ^World"));
assertEquals("foo^(bar^)", MacrosUpdater.escapeWindowsCmdSpecialCharacters("foo(bar)"));
assertEquals("^\"Hello^\"", MacrosUpdater.escapeWindowsCmdSpecialCharacters("\"Hello\""));
}

@Test
public void testEscapePowershellSpecialCharacters() {
assertEquals(
"This is a test with `\"double quotes`\" and `$variables`.",
MacrosUpdater.escapePowershellSpecialCharacters("This is a test with \"double quotes\" and $variables.")
);
assertEquals(
"Escaped characters: `\n, `\t, `\r, `$, `{, `}, `[, `], `#, and `\0`.",
MacrosUpdater.escapePowershellSpecialCharacters("Escaped characters: \n, \t, \r, $, {, }, [, ], #, and \0.")
);
assertEquals(
"No special characters here",
MacrosUpdater.escapePowershellSpecialCharacters("No special characters here")
);
}

@Test
public void testEscapeBashSpecialCharacters() {
assertEquals("\\$\\!\\*", MacrosUpdater.escapeBashSpecialCharacters("$!*"));
assertEquals("\\&\\|\\<\\>", MacrosUpdater.escapeBashSpecialCharacters("&|<>"));
assertEquals("\\\\'Hello\\\\'", MacrosUpdater.escapeBashSpecialCharacters("'Hello'"));
assertEquals("\\\\\"Hello\\\\\"", MacrosUpdater.escapeBashSpecialCharacters("\"Hello\""));
}

@Test
public void testEscapeSqlSpecialCharacters() {
assertEquals("O''Reilly", MacrosUpdater.escapeSqlSpecialCharacters("O'Reilly"));
assertEquals("Line\\nBreak", MacrosUpdater.escapeSqlSpecialCharacters("Line\nBreak"));
assertEquals("Column\\tTab", MacrosUpdater.escapeSqlSpecialCharacters("Column\tTab"));
assertEquals("\\\\\"Quote\\\\\"", MacrosUpdater.escapeSqlSpecialCharacters("\"Quote\""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import org.sentrysoftware.http.HttpResponse;
import org.sentrysoftware.metricshub.engine.common.exception.RetryableException;
import org.sentrysoftware.metricshub.engine.common.helpers.LoggingHelper;
import org.sentrysoftware.metricshub.engine.common.helpers.MacrosUpdater;
import org.sentrysoftware.metricshub.engine.common.helpers.StringHelper;
import org.sentrysoftware.metricshub.engine.connector.model.common.ResultContent;
import org.sentrysoftware.metricshub.engine.strategy.utils.RetryOperation;
import org.sentrysoftware.metricshub.engine.telemetry.TelemetryManager;
import org.sentrysoftware.metricshub.extension.http.utils.Body;
import org.sentrysoftware.metricshub.extension.http.utils.Header;
import org.sentrysoftware.metricshub.extension.http.utils.HttpMacrosUpdater;
import org.sentrysoftware.metricshub.extension.http.utils.HttpRequest;
import org.sentrysoftware.metricshub.extension.http.utils.UrlHelper;

Expand Down Expand Up @@ -89,7 +89,7 @@ public String executeHttp(

// Update macros in the authentication token
final String httpRequestAuthToken = httpRequest.getAuthenticationToken();
final String authenticationToken = HttpMacrosUpdater.update(
final String authenticationToken = MacrosUpdater.update(
httpRequestAuthToken,
username,
password,
Expand Down Expand Up @@ -135,10 +135,10 @@ public String executeHttp(
final String httpRequestPath = httpRequest.getPath();

// Update the known HTTP macros, and return empty if the httpRequestPath is null
final String path = HttpMacrosUpdater.update(httpRequestPath, username, password, authenticationToken, hostname);
final String path = MacrosUpdater.update(httpRequestPath, username, password, authenticationToken, hostname);

// Update the known HTTP macros, and return empty if the httpRequestUrl is null
final String url = HttpMacrosUpdater.update(httpRequestUrl, username, password, authenticationToken, hostname);
final String url = MacrosUpdater.update(httpRequestUrl, username, password, authenticationToken, hostname);

// Build the full URL
final String fullUrl = UrlHelper.format(protocol, hostname, httpConfiguration.getPort(), path, url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.sentrysoftware.metricshub.engine.common.helpers.MacrosUpdater;
import org.sentrysoftware.metricshub.engine.connector.model.common.EmbeddedFile;

/**
Expand All @@ -53,7 +54,7 @@ public String getContent(String username, char[] password, String authentication
return EMPTY;
}

return HttpMacrosUpdater.update(body.getContentAsString(), username, password, authenticationToken, hostname);
return MacrosUpdater.update(body.getContentAsString(), username, password, authenticationToken, hostname);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.function.UnaryOperator;
import org.sentrysoftware.metricshub.engine.common.helpers.MacrosUpdater;

/**
* Represents the header of an HTTP request.
Expand Down Expand Up @@ -108,7 +109,7 @@ static Map<String, String> resolveAndParseHeader(
String authenticationToken,
String hostname
) {
final String resolvedHeader = HttpMacrosUpdater.update(header, username, password, authenticationToken, hostname);
final String resolvedHeader = MacrosUpdater.update(header, username, password, authenticationToken, hostname);

return parseHeader(resolvedHeader);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.sentrysoftware.metricshub.engine.common.helpers.MacrosUpdater;

/**
* Represents an HTTP request body with a string content.
Expand All @@ -46,7 +47,7 @@ public class StringBody implements Body {

@Override
public String getContent(String username, char[] password, String authenticationToken, @NonNull String hostname) {
return HttpMacrosUpdater.update(body, username, password, authenticationToken, hostname);
return MacrosUpdater.update(body, username, password, authenticationToken, hostname);
}

@Override
Expand Down
Loading