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

Api token authc/z implementation with Cache #4992

Merged
merged 31 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9f695fa
Naive cluster permission authz and authc based on token validity
derek-ho Dec 24, 2024
d3fcc4a
Crude index permissions authz
derek-ho Dec 24, 2024
6904317
Fix tests
derek-ho Dec 26, 2024
17bca93
Revert mis-merge in abstractauditlog
derek-ho Dec 26, 2024
92d4e60
Add allowlist for authc, add basic test showing it works
derek-ho Dec 27, 2024
22cfbe8
Add more extensive tests for authenticator, switch to list of indexPe…
derek-ho Dec 27, 2024
665b9e9
Directly store permissions in the cache
derek-ho Dec 30, 2024
e39df0d
Remove permissions from jti
derek-ho Dec 30, 2024
ad63974
Onboard onto clusterPrivileges
derek-ho Dec 30, 2024
73eb2ab
Add index permissions api token eval
derek-ho Dec 31, 2024
6418226
Add testing for cluster and index priv
derek-ho Dec 31, 2024
bc8aacf
Use transport action
derek-ho Jan 6, 2025
b90bae9
Cleanup tests and constants
derek-ho Jan 6, 2025
552aeda
Fix test
derek-ho Jan 6, 2025
aa506e7
Remove unecessary id to jti map since we are reloading every time and…
derek-ho Jan 6, 2025
d9cf78d
PR review
derek-ho Jan 14, 2025
d7b7e47
Spotless
derek-ho Jan 14, 2025
6571d9d
Fix tests
derek-ho Jan 14, 2025
9fcb720
PR review
derek-ho Jan 14, 2025
e44072c
Inject
derek-ho Jan 14, 2025
0927d99
Fix
derek-ho Jan 15, 2025
fb79014
Fix tests
derek-ho Jan 16, 2025
0f60569
Final cleanup
derek-ho Jan 16, 2025
6634896
Merge branch 'feature/api-tokens' of github.com:opensearch-project/se…
derek-ho Jan 17, 2025
83769b1
Fix tests
derek-ho Jan 17, 2025
685dcfc
PR feedback
derek-ho Jan 21, 2025
6ffc060
Mock api tokenrepository behavior
derek-ho Jan 21, 2025
763a9ff
Refactor to abstract class
derek-ho Jan 22, 2025
a685951
Fix test and cleanup
derek-ho Jan 23, 2025
77ad6c3
Separate the calls out based on type
derek-ho Jan 23, 2025
7a20de3
PR comments
derek-ho Feb 4, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.security.action.apitokens.ApiToken;
import org.opensearch.security.action.apitokens.ApiTokenRepository;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.resolver.IndexResolverReplacer;
import org.opensearch.security.securityconf.FlattenedActionGroups;
import org.opensearch.security.securityconf.impl.CType;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
import org.opensearch.security.securityconf.impl.v7.ActionGroupsV7;
import org.opensearch.security.securityconf.impl.v7.RoleV7;
import org.opensearch.security.user.User;
import org.opensearch.security.util.MockIndexMetadataBuilder;

import org.mockito.Mockito;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.opensearch.security.privileges.PrivilegeEvaluatorResponseMatcher.isAllowed;
import static org.opensearch.security.privileges.PrivilegeEvaluatorResponseMatcher.isForbidden;
Expand Down Expand Up @@ -280,6 +286,63 @@ public void hasAny_wildcard() throws Exception {
isForbidden(missingPrivileges("cluster:whatever"))
);
}

@Test
public void apiToken_explicit_failsWithWildcard() throws Exception {
SecurityDynamicConfiguration<RoleV7> roles = SecurityDynamicConfiguration.empty(CType.ROLES);
ActionPrivileges subject = new ActionPrivileges(roles, FlattenedActionGroups.EMPTY, null, Settings.EMPTY);
String token = "blah";
PrivilegesEvaluationContext context = ctxForApiToken("apitoken:" + token, new Permissions(List.of("*"), List.of()));
// Explicit fails
assertThat(
subject.hasExplicitClusterPrivilege(context, "cluster:whatever"),
isForbidden(missingPrivileges("cluster:whatever"))
);
// Not explicit succeeds
assertThat(subject.hasClusterPrivilege(context, "cluster:whatever"), isAllowed());
// Any succeeds
assertThat(subject.hasAnyClusterPrivilege(context, ImmutableSet.of("cluster:whatever")), isAllowed());
}

