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

[SDKS-7516] Create FlagSetSanitizer to validate flag sets #435

Merged
merged 9 commits into from
Sep 14, 2023
6 changes: 3 additions & 3 deletions client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Properties;
import java.util.concurrent.ThreadFactory;

import static io.split.client.utils.FlagSetSanitizer.sanitizeFlagSet;

/**
* Configurations for the SplitClient.
*
Expand Down Expand Up @@ -195,7 +197,6 @@ private SplitClientConfig(String endpoint,
_threadFactory = threadFactory;
_flagSetsFilter = flagSetsFilter;


Properties props = new Properties();
try {
props.load(this.getClass().getClassLoader().getResourceAsStream("splitversion.properties"));
Expand Down Expand Up @@ -921,8 +922,7 @@ public Builder customStorageWrapper(CustomStorageWrapper customStorageWrapper) {
* @return this builder
*/
public Builder flagSetsFilter(List<String> flagSetsFilter) {
_flagSetsFilter = flagSetsFilter;
//TODO Apply validations
_flagSetsFilter = sanitizeFlagSet(flagSetsFilter);
return this;
}

Expand Down
46 changes: 46 additions & 0 deletions client/src/main/java/io/split/client/utils/FlagSetSanitizer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.split.client.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public final class FlagSetSanitizer {
nmayorsplit marked this conversation as resolved.
Show resolved Hide resolved

private static final String FLAG_SET_REGEX = "^[a-z0-9][_a-z0-9]{0,49}$";
private static final Logger _log = LoggerFactory.getLogger(FlagSetSanitizer.class);

private FlagSetSanitizer() {
throw new IllegalStateException("Utility class");
}

public static List<String> sanitizeFlagSet(List<String> flagSets) {
nmayorsplit marked this conversation as resolved.
Show resolved Hide resolved
if (flagSets == null || flagSets.isEmpty()) {
_log.error("FlagSets must be a non-empty list.");
return new ArrayList<>();
}
HashSet<String> sanitizedFlagSets = new HashSet<>();
for (String flagSet: flagSets) {
if(flagSet != flagSet.toLowerCase()) {
_log.warn(String.format("Flag Set name %s should be all lowercase - converting string to lowercase", flagSet));
flagSet = flagSet.toLowerCase();
}
if (flagSet.trim() != flagSet) {
_log.warn(String.format("Flag Set name %s has extra whitespace, trimming", flagSet));
flagSet = flagSet.trim();
}
if (!Pattern.matches(FLAG_SET_REGEX, flagSet)) {
_log.warn(String.format("you passed %s, Flag Set must adhere to the regular expressions %s. This means an Flag Set must be " +
"start with a letter, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.",
flagSet, FLAG_SET_REGEX, flagSet));
continue;
}
sanitizedFlagSets.add(flagSet);
}
return sanitizedFlagSets.stream().sorted().collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ public void checkDefaultRateForFeatureAndSegment() {

@Test
public void checkSetFlagSetsFilter() {
List<String> sets = Stream.of("test1", "test2").collect(Collectors.toList());
List<String> sets = Stream.of("test1", "test2", "TEST3", "test-4").collect(Collectors.toList());
SplitClientConfig config = SplitClientConfig.builder().flagSetsFilter(sets).build();
Assert.assertNotNull(config.getSetsFilter());
Assert.assertEquals(2, config.getSetsFilter().size());
Assert.assertEquals(3, config.getSetsFilter().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.split.client.utils;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class FlagSetSanitizerTest {

@Test
public void testEmptyFlagSets() {
List<String> flagSets = new ArrayList<>();
List<String> cleanFlagSets = FlagSetSanitizer.sanitizeFlagSet(flagSets);
Assert.assertTrue(cleanFlagSets.isEmpty());
}

@Test
public void testUpperFlagSets() {
List<String> flagSets = new ArrayList<>();
flagSets.add("Test1");
flagSets.add("TEST2");
List<String> cleanFlagSets = FlagSetSanitizer.sanitizeFlagSet(flagSets);
Assert.assertEquals("test1", cleanFlagSets.get(0));
Assert.assertEquals("test2", cleanFlagSets.get(1));
}

@Test
public void testTrimFlagSets() {
List<String> flagSets = new ArrayList<>();
flagSets.add(" test1");
flagSets.add(" test2 ");
List<String> cleanFlagSets = FlagSetSanitizer.sanitizeFlagSet(flagSets);
Assert.assertEquals("test1", cleanFlagSets.get(0));
Assert.assertEquals("test2", cleanFlagSets.get(1));
}

@Test
public void testRegexFlagSets() {
List<String> flagSets = new ArrayList<>();
flagSets.add(" test1");
flagSets.add(" test-2 ");
List<String> cleanFlagSets = FlagSetSanitizer.sanitizeFlagSet(flagSets);
Assert.assertEquals(1, cleanFlagSets.size());
Assert.assertEquals("test1", cleanFlagSets.get(0));
}

@Test
public void testDuplicateFlagSets() {
List<String> flagSets = new ArrayList<>();
flagSets.add(" test1");
flagSets.add(" test1 ");
List<String> cleanFlagSets = FlagSetSanitizer.sanitizeFlagSet(flagSets);
Assert.assertEquals(1, cleanFlagSets.size());
Assert.assertEquals("test1", cleanFlagSets.get(0));
}
}