Skip to content

Commit 66d2ec1

Browse files
committed
Require keystore file and classname when generating a valid secure config, safely resolve keystore file and classname from the settings if available. If they both not available, it might be that user intentionally turned off the keystore otherwise require both.
1 parent f705a9d commit 66d2ec1

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

logstash-core/src/main/java/org/logstash/execution/AbstractPipelineExt.java

+24-10
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,8 @@ private AbstractPipelineExt initialize(final ThreadContext context,
281281
}
282282
}
283283
boolean supportEscapes = getSetting(context, "config.support_escapes").isTrue();
284-
try (ConfigVariableExpander cve = new ConfigVariableExpander(getSecretStore(context), EnvironmentVariableProvider.defaultProvider())) {
284+
try (ConfigVariableExpander cve = new ConfigVariableExpander(getSecretStore(context),
285+
EnvironmentVariableProvider.defaultProvider())) {
285286
lir = ConfigCompiler.configToPipelineIR(configParts, supportEscapes, cve);
286287
} catch (InvalidIRException iirex) {
287288
throw new IllegalArgumentException(iirex);
@@ -842,15 +843,28 @@ protected final boolean hasSetting(final ThreadContext context, final String nam
842843
}
843844

844845
protected SecretStore getSecretStore(final ThreadContext context) {
845-
String keystoreFile = hasSetting(context, "keystore.file")
846-
? getSetting(context, "keystore.file").asJavaString()
847-
: null;
848-
String keystoreClassname = hasSetting(context, "keystore.classname")
849-
? getSetting(context, "keystore.classname").asJavaString()
850-
: null;
851-
return (keystoreFile != null && keystoreClassname != null)
852-
? SecretStoreExt.getIfExists(keystoreFile, keystoreClassname)
853-
: null;
846+
final String keystoreFile = safelyGetSettingValueAsString(context, "keystore.file");
847+
final String keystoreClassname = safelyGetSettingValueAsString(context, "keystore.classname");
848+
if (keystoreFile == null && keystoreClassname == null) {
849+
// explicitly set keystore and classname null
850+
return null;
851+
}
852+
853+
if (keystoreFile == null | keystoreClassname == null) {
854+
throw new IllegalStateException("Setting `keystore.file` requires `keystore.classname`, or vice versa");
855+
}
856+
return SecretStoreExt.getIfExists(keystoreFile, keystoreClassname);
857+
}
858+
859+
private String safelyGetSettingValueAsString(final ThreadContext context, final String settingName) {
860+
final boolean hasKeystoreFileSetting = hasSetting(context, settingName);
861+
if (hasKeystoreFileSetting) {
862+
final IRubyObject keystoreFileSettingValue = getSetting(context, settingName);
863+
if (!keystoreFileSettingValue.isNil()) {
864+
return keystoreFileSettingValue.asJavaString();
865+
}
866+
}
867+
return null;
854868
}
855869

856870
private AbstractNamespacedMetricExt getDlqMetric(final ThreadContext context) {

logstash-core/src/main/java/org/logstash/secret/store/SecretStoreExt.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,20 @@ public class SecretStoreExt {
3131

3232
private static final SecretStoreFactory SECRET_STORE_FACTORY = SecretStoreFactory.fromEnvironment();
3333

34-
public static SecureConfig getConfig(String keystoreFile, String keystoreClassname) {
34+
public static SecureConfig getConfig(final String keystoreFile, final String keystoreClassname) {
3535
return getSecureConfig(RubyUtil.RUBY.getENV(), keystoreFile, keystoreClassname);
3636
}
3737

38-
private static SecureConfig getSecureConfig(RubyHash env, String file, String classname) {
38+
private static SecureConfig getSecureConfig(final RubyHash env, final String file, final String classname) {
3939
String keystorePass = (String) env.get("LOGSTASH_KEYSTORE_PASS");
4040
return getSecureConfig(file, keystorePass, classname);
4141
}
4242

43-
private static SecureConfig getSecureConfig(String keystoreFile, String keystorePass, String keystoreClassname) {
43+
private static SecureConfig getSecureConfig(final String keystoreFile, final String keystorePass, final String keystoreClassname) {
44+
if (keystoreFile == null || keystoreClassname == null) {
45+
throw new IllegalArgumentException("`keystore.file` and `keystore.classname` cannot be null");
46+
}
47+
4448
SecureConfig sc = new SecureConfig();
4549
sc.add("keystore.file", keystoreFile.toCharArray());
4650
if (keystorePass != null) {
@@ -50,18 +54,18 @@ private static SecureConfig getSecureConfig(String keystoreFile, String keystore
5054
return sc;
5155
}
5256

53-
public static boolean exists(String keystoreFile, String keystoreClassname) {
57+
public static boolean exists(final String keystoreFile, final String keystoreClassname) {
5458
return SECRET_STORE_FACTORY.exists(getConfig(keystoreFile, keystoreClassname));
5559
}
5660

57-
public static SecretStore getIfExists(String keystoreFile, String keystoreClassname) {
61+
public static SecretStore getIfExists(final String keystoreFile, final String keystoreClassname) {
5862
SecureConfig sc = getConfig(keystoreFile, keystoreClassname);
5963
return SECRET_STORE_FACTORY.exists(sc)
6064
? SECRET_STORE_FACTORY.load(sc)
6165
: null;
6266
}
6367

64-
public static SecretIdentifier getStoreId(String id) {
68+
public static SecretIdentifier getStoreId(final String id) {
6569
return new SecretIdentifier(id);
6670
}
6771
}

0 commit comments

Comments
 (0)