-
Notifications
You must be signed in to change notification settings - Fork 301
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
Deprecate opendistro_security_roles and add opensearch_security_roles #5113
base: main
Are you sure you want to change the base?
Changes from 16 commits
8c6601b
54c72fa
ab26317
4b27f5c
afe19d8
cb3e1cd
879f575
23b5916
47d755a
ba574b6
20768b0
736dc78
7e0fcc9
5f10ad6
9e28622
0674469
5b85ba6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.api; | ||
|
||
import static org.opensearch.security.dlic.rest.api.InternalUsersApiAction.DIRECT_SECURITY_ROLES; | ||
|
||
public class InternalUsersRestApiDirectRolesIntegrationTest extends AbstractInternalUsersRestApiIntegrationTest { | ||
@Override | ||
protected String getRoleField() { | ||
return DIRECT_SECURITY_ROLES; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.api; | ||
|
||
import static org.opensearch.security.dlic.rest.api.InternalUsersApiAction.OPENDISTRO_SECURITY_ROLES; | ||
|
||
public class InternalUsersRestApiOpenDistroRolesIntegrationTest extends AbstractInternalUsersRestApiIntegrationTest { | ||
@Override | ||
protected String getRoleField() { | ||
return OPENDISTRO_SECURITY_ROLES; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,17 @@ | |
package org.opensearch.security.dlic.rest.api; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.inject.Inject; | ||
|
@@ -53,8 +58,17 @@ | |
|
||
public class InternalUsersApiAction extends AbstractApiAction { | ||
|
||
private static final Logger log = LogManager.getLogger(InternalUsersApiAction.class); | ||
private final PasswordHasher passwordHasher; | ||
|
||
/** | ||
* @deprecated Use direct_security_roles instead. This field will be removed in a future release. | ||
*/ | ||
@Deprecated | ||
public static final String OPENDISTRO_SECURITY_ROLES = "opendistro_security_roles"; | ||
|
||
public static final String DIRECT_SECURITY_ROLES = "direct_security_roles"; | ||
|
||
@Override | ||
protected void consumeParameters(final RestRequest request) { | ||
request.param("name"); | ||
|
@@ -209,8 +223,7 @@ ValidationResult<SecurityConfiguration> validateSecurityRoles(final SecurityConf | |
rolesConfiguration -> loadConfiguration(CType.ROLESMAPPING, false, false).map(roleMappingsConfiguration -> { | ||
final var contentAsNode = (ObjectNode) securityConfiguration.requestContent(); | ||
final var securityJsonNode = new SecurityJsonNode(contentAsNode); | ||
var securityRoles = securityJsonNode.get("opendistro_security_roles").asList(); | ||
securityRoles = securityRoles == null ? List.of() : securityRoles; | ||
var securityRoles = getRoles(securityJsonNode); | ||
final var rolesValid = endpointValidator.validateRoles(securityRoles, rolesConfiguration); | ||
if (!rolesValid.isValid()) { | ||
return ValidationResult.error(rolesValid.status(), rolesValid.errorMessage()); | ||
|
@@ -228,6 +241,37 @@ ValidationResult<SecurityConfiguration> validateSecurityRoles(final SecurityConf | |
); | ||
} | ||
|
||
/** | ||
* This method combines roles from both 'direct_security_roles' and the deprecated 'opendistro_security_roles' fields. | ||
* If the deprecated field is used, a warning message is logged. | ||
* | ||
* @param content The SecurityJsonNode containing the security configuration | ||
* @return A List of merged unique roles. Returns an empty list if no roles are found in either field. | ||
*/ | ||
List<String> getRoles(SecurityJsonNode content) { | ||
List<String> openSearchRoles = content.get(DIRECT_SECURITY_ROLES).asList(); | ||
List<String> openDistroRoles = content.get(OPENDISTRO_SECURITY_ROLES).asList(); | ||
|
||
Set<String> mergedRoles = new HashSet<>(); | ||
|
||
// Add OpenSearch roles if present | ||
if (openSearchRoles != null) { | ||
mergedRoles.addAll(openSearchRoles); | ||
} | ||
|
||
// Add OpenDistro roles if present | ||
if (openDistroRoles != null) { | ||
mergedRoles.addAll(openDistroRoles); | ||
log.warn( | ||
"The field '{}' is deprecated and will be removed in a future release. Please use '{}' instead.", | ||
OPENDISTRO_SECURITY_ROLES, | ||
DIRECT_SECURITY_ROLES | ||
); | ||
} | ||
|
||
return mergedRoles.isEmpty() ? List.of() : new ArrayList<>(mergedRoles); | ||
} | ||
|
||
ValidationResult<SecurityConfiguration> createOrUpdateAccount( | ||
final RestRequest request, | ||
final SecurityConfiguration securityConfiguration | ||
|
@@ -335,7 +379,8 @@ public Map<String, RequestContentValidator.DataType> allowedKeys() { | |
return allowedKeys.put("backend_roles", DataType.ARRAY) | ||
.put("attributes", DataType.OBJECT) | ||
.put("description", DataType.STRING) | ||
.put("opendistro_security_roles", DataType.ARRAY) | ||
.put(OPENDISTRO_SECURITY_ROLES, DataType.ARRAY) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want either to be set or both should be allowed for now? What is the plan to remove this deprecated key from allowed_keys? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The plan is to mark these deprecated in 3.0, removal in 4.0. Hence, we need to keep both in 3.0 or decide on whether we just document in 3.0 and remove cleanly in 4.0 |
||
.put(DIRECT_SECURITY_ROLES, DataType.ARRAY) | ||
.put("hash", DataType.STRING) | ||
.put("password", DataType.STRING) | ||
.build(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,6 +308,8 @@ | |
|
||
private static class InternalUsersModelV7 extends InternalUsersModel { | ||
|
||
protected final Logger log = LogManager.getLogger(InternalUsersModelV7.class); | ||
|
||
private final SecurityDynamicConfiguration<InternalUserV7> internalUserV7SecurityDynamicConfiguration; | ||
|
||
private final SecurityDynamicConfiguration<RoleV7> rolesV7SecurityDynamicConfiguration; | ||
|
@@ -357,14 +359,35 @@ | |
public List<String> getSecurityRoles(String user) { | ||
InternalUserV7 tmp = internalUserV7SecurityDynamicConfiguration.getCEntry(user); | ||
|
||
if (tmp == null) { | ||
return ImmutableList.of(); | ||
} | ||
|
||
// Log opendistro_security_roles regardless of which path we take | ||
if (tmp.getOpendistro_security_roles() != null && !tmp.getOpendistro_security_roles().isEmpty()) { | ||
log.warn( | ||
"Deprecated configuration opendistro_security_roles set for: {} " | ||
+ "opendistro_security_roles will not be used in favor of direct_security_roles.", | ||
user | ||
); | ||
} | ||
|
||
// Security roles should only contain roles that exist in the roles dynamic config. | ||
// We should filter out any roles that have hidden rolesmapping. | ||
return tmp == null | ||
? ImmutableList.of() | ||
: tmp.getOpendistro_security_roles() | ||
if (tmp.getDirect_security_roles() != null && !tmp.getDirect_security_roles().isEmpty()) { | ||
return tmp.getDirect_security_roles() | ||
.stream() | ||
.filter(role -> !isRolesMappingHidden(role) && rolesV7SecurityDynamicConfiguration.exists(role)) | ||
.collect(ImmutableList.toImmutableList()); | ||
} | ||
|
||
if (tmp.getOpendistro_security_roles() != null && !tmp.getOpendistro_security_roles().isEmpty()) { | ||
return tmp.getOpendistro_security_roles() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: line 368 logger can be moved here. |
||
.stream() | ||
Check warning on line 386 in src/main/java/org/opensearch/security/securityconf/DynamicConfigFactory.java
|
||
.filter(role -> !isRolesMappingHidden(role) && rolesV7SecurityDynamicConfiguration.exists(role)) | ||
.collect(ImmutableList.toImmutableList()); | ||
} | ||
return ImmutableList.of(); | ||
} | ||
|
||
// Remove any hidden rolesmapping from the security roles | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,13 +33,17 @@ | |
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import org.opensearch.security.securityconf.Hashed; | ||
import org.opensearch.security.securityconf.Hideable; | ||
import org.opensearch.security.securityconf.StaticDefinable; | ||
|
||
public class InternalUserV7 implements Hideable, Hashed, StaticDefinable { | ||
|
||
private final Logger log = LogManager.getLogger(InternalUserV7.class); | ||
|
||
private String hash; | ||
private boolean reserved; | ||
private boolean hidden; | ||
|
@@ -51,6 +55,7 @@ public class InternalUserV7 implements Hideable, Hashed, StaticDefinable { | |
private Map<String, String> attributes = Collections.emptyMap(); | ||
private String description; | ||
private List<String> opendistro_security_roles = Collections.emptyList(); | ||
private List<String> direct_security_roles = Collections.emptyList(); | ||
|
||
private InternalUserV7(String hash, boolean reserved, boolean hidden, List<String> backend_roles, Map<String, String> attributes) { | ||
super(); | ||
|
@@ -116,7 +121,18 @@ public List<String> getOpendistro_security_roles() { | |
} | ||
|
||
public void setOpendistro_security_roles(List<String> opendistro_security_roles) { | ||
log.warn("Deprecated configuration opendistro_security_roles set. Kindly use direct_security_roles instead."); | ||
this.opendistro_security_roles = opendistro_security_roles; | ||
this.direct_security_roles = opendistro_security_roles; | ||
} | ||
Comment on lines
123
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the delay, I was a bit busy with other stuff. I have thought about this log message again. This method will be called by Jackson when it de-serializes the internal user JSON. It will be called for each user object. As in the security config index, all user records are stored as one big JSON in a single document, Jackson needs to de-serialize all user objects, even the security plugin needs only one (for example for a REST request). This will have the effect that whenever the user config is being loaded from the index, you'll get this log message repeated for each user stored in that index. On clusters with many users, these will be quite a lot repeated log messages at once. This would happen in these cases:
I think such repeated log messages won't be helpful to the user. I would guess that we need a different solution to notify the user about the deprecated attribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No worries. Thanks for the detailed explanation. My understanding was that the log lines would be added only during initial read and during updates (limited to a specific user); And that once the configuration is in memory, it won't be fetched from the index unless a flush was called. I will remove the log line to avoid excessive logging.
Do you think documentation should be enough? something like- https://opensearch.org/docs/latest/breaking-changes/#deprecate-non-inclusive-terms There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is actually a question which I cannot answer just on my own. This really depends on the deprecation philosophy/strategy of the whole project. I am not sure if there is actually something like this formally defined. In my own humble opinion, a good solution would be like following (disclaimer: I really do not want to give a direction here, I am just sharing my private opinion): There are two main alternative options:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
nit: This might get problematic with number of clients? For e.g. client A using old attribute v/s client B using the new attribute. I am more aligned with option 1 - i.e. track with a major update if required. Alternatively, we could introduce new APIs to deprecate old functionality. i.e. new APIs return new attribute names, existing ones use old one. (would have been cleaner if this was done when APIs moved to _plugins/_security from _opendistro API paths) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, good point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we show both for now with deprecation documented on the website? We do set the direct_security_roles atttribute even when opendistro_security_roles is set, which should not affect any roles being missed in the new key when populating the deprecated key. Apologies if I didn't understand the problem correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following is really just IMHO, so please be invited to have different opinions: ;-)
|
||
|
||
public List<String> getDirect_security_roles() { | ||
return direct_security_roles; | ||
} | ||
|
||
public void setDirect_security_roles(List<String> direct_security_roles) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does the serialized JSON produced from this bean look like? I reckon, it will contain both attributes, even if one is unused, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. correct, I have not removed/modified the old attribute to keep it backwards compatible. Do you think it will lead to confusion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am generally wondering how clients consuming that API should handle this. This actually already applies to the security Dashboards plugin, which uses this API. Ideally, an API could just switch the the new attribute and use that. However, in this case, all API clients would also need to have a kind of detection mechanism to see which attribute is the one that is actually in use. This is especially critical for the PATCH API, which such clients cannot use anymore without making a GET in advance to find out what attribute would be in use. Generally, that seems to be not a good design of an API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since both the fields are maintained and updated on changes, clients using existing field will not break. As the existing field is being deprecated, guidance to use new field will be documented. As both the fields are available, I don't think a detection mechanism is required. I agree it would have been cleaner to switch to the new attribute, however I am not sure if we can do it without breaking existing clients. See example below-
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initial name was |
||
this.opendistro_security_roles = direct_security_roles; | ||
this.direct_security_roles = direct_security_roles; | ||
} | ||
|
||
public Map<String, String> getAttributes() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indirectly related: What's the migration path towards having only the new attribute?
This will only log warnings when the REST API is used. No warnings will be logged for usage of the securityadmin tool or just the existing data in the config index.
Will there be an additional change in the future to address existing data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at other migrations/deprecation of terms, I think we can announce deprecation/removal in docs.
e.g. - https://opensearch.org/docs/latest/breaking-changes/#deprecate-non-inclusive-terms
Yes, I was thinking Security plugin should log a warning during initialization if configs have
opendistro_security_role
present. will this suffice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do a clean break with a deprecation message that clearly states that this will be remove in 3.0 GA?