-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Query built in roles #111407
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
Closed
albertzaharovits
wants to merge
11
commits into
elastic:main
from
albertzaharovits:query-built-in-roles
Closed
Query built in roles #111407
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
948de97
set index metadata propery action
albertzaharovits 1d890e5
Merge branch 'main' into query-built-in-roles
albertzaharovits ed2987f
WIP
albertzaharovits 4a4a68e
Done
albertzaharovits 4bcdf93
Fix :x-pack:plugin:security:compileTestJava
albertzaharovits eb93aaf
Merge branch 'main' into query-built-in-roles
albertzaharovits d39fc78
Fix metadata for all reserved
albertzaharovits 209e1bb
Merge branch 'main' into query-built-in-roles
albertzaharovits d20872f
Merge branch 'main' into query-built-in-roles
albertzaharovits e85881b
Spotless
albertzaharovits 640e1df
Fix QueryRoleIT
albertzaharovits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
197 changes: 197 additions & 0 deletions
197
...ain/java/org/elasticsearch/xpack/core/security/action/SetIndexMetadataPropertyAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.action; | ||
|
|
||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.action.ActionType; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.master.TransportMasterNodeAction; | ||
| import org.elasticsearch.cluster.ClusterState; | ||
| import org.elasticsearch.cluster.ClusterStateTaskListener; | ||
| import org.elasticsearch.cluster.SimpleBatchedExecutor; | ||
| import org.elasticsearch.cluster.block.ClusterBlockException; | ||
| import org.elasticsearch.cluster.block.ClusterBlockLevel; | ||
| import org.elasticsearch.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; | ||
| import org.elasticsearch.cluster.metadata.Metadata; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.cluster.service.MasterServiceTaskQueue; | ||
| import org.elasticsearch.common.Priority; | ||
| import org.elasticsearch.common.collect.ImmutableOpenMap; | ||
| import org.elasticsearch.common.inject.Inject; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.core.Tuple; | ||
| import org.elasticsearch.index.Index; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class SetIndexMetadataPropertyAction extends ActionType<SetIndexMetadataPropertyResponse> { | ||
|
|
||
| public static final String NAME = "indices:internal/admin/metadata/custom/set"; | ||
|
|
||
| private SetIndexMetadataPropertyAction() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| public static final SetIndexMetadataPropertyAction INSTANCE = new SetIndexMetadataPropertyAction(); | ||
|
|
||
| public static class TransportAction extends TransportMasterNodeAction< | ||
| SetIndexMetadataPropertyRequest, | ||
| SetIndexMetadataPropertyResponse> { | ||
| private final MasterServiceTaskQueue< | ||
| SetIndexMetadataPropertyAction.TransportAction.SetIndexMetadataPropertyTask> setIndexMetadataPropertyTaskMasterServiceTaskQueue; | ||
|
|
||
| @Inject | ||
| public TransportAction( | ||
| TransportService transportService, | ||
| ClusterService clusterService, | ||
| ThreadPool threadPool, | ||
| ActionFilters actionFilters, | ||
| IndexNameExpressionResolver indexNameExpressionResolver | ||
| ) { | ||
| super( | ||
| SetIndexMetadataPropertyAction.NAME, | ||
| transportService, | ||
| clusterService, | ||
| threadPool, | ||
| actionFilters, | ||
| SetIndexMetadataPropertyRequest::new, | ||
| indexNameExpressionResolver, | ||
| SetIndexMetadataPropertyResponse::new, | ||
| threadPool.executor(ThreadPool.Names.MANAGEMENT) | ||
| ); | ||
| this.setIndexMetadataPropertyTaskMasterServiceTaskQueue = clusterService.createTaskQueue( | ||
| "set-index-metadata-property-task-queue", | ||
| Priority.LOW, | ||
| SET_INDEX_METADATA_PROPERTY_TASK_VOID_SIMPLE_BATCHED_EXECUTOR | ||
| ); | ||
| } | ||
|
|
||
| private static final SimpleBatchedExecutor< | ||
| SetIndexMetadataPropertyAction.TransportAction.SetIndexMetadataPropertyTask, | ||
| Map<String, String>> SET_INDEX_METADATA_PROPERTY_TASK_VOID_SIMPLE_BATCHED_EXECUTOR = new SimpleBatchedExecutor<>() { | ||
| @Override | ||
| public Tuple<ClusterState, Map<String, String>> executeTask( | ||
| SetIndexMetadataPropertyAction.TransportAction.SetIndexMetadataPropertyTask task, | ||
| ClusterState clusterState | ||
| ) { | ||
| return task.execute(clusterState); | ||
| } | ||
|
|
||
| @Override | ||
| public void taskSucceeded( | ||
| SetIndexMetadataPropertyAction.TransportAction.SetIndexMetadataPropertyTask task, | ||
| Map<String, String> value | ||
| ) { | ||
| task.success(value); | ||
| } | ||
| }; | ||
|
|
||
| static class SetIndexMetadataPropertyTask implements ClusterStateTaskListener { | ||
| private final ActionListener<SetIndexMetadataPropertyResponse> listener; | ||
| private final Index index; | ||
| private final String key; | ||
| @Nullable | ||
| private final Map<String, String> expected; | ||
| @Nullable | ||
| private final Map<String, String> value; | ||
|
|
||
| SetIndexMetadataPropertyTask( | ||
| ActionListener<SetIndexMetadataPropertyResponse> listener, | ||
| Index index, | ||
| String key, | ||
| @Nullable Map<String, String> expected, | ||
| @Nullable Map<String, String> value | ||
| ) { | ||
| this.listener = listener; | ||
| this.index = index; | ||
| this.key = key; | ||
| this.expected = expected; | ||
| this.value = value; | ||
| } | ||
|
|
||
| Tuple<ClusterState, Map<String, String>> execute(ClusterState state) { | ||
| IndexMetadata indexMetadata = state.metadata().getIndexSafe(index); | ||
| Map<String, String> existingValue = indexMetadata.getCustomData(key); | ||
| if (Objects.equals(expected, existingValue)) { | ||
|
Contributor
Author
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 action works as a compare-and-swap rather than a simple write. |
||
| IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexMetadata); | ||
| if (value != null) { | ||
| indexMetadataBuilder.putCustom(key, value); | ||
| } else { | ||
| indexMetadataBuilder.removeCustom(key); | ||
| } | ||
| indexMetadataBuilder.version(indexMetadataBuilder.version() + 1); | ||
| ImmutableOpenMap.Builder<String, IndexMetadata> builder = ImmutableOpenMap.builder(state.metadata().indices()); | ||
| builder.put(index.getName(), indexMetadataBuilder.build()); | ||
| return new Tuple<>( | ||
| ClusterState.builder(state).metadata(Metadata.builder(state.metadata()).indices(builder.build()).build()).build(), | ||
| value | ||
| ); | ||
| } else { | ||
| // returns existing value when expectation is not met | ||
| return new Tuple<>(state, existingValue); | ||
| } | ||
| } | ||
|
|
||
| void success(Map<String, String> value) { | ||
| listener.onResponse(new SetIndexMetadataPropertyResponse(value)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Exception e) { | ||
| listener.onFailure(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected void masterOperation( | ||
| Task task, | ||
| SetIndexMetadataPropertyRequest request, | ||
| ClusterState state, | ||
| ActionListener<SetIndexMetadataPropertyResponse> listener | ||
| ) throws Exception { | ||
| Index[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request); | ||
| if (concreteIndices.length != 1) { | ||
| listener.onFailure( | ||
| new ElasticsearchException("Exactly one concrete index expected, but resolved " + Arrays.toString(concreteIndices)) | ||
| ); | ||
| return; | ||
| } | ||
| IndexMetadata indexMetadata = state.metadata().getIndexSafe(concreteIndices[0]); | ||
| Map<String, String> existingValue = indexMetadata.getCustomData(request.key()); | ||
| if (Objects.equals(request.expected(), existingValue)) { | ||
| setIndexMetadataPropertyTaskMasterServiceTaskQueue.submitTask( | ||
| "Set index metadata custom value", | ||
| new SetIndexMetadataPropertyAction.TransportAction.SetIndexMetadataPropertyTask( | ||
| listener, | ||
| concreteIndices[0], | ||
| request.key(), | ||
| request.expected(), | ||
| request.value() | ||
| ), | ||
| null | ||
| ); | ||
| } else { | ||
| // returns existing value when expectation is not met | ||
| listener.onResponse(new SetIndexMetadataPropertyResponse(existingValue)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected ClusterBlockException checkBlock(SetIndexMetadataPropertyRequest request, ClusterState state) { | ||
| return state.blocks() | ||
| .indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, indexNameExpressionResolver.concreteIndexNames(state, request)); | ||
| } | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
...in/java/org/elasticsearch/xpack/core/security/action/SetIndexMetadataPropertyRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.action; | ||
|
|
||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.IndicesRequest; | ||
| import org.elasticsearch.action.support.IndicesOptions; | ||
| import org.elasticsearch.action.support.master.MasterNodeRequest; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.core.TimeValue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class SetIndexMetadataPropertyRequest extends MasterNodeRequest<SetIndexMetadataPropertyRequest> implements IndicesRequest { | ||
| private final String index; | ||
| private final String key; | ||
| @Nullable | ||
| private final Map<String, String> expected; | ||
| @Nullable | ||
| private final Map<String, String> value; | ||
|
|
||
| public SetIndexMetadataPropertyRequest( | ||
| TimeValue timeout, | ||
| String index, | ||
| String key, | ||
| @Nullable Map<String, String> expected, | ||
| @Nullable Map<String, String> value | ||
| ) { | ||
| super(timeout); | ||
| this.index = Objects.requireNonNull(index); | ||
| this.key = Objects.requireNonNull(key); | ||
| this.expected = expected; | ||
| this.value = value; | ||
| } | ||
|
|
||
| protected SetIndexMetadataPropertyRequest(StreamInput in) throws IOException { | ||
| super(in); | ||
| this.index = in.readString(); | ||
| this.key = in.readString(); | ||
| this.expected = readOptionalStringMap(in); | ||
| this.value = readOptionalStringMap(in); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(index); | ||
| out.writeString(key); | ||
| writeOptionalStringMap(expected, out); | ||
| writeOptionalStringMap(value, out); | ||
| } | ||
|
|
||
| static void writeOptionalStringMap(@Nullable Map<String, String> map, StreamOutput out) throws IOException { | ||
| if (map == null) { | ||
| out.writeBoolean(false); | ||
| } else { | ||
| out.writeBoolean(true); | ||
| out.writeMap(map, StreamOutput::writeString); | ||
| } | ||
| } | ||
|
|
||
| static Map<String, String> readOptionalStringMap(StreamInput in) throws IOException { | ||
| if (in.readBoolean()) { | ||
| return in.readImmutableMap(StreamInput::readString); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public String[] indices() { | ||
| return new String[] { index }; | ||
| } | ||
|
|
||
| @Override | ||
| public IndicesOptions indicesOptions() { | ||
| return IndicesOptions.strictSingleIndexNoExpandForbidClosed(); | ||
| } | ||
|
|
||
| public String index() { | ||
| return index; | ||
| } | ||
|
|
||
| public String key() { | ||
| return key; | ||
| } | ||
|
|
||
| public @Nullable Map<String, String> expected() { | ||
| return expected; | ||
| } | ||
|
|
||
| public @Nullable Map<String, String> value() { | ||
| return value; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
...n/java/org/elasticsearch/xpack/core/security/action/SetIndexMetadataPropertyResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.action; | ||
|
|
||
| import org.elasticsearch.action.ActionResponse; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.core.Nullable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.xpack.core.security.action.SetIndexMetadataPropertyRequest.readOptionalStringMap; | ||
| import static org.elasticsearch.xpack.core.security.action.SetIndexMetadataPropertyRequest.writeOptionalStringMap; | ||
|
|
||
| public class SetIndexMetadataPropertyResponse extends ActionResponse { | ||
| @Nullable | ||
| private final Map<String, String> value; | ||
|
|
||
| public SetIndexMetadataPropertyResponse(@Nullable Map<String, String> value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public SetIndexMetadataPropertyResponse(StreamInput in) throws IOException { | ||
| value = readOptionalStringMap(in); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| writeOptionalStringMap(value, out); | ||
| } | ||
|
|
||
| public @Nullable Map<String, String> value() { | ||
| return value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
New transport action to set any metadata custom property on a single index.
This is used to "mark" the .security index after the built-in reserved roles have been indexed.