Skip to content

Commit 0a29eae

Browse files
committed
Handle case where there are multiple services
1 parent ba8b68f commit 0a29eae

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

webapp/src/main/java/com/box/l10n/mojito/security/ServiceIdentifierParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ String parseHeader(String header) throws ServiceNotIdentifiableException {
1313
if (serviceIdentifierParserConfig.isEnabled()) {
1414
return keyValueParser.parseHeader(
1515
header,
16+
serviceIdentifierParserConfig.getServiceInstanceDelimiter(),
1617
serviceIdentifierParserConfig.getKeyValueDelimiter(),
1718
serviceIdentifierParserConfig.getValueDelimiter(),
1819
serviceIdentifierParserConfig.getIdentifierKey());

webapp/src/main/java/com/box/l10n/mojito/security/ServiceIdentifierParserConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
@Component
77
public class ServiceIdentifierParserConfig {
8+
@Value("${l10n.spring.security.header.parser.serviceInstanceDelimiter:,}")
9+
protected String serviceInstanceDelimiter;
10+
811
@Value("${l10n.spring.security.header.parser.keyValueDelimiter:;}")
912
protected String keyValueDelimiter;
1013

@@ -48,4 +51,12 @@ public String getIdentifierKey() {
4851
public void setIdentifierKey(String identifierKey) {
4952
this.identifierKey = identifierKey;
5053
}
54+
55+
public String getServiceInstanceDelimiter() {
56+
return serviceInstanceDelimiter;
57+
}
58+
59+
public void setServiceInstanceDelimiter(String serviceInstanceDelimiter) {
60+
this.serviceInstanceDelimiter = serviceInstanceDelimiter;
61+
}
5162
}

webapp/src/main/java/com/box/l10n/mojito/security/ServiceKeyValueParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ public class ServiceKeyValueParser {
1414
Logger logger = LoggerFactory.getLogger(ServiceKeyValueParser.class);
1515

1616
public String parseHeader(
17-
String header, String keyValueDelimiter, String valueDelimiter, String identifierKey)
17+
String header, String serviceInstanceDelimiter, String keyValueDelimiter, String valueDelimiter, String identifierKey)
1818
throws ServiceNotIdentifiableException {
1919
logger.debug("Parsing header: {}", header);
2020
if (header == null || header.isEmpty()) {
2121
throw new ServiceNotIdentifiableException("Service identifier not found");
2222
}
2323

24+
String[] serviceIdentifiers = header.split(serviceInstanceDelimiter);
25+
// Similarly to pinboard. We assume that only the last service identifier is relevant
26+
String relevantService = serviceIdentifiers[serviceIdentifiers.length - 1];
27+
2428
Map<String, String> keyValueMap =
25-
Stream.of(header.split(keyValueDelimiter))
29+
Stream.of(relevantService.split(keyValueDelimiter))
2630
.map(kv -> kv.split(valueDelimiter))
2731
.filter(pair -> pair.length == 2)
2832
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1]));

webapp/src/test/java/com/box/l10n/mojito/security/ServiceKeyValueParserTest.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,37 @@ public void setUp() {
1717

1818
@Test
1919
public void testParseHeader_EmptyHeaderOrNull() {
20-
Executable executable = () -> parser.parseHeader("", ",", "=", "id");
20+
Executable executable = () -> parser.parseHeader("", "/", ",", "=", "id");
2121
assertThrows(ServiceNotIdentifiableException.class, executable);
22-
executable = () -> parser.parseHeader(null, ",", "=", "id");
22+
executable = () -> parser.parseHeader(null,"&", ",", "=", "id");
2323
assertThrows(ServiceNotIdentifiableException.class, executable);
2424
}
2525

2626
@Test
2727
public void testParseHeader_ValidCsvInput() {
2828
String header = "key1=value1,key2=value2,id=someService";
29-
String result = parser.parseHeader(header, ",", "=", "id");
29+
String result = parser.parseHeader(header, "&", ",", "=", "id");
3030
assertEquals("someService", result);
3131
}
3232

33+
@Test
34+
public void testParseHeader_ValidMultipleServiceInstanceInput() {
35+
String header = "key1=value1,key2=value2,id=firstService&id=lastService";
36+
String result = parser.parseHeader(header, "&", ",", "=", "id");
37+
assertEquals("lastService", result);
38+
}
39+
3340
@Test
3441
public void testParseHeader_ValidSemicolonInput() {
3542
String header = "key1:=value1;key2:=value2;id:=someService";
36-
String result = parser.parseHeader(header, ";", ":=", "id");
43+
String result = parser.parseHeader(header, "&",";", ":=", "id");
3744
assertEquals("someService", result);
3845
}
3946

4047
@Test
4148
public void testParseHeader_NoIdentifierKey() {
4249
String header = "key1=value1,key2=value2";
43-
Executable executable = () -> parser.parseHeader(header, ",", "=", "id");
50+
Executable executable = () -> parser.parseHeader(header, "&",",", "=", "id");
4451
ServiceNotIdentifiableException exception =
4552
assertThrows(ServiceNotIdentifiableException.class, executable);
4653
assertEquals("Service identifier not found", exception.getMessage());
@@ -49,14 +56,14 @@ public void testParseHeader_NoIdentifierKey() {
4956
@Test
5057
public void testParseHeader_InvalidFormat() {
5158
String header = "key1=value1&key2value2&id=someService";
52-
Executable executable = () -> parser.parseHeader(header, ";", "=", "id");
59+
Executable executable = () -> parser.parseHeader(header, "-",";", "=", "id");
5360
assertThrows(ServiceNotIdentifiableException.class, executable);
5461
}
5562

5663
@Test
5764
public void testParseHeader_EmptyServiceIdentifier() {
5865
String header = "key1=value1,key2=value2,id=";
59-
Executable executable = () -> parser.parseHeader(header, ",", "=", "id");
66+
Executable executable = () -> parser.parseHeader(header, "&",",", "=", "id");
6067
ServiceNotIdentifiableException exception =
6168
assertThrows(ServiceNotIdentifiableException.class, executable);
6269
assertEquals("Service identifier not found", exception.getMessage());

0 commit comments

Comments
 (0)