Skip to content

Commit

Permalink
Closes #758 - Use case-insensitive check for agent-mapping assignment (
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusoe authored May 28, 2020
1 parent ed27ce1 commit d0a55b0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private synchronized void replaceConfigurations(List<AgentConfiguration> newConf
@Override
public AgentConfiguration load(Map<String, String> agentAttributes) {
return newConfigurations.stream()
.filter(config -> config.getMapping().matchesAttributes(agentAttributes))
.filter(configuration -> configuration.getMapping().matchesAttributes(agentAttributes))
.findFirst()
.orElse(NO_MATCHING_MAPPING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
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;

import javax.validation.constraints.NotBlank;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
* The model of the agent mappings.
Expand Down Expand Up @@ -54,8 +56,13 @@ public AgentMapping(@JsonProperty("name") String name, @JsonProperty("sources")
*/
public boolean matchesAttributes(Map<String, String> agentAttributes) {
for (Map.Entry<String, String> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> attributes = new HashMap<>();
attributes.put("first", "01234");
attributes.put("second", "AnAttribute");
attributes.put("third", "\\d+");
List<String> 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<String, String> attributes = ImmutableMap.of("first", "01234", "second", "AnAttribute", "third", "0");

boolean result = mapping.matchesAttributes(attributes);

assertThat(result).isTrue();
}

@Test
public void differentCasing() {
Map<String, String> attributes = ImmutableMap.of("first", "01234", "second", "anaTTribute", "third", "0");

boolean result = mapping.matchesAttributes(attributes);

assertThat(result).isTrue();
}

@Test
public void differentAttributes() {
Map<String, String> attributes = ImmutableMap.of("first", "00000", "second", "AnAttribute", "third", "0");

boolean result = mapping.matchesAttributes(attributes);

assertThat(result).isFalse();
}

@Test
public void emptyAttributes() {
Map<String, String> attributes = Collections.emptyMap();

boolean result = mapping.matchesAttributes(attributes);

assertThat(result).isFalse();
}

@Test
public void wrongRegexAttribute() {
Map<String, String> attributes = ImmutableMap.of("first", "01234", "second", "AnAttribute", "third", "");

boolean result = mapping.matchesAttributes(attributes);

assertThat(result).isFalse();
}
}

}

0 comments on commit d0a55b0

Please sign in to comment.