Skip to content

Commit

Permalink
Reset files
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Crawford <[email protected]>
  • Loading branch information
stephen-crawford committed Sep 8, 2023
1 parent 33bab6b commit 5134cd8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,13 @@ private SecurityDynamicConfiguration<?> toConfig(GetResponse singleGetResponse,
configVersion,
seqNo,
primaryTerm,
acceptInvalid,
settings
acceptInvalid
);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("Unable to load " + id + " with version " + configVersion + " - Try loading legacy format ...");
}
return SecurityDynamicConfiguration.fromJson(jsonAsString, CType.fromString(id), 0, seqNo, primaryTerm, acceptInvalid, settings);
return SecurityDynamicConfiguration.fromJson(jsonAsString, CType.fromString(id), 0, seqNo, primaryTerm, acceptInvalid);
}
}
return SecurityDynamicConfiguration.fromJson(
Expand All @@ -325,8 +324,7 @@ private SecurityDynamicConfiguration<?> toConfig(GetResponse singleGetResponse,
configVersion,
seqNo,
primaryTerm,
acceptInvalid,
settings
acceptInvalid
);

} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,44 +143,36 @@ private ConfigurationRepository(
createSecurityIndexIfAbsent();
waitForSecurityIndexToBeAtLeastYellow();

ConfigHelper.uploadFile(client, cd + "config.yml", securityIndex, CType.CONFIG, DEFAULT_CONFIG_VERSION, false, settings);
ConfigHelper.uploadFile(client, cd + "roles.yml", securityIndex, CType.ROLES, DEFAULT_CONFIG_VERSION, false, settings);
ConfigHelper.uploadFile(client, cd + "config.yml", securityIndex, CType.CONFIG, DEFAULT_CONFIG_VERSION);
ConfigHelper.uploadFile(client, cd + "roles.yml", securityIndex, CType.ROLES, DEFAULT_CONFIG_VERSION);
ConfigHelper.uploadFile(
client,
cd + "roles_mapping.yml",
securityIndex,
CType.ROLESMAPPING,
DEFAULT_CONFIG_VERSION,
false,
settings
DEFAULT_CONFIG_VERSION
);
ConfigHelper.uploadFile(
client,
cd + "internal_users.yml",
securityIndex,
CType.INTERNALUSERS,
DEFAULT_CONFIG_VERSION,
false,
settings
DEFAULT_CONFIG_VERSION
);
ConfigHelper.uploadFile(
client,
cd + "action_groups.yml",
securityIndex,
CType.ACTIONGROUPS,
DEFAULT_CONFIG_VERSION,
false,
settings
DEFAULT_CONFIG_VERSION
);
if (DEFAULT_CONFIG_VERSION == 2) {
ConfigHelper.uploadFile(
client,
cd + "tenants.yml",
securityIndex,
CType.TENANTS,
DEFAULT_CONFIG_VERSION,
false,
settings
DEFAULT_CONFIG_VERSION
);
}
final boolean populateEmptyIfFileMissing = true;
Expand All @@ -190,32 +182,29 @@ private ConfigurationRepository(
securityIndex,
CType.NODESDN,
DEFAULT_CONFIG_VERSION,
populateEmptyIfFileMissing,
settings
populateEmptyIfFileMissing
);
ConfigHelper.uploadFile(
client,
cd + "whitelist.yml",
securityIndex,
CType.WHITELIST,
DEFAULT_CONFIG_VERSION,
populateEmptyIfFileMissing,
settings
populateEmptyIfFileMissing
);
ConfigHelper.uploadFile(
client,
cd + "allowlist.yml",
securityIndex,
CType.ALLOWLIST,
DEFAULT_CONFIG_VERSION,
populateEmptyIfFileMissing,
settings
populateEmptyIfFileMissing
);

// audit.yml is not packaged by default
final String auditConfigPath = cd + "audit.yml";
if (new File(auditConfigPath).exists()) {
ConfigHelper.uploadFile(client, auditConfigPath, securityIndex, CType.AUDIT, DEFAULT_CONFIG_VERSION, false, settings);
ConfigHelper.uploadFile(client, auditConfigPath, securityIndex, CType.AUDIT, DEFAULT_CONFIG_VERSION);
}
}
} else {
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/org/opensearch/security/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,37 +314,4 @@ public static void saveAndUpdateConfigs(
throw ExceptionsHelper.convertToOpenSearchException(e);
}
}

