|
23 | 23 | import org.apache.logging.log4j.Logger; |
24 | 24 | import org.apache.logging.log4j.LogManager; |
25 | 25 | import org.apache.logging.log4j.util.Strings; |
| 26 | +import org.logstash.plugins.ConfigVariableExpander; |
26 | 27 |
|
27 | 28 | import java.util.Locale; |
28 | 29 | import java.util.regex.Pattern; |
|
33 | 34 | */ |
34 | 35 | public class SecretIdentifier { |
35 | 36 |
|
36 | | - // aligns with ConfigVariableExpander substitution pattern |
37 | | - public final static Pattern KEY_PATTERN = Pattern.compile("[a-zA-Z_.][a-zA-Z0-9_.]*"); |
38 | | - |
39 | 37 | private final static Pattern colonPattern = Pattern.compile(":"); |
40 | 38 | private final static String VERSION = "v1"; |
41 | 39 | private final static Pattern urnPattern = Pattern.compile("urn:logstash:secret:"+ VERSION + ":.*$"); |
@@ -66,24 +64,6 @@ public static SecretIdentifier fromExternalForm(final String urn) { |
66 | 64 | return new SecretIdentifier(validateWithTransform(parts[4], "key")); |
67 | 65 | } |
68 | 66 |
|
69 | | - /** |
70 | | - * Minor validation and downcases the parts |
71 | | - * |
72 | | - * @param key The key, a part of the URN to validate. |
73 | | - * @param partName The name of the part used for logging. |
74 | | - * @return The validated and transformed part. |
75 | | - */ |
76 | | - private static String validateWithTransform(final String key, final String partName) { |
77 | | - if (key == null || key.isEmpty() || Strings.isBlank(key)) { |
78 | | - throw new IllegalArgumentException(String.format("%s may not be null or empty", partName)); |
79 | | - } |
80 | | - |
81 | | - if (!KEY_PATTERN.matcher(key).matches()) { |
82 | | - logger.warn(String.format("Invalid `%s` key appeared in keystore. Please remove it as it cannot be used in the pipelines", key)); |
83 | | - } |
84 | | - return key.toLowerCase(Locale.US); |
85 | | - } |
86 | | - |
87 | 67 | @Override |
88 | 68 | public boolean equals(Object o) { |
89 | 69 | if (this == o) return true; |
@@ -131,4 +111,31 @@ public String toExternalForm() { |
131 | 111 | public String toString() { |
132 | 112 | return toExternalForm(); |
133 | 113 | } |
| 114 | + |
| 115 | + /** |
| 116 | + * Validates the provided key against null, empty and {@link ConfigVariableExpander#KEY_PATTERN} |
| 117 | + * @param key a key to be validated |
| 118 | + * @param keyName a key name |
| 119 | + */ |
| 120 | + private static void validateKey(final String key, final String keyName) { |
| 121 | + if (key == null || key.isEmpty() || Strings.isBlank(key)) { |
| 122 | + throw new IllegalArgumentException(String.format("%s may not be null or empty", keyName)); |
| 123 | + } |
| 124 | + |
| 125 | + if (!ConfigVariableExpander.KEY_PATTERN.matcher(key).matches()) { |
| 126 | + logger.warn(String.format("Invalid secret key name `%s` provided.", key) + ConfigVariableExpander.KEY_PATTERN_DESCRIPTION); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Minor validation and downcases the parts |
| 132 | + * |
| 133 | + * @param key The key, a part of the URN to validate. |
| 134 | + * @param partName The name of the part used for logging. |
| 135 | + * @return The validated and transformed part. |
| 136 | + */ |
| 137 | + private static String validateWithTransform(final String key, final String partName) { |
| 138 | + validateKey(key, partName); |
| 139 | + return key.toLowerCase(Locale.US); |
| 140 | + } |
134 | 141 | } |
0 commit comments