diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/agentconfiguration/AgentConfigurationManager.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/agentconfiguration/AgentConfigurationManager.java index 8108076886..e0cb7ed1b9 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/agentconfiguration/AgentConfigurationManager.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/agentconfiguration/AgentConfigurationManager.java @@ -100,7 +100,7 @@ private synchronized void replaceConfigurations(List newConf @Override public AgentConfiguration load(Map agentAttributes) { return newConfigurations.stream() - .filter(config -> config.getMapping().matchesAttributes(agentAttributes)) + .filter(configuration -> configuration.getMapping().matchesAttributes(agentAttributes)) .findFirst() .orElse(NO_MATCHING_MAPPING); } diff --git a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/mappings/model/AgentMapping.java b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/mappings/model/AgentMapping.java index 6c0549f628..f0c24020b1 100644 --- a/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/mappings/model/AgentMapping.java +++ b/components/inspectit-ocelot-configurationserver/src/main/java/rocks/inspectit/ocelot/mappings/model/AgentMapping.java @@ -6,6 +6,7 @@ import lombok.Builder; import lombok.Singular; import lombok.Value; +import org.apache.commons.lang3.StringUtils; import rocks.inspectit.ocelot.security.audit.AuditDetail; import rocks.inspectit.ocelot.security.audit.Auditable; @@ -13,6 +14,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; /** * The model of the agent mappings. @@ -54,8 +56,13 @@ public AgentMapping(@JsonProperty("name") String name, @JsonProperty("sources") */ public boolean matchesAttributes(Map agentAttributes) { for (Map.Entry pair : attributes.entrySet()) { + String pattern = pair.getValue(); String value = agentAttributes.getOrDefault(pair.getKey(), ""); - if (!value.matches(pair.getValue())) { + + boolean matches = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE) + .matcher(value) + .matches(); + if (!matches) { return false; } } diff --git a/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/mappings/model/AgentMappingTest.java b/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/mappings/model/AgentMappingTest.java new file mode 100644 index 0000000000..2aa484cb44 --- /dev/null +++ b/components/inspectit-ocelot-configurationserver/src/test/java/rocks/inspectit/ocelot/mappings/model/AgentMappingTest.java @@ -0,0 +1,81 @@ +package rocks.inspectit.ocelot.mappings.model; + +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.*; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +class AgentMappingTest { + + private AgentMapping mapping; + + @BeforeEach + public void beforeEach() { + Map attributes = new HashMap<>(); + attributes.put("first", "01234"); + attributes.put("second", "AnAttribute"); + attributes.put("third", "\\d+"); + List sources = new ArrayList<>(); + sources.add("/one"); + sources.add("/two/a.yml"); + sources.add("/two/b.yml"); + mapping = new AgentMapping("test-mapping", sources, attributes); + } + + @Nested + class MatchesAttributes { + + @Test + public void sameAttributes() { + Map attributes = ImmutableMap.of("first", "01234", "second", "AnAttribute", "third", "0"); + + boolean result = mapping.matchesAttributes(attributes); + + assertThat(result).isTrue(); + } + + @Test + public void differentCasing() { + Map attributes = ImmutableMap.of("first", "01234", "second", "anaTTribute", "third", "0"); + + boolean result = mapping.matchesAttributes(attributes); + + assertThat(result).isTrue(); + } + + @Test + public void differentAttributes() { + Map attributes = ImmutableMap.of("first", "00000", "second", "AnAttribute", "third", "0"); + + boolean result = mapping.matchesAttributes(attributes); + + assertThat(result).isFalse(); + } + + @Test + public void emptyAttributes() { + Map attributes = Collections.emptyMap(); + + boolean result = mapping.matchesAttributes(attributes); + + assertThat(result).isFalse(); + } + + @Test + public void wrongRegexAttribute() { + Map attributes = ImmutableMap.of("first", "01234", "second", "AnAttribute", "third", ""); + + boolean result = mapping.matchesAttributes(attributes); + + assertThat(result).isFalse(); + } + } + +} \ No newline at end of file