-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-1464 - Vault alias decorator (#304)
* vault alias decorator * fix tests * refactor * fix it * tests
- Loading branch information
Showing
9 changed files
with
365 additions
and
18 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
larky/src/main/java/com/verygood/security/larky/modules/DecoratorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.verygood.security.larky.modules; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public class DecoratorConfig { | ||
|
||
public static class InvalidDecoratorConfigException extends RuntimeException { | ||
|
||
public InvalidDecoratorConfigException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public static class NonLuhnValidTransformPattern { | ||
|
||
private final String search; | ||
private final String replace; | ||
} | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
public static class NonLuhnValidPattern { | ||
|
||
private final String validatePattern; | ||
private final List<NonLuhnValidTransformPattern> transformPatterns; | ||
} | ||
|
||
private final String searchPattern; | ||
private final String replacePattern; | ||
private final NonLuhnValidPattern nonLuhnValidPattern; | ||
|
||
|
||
public static DecoratorConfig fromObject(Object decoratorConfig) { | ||
if (!(decoratorConfig instanceof Map)) { | ||
return null; | ||
} | ||
Map map = (Map) decoratorConfig; | ||
|
||
DecoratorConfigBuilder decoratorConfigBuilder = DecoratorConfig.builder(); | ||
|
||
decoratorConfigBuilder.searchPattern(getString(map, "searchPattern")); | ||
decoratorConfigBuilder.replacePattern(getString(map, "replacePattern")); | ||
|
||
Map nonLuhnValidPattern = getMap(map, "nonLuhnValidPattern"); | ||
if (nonLuhnValidPattern != null) { | ||
NonLuhnValidPattern.NonLuhnValidPatternBuilder nonLuhnValidPatternBuilder = NonLuhnValidPattern.builder(); | ||
nonLuhnValidPatternBuilder.validatePattern(getString(nonLuhnValidPattern, "validatePattern")); | ||
ImmutableList.Builder<NonLuhnValidTransformPattern> transformPatterns = ImmutableList.builder(); | ||
for (Object transformPattern : getList(nonLuhnValidPattern, "transformPatterns")) { | ||
NonLuhnValidTransformPattern.NonLuhnValidTransformPatternBuilder transformPatternBuilder = NonLuhnValidTransformPattern.builder(); | ||
Map transformPatternMap = toMap(transformPattern); | ||
transformPatternBuilder.search(getString(transformPatternMap, "search")); | ||
transformPatternBuilder.replace(getString(transformPatternMap, "replace")); | ||
transformPatterns.add(transformPatternBuilder.build()); | ||
} | ||
nonLuhnValidPatternBuilder.transformPatterns(transformPatterns.build()); | ||
decoratorConfigBuilder.nonLuhnValidPattern(nonLuhnValidPatternBuilder.build()); | ||
} | ||
return decoratorConfigBuilder.build(); | ||
} | ||
|
||
private static Map toMap(Object obj) { | ||
if (obj == null) { | ||
return null; | ||
} | ||
if (!(obj instanceof Map)) { | ||
throw new InvalidDecoratorConfigException( | ||
String.format("'%s' must be dict", obj) | ||
); | ||
} | ||
return (Map) obj; | ||
} | ||
|
||
private static String getString(Map map, String field) { | ||
if (!map.containsKey(field)) { | ||
return null; | ||
} | ||
Object value = map.get(field); | ||
if (!(value instanceof String)) { | ||
throw new InvalidDecoratorConfigException( | ||
String.format("'%s' field must be string", field) | ||
); | ||
} | ||
return (String) value; | ||
} | ||
|
||
private static Map getMap(Map map, String field) { | ||
if (!map.containsKey(field)) { | ||
return null; | ||
} | ||
Object value = map.get(field); | ||
if (!(value instanceof Map)) { | ||
throw new InvalidDecoratorConfigException( | ||
String.format("'%s' field must be dict", field) | ||
); | ||
} | ||
return (Map) value; | ||
} | ||
|
||
private static List getList(Map map, String field) { | ||
if (!map.containsKey(field)) { | ||
return Collections.emptyList(); | ||
} | ||
Object value = map.get(field); | ||
if (!(value instanceof List)) { | ||
throw new InvalidDecoratorConfigException( | ||
String.format("'%s' field must be array", field) | ||
); | ||
} | ||
return (List) value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
larky/src/main/java/com/verygood/security/larky/modules/vgs/vault/NoopVault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../src/main/java/com/verygood/security/larky/modules/vgs/vault/defaults/AliasDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.verygood.security.larky.modules.vgs.vault.defaults; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import net.starlark.java.eval.EvalException; | ||
|
||
public class AliasDecorator implements TokenizeFunction { | ||
|
||
private final TokenizeFunction tokenizeFunction; | ||
private final Pattern searchPattern; | ||
private final String replacePattern; | ||
|
||
|
||
public AliasDecorator( | ||
TokenizeFunction tokenizeFunction, | ||
String searchPattern, | ||
String replacePattern) { | ||
this.tokenizeFunction = tokenizeFunction; | ||
this.searchPattern = Pattern.compile(searchPattern); | ||
this.replacePattern = replacePattern; | ||
} | ||
|
||
@Override | ||
public String tokenize(String toTokenize) throws EvalException { | ||
|
||
final Matcher matcher = searchPattern.matcher(toTokenize); | ||
|
||
if (!matcher.find()) { | ||
// Fallback to generic | ||
return new UUIDAliasGenerator().generate(toTokenize); | ||
|
||
} | ||
return tokenize(matcher, replacePattern); | ||
} | ||
|
||
private String tokenize(Matcher matcher, String replacePattern) throws EvalException { | ||
final String tokenGroup = matcher.group("token"); | ||
String tokenized = tokenizeFunction.tokenize(tokenGroup); | ||
String preFormatted = replacePattern.replace("${token}", "%s"); | ||
return matcher.replaceFirst(String.format(preFormatted, tokenized)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...rc/main/java/com/verygood/security/larky/modules/vgs/vault/defaults/TokenizeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.verygood.security.larky.modules.vgs.vault.defaults; | ||
|
||
import net.starlark.java.eval.EvalException; | ||
|
||
@FunctionalInterface | ||
public interface TokenizeFunction { | ||
|
||
String tokenize(String value) throws EvalException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.