-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Allow custom authorization with an authorization engine #38358
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
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
8ccdc19
Introduce asynchronous RBACEngine (#36245)
jaymode 0246442
Merge branch 'master' into security_authz_engine
jaymode 1362ab6
Replace AuthorizedIndices class with a List (#37328)
jaymode c555a44
Merge branch 'master' into security_authz_engine
jaymode 6278eab
Merge branch 'master' into security_authz_engine
jaymode 9a240c6
Encapsulate request, auth, and action name (#37495)
jaymode 83cde40
Add javadoc to the AuthorizationEngine interface (#37620)
jaymode d98a77a
Merge branch 'master' into security_authz_engine
jaymode b9a2c81
Fix resolving restricted indices after merging
jaymode 7846ee8
Merge branch 'master' into security_authz_engine
jaymode 74f2e99
Merge branch 'master' into security_authz_engine
jaymode 5074683
Merge branch 'master' into security_authz_engine
jaymode d628008
fix RBACEngine after restricted indices changes
jaymode 3280607
Allow authorization engines as an extension (#37785)
jaymode 0e1c191
Merge branch 'master' into security_authz_engine
jaymode e5615d2
Move request interceptors to AuthorizationService (#38137)
jaymode 54d7b4c
Merge branch 'master' into security_authz_engine
jaymode 34aa55a
Authorization engines evaluate privileges for APIs (#38219)
jaymode 1c9a8e1
fix inconsistency in parameter name/type
jaymode 3e60a91
add licensing for authorization engine
jaymode 58e15aa
remove unused import
jaymode 2372c17
fix building of AliasOrIndex.Index
jaymode 4c03fd5
Update plugins/examples/security-authorization-engine/src/main/java/o…
tvernum f903fca
Merge branch 'master' into security_authz_engine
jaymode 835d1ce
Merge branch 'master' into security_authz_engine
jaymode de06e4c
fixes after merge
jaymode cf2b300
Merge branch 'master' into security_authz_engine
jaymode 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 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
46 changes: 46 additions & 0 deletions
46
plugins/examples/security-authorization-engine/build.gradle
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,46 @@ | ||
| apply plugin: 'elasticsearch.esplugin' | ||
|
|
||
| esplugin { | ||
| name 'security-authorization-engine' | ||
| description 'An example spi extension plugin for security that implements an Authorization Engine' | ||
| classname 'org.elasticsearch.example.AuthorizationEnginePlugin' | ||
| extendedPlugins = ['x-pack-security'] | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly "org.elasticsearch.plugin:x-pack-core:${version}" | ||
| testCompile "org.elasticsearch.client.x-pack-transport:${version}" | ||
| } | ||
|
|
||
|
|
||
| integTestRunner { | ||
| systemProperty 'tests.security.manager', 'false' | ||
| } | ||
|
|
||
| integTestCluster { | ||
| dependsOn buildZip | ||
| setting 'xpack.security.enabled', 'true' | ||
| setting 'xpack.ilm.enabled', 'false' | ||
| setting 'xpack.ml.enabled', 'false' | ||
| setting 'xpack.monitoring.enabled', 'false' | ||
| setting 'xpack.license.self_generated.type', 'trial' | ||
|
|
||
| // This is important, so that all the modules are available too. | ||
| // There are index templates that use token filters that are in analysis-module and | ||
| // processors are being used that are in ingest-common module. | ||
| distribution = 'default' | ||
|
|
||
| setupCommand 'setupDummyUser', | ||
| 'bin/elasticsearch-users', 'useradd', 'test_user', '-p', 'x-pack-test-password', '-r', 'custom_superuser' | ||
| waitCondition = { node, ant -> | ||
| File tmpFile = new File(node.cwd, 'wait.success') | ||
| ant.get(src: "http://${node.httpUri()}/_cluster/health?wait_for_nodes=>=${numNodes}&wait_for_status=yellow", | ||
| dest: tmpFile.toString(), | ||
| username: 'test_user', | ||
| password: 'x-pack-test-password', | ||
| ignoreerrors: true, | ||
| retries: 10) | ||
| return tmpFile.exists() | ||
| } | ||
| } | ||
| check.dependsOn integTest |
30 changes: 30 additions & 0 deletions
30
...thorization-engine/src/main/java/org/elasticsearch/example/AuthorizationEnginePlugin.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,30 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.example; | ||
|
|
||
| import org.elasticsearch.plugins.ActionPlugin; | ||
| import org.elasticsearch.plugins.Plugin; | ||
|
|
||
| /** | ||
| * Plugin class that is required so that the code contained here may be loaded as a plugin. | ||
| * Additional items such as settings and actions can be registered using this plugin class. | ||
| */ | ||
| public class AuthorizationEnginePlugin extends Plugin implements ActionPlugin { | ||
| } |
238 changes: 238 additions & 0 deletions
238
...thorization-engine/src/main/java/org/elasticsearch/example/CustomAuthorizationEngine.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,238 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.example; | ||
|
|
||
| import org.elasticsearch.action.ActionListener; | ||
| import org.elasticsearch.cluster.metadata.AliasOrIndex; | ||
| import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesRequest; | ||
| import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse; | ||
| import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse.Indices; | ||
| import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequest; | ||
| import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse; | ||
| import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse.ResourcePrivileges; | ||
| import org.elasticsearch.xpack.core.security.authc.Authentication; | ||
| import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; | ||
| import org.elasticsearch.xpack.core.security.authz.ResolvedIndices; | ||
| import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; | ||
| import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges; | ||
| import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl; | ||
| import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl.IndexAccessControl; | ||
| import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions; | ||
| import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor; | ||
| import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege; | ||
| import org.elasticsearch.xpack.core.security.user.User; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * A custom implementation of an authorization engine. This engine is extremely basic in that it | ||
| * authorizes based upon the name of a single role. If users have this role they are granted access. | ||
| */ | ||
| public class CustomAuthorizationEngine implements AuthorizationEngine { | ||
|
|
||
| @Override | ||
| public void resolveAuthorizationInfo(RequestInfo requestInfo, ActionListener<AuthorizationInfo> listener) { | ||
| final Authentication authentication = requestInfo.getAuthentication(); | ||
| if (authentication.getUser().isRunAs()) { | ||
| final CustomAuthorizationInfo authenticatedUserAuthzInfo = | ||
| new CustomAuthorizationInfo(authentication.getUser().authenticatedUser().roles(), null); | ||
| listener.onResponse(new CustomAuthorizationInfo(authentication.getUser().roles(), authenticatedUserAuthzInfo)); | ||
| } else { | ||
| listener.onResponse(new CustomAuthorizationInfo(authentication.getUser().roles(), null)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void authorizeRunAs(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, ActionListener<AuthorizationResult> listener) { | ||
| if (isSuperuser(requestInfo.getAuthentication().getUser().authenticatedUser())) { | ||
| listener.onResponse(AuthorizationResult.granted()); | ||
| } else { | ||
| listener.onResponse(AuthorizationResult.deny()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, | ||
| ActionListener<AuthorizationResult> listener) { | ||
| if (isSuperuser(requestInfo.getAuthentication().getUser())) { | ||
| listener.onResponse(AuthorizationResult.granted()); | ||
| } else { | ||
| listener.onResponse(AuthorizationResult.deny()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, | ||
| AsyncSupplier<ResolvedIndices> indicesAsyncSupplier, | ||
| Map<String, AliasOrIndex> aliasOrIndexLookup, | ||
| ActionListener<IndexAuthorizationResult> listener) { | ||
| if (isSuperuser(requestInfo.getAuthentication().getUser())) { | ||
| indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> { | ||
| Map<String, IndexAccessControl> indexAccessControlMap = new HashMap<>(); | ||
| for (String name : resolvedIndices.getLocal()) { | ||
| indexAccessControlMap.put(name, new IndexAccessControl(true, FieldPermissions.DEFAULT, null)); | ||
| } | ||
| IndicesAccessControl indicesAccessControl = | ||
| new IndicesAccessControl(true, Collections.unmodifiableMap(indexAccessControlMap)); | ||
| listener.onResponse(new IndexAuthorizationResult(true, indicesAccessControl)); | ||
| }, listener::onFailure)); | ||
| } else { | ||
| listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.DENIED)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, | ||
| Map<String, AliasOrIndex> aliasOrIndexLookup, ActionListener<List<String>> listener) { | ||
| if (isSuperuser(requestInfo.getAuthentication().getUser())) { | ||
| listener.onResponse(new ArrayList<>(aliasOrIndexLookup.keySet())); | ||
| } else { | ||
| listener.onResponse(Collections.emptyList()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void validateIndexPermissionsAreSubset(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, | ||
| Map<String, List<String>> indexNameToNewNames, | ||
| ActionListener<AuthorizationResult> listener) { | ||
| if (isSuperuser(requestInfo.getAuthentication().getUser())) { | ||
| listener.onResponse(AuthorizationResult.granted()); | ||
| } else { | ||
| listener.onResponse(AuthorizationResult.deny()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void checkPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, | ||
| HasPrivilegesRequest hasPrivilegesRequest, | ||
| Collection<ApplicationPrivilegeDescriptor> applicationPrivilegeDescriptors, | ||
| ActionListener<HasPrivilegesResponse> listener) { | ||
| if (isSuperuser(authentication.getUser())) { | ||
| listener.onResponse(getHasPrivilegesResponse(authentication, hasPrivilegesRequest, true)); | ||
| } else { | ||
| listener.onResponse(getHasPrivilegesResponse(authentication, hasPrivilegesRequest, false)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void getUserPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, GetUserPrivilegesRequest request, | ||
| ActionListener<GetUserPrivilegesResponse> listener) { | ||
| if (isSuperuser(authentication.getUser())) { | ||
| listener.onResponse(getUserPrivilegesResponse(true)); | ||
| } else { | ||
| listener.onResponse(getUserPrivilegesResponse(false)); | ||
| } | ||
| } | ||
|
|
||
| private HasPrivilegesResponse getHasPrivilegesResponse(Authentication authentication, HasPrivilegesRequest hasPrivilegesRequest, | ||
| boolean authorized) { | ||
| Map<String, Boolean> clusterPrivMap = new HashMap<>(); | ||
| for (String clusterPriv : hasPrivilegesRequest.clusterPrivileges()) { | ||
| clusterPrivMap.put(clusterPriv, authorized); | ||
| } | ||
| final Map<String, ResourcePrivileges> indices = new LinkedHashMap<>(); | ||
| for (IndicesPrivileges check : hasPrivilegesRequest.indexPrivileges()) { | ||
| for (String index : check.getIndices()) { | ||
| final Map<String, Boolean> privileges = new HashMap<>(); | ||
| final HasPrivilegesResponse.ResourcePrivileges existing = indices.get(index); | ||
| if (existing != null) { | ||
| privileges.putAll(existing.getPrivileges()); | ||
| } | ||
| for (String privilege : check.getPrivileges()) { | ||
| privileges.put(privilege, authorized); | ||
| } | ||
| indices.put(index, new ResourcePrivileges(index, privileges)); | ||
| } | ||
| } | ||
| final Map<String, Collection<ResourcePrivileges>> privilegesByApplication = new HashMap<>(); | ||
| Set<String> applicationNames = Arrays.stream(hasPrivilegesRequest.applicationPrivileges()) | ||
| .map(RoleDescriptor.ApplicationResourcePrivileges::getApplication) | ||
| .collect(Collectors.toSet()); | ||
| for (String applicationName : applicationNames) { | ||
| final Map<String, HasPrivilegesResponse.ResourcePrivileges> appPrivilegesByResource = new LinkedHashMap<>(); | ||
| for (RoleDescriptor.ApplicationResourcePrivileges p : hasPrivilegesRequest.applicationPrivileges()) { | ||
| if (applicationName.equals(p.getApplication())) { | ||
| for (String resource : p.getResources()) { | ||
| final Map<String, Boolean> privileges = new HashMap<>(); | ||
| final HasPrivilegesResponse.ResourcePrivileges existing = appPrivilegesByResource.get(resource); | ||
| if (existing != null) { | ||
| privileges.putAll(existing.getPrivileges()); | ||
| } | ||
| for (String privilege : p.getPrivileges()) { | ||
| privileges.put(privilege, authorized); | ||
| } | ||
| appPrivilegesByResource.put(resource, new HasPrivilegesResponse.ResourcePrivileges(resource, privileges)); | ||
| } | ||
| } | ||
| } | ||
| privilegesByApplication.put(applicationName, appPrivilegesByResource.values()); | ||
| } | ||
| return new HasPrivilegesResponse(authentication.getUser().principal(), authorized, clusterPrivMap, indices.values(), | ||
| privilegesByApplication); | ||
| } | ||
|
|
||
| private GetUserPrivilegesResponse getUserPrivilegesResponse(boolean isSuperuser) { | ||
| final Set<String> cluster = isSuperuser ? Collections.singleton("ALL") : Collections.emptySet(); | ||
| final Set<ConditionalClusterPrivilege> conditionalCluster = Collections.emptySet(); | ||
| final Set<GetUserPrivilegesResponse.Indices> indices = isSuperuser ? Collections.singleton(new Indices(Collections.singleton("*"), | ||
| Collections.singleton("*"), Collections.emptySet(), Collections.emptySet(), true)) : Collections.emptySet(); | ||
|
|
||
| final Set<RoleDescriptor.ApplicationResourcePrivileges> application = isSuperuser ? | ||
| Collections.singleton( | ||
| RoleDescriptor.ApplicationResourcePrivileges.builder().application("*").privileges("*").resources("*").build()) : | ||
| Collections.emptySet(); | ||
| final Set<String> runAs = isSuperuser ? Collections.singleton("*") : Collections.emptySet(); | ||
| return new GetUserPrivilegesResponse(cluster, conditionalCluster, indices, application, runAs); | ||
| } | ||
|
|
||
| public static class CustomAuthorizationInfo implements AuthorizationInfo { | ||
|
|
||
| private final String[] roles; | ||
| private final CustomAuthorizationInfo authenticatedAuthzInfo; | ||
|
|
||
| CustomAuthorizationInfo(String[] roles, CustomAuthorizationInfo authenticatedAuthzInfo) { | ||
| this.roles = roles; | ||
| this.authenticatedAuthzInfo = authenticatedAuthzInfo; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> asMap() { | ||
| return Collections.singletonMap("roles", roles); | ||
| } | ||
|
|
||
| @Override | ||
| public CustomAuthorizationInfo getAuthenticatedUserAuthorizationInfo() { | ||
| return authenticatedAuthzInfo; | ||
| } | ||
| } | ||
|
|
||
| private boolean isSuperuser(User user) { | ||
| return Arrays.binarySearch(user.roles(), "custom_superuser") > -1; | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
...n-engine/src/main/java/org/elasticsearch/example/ExampleAuthorizationEngineExtension.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,35 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.example; | ||
|
|
||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.xpack.core.security.SecurityExtension; | ||
| import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; | ||
|
|
||
| /** | ||
| * Security extension class that registers the custom authorization engine to be used | ||
| */ | ||
| public class ExampleAuthorizationEngineExtension implements SecurityExtension { | ||
|
|
||
| @Override | ||
| public AuthorizationEngine getAuthorizationEngine(Settings settings) { | ||
| return new CustomAuthorizationEngine(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
.../main/resources/META-INF/services/org.elasticsearch.xpack.core.security.SecurityExtension
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 @@ | ||
| org.elasticsearch.example.ExampleAuthorizationEngineExtension |
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.
Uh oh!
There was an error while loading. Please reload this page.