@Test
public void apiToken_succeedsWithExactMatch() throws Exception {
SecurityDynamicConfiguration<RoleV7> roles = SecurityDynamicConfiguration.empty(CType.ROLES);
ActionPrivileges subject = new ActionPrivileges(roles, FlattenedActionGroups.EMPTY, null, Settings.EMPTY);
String token = "blah";
PrivilegesEvaluationContext context = ctxForApiToken(
"apitoken:" + token,
new Permissions(List.of("cluster:whatever"), List.of())
);
// Explicit succeeds
assertThat(subject.hasExplicitClusterPrivilege(context, "cluster:whatever"), isAllowed());
// Not explicit succeeds
assertThat(subject.hasClusterPrivilege(context, "cluster:whatever"), isAllowed());
// Any succeeds
assertThat(subject.hasAnyClusterPrivilege(context, ImmutableSet.of("cluster:whatever")), isAllowed());
// Any succeeds
assertThat(subject.hasAnyClusterPrivilege(context, ImmutableSet.of("cluster:whatever", "cluster:other")), isAllowed());
}

@Test
public void apiToken_succeedsWithActionGroupsExapnded() throws Exception {
SecurityDynamicConfiguration<RoleV7> roles = SecurityDynamicConfiguration.empty(CType.ROLES);

SecurityDynamicConfiguration<ActionGroupsV7> config = SecurityDynamicConfiguration.fromYaml(
"CLUSTER_ALL:\n allowed_actions:\n - \"cluster:*\"",
CType.ACTIONGROUPS
);

FlattenedActionGroups actionGroups = new FlattenedActionGroups(config);
ActionPrivileges subject = new ActionPrivileges(roles, actionGroups, null, Settings.EMPTY);
String token = "blah";
PrivilegesEvaluationContext context = ctxForApiToken("apitoken:" + token, new Permissions(List.of("CLUSTER_ALL"), List.of()));
// Explicit succeeds
assertThat(subject.hasExplicitClusterPrivilege(context, "cluster:whatever"), isAllowed());
// Not explicit succeeds
assertThat(subject.hasClusterPrivilege(context, "cluster:whatever"), isAllowed());
// Any succeeds
assertThat(subject.hasClusterPrivilege(context, "cluster:monitor/main"), isAllowed());
}
}

