Skip to content

Commit

Permalink
Updates the way keys are looked up in yaml file
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 12, 2023
1 parent 3366949 commit 13e7e11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Installer {

final String FILE_EXTENSION;

static final File RPM_DEB_OPENSEARCH_HOME = new File("/usr/share/opensearch");
static File RPM_DEB_OPENSEARCH_HOME = new File("/usr/share/opensearch");

private final Options options;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;

import org.opensearch.common.settings.Settings;
import org.opensearch.security.DefaultObjectMapper;
import org.opensearch.security.dlic.rest.validation.PasswordValidator;
import org.opensearch.security.dlic.rest.validation.RequestContentValidator;
import org.opensearch.security.tools.Hasher;
Expand Down Expand Up @@ -67,6 +70,7 @@ public class SecuritySettingsConfigurer {
".geospatial-ip2geo-data*"
);
static String ADMIN_PASSWORD = "";
static String ADMIN_USERNAME = "admin";

private final Installer installer;

Expand Down Expand Up @@ -120,7 +124,7 @@ void updateAdminPassword() {
// If script execution environment is set to demo, validate custom password, else if set to test, skip validation
if (shouldValidatePassword
&& !ADMIN_PASSWORD.isEmpty()
&& passwordValidator.validate("admin", ADMIN_PASSWORD) != RequestContentValidator.ValidationError.NONE) {
&& passwordValidator.validate(ADMIN_USERNAME, ADMIN_PASSWORD) != RequestContentValidator.ValidationError.NONE) {
System.out.println("Password " + ADMIN_PASSWORD + " is weak. Please re-try with a stronger password.");
System.exit(-1);
}
Expand Down Expand Up @@ -286,8 +290,8 @@ Map<String, Object> buildSecurityConfigMap() {
*/
static boolean isNetworkHostAlreadyPresent(String filePath) {
try {
String searchString = "network.host:";
return isStringAlreadyPresentInFile(filePath, searchString);
String searchString = "network.host";
return isStringAlreadyPresentInYMLFile(filePath, searchString);
} catch (IOException e) {
return false;
}
Expand All @@ -300,30 +304,29 @@ static boolean isNetworkHostAlreadyPresent(String filePath) {
*/
static boolean isNodeMaxLocalStorageNodesAlreadyPresent(String filePath) {
try {
String searchString = "node.max_local_storage_nodes:";
return isStringAlreadyPresentInFile(filePath, searchString);
String searchString = "node.max_local_storage_nodes";
return isStringAlreadyPresentInYMLFile(filePath, searchString);
} catch (IOException e) {
return false;
}
}

/**
* Checks if given string is already present in the file
* @param filePath path to file in which given string should be searched
* @param searchString the string to be searched for
* Checks if given string is already present in the yml file
* @param filePath path to yml file in which given string should be searched
* @param fieldName the field name to be searched for
* @return true if string is present, false otherwise
* @throws IOException if there was exception reading the file
*/
static boolean isStringAlreadyPresentInFile(String filePath, String searchString) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(searchString)) {
return true;
}
}
static boolean isStringAlreadyPresentInYMLFile(String filePath, String fieldName) throws IOException {
JsonNode node;
try {
node = DefaultObjectMapper.YAML_MAPPER.readTree(new File(filePath));
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;

return node.has(fieldName);
}

/**
Expand Down

0 comments on commit 13e7e11

Please sign in to comment.