Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
* Use isClusterPerm instead of requestedResolved.isLocalAll() to determine if action is a cluster action ([#5445](https://github.com/opensearch-project/security/pull/5445))
* Fix config update with deprecated config types failing in mixed clusters ([#5456](https://github.com/opensearch-project/security/pull/5456))
* Fix usage of jwt_clock_skew_tolerance_seconds in HTTPJwtAuthenticator ([#5506](https://github.com/opensearch-project/security/pull/5506))
* Always install demo certs if configured with demo certs ([#5517](https://github.com/opensearch-project/security/pull/5517))

### Refactoring

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public CertificateGenerator(Installer installer) {
public void createDemoCertificates() {
for (Certificates cert : Certificates.values()) {
String filePath = this.installer.OPENSEARCH_CONF_DIR + File.separator + cert.getFileName();
File file = new File(filePath);
if (file.exists()) {
System.out.println("File " + filePath + " already exists. Skipping.");
continue;
}
try {
FileWriter fileWriter = new FileWriter(filePath, StandardCharsets.UTF_8);
fileWriter.write(cert.getContent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Installer {
private static Installer instance;

private static SecuritySettingsConfigurer securitySettingsConfigurer;
private static CertificateGenerator certificateGenerator;
static CertificateGenerator certificateGenerator;

boolean assumeyes = false;
boolean initsecurity = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.yaml.snakeyaml.Yaml;

import static org.opensearch.security.DefaultObjectMapper.YAML_MAPPER;
import static org.opensearch.security.tools.democonfig.Installer.certificateGenerator;

/**
* This class updates the security related configuration, as needed.
Expand Down Expand Up @@ -105,6 +106,59 @@ public void configureSecuritySettings() throws IOException {
writeSecurityConfigToOpenSearchYML();
}

boolean isSecurityPluginIsConfiguredWithDemoCerts() {
if (installer.OPENSEARCH_CONF_FILE == null || !new File(installer.OPENSEARCH_CONF_FILE).exists()) {
return false;
}

try (BufferedReader br = new BufferedReader(new FileReader(installer.OPENSEARCH_CONF_FILE, StandardCharsets.UTF_8))) {
Yaml yaml = new Yaml();
Map<String, Object> yamlData = yaml.load(br);
if (yamlData == null) return false;

String[] requiredSettings = {
"plugins.security.ssl.transport.pemcert_filepath",
"plugins.security.ssl.transport.pemkey_filepath",
"plugins.security.ssl.transport.pemtrustedcas_filepath",
"plugins.security.ssl.http.pemcert_filepath",
"plugins.security.ssl.http.pemkey_filepath",
"plugins.security.ssl.http.pemtrustedcas_filepath" };

String[] expectedValues = { "esnode.pem", "esnode-key.pem", "root-ca.pem", "esnode.pem", "esnode-key.pem", "root-ca.pem" };

for (int i = 0; i < requiredSettings.length; i++) {
String value = getNestedValue(yamlData, requiredSettings[i]);
if (!expectedValues[i].equals(value)) {
return false;
}
}
return true;
} catch (IOException e) {
return false;
}
}

@SuppressWarnings("unchecked")
private String getNestedValue(Map<String, Object> yamlData, String key) {
// Check for flattened key first
if (yamlData.containsKey(key)) {
Object value = yamlData.get(key);
return value instanceof String ? (String) value : null;
}

// Check for nested structure
String[] parts = key.split("\\.");
Object current = yamlData;
for (String part : parts) {
if (current instanceof Map) {
current = ((Map<String, Object>) current).get(part);
} else {
return null;
}
}
return current instanceof String ? (String) current : null;
}

/**
* Checks if security plugin is already configured. If so, the script execution will exit.
*/
Expand All @@ -119,6 +173,9 @@ void checkIfSecurityPluginIsAlreadyConfigured() {
// Check for flat keys
for (String key : yamlData.keySet()) {
if (key.startsWith("plugins.security")) {
if (isSecurityPluginIsConfiguredWithDemoCerts()) {
certificateGenerator.createDemoCertificates();
}
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
installer.getExitHandler().exit(installer.skip_updates);
}
Expand All @@ -128,6 +185,9 @@ void checkIfSecurityPluginIsAlreadyConfigured() {
Map<String, Object> plugins = (Map<String, Object>) yamlData.get("plugins");
for (String key : plugins.keySet()) {
if (key.startsWith("security")) {
if (isSecurityPluginIsConfiguredWithDemoCerts()) {
certificateGenerator.createDemoCertificates();
}
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
installer.getExitHandler().exit(installer.skip_updates);
}
Expand Down
Loading