/**
Expand Down Expand Up @@ -314,6 +377,17 @@ public void positive_full() throws Exception {
assertThat(result, isAllowed());
}

@Test
public void apiTokens_positive_full() throws Exception {
String token = "blah";
PrivilegesEvaluationContext context = ctxForApiToken(
"apitoken:" + token,
new Permissions(List.of("index_a11"), List.of(new ApiToken.IndexPermission(List.of("index_a11"), List.of("*"))))
);
PrivilegesEvaluatorResponse result = subject.hasIndexPrivilege(context, requiredActions, resolved("index_a11"));
assertThat(result, isAllowed());
}

@Test
public void positive_partial() throws Exception {
PrivilegesEvaluationContext ctx = ctx("test_role");
Expand Down Expand Up @@ -368,6 +442,18 @@ public void negative_wrongRole() throws Exception {
assertThat(result, isForbidden(missingPrivileges(requiredActions)));
}

@Test
public void apiToken_negative_noPermissions() throws Exception {
String token = "blah";
PrivilegesEvaluationContext context = ctxForApiToken(
"apitoken:" + token,
new Permissions(List.of(), List.of(new ApiToken.IndexPermission(List.of(), List.of())))
);

PrivilegesEvaluatorResponse result = subject.hasIndexPrivilege(context, requiredActions, resolved("index_a11"));
assertThat(result, isForbidden(missingPrivileges(requiredActions)));
}

@Test
public void negative_wrongAction() throws Exception {
PrivilegesEvaluationContext ctx = ctx("test_role");
Expand Down Expand Up @@ -397,6 +483,20 @@ public void positive_hasExplicit_full() {
}
}

@Test
public void apiTokens_positive_hasExplicit_full() {
String token = "blah";
PrivilegesEvaluationContext context = ctxForApiToken(
"apitoken:" + token,
new Permissions(List.of("index_a11"), List.of(new ApiToken.IndexPermission(List.of("index_a11"), List.of("*"))))
);

PrivilegesEvaluatorResponse result = subject.hasExplicitIndexPrivilege(context, requiredActions, resolved("index_a11"));

assertThat(result, isForbidden(missingPrivileges(requiredActions)));

}

private boolean covers(PrivilegesEvaluationContext ctx, String... indices) {
for (String index : indices) {
if (!indexSpec.covers(ctx.getUser(), index)) {
Expand Down Expand Up @@ -1040,8 +1140,13 @@ static SecurityDynamicConfiguration<RoleV7> createRoles(int numberOfRoles, int n
}

static PrivilegesEvaluationContext ctx(String... roles) {
User user = new User("test_user");
return ctxWithUserName("test-user", roles);
}

static PrivilegesEvaluationContext ctxWithUserName(String userName, String... roles) {
User user = new User(userName);
user.addAttributes(ImmutableMap.of("attrs.dept_no", "a11"));
ApiTokenRepository mockRepository = Mockito.mock(ApiTokenRepository.class);
return new PrivilegesEvaluationContext(
user,
ImmutableSet.copyOf(roles),
Expand All @@ -1050,7 +1155,24 @@ static PrivilegesEvaluationContext ctx(String... roles) {
null,
null,
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
null
null,
new Permissions()
);
}

static PrivilegesEvaluationContext ctxForApiToken(String userName, Permissions permissions) {
User user = new User(userName);
user.addAttributes(ImmutableMap.of("attrs.dept_no", "a11"));
return new PrivilegesEvaluationContext(
user,
ImmutableSet.of(),
null,
null,
null,
null,
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
null,
permissions
);
}

Expand All @@ -1065,7 +1187,8 @@ static PrivilegesEvaluationContext ctxByUsername(String username) {
null,
null,
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
null
null,
new Permissions()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.resolver.IndexResolverReplacer;
import org.opensearch.security.support.WildcardMatcher;
import org.opensearch.security.user.User;
Expand Down Expand Up @@ -246,7 +247,8 @@ private static PrivilegesEvaluationContext ctx() {
null,
indexResolverReplacer,
indexNameExpressionResolver,
() -> CLUSTER_STATE
() -> CLUSTER_STATE,
new Permissions()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import org.opensearch.common.settings.Settings;
import org.opensearch.security.DefaultObjectMapper;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.dlic.rest.api.Endpoint;
import org.opensearch.security.dlic.rest.api.RestApiAdminPrivilegesEvaluator.PermissionBuilder;
import org.opensearch.security.securityconf.FlattenedActionGroups;
Expand Down Expand Up @@ -251,7 +252,17 @@ static SecurityDynamicConfiguration<RoleV7> createRolesConfig() throws IOExcepti
}

static PrivilegesEvaluationContext ctx(String... roles) {
return new PrivilegesEvaluationContext(new User("test_user"), ImmutableSet.copyOf(roles), null, null, null, null, null, null);
return new PrivilegesEvaluationContext(
new User("test_user"),
ImmutableSet.copyOf(roles),
null,
null,
null,
null,
null,
null,
new Permissions()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opensearch.index.query.RangeQueryBuilder;
import org.opensearch.index.query.TermQueryBuilder;
import org.opensearch.search.internal.ShardSearchRequest;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.privileges.PrivilegesEvaluationContext;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
import org.opensearch.security.securityconf.impl.v7.RoleV7;
Expand All @@ -55,6 +56,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class DlsFlsLegacyHeadersTest {
static NamedXContentRegistry xContentRegistry = new NamedXContentRegistry(
Expand Down Expand Up @@ -255,11 +257,11 @@ public void performHeaderDecoration_oldNode() throws Exception {
Metadata metadata = exampleMetadata();
DlsFlsProcessedConfig dlsFlsProcessedConfig = dlsFlsProcessedConfig(exampleRolesConfig(), metadata);

Transport.Connection connection = Mockito.mock(Transport.Connection.class);
Transport.Connection connection = mock(Transport.Connection.class);
Mockito.when(connection.getVersion()).thenReturn(Version.V_2_0_0);

// ShardSearchRequest does not extend ActionRequest, thus the headers must be set
ShardSearchRequest request = Mockito.mock(ShardSearchRequest.class);
ShardSearchRequest request = mock(ShardSearchRequest.class);

Map<String, String> headerSink = new HashMap<>();

Expand All @@ -277,7 +279,7 @@ public void performHeaderDecoration_actionRequest() throws Exception {
Metadata metadata = exampleMetadata();
DlsFlsProcessedConfig dlsFlsProcessedConfig = dlsFlsProcessedConfig(exampleRolesConfig(), metadata);

Transport.Connection connection = Mockito.mock(Transport.Connection.class);
Transport.Connection connection = mock(Transport.Connection.class);
Mockito.when(connection.getVersion()).thenReturn(Version.V_2_0_0);

// SearchRequest does extend ActionRequest, thus the headers must not be set
Expand All @@ -296,11 +298,11 @@ public void performHeaderDecoration_newNode() throws Exception {
Metadata metadata = exampleMetadata();
DlsFlsProcessedConfig dlsFlsProcessedConfig = dlsFlsProcessedConfig(exampleRolesConfig(), metadata);

Transport.Connection connection = Mockito.mock(Transport.Connection.class);
Transport.Connection connection = mock(Transport.Connection.class);
Mockito.when(connection.getVersion()).thenReturn(Version.V_3_0_0);

// ShardSearchRequest does not extend ActionRequest, thus the headers must be set
ShardSearchRequest request = Mockito.mock(ShardSearchRequest.class);
ShardSearchRequest request = mock(ShardSearchRequest.class);

Map<String, String> headerSink = new HashMap<>();

Expand Down Expand Up @@ -345,7 +347,8 @@ public void prepare_ccs() throws Exception {
null,
null,
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
() -> clusterState
() -> clusterState,
new Permissions()
);

DlsFlsLegacyHeaders.prepare(threadContext, ctx, dlsFlsProcessedConfig(exampleRolesConfig(), metadata), metadata, false);
Expand All @@ -364,7 +367,8 @@ static PrivilegesEvaluationContext ctx(Metadata metadata, String... roles) {
null,
null,
new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)),
() -> clusterState
() -> clusterState,
new Permissions()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryBuilders;
import org.opensearch.index.query.TermQueryBuilder;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.privileges.PrivilegesConfigurationValidationException;
import org.opensearch.security.privileges.PrivilegesEvaluationContext;
import org.opensearch.security.privileges.PrivilegesEvaluationException;
Expand Down Expand Up @@ -526,7 +527,8 @@ public IndicesAndAliases_getRestriction(
null,
null,
null,
() -> CLUSTER_STATE
() -> CLUSTER_STATE,
new Permissions()
);
this.statefulness = statefulness;
this.dfmEmptyOverridesAll = dfmEmptyOverridesAll == DfmEmptyOverridesAll.DFM_EMPTY_OVERRIDES_ALL_TRUE;
Expand Down Expand Up @@ -841,7 +843,8 @@ public IndicesRequest indices(String... strings) {
null,
RESOLVER_REPLACER,
INDEX_NAME_EXPRESSION_RESOLVER,
() -> CLUSTER_STATE
() -> CLUSTER_STATE,
new Permissions()
);
this.statefulness = statefulness;
this.dfmEmptyOverridesAll = dfmEmptyOverridesAll == DfmEmptyOverridesAll.DFM_EMPTY_OVERRIDES_ALL_TRUE;
Expand Down Expand Up @@ -1126,7 +1129,8 @@ public DataStreams_getRestriction(
null,
null,
null,
() -> CLUSTER_STATE
() -> CLUSTER_STATE,
new Permissions()
);
this.statefulness = statefulness;
this.dfmEmptyOverridesAll = dfmEmptyOverridesAll == DfmEmptyOverridesAll.DFM_EMPTY_OVERRIDES_ALL_TRUE;
Expand All @@ -1146,7 +1150,19 @@ public void invalidQuery() throws Exception {
@Test(expected = PrivilegesEvaluationException.class)
public void invalidTemplatedQuery() throws Exception {
DocumentPrivileges.DlsQuery.create("{\"invalid\": \"totally ${attr.foo}\"}", xContentRegistry)
.evaluate(new PrivilegesEvaluationContext(new User("test_user"), ImmutableSet.of(), null, null, null, null, null, null));
.evaluate(
new PrivilegesEvaluationContext(
new User("test_user"),
ImmutableSet.of(),
null,
null,
null,
null,
null,
null,
new Permissions()
)
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.security.action.apitokens.Permissions;
import org.opensearch.security.privileges.PrivilegesConfigurationValidationException;
import org.opensearch.security.privileges.PrivilegesEvaluationContext;
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration;
Expand Down Expand Up @@ -123,7 +124,8 @@ static PrivilegesEvaluationContext ctx(String... roles) {
null,
null,
null,
() -> CLUSTER_STATE
() -> CLUSTER_STATE,
new Permissions()
);
}
}
Expand Down
Loading
Loading