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

[Backport 2.x] Handle non-flat yaml settings for demo configuration detection #4798

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,30 @@ public void configureSecuritySettings() throws IOException {
/**
* Checks if security plugin is already configured. If so, the script execution will exit.
*/
@SuppressWarnings("unchecked")
void checkIfSecurityPluginIsAlreadyConfigured() {
// Check if the configuration file contains the 'plugins.security' string
// Check if the configuration file contains security settings
if (installer.OPENSEARCH_CONF_FILE != null && new File(installer.OPENSEARCH_CONF_FILE).exists()) {
try (BufferedReader br = new BufferedReader(new FileReader(installer.OPENSEARCH_CONF_FILE, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
if (line.toLowerCase().contains("plugins.security")) {
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
System.exit(installer.skip_updates);
Yaml yaml = new Yaml();
Map<String, Object> yamlData = yaml.load(br);
if (yamlData != null) {
// Check for flat keys
for (String key : yamlData.keySet()) {
if (key.startsWith("plugins.security")) {
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
System.exit(installer.skip_updates);
}
}
// Check for nested keys
if (yamlData.containsKey("plugins")) {
Map<String, Object> plugins = (Map<String, Object>) yamlData.get("plugins");
for (String key : plugins.keySet()) {
if (key.startsWith("security")) {
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
System.exit(installer.skip_updates);
}
}
}
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,24 @@ public void testCreateSecurityAdminDemoScript_invalidPath() {
}
}

@Test
public void testReadNonFlatYamlAlreadyConfigured() throws IOException {
installer.OPENSEARCH_CONF_FILE = Paths.get("src/test/resources/opensearch-config-non-flat.yaml").toFile().getAbsolutePath();
String expectedMessage = installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.";
try {
System.setSecurityManager(new NoExitSecurityManager());
securitySettingsConfigurer.checkIfSecurityPluginIsAlreadyConfigured();
} catch (SecurityException e) {
assertThat(e.getMessage(), equalTo("System.exit(-1) blocked to allow print statement testing."));
} finally {
System.setSecurityManager(null);
}
verifyStdOutContainsString(expectedMessage);

// reset the file pointer
installer.OPENSEARCH_CONF_FILE = installer.OPENSEARCH_CONF_DIR + "opensearch.yml";
}

@SuppressWarnings("unchecked")
public static void setEnv(String key, String value) throws NoSuchFieldException, IllegalAccessException {
Class<?>[] classes = Collections.class.getDeclaredClasses();
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/opensearch-config-non-flat.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins:
security:
ssl:
transport:
pemcert_filepath: esnode.pem
pemkey_filepath: esnode-key.pem
pemtrustedcas_filepath: root-ca.pem
enforce_hostname_verification: false
http:
enabled: true
pemcert_filepath: esnode.pem
pemkey_filepath: esnode-key.pem
pemtrustedcas_filepath: root-ca.pem
allow_unsafe_democertificates: true
Loading