Skip to content
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

Cluster state role mapper file settings service #107886

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
141dd1b
WIP
albertzaharovits Apr 12, 2024
86f4901
Reverted some unrelated changes
albertzaharovits Apr 15, 2024
7115015
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 15, 2024
b382940
Ooops
albertzaharovits Apr 15, 2024
31aa857
Settings with false as the default
albertzaharovits Apr 15, 2024
d499cb4
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 15, 2024
ae87cb2
Some ClusterStateRoleMapperTests
albertzaharovits Apr 15, 2024
94c6b2b
testRoleMappingChangesTriggerRealmCacheClear
albertzaharovits Apr 16, 2024
90b1925
ExpressionRoleMapping.resolveRoles
albertzaharovits Apr 16, 2024
8c7184d
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 16, 2024
8836846
testRealmsRegisterForRefreshAtRoleMapper
albertzaharovits Apr 16, 2024
1623412
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 22, 2024
2099f75
ClusterStateRoleMapper extends AbstractRoleMapperClearRealmCache
albertzaharovits Apr 22, 2024
0fafa37
ExpressionRoleMapping javadoc
albertzaharovits Apr 22, 2024
3e34893
Merge fallout
albertzaharovits Apr 22, 2024
c390ab1
Javadocs
albertzaharovits Apr 22, 2024
782d4c8
Spotless
albertzaharovits Apr 22, 2024
1a9fa21
SecuritySettingsSource with UnregistedSecuritySettingsPlugin
albertzaharovits Apr 22, 2024
9d8512e
JwtRoleMappingsIntegTests
albertzaharovits Apr 23, 2024
242c3d2
Fix readDiffFrom
albertzaharovits Apr 23, 2024
81d0df8
More JwtRoleMappingsIntegTests
albertzaharovits Apr 23, 2024
465d027
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 23, 2024
d0bdda5
Ooops
albertzaharovits Apr 23, 2024
fafa59b
Nit
albertzaharovits Apr 23, 2024
7153c7a
Nit
albertzaharovits Apr 23, 2024
8952d4e
Ooops containsInAnyOrder
albertzaharovits Apr 23, 2024
c89a3f6
MustachePlugin.class
albertzaharovits Apr 24, 2024
0bbfb73
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 24, 2024
651bad9
Spotless
albertzaharovits Apr 24, 2024
7d7480b
Update docs/changelog/107410.yaml
albertzaharovits Apr 24, 2024
2a94670
clearRoleMappings
albertzaharovits Apr 24, 2024
522306e
RoleMappingMetadataTests
albertzaharovits Apr 24, 2024
45d4c88
Nit
albertzaharovits Apr 24, 2024
5c5c24e
CompositeRoleMapperTests
albertzaharovits Apr 24, 2024
4cb5b4a
Persistent metadata
albertzaharovits Apr 24, 2024
dca53a9
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 24, 2024
0043bf2
put and delete roles actions are not reserved anymore
albertzaharovits Apr 25, 2024
a3aa1a8
Almost?
albertzaharovits Apr 25, 2024
58b479b
Almost almost
albertzaharovits Apr 25, 2024
ad17e95
Fix compile RoleMappingFileSettingsIT
albertzaharovits Apr 25, 2024
c2e6ee9
Removed FileSettingsRoleMappingsStartupIT
albertzaharovits Apr 25, 2024
e8bf5e9
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 25, 2024
749e323
Merge branch 'cluster-state-role-mapper' into cluster-state-role-mapp…
albertzaharovits Apr 25, 2024
6083838
RoleMappingFileSettingsIT WIP
albertzaharovits Apr 25, 2024
30d8b4d
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 25, 2024
206f645
Merge branch 'cluster-state-role-mapper' into cluster-state-role-mapp…
albertzaharovits Apr 25, 2024
7b8b3b8
Fix RoleMappingFileSettingsIT
albertzaharovits Apr 25, 2024
4bc6ea8
Update docs/changelog/107886.yaml
albertzaharovits Apr 25, 2024
9e1e5e6
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 30, 2024
4135aeb
Serialization tests
albertzaharovits Apr 30, 2024
8d0ce73
Merge branch 'main' into cluster-state-role-mapper
albertzaharovits Apr 30, 2024
806cfce
Merge branch 'cluster-state-role-mapper' into cluster-state-role-mapp…
albertzaharovits Apr 30, 2024
1e91590
Merge branch 'main' into cluster-state-role-mapper-file-settings-service
albertzaharovits Apr 30, 2024
678e9fe
Merge branch 'main' into cluster-state-role-mapper-file-settings-service
albertzaharovits May 1, 2024
bd74cee
Merge branch 'main' into cluster-state-role-mapper-file-settings-service
albertzaharovits May 1, 2024
8ba564d
Merge branch 'main' into cluster-state-role-mapper-file-settings-service
albertzaharovits May 1, 2024
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
5 changes: 5 additions & 0 deletions docs/changelog/107886.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 107886
summary: Cluster state role mapper file settings service
area: Authorization
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,4 @@ public void writeTo(StreamOutput out) throws IOException {
public ExpressionRoleMapping getMapping() {
return new ExpressionRoleMapping(name, rules, roles, roleTemplates, metadata, enabled);
}

public static PutRoleMappingRequest fromMapping(ExpressionRoleMapping mapping) {
var request = new PutRoleMappingRequest();
request.setName(mapping.getName());
request.setEnabled(mapping.isEnabled());
request.setRoles(mapping.getRoles());
request.setRoleTemplates(mapping.getRoleTemplates());
request.setRules(mapping.getExpression());
request.setMetadata(mapping.getMetadata());

return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.support.WriteRequestBuilder;
import org.elasticsearch.client.internal.ElasticsearchClient;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping;
import org.elasticsearch.xpack.core.security.authc.support.mapper.TemplateRoleName;
import org.elasticsearch.xpack.core.security.authc.support.mapper.expressiondsl.RoleMapperExpression;
Expand All @@ -35,8 +34,8 @@ public PutRoleMappingRequestBuilder(ElasticsearchClient client) {
/**
* Populate the put role request from the source and the role's name
*/
public PutRoleMappingRequestBuilder source(String name, BytesReference source, XContentType xContentType) throws IOException {
ExpressionRoleMapping mapping = ExpressionRoleMapping.parse(name, source, xContentType);
public PutRoleMappingRequestBuilder source(String name, XContentParser parser) throws IOException {
ExpressionRoleMapping mapping = ExpressionRoleMapping.parse(name, parser);
request.setName(name);
request.setEnabled(mapping.isEnabled());
request.setRoles(mapping.getRoles());
Expand Down

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,7 @@ Collection<Object> createComponents(
new SecurityUsageServices(realms, allRolesStore, nativeRoleMappingStore, ipFilter.get(), profileService, apiKeyService)
);

reservedRoleMappingAction.set(new ReservedRoleMappingAction(nativeRoleMappingStore));
systemIndices.getMainIndexManager().onStateRecovered(state -> reservedRoleMappingAction.get().securityIndexRecovered());
reservedRoleMappingAction.set(new ReservedRoleMappingAction());

cacheInvalidatorRegistry.validate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@

package org.elasticsearch.xpack.security.action.rolemapping;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.GroupedActionListener;
import org.elasticsearch.common.util.concurrent.ListenableFuture;
import org.elasticsearch.reservedstate.NonStateTransformResult;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.reservedstate.ReservedClusterStateHandler;
import org.elasticsearch.reservedstate.TransformState;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xpack.core.security.action.rolemapping.DeleteRoleMappingRequest;
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequest;
import org.elasticsearch.xpack.core.security.action.rolemapping.PutRoleMappingRequestBuilder;
import org.elasticsearch.xpack.core.security.authc.support.mapper.ExpressionRoleMapping;
import org.elasticsearch.xpack.security.authc.support.mapper.NativeRoleMappingStore;
import org.elasticsearch.xpack.core.security.authz.RoleMappingMetadata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -38,123 +32,59 @@
* It is used by the ReservedClusterStateService to add/update or remove role mappings. Typical usage
* for this action is in the context of file based settings.
*/
public class ReservedRoleMappingAction implements ReservedClusterStateHandler<List<ExpressionRoleMapping>> {
public class ReservedRoleMappingAction implements ReservedClusterStateHandler<List<PutRoleMappingRequest>> {
public static final String NAME = "role_mappings";

private final NativeRoleMappingStore roleMappingStore;
private final ListenableFuture<Void> securityIndexRecoveryListener = new ListenableFuture<>();

/**
* Creates a ReservedRoleMappingAction
*
* @param roleMappingStore requires {@link NativeRoleMappingStore} for storing/deleting the mappings
*/
public ReservedRoleMappingAction(NativeRoleMappingStore roleMappingStore) {
this.roleMappingStore = roleMappingStore;
}

@Override
public String name() {
return NAME;
}

private static Collection<PutRoleMappingRequest> prepare(List<ExpressionRoleMapping> roleMappings) {
List<PutRoleMappingRequest> requests = roleMappings.stream().map(rm -> PutRoleMappingRequest.fromMapping(rm)).toList();

var exceptions = new ArrayList<Exception>();
for (var request : requests) {
// File based defined role mappings are allowed to use MetadataUtils.RESERVED_PREFIX
var exception = request.validate(false);
if (exception != null) {
exceptions.add(exception);
}
}

if (exceptions.isEmpty() == false) {
var illegalArgumentException = new IllegalArgumentException("error on validating put role mapping requests");
exceptions.forEach(illegalArgumentException::addSuppressed);
throw illegalArgumentException;
}

return requests;
}

@Override
public TransformState transform(Object source, TransformState prevState) throws Exception {
// We execute the prepare() call to catch any errors in the transform phase.
// Since we store the role mappings outside the cluster state, we do the actual save with a
// non cluster state transform call.
@SuppressWarnings("unchecked")
var requests = prepare((List<ExpressionRoleMapping>) source);
return new TransformState(
prevState.state(),
prevState.keys(),
l -> securityIndexRecoveryListener.addListener(
ActionListener.wrap(ignored -> nonStateTransform(requests, prevState, l), l::onFailure)
)
);
}

// Exposed for testing purposes
protected void nonStateTransform(
Collection<PutRoleMappingRequest> requests,
TransformState prevState,
ActionListener<NonStateTransformResult> listener
) {
Set<String> entities = requests.stream().map(r -> r.getName()).collect(Collectors.toSet());
Set<String> toDelete = new HashSet<>(prevState.keys());
toDelete.removeAll(entities);

final int tasksCount = requests.size() + toDelete.size();

// Nothing to do, don't start a group listener with 0 actions
if (tasksCount == 0) {
listener.onResponse(new NonStateTransformResult(ReservedRoleMappingAction.NAME, Set.of()));
return;
}

GroupedActionListener<Boolean> taskListener = new GroupedActionListener<>(tasksCount, new ActionListener<>() {
@Override
public void onResponse(Collection<Boolean> booleans) {
listener.onResponse(new NonStateTransformResult(ReservedRoleMappingAction.NAME, Collections.unmodifiableSet(entities)));
}

@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});

for (var request : requests) {
roleMappingStore.putRoleMapping(request, taskListener);
}

for (var mappingToDelete : toDelete) {
var deleteRequest = new DeleteRoleMappingRequest();
deleteRequest.setName(mappingToDelete);
roleMappingStore.deleteRoleMapping(deleteRequest, taskListener);
Set<ExpressionRoleMapping> roleMappings = validate((List<PutRoleMappingRequest>) source);
RoleMappingMetadata newRoleMappingMetadata = new RoleMappingMetadata(roleMappings);
if (newRoleMappingMetadata.equals(RoleMappingMetadata.getFromClusterState(prevState.state()))) {
return prevState;
} else {
ClusterState newState = newRoleMappingMetadata.updateClusterState(prevState.state());
Set<String> entities = newRoleMappingMetadata.getRoleMappings()
.stream()
.map(ExpressionRoleMapping::getName)
.collect(Collectors.toSet());
return new TransformState(newState, entities);
}
}

@Override
public List<ExpressionRoleMapping> fromXContent(XContentParser parser) throws IOException {
List<ExpressionRoleMapping> result = new ArrayList<>();

public List<PutRoleMappingRequest> fromXContent(XContentParser parser) throws IOException {
List<PutRoleMappingRequest> result = new ArrayList<>();
Map<String, ?> source = parser.map();

for (String name : source.keySet()) {
@SuppressWarnings("unchecked")
Map<String, ?> content = (Map<String, ?>) source.get(name);
try (XContentParser mappingParser = mapToXContentParser(XContentParserConfiguration.EMPTY, content)) {
ExpressionRoleMapping mapping = ExpressionRoleMapping.parse(name, mappingParser);
result.add(mapping);
result.add(new PutRoleMappingRequestBuilder(null).source(name, mappingParser).request());
}
}

return result;
}

public void securityIndexRecovered() {
securityIndexRecoveryListener.onResponse(null);
private Set<ExpressionRoleMapping> validate(List<PutRoleMappingRequest> roleMappings) {
var exceptions = new ArrayList<Exception>();
for (var roleMapping : roleMappings) {
// File based defined role mappings are allowed to use MetadataUtils.RESERVED_PREFIX
var exception = roleMapping.validate(false);
if (exception != null) {
exceptions.add(exception);
}
}
if (exceptions.isEmpty() == false) {
var illegalArgumentException = new IllegalArgumentException("error on validating put role mapping requests");
exceptions.forEach(illegalArgumentException::addSuppressed);
throw illegalArgumentException;
}
return roleMappings.stream().map(PutRoleMappingRequest::getMapping).collect(Collectors.toUnmodifiableSet());
}
}
Loading