-
Notifications
You must be signed in to change notification settings - Fork 274
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
Optimized Privilege Evaluation #4380
Open
nibix
wants to merge
10
commits into
opensearch-project:main
Choose a base branch
from
nibix:optimized-privilege-evaluation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10,864
−3,714
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ed13b9b
Optimized privilege evaluation
nibix b64f535
Optimized DLS/FLS
nibix 4cb4a5f
Removed code which is no longer needed
nibix d752c93
Made addStatics() generic
nibix 2327925
Review: Properly abort in case a restricted role fails during rule ev…
nibix a6ea16c
Tests and cleanup
nibix 7aed7c5
Tests and cleanup
nibix 4d07013
Applied changes from https://github.com/opensearch-project/security/p…
nibix 6212572
Applied spotless
nibix 1fef11b
Applied spotless
nibix 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
This file contains 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
1,040 changes: 1,040 additions & 0 deletions
1,040
src/integrationTest/java/org/opensearch/security/privileges/ActionPrivilegesTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
117 changes: 117 additions & 0 deletions
117
.../java/org/opensearch/security/privileges/ClusterStateMetadataDependentPrivilegesTest.java
This file contains 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,117 @@ | ||
/* | ||
* 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.privileges; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.node.Node; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import org.mockito.Mockito; | ||
import org.mockito.stubbing.Answer; | ||
|
||
public class ClusterStateMetadataDependentPrivilegesTest { | ||
|
||
@Test | ||
public void simpleUpdate() { | ||
ThreadPool threadPool = threadPool(); | ||
try { | ||
ConcreteTestSubject subject = new ConcreteTestSubject(); | ||
ClusterState clusterState = clusterState(metadata(1)); | ||
ClusterService clusterService = Mockito.mock(ClusterService.class); | ||
Mockito.when(clusterService.state()).thenReturn(clusterState); | ||
|
||
subject.updateClusterStateMetadataAsync(clusterService, threadPool); | ||
Awaitility.await().until(() -> subject.getCurrentlyUsedMetadataVersion() == 1); | ||
subject.shutdown(); | ||
} finally { | ||
threadPool.shutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void frequentUpdates() throws Exception { | ||
ThreadPool threadPool = threadPool(); | ||
try { | ||
ConcreteTestSubject subject = new ConcreteTestSubject(); | ||
AtomicReference<ClusterState> clusterStateReference = new AtomicReference<>(clusterState(metadata(1))); | ||
ClusterService clusterService = Mockito.mock(ClusterService.class); | ||
Mockito.when(clusterService.state()).thenAnswer((Answer<ClusterState>) invocationOnMock -> clusterStateReference.get()); | ||
subject.updateClusterStateMetadataAsync(clusterService, threadPool); | ||
subject.updateClusterStateMetadataAsync(clusterService, threadPool); | ||
|
||
for (int i = 2; i <= 100; i++) { | ||
clusterStateReference.set(clusterState(metadata(i))); | ||
subject.updateClusterStateMetadataAsync(clusterService, threadPool); | ||
Thread.sleep(10); | ||
} | ||
|
||
Awaitility.await().until(() -> subject.getCurrentlyUsedMetadataVersion() == 100); | ||
subject.shutdown(); | ||
} finally { | ||
threadPool.shutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void shutdown() { | ||
ThreadPool threadPool = threadPool(); | ||
try { | ||
ConcreteTestSubject subject = new ConcreteTestSubject(); | ||
ClusterState clusterState = clusterState(metadata(1)); | ||
ClusterService clusterService = Mockito.mock(ClusterService.class); | ||
Mockito.when(clusterService.state()).thenReturn(clusterState); | ||
subject.updateClusterStateMetadataAsync(clusterService, threadPool); | ||
subject.shutdown(); | ||
} finally { | ||
threadPool.shutdown(); | ||
} | ||
} | ||
|
||
static Metadata metadata(long version) { | ||
return Metadata.builder().version(version).build(); | ||
} | ||
|
||
static ClusterState clusterState(Metadata metadata) { | ||
return ClusterState.builder(ClusterState.EMPTY_STATE).metadata(metadata).build(); | ||
} | ||
|
||
static ThreadPool threadPool() { | ||
return new ThreadPool(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "name").build()); | ||
} | ||
|
||
static class ConcreteTestSubject extends ClusterStateMetadataDependentPrivileges { | ||
|
||
private long currentMetadataVersion; | ||
|
||
@Override | ||
protected void updateClusterStateMetadata(Metadata metadata) { | ||
// We need to be slow with updates to test the debounce-functionality | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) {} | ||
|
||
this.currentMetadataVersion = metadata.version(); | ||
} | ||
|
||
@Override | ||
protected long getCurrentlyUsedMetadataVersion() { | ||
return this.currentMetadataVersion; | ||
} | ||
} | ||
} |
252 changes: 252 additions & 0 deletions
252
src/integrationTest/java/org/opensearch/security/privileges/IndexPatternTest.java
This file contains 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,252 @@ | ||
/* | ||
* 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.privileges; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.time.temporal.ChronoField; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.cluster.ClusterState; | ||
import org.opensearch.cluster.metadata.IndexNameExpressionResolver; | ||
import org.opensearch.cluster.metadata.Metadata; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.security.resolver.IndexResolverReplacer; | ||
import org.opensearch.security.support.WildcardMatcher; | ||
import org.opensearch.security.user.User; | ||
|
||
import static org.opensearch.security.util.MockIndexMetadataBuilder.indices; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class IndexPatternTest { | ||
final static int CURRENT_YEAR = ZonedDateTime.now().get(ChronoField.YEAR); | ||
final static int NEXT_YEAR = CURRENT_YEAR + 1; | ||
|
||
final static Metadata INDEX_METADATA = // | ||
indices("index_a11", "index_a12", "index_a21", "index_a22", "index_b1", "index_b2")// | ||
.alias("alias_a") | ||
.of("index_a11", "index_a12", "index_a21", "index_a22")// | ||
.alias("alias_b") | ||
.of("index_b1", "index_b2")// | ||
.dataStream("data_stream_a1")// | ||
.dataStream("data_stream_b1")// | ||
.index("index_year_" + CURRENT_YEAR)// | ||
.index("index_year_" + NEXT_YEAR)// | ||
.alias("alias_year_" + CURRENT_YEAR) | ||
.of("index_current_year")// | ||
.alias("alias_year_" + NEXT_YEAR) | ||
.of("index_next_year")// | ||
.build(); | ||
final static ClusterState CLUSTER_STATE = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(INDEX_METADATA).build(); | ||
|
||
@Test | ||
public void constantIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("index_a11"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
assertFalse(indexPattern.isEmpty()); | ||
assertTrue(indexPattern.dynamicOnly().isEmpty()); | ||
assertEquals("index_a11", indexPattern.toString()); | ||
|
||
assertTrue(indexPattern.matches("index_a11", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_a12", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void constantAlias() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("alias_a"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches("alias_a", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("alias_a1", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void constantAlias_onIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("alias_a"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches("index_a11", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_b1", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void constantDataStream_onIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("data_stream_a1"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches(".ds-data_stream_a1-000001", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches(".ds-data_stream_a2-000001", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void patternIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("index_a1*"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches("index_a11", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_a21", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void patternAlias() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("alias_a*"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches("alias_a", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("alias_b", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void patternAlias_onIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("alias_a*"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches("index_a11", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_b1", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void patternDataStream_onIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("data_stream_a*"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
|
||
assertTrue(indexPattern.matches(".ds-data_stream_a1-000001", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches(".ds-data_stream_b1-000001", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
/** | ||
* Static invalid regular expressions are just ignored | ||
*/ | ||
@Test | ||
public void regex_invalid() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("/index_x\\/"); | ||
assertFalse(indexPattern.hasStaticPattern()); | ||
assertFalse(indexPattern.hasDynamicPattern()); | ||
assertTrue(indexPattern.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void dateMathIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("<index_year_{now/y{yyyy}}>"); | ||
assertFalse(indexPattern.hasStaticPattern()); | ||
assertTrue(indexPattern.hasDynamicPattern()); | ||
assertEquals("<index_year_{now/y{yyyy}}>", indexPattern.toString()); | ||
|
||
assertTrue(indexPattern.matches("index_year_" + CURRENT_YEAR, ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_year_" + NEXT_YEAR, ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test | ||
public void dateMathAlias_onIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("<alias_year_{now/y{yyyy}}>"); | ||
assertFalse(indexPattern.hasStaticPattern()); | ||
assertTrue(indexPattern.hasDynamicPattern()); | ||
assertFalse(indexPattern.isEmpty()); | ||
|
||
assertTrue(indexPattern.matches("index_current_year", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_next_year", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test(expected = PrivilegesEvaluationException.class) | ||
public void dateMathIndex_invalid() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("<index_year_{now/y{yyyy}>"); | ||
indexPattern.matches("index_year_" + CURRENT_YEAR, ctx(), INDEX_METADATA.getIndicesLookup()); | ||
} | ||
|
||
@Test | ||
public void templatedIndex() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("index_${attrs.a11}"); | ||
assertFalse(indexPattern.hasStaticPattern()); | ||
assertTrue(indexPattern.hasDynamicPattern()); | ||
assertEquals(indexPattern, indexPattern.dynamicOnly()); | ||
|
||
assertTrue(indexPattern.matches("index_a11", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
assertFalse(indexPattern.matches("index_a12", ctx(), INDEX_METADATA.getIndicesLookup())); | ||
} | ||
|
||
@Test(expected = PrivilegesEvaluationException.class) | ||
public void templatedIndex_invalid() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("/index_${attrs.a11}\\/"); | ||
assertFalse(indexPattern.hasStaticPattern()); | ||
assertTrue(indexPattern.hasDynamicPattern()); | ||
|
||
indexPattern.matches("whatever", ctx(), INDEX_METADATA.getIndicesLookup()); | ||
} | ||
|
||
@Test | ||
public void mixed() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("index_${attrs.a11}", "index_a12"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertTrue(indexPattern.hasDynamicPattern()); | ||
assertFalse(indexPattern.isEmpty()); | ||
|
||
assertEquals(WildcardMatcher.from("index_a12"), indexPattern.getStaticPattern()); | ||
assertEquals(IndexPattern.from("index_${attrs.a11}"), indexPattern.dynamicOnly()); | ||
assertEquals("index_a12 index_${attrs.a11}", indexPattern.toString()); | ||
} | ||
|
||
@Test | ||
public void mixed2() throws Exception { | ||
IndexPattern indexPattern = IndexPattern.from("<index_year_{now/y{yyyy}}>", "index_a12"); | ||
assertTrue(indexPattern.hasStaticPattern()); | ||
assertTrue(indexPattern.hasDynamicPattern()); | ||
assertFalse(indexPattern.isEmpty()); | ||
|
||
assertEquals(WildcardMatcher.from("index_a12"), indexPattern.getStaticPattern()); | ||
assertEquals(IndexPattern.from("<index_year_{now/y{yyyy}}>"), indexPattern.dynamicOnly()); | ||
assertEquals("index_a12 <index_year_{now/y{yyyy}}>", indexPattern.toString()); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
IndexPattern a1 = IndexPattern.from("data_stream_a*"); | ||
IndexPattern a2 = IndexPattern.from("data_stream_a*"); | ||
IndexPattern b = IndexPattern.from("<index_year_{now/y{yyyy}}>", "data_stream_a*"); | ||
|
||
assertEquals(a1, a1); | ||
assertEquals(a1, a2); | ||
assertNotEquals(a1, b); | ||
assertFalse(a1.equals(a1.toString())); | ||
} | ||
|
||
private static PrivilegesEvaluationContext ctx() { | ||
IndexNameExpressionResolver indexNameExpressionResolver = new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)); | ||
IndexResolverReplacer indexResolverReplacer = new IndexResolverReplacer(indexNameExpressionResolver, () -> CLUSTER_STATE, null); | ||
User user = new User("test_user"); | ||
user.addAttributes(ImmutableMap.of("attrs.a11", "a11")); | ||
user.addAttributes(ImmutableMap.of("attrs.year", "year")); | ||
|
||
return new PrivilegesEvaluationContext( | ||
user, | ||
ImmutableSet.of(), | ||
"indices:action/test", | ||
null, | ||
null, | ||
indexResolverReplacer, | ||
indexNameExpressionResolver, | ||
() -> CLUSTER_STATE | ||
); | ||
} | ||
} |
Oops, something went wrong.
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.
Is this the source for this lib? https://github.com/nibix/collections
Nice to see 95% coverage!
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.
Yep 👍😃