Skip to content

Commit 1c2792a

Browse files
authored
Always install demo certs if configured with demo certs (#5517)
Signed-off-by: Craig Perkins <cwperx@amazon.com>
1 parent 38e6c0f commit 1c2792a

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3232
* Use isClusterPerm instead of requestedResolved.isLocalAll() to determine if action is a cluster action ([#5445](https://github.com/opensearch-project/security/pull/5445))
3333
* Fix config update with deprecated config types failing in mixed clusters ([#5456](https://github.com/opensearch-project/security/pull/5456))
3434
* Fix usage of jwt_clock_skew_tolerance_seconds in HTTPJwtAuthenticator ([#5506](https://github.com/opensearch-project/security/pull/5506))
35+
* Always install demo certs if configured with demo certs ([#5517](https://github.com/opensearch-project/security/pull/5517))
3536

3637
### Refactoring
3738

src/main/java/org/opensearch/security/tools/democonfig/CertificateGenerator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public CertificateGenerator(Installer installer) {
3333
public void createDemoCertificates() {
3434
for (Certificates cert : Certificates.values()) {
3535
String filePath = this.installer.OPENSEARCH_CONF_DIR + File.separator + cert.getFileName();
36+
File file = new File(filePath);
37+
if (file.exists()) {
38+
System.out.println("File " + filePath + " already exists. Skipping.");
39+
continue;
40+
}
3641
try {
3742
FileWriter fileWriter = new FileWriter(filePath, StandardCharsets.UTF_8);
3843
fileWriter.write(cert.getContent());

src/main/java/org/opensearch/security/tools/democonfig/Installer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Installer {
4141
private static Installer instance;
4242

4343
private static SecuritySettingsConfigurer securitySettingsConfigurer;
44-
private static CertificateGenerator certificateGenerator;
44+
static CertificateGenerator certificateGenerator;
4545

4646
boolean assumeyes = false;
4747
boolean initsecurity = false;

src/main/java/org/opensearch/security/tools/democonfig/SecuritySettingsConfigurer.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.yaml.snakeyaml.Yaml;
3838

3939
import static org.opensearch.security.DefaultObjectMapper.YAML_MAPPER;
40+
import static org.opensearch.security.tools.democonfig.Installer.certificateGenerator;
4041

4142
/**
4243
* This class updates the security related configuration, as needed.
@@ -105,6 +106,59 @@ public void configureSecuritySettings() throws IOException {
105106
writeSecurityConfigToOpenSearchYML();
106107
}
107108

109+
boolean isSecurityPluginIsConfiguredWithDemoCerts() {
110+
if (installer.OPENSEARCH_CONF_FILE == null || !new File(installer.OPENSEARCH_CONF_FILE).exists()) {
111+
return false;
112+
}
113+
114+
try (BufferedReader br = new BufferedReader(new FileReader(installer.OPENSEARCH_CONF_FILE, StandardCharsets.UTF_8))) {
115+
Yaml yaml = new Yaml();
116+
Map<String, Object> yamlData = yaml.load(br);
117+
if (yamlData == null) return false;
118+
119+
String[] requiredSettings = {
120+
"plugins.security.ssl.transport.pemcert_filepath",
121+
"plugins.security.ssl.transport.pemkey_filepath",
122+
"plugins.security.ssl.transport.pemtrustedcas_filepath",
123+
"plugins.security.ssl.http.pemcert_filepath",
124+
"plugins.security.ssl.http.pemkey_filepath",
125+
"plugins.security.ssl.http.pemtrustedcas_filepath" };
126+
127+
String[] expectedValues = { "esnode.pem", "esnode-key.pem", "root-ca.pem", "esnode.pem", "esnode-key.pem", "root-ca.pem" };
128+
129+
for (int i = 0; i < requiredSettings.length; i++) {
130+
String value = getNestedValue(yamlData, requiredSettings[i]);
131+
if (!expectedValues[i].equals(value)) {
132+
return false;
133+
}
134+
}
135+
return true;
136+
} catch (IOException e) {
137+
return false;
138+
}
139+
}
140+
141+
@SuppressWarnings("unchecked")
142+
private String getNestedValue(Map<String, Object> yamlData, String key) {
143+
// Check for flattened key first
144+
if (yamlData.containsKey(key)) {
145+
Object value = yamlData.get(key);
146+
return value instanceof String ? (String) value : null;
147+
}
148+
149+
// Check for nested structure
150+
String[] parts = key.split("\\.");
151+
Object current = yamlData;
152+
for (String part : parts) {
153+
if (current instanceof Map) {
154+
current = ((Map<String, Object>) current).get(part);
155+
} else {
156+
return null;
157+
}
158+
}
159+
return current instanceof String ? (String) current : null;
160+
}
161+
108162
/**
109163
* Checks if security plugin is already configured. If so, the script execution will exit.
110164
*/
@@ -119,6 +173,9 @@ void checkIfSecurityPluginIsAlreadyConfigured() {
119173
// Check for flat keys
120174
for (String key : yamlData.keySet()) {
121175
if (key.startsWith("plugins.security")) {
176+
if (isSecurityPluginIsConfiguredWithDemoCerts()) {
177+
certificateGenerator.createDemoCertificates();
178+
}
122179
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
123180
installer.getExitHandler().exit(installer.skip_updates);
124181
}
@@ -128,6 +185,9 @@ void checkIfSecurityPluginIsAlreadyConfigured() {
128185
Map<String, Object> plugins = (Map<String, Object>) yamlData.get("plugins");
129186
for (String key : plugins.keySet()) {
130187
if (key.startsWith("security")) {
188+
if (isSecurityPluginIsConfiguredWithDemoCerts()) {
189+
certificateGenerator.createDemoCertificates();
190+
}
131191
System.out.println(installer.OPENSEARCH_CONF_FILE + " seems to be already configured for Security. Quit.");
132192
installer.getExitHandler().exit(installer.skip_updates);
133193
}

0 commit comments

Comments
 (0)