/**
* This method updates the default admin password by modifying the internal user store on node start.
*
* @param plainTextPassword The default password to be used -- specified in the opensearch.yml file
*/
public void setDefaultAdminPassword(String plainTextPassword) {
final SecurityDynamicConfiguration<?> internalUsersConfiguration = load(getUserConfigName(), false);

try {

if (!internalUsersConfiguration.exists("admin")) {
throw new UserServiceException(FAILED_ACCOUNT_RETRIEVAL_MESSAGE);
}

DefaultObjectMapper mapper = new DefaultObjectMapper();
JsonNode accountDetails = mapper.readTree(internalUsersConfiguration.getCEntry("admin").toString());
final ObjectNode contentAsNode = (ObjectNode) accountDetails;
contentAsNode.put("hash", hash(plainTextPassword.toCharArray()));

internalUsersConfiguration.remove("admin");
contentAsNode.remove("name");
internalUsersConfiguration.putCObject(
"admin",
DefaultObjectMapper.readTree(contentAsNode, internalUsersConfiguration.getImplementingClass())
);
saveAndUpdateConfigs(getUserConfigName().toString(), client, CType.INTERNALUSERS, internalUsersConfiguration);
} catch (IOException e) {
throw ExceptionsHelper.convertToOpenSearchException(e);
} catch (UserServiceException e) {
throw ExceptionsHelper.convertToOpenSearchException(e);
}
}
}
7 changes: 3 additions & 4 deletions src/test/java/org/opensearch/security/ConfigTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.junit.Test;

import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.security.securityconf.Migration;
Expand Down Expand Up @@ -112,8 +111,8 @@ private void check(String file, CType cType) throws Exception {
Assert.assertNotNull(dc);
// Assert.assertTrue(dc.getCEntries().size() > 0);
String jsonSerialize = DefaultObjectMapper.objectMapper.writeValueAsString(dc);
SecurityDynamicConfiguration<?> conf = SecurityDynamicConfiguration.fromJson(jsonSerialize, cType, configVersion, 0, 0, Settings.EMPTY);
SecurityDynamicConfiguration.fromJson(Strings.toString(XContentType.JSON, conf), cType, configVersion, 0, 0, Settings.EMPTY);
SecurityDynamicConfiguration<?> conf = SecurityDynamicConfiguration.fromJson(jsonSerialize, cType, configVersion, 0, 0);
SecurityDynamicConfiguration.fromJson(Strings.toString(XContentType.JSON, conf), cType, configVersion, 0, 0);

}

Expand All @@ -126,6 +125,6 @@ private SecurityDynamicConfiguration<?> load(String file, CType cType) throws Ex
Assert.assertEquals(jsonNode.get("_meta").get("type").asText(), cType.toLCString());
configVersion = jsonNode.get("_meta").get("config_version").asInt();
}
return SecurityDynamicConfiguration.fromNode(jsonNode, cType, configVersion, 0, 0, Settings.EMPTY);
return SecurityDynamicConfiguration.fromNode(jsonNode, cType, configVersion, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void setupRolesConfiguration() throws IOException {
config.set("rest_api_admin_role", objectMapper.createObjectNode().set("cluster_permissions", array));
config.set("regular_role", objectMapper.createObjectNode().set("cluster_permissions", objectMapper.createArrayNode().add("*")));

rolesConfiguration = SecurityDynamicConfiguration.fromJson(objectMapper.writeValueAsString(config), CType.ROLES, 2, 1, 1, Settings.EMPTY);
rolesConfiguration = SecurityDynamicConfiguration.fromJson(objectMapper.writeValueAsString(config), CType.ROLES, 2, 1, 1);
when(configurationRepository.getConfigurationsFromIndex(List.of(CType.ROLES), false)).thenReturn(
Map.of(CType.ROLES, rolesConfiguration)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public void onChangeVerifyReadonlyFields() throws Exception {
CType.AUDIT,
2,
1,
1,
Settings.EMPTY
1
);
final var result = auditApiActionEndpointValidator.onConfigChange(
SecurityConfiguration.of(objectMapper.valueToTree(AuditConfig.from(Settings.EMPTY)), "config", dynamicConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.rest.RestRequest;
import org.opensearch.security.DefaultObjectMapper;
Expand Down Expand Up @@ -73,8 +72,7 @@ public void setupRolesAndMappings() throws IOException {
CType.ROLES,
2,
1,
1,
Settings.EMPTY
1
);
when(configurationRepository.getConfigurationsFromIndex(List.of(CType.ROLESMAPPING), false)).thenReturn(
Map.of(CType.ROLESMAPPING, rolesMappingConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.opensearch.common.settings.Settings;
import org.opensearch.security.DefaultObjectMapper;
import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
Expand All @@ -39,7 +38,7 @@ public void setConfiguration() throws Exception {
config.set("_meta", objectMapper.createObjectNode().put("type", CType.ROLES.toLCString()).put("config_version", 2));
config.set("kibana_read_only", objectMapper.createObjectNode().put("reserved", true));
config.set("security_rest_api_access", objectMapper.createObjectNode().put("reserved", true));
configuration = SecurityDynamicConfiguration.fromJson(objectMapper.writeValueAsString(config), CType.ROLES, 2, 1, 1, Settings.EMPTY);
configuration = SecurityDynamicConfiguration.fromJson(objectMapper.writeValueAsString(config), CType.ROLES, 2, 1, 1);
}

@Test
Expand Down

0 comments on commit 5134cd8

Please sign in to comment.