Skip to content

Commit 3a0c896

Browse files
authored
Allow custom authorization with an authorization engine (#38228)
For some users, the built in authorization mechanism does not fit their needs and no feature that we offer would allow them to control the authorization process to meet their needs. In order to support this, a concept of an AuthorizationEngine is being introduced, which can be provided using the security extension mechanism. An AuthorizationEngine is responsible for making the authorization decisions about a request. The engine is responsible for knowing how to authorize and can be backed by whatever mechanism a user wants. The default mechanism is one backed by roles to provide the authorization decisions. The AuthorizationEngine will be called by the AuthorizationService, which handles more of the internal workings that apply in general to authorization within Elasticsearch. In order to support external authorization services that would back an authorization engine, the entire authorization process has become asynchronous, which also includes all calls to the AuthorizationEngine. The use of roles also leaked out of the AuthorizationService in our existing code that is not specifically related to roles so this also needed to be addressed. RequestInterceptor instances sometimes used a role to ensure a user was not attempting to escalate their privileges. Addressing this leakage of roles meant that the RequestInterceptor execution needed to move within the AuthorizationService and that AuthorizationEngines needed to support detection of whether a user has more privileges on a name than another. The second area where roles leaked to the user is in the handling of a few privilege APIs that could be used to retrieve the user's privileges or ask if a user has privileges to perform an action. To remove the leakage of roles from these actions, the AuthorizationService and AuthorizationEngine gained methods that enabled an AuthorizationEngine to return the response for these APIs. Ultimately this feature is the work included in: #37785, #37495, #37328, #36245, #38137, #38219 Closes #32435
1 parent 31be27e commit 3a0c896

File tree

78 files changed

+4840
-2750
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+4840
-2750
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ allprojects {
244244
"org.elasticsearch.plugin:aggs-matrix-stats-client:${version}": ':modules:aggs-matrix-stats',
245245
"org.elasticsearch.plugin:percolator-client:${version}": ':modules:percolator',
246246
"org.elasticsearch.plugin:rank-eval-client:${version}": ':modules:rank-eval',
247+
// for security example plugins
248+
"org.elasticsearch.plugin:x-pack-core:${version}": ':x-pack:plugin:core',
249+
"org.elasticsearch.client.x-pack-transport:${version}": ':x-pack:transport-client'
247250
]
248251
// substitute unreleased versions with projects that check out and build locally
249252
bwcVersions.forPreviousUnreleased { VersionCollection.UnreleasedVersionInfo unreleasedVersion ->
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'elasticsearch.esplugin'
2+
3+
esplugin {
4+
name 'security-authorization-engine'
5+
description 'An example spi extension plugin for security that implements an Authorization Engine'
6+
classname 'org.elasticsearch.example.AuthorizationEnginePlugin'
7+
extendedPlugins = ['x-pack-security']
8+
}
9+
10+
dependencies {
11+
compileOnly "org.elasticsearch.plugin:x-pack-core:${version}"
12+
testCompile "org.elasticsearch.client.x-pack-transport:${version}"
13+
}
14+
15+
16+
integTestRunner {
17+
systemProperty 'tests.security.manager', 'false'
18+
}
19+
20+
integTestCluster {
21+
dependsOn buildZip
22+
setting 'xpack.security.enabled', 'true'
23+
setting 'xpack.ilm.enabled', 'false'
24+
setting 'xpack.ml.enabled', 'false'
25+
setting 'xpack.monitoring.enabled', 'false'
26+
setting 'xpack.license.self_generated.type', 'trial'
27+
28+
// This is important, so that all the modules are available too.
29+
// There are index templates that use token filters that are in analysis-module and
30+
// processors are being used that are in ingest-common module.
31+
distribution = 'zip'
32+
33+
setupCommand 'setupDummyUser',
34+
'bin/elasticsearch-users', 'useradd', 'test_user', '-p', 'x-pack-test-password', '-r', 'custom_superuser'
35+
waitCondition = { node, ant ->
36+
File tmpFile = new File(node.cwd, 'wait.success')
37+
ant.get(src: "http://${node.httpUri()}/_cluster/health?wait_for_nodes=>=${numNodes}&wait_for_status=yellow",
38+
dest: tmpFile.toString(),
39+
username: 'test_user',
40+
password: 'x-pack-test-password',
41+
ignoreerrors: true,
42+
retries: 10)
43+
return tmpFile.exists()
44+
}
45+
}
46+
check.dependsOn integTest
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.example;
21+
22+
import org.elasticsearch.plugins.ActionPlugin;
23+
import org.elasticsearch.plugins.Plugin;
24+
25+
/**
26+
* Plugin class that is required so that the code contained here may be loaded as a plugin.
27+
* Additional items such as settings and actions can be registered using this plugin class.
28+
*/
29+
public class AuthorizationEnginePlugin extends Plugin implements ActionPlugin {
30+
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.example;
21+
22+
import org.elasticsearch.action.ActionListener;
23+
import org.elasticsearch.cluster.metadata.AliasOrIndex;
24+
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesRequest;
25+
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse;
26+
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse.Indices;
27+
import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesRequest;
28+
import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse;
29+
import org.elasticsearch.xpack.core.security.authc.Authentication;
30+
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine;
31+
import org.elasticsearch.xpack.core.security.authz.ResolvedIndices;
32+
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
33+
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.IndicesPrivileges;
34+
import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl;
35+
import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl.IndexAccessControl;
36+
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions;
37+
import org.elasticsearch.xpack.core.security.authz.permission.ResourcePrivileges;
38+
import org.elasticsearch.xpack.core.security.authz.privilege.ApplicationPrivilegeDescriptor;
39+
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege;
40+
import org.elasticsearch.xpack.core.security.user.User;
41+
42+
import java.util.ArrayList;
43+
import java.util.Arrays;
44+
import java.util.Collection;
45+
import java.util.Collections;
46+
import java.util.HashMap;
47+
import java.util.LinkedHashMap;
48+
import java.util.List;
49+
import java.util.Map;
50+
import java.util.Set;
51+
import java.util.stream.Collectors;
52+
53+
/**
54+
* A custom implementation of an authorization engine. This engine is extremely basic in that it
55+
* authorizes based upon the name of a single role. If users have this role they are granted access.
56+
*/
57+
public class CustomAuthorizationEngine implements AuthorizationEngine {
58+
59+
@Override
60+
public void resolveAuthorizationInfo(RequestInfo requestInfo, ActionListener<AuthorizationInfo> listener) {
61+
final Authentication authentication = requestInfo.getAuthentication();
62+
if (authentication.getUser().isRunAs()) {
63+
final CustomAuthorizationInfo authenticatedUserAuthzInfo =
64+
new CustomAuthorizationInfo(authentication.getUser().authenticatedUser().roles(), null);
65+
listener.onResponse(new CustomAuthorizationInfo(authentication.getUser().roles(), authenticatedUserAuthzInfo));
66+
} else {
67+
listener.onResponse(new CustomAuthorizationInfo(authentication.getUser().roles(), null));
68+
}
69+
}
70+
71+
@Override
72+
public void authorizeRunAs(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, ActionListener<AuthorizationResult> listener) {
73+
if (isSuperuser(requestInfo.getAuthentication().getUser().authenticatedUser())) {
74+
listener.onResponse(AuthorizationResult.granted());
75+
} else {
76+
listener.onResponse(AuthorizationResult.deny());
77+
}
78+
}
79+
80+
@Override
81+
public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo,
82+
ActionListener<AuthorizationResult> listener) {
83+
if (isSuperuser(requestInfo.getAuthentication().getUser())) {
84+
listener.onResponse(AuthorizationResult.granted());
85+
} else {
86+
listener.onResponse(AuthorizationResult.deny());
87+
}
88+
}
89+
90+
@Override
91+
public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo,
92+
AsyncSupplier<ResolvedIndices> indicesAsyncSupplier,
93+
Map<String, AliasOrIndex> aliasOrIndexLookup,
94+
ActionListener<IndexAuthorizationResult> listener) {
95+
if (isSuperuser(requestInfo.getAuthentication().getUser())) {
96+
indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> {
97+
Map<String, IndexAccessControl> indexAccessControlMap = new HashMap<>();
98+
for (String name : resolvedIndices.getLocal()) {
99+
indexAccessControlMap.put(name, new IndexAccessControl(true, FieldPermissions.DEFAULT, null));
100+
}
101+
IndicesAccessControl indicesAccessControl =
102+
new IndicesAccessControl(true, Collections.unmodifiableMap(indexAccessControlMap));
103+
listener.onResponse(new IndexAuthorizationResult(true, indicesAccessControl));
104+
}, listener::onFailure));
105+
} else {
106+
listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.DENIED));
107+
}
108+
}
109+
110+
@Override
111+
public void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo,
112+
Map<String, AliasOrIndex> aliasOrIndexLookup, ActionListener<List<String>> listener) {
113+
if (isSuperuser(requestInfo.getAuthentication().getUser())) {
114+
listener.onResponse(new ArrayList<>(aliasOrIndexLookup.keySet()));
115+
} else {
116+
listener.onResponse(Collections.emptyList());
117+
}
118+
}
119+
120+
@Override
121+
public void validateIndexPermissionsAreSubset(RequestInfo requestInfo, AuthorizationInfo authorizationInfo,
122+
Map<String, List<String>> indexNameToNewNames,
123+
ActionListener<AuthorizationResult> listener) {
124+
if (isSuperuser(requestInfo.getAuthentication().getUser())) {
125+
listener.onResponse(AuthorizationResult.granted());
126+
} else {
127+
listener.onResponse(AuthorizationResult.deny());
128+
}
129+
}
130+
131+
@Override
132+
public void checkPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo,
133+
HasPrivilegesRequest hasPrivilegesRequest,
134+
Collection<ApplicationPrivilegeDescriptor> applicationPrivilegeDescriptors,
135+
ActionListener<HasPrivilegesResponse> listener) {
136+
if (isSuperuser(authentication.getUser())) {
137+
listener.onResponse(getHasPrivilegesResponse(authentication, hasPrivilegesRequest, true));
138+
} else {
139+
listener.onResponse(getHasPrivilegesResponse(authentication, hasPrivilegesRequest, false));
140+
}
141+
}
142+
143+
@Override
144+
public void getUserPrivileges(Authentication authentication, AuthorizationInfo authorizationInfo, GetUserPrivilegesRequest request,
145+
ActionListener<GetUserPrivilegesResponse> listener) {
146+
if (isSuperuser(authentication.getUser())) {
147+
listener.onResponse(getUserPrivilegesResponse(true));
148+
} else {
149+
listener.onResponse(getUserPrivilegesResponse(false));
150+
}
151+
}
152+
153+
private HasPrivilegesResponse getHasPrivilegesResponse(Authentication authentication, HasPrivilegesRequest hasPrivilegesRequest,
154+
boolean authorized) {
155+
Map<String, Boolean> clusterPrivMap = new HashMap<>();
156+
for (String clusterPriv : hasPrivilegesRequest.clusterPrivileges()) {
157+
clusterPrivMap.put(clusterPriv, authorized);
158+
}
159+
final Map<String, ResourcePrivileges> indices = new LinkedHashMap<>();
160+
for (IndicesPrivileges check : hasPrivilegesRequest.indexPrivileges()) {
161+
for (String index : check.getIndices()) {
162+
final Map<String, Boolean> privileges = new HashMap<>();
163+
final ResourcePrivileges existing = indices.get(index);
164+
if (existing != null) {
165+
privileges.putAll(existing.getPrivileges());
166+
}
167+
for (String privilege : check.getPrivileges()) {
168+
privileges.put(privilege, authorized);
169+
}
170+
indices.put(index, ResourcePrivileges.builder(index).addPrivileges(privileges).build());
171+
}
172+
}
173+
final Map<String, Collection<ResourcePrivileges>> privilegesByApplication = new HashMap<>();
174+
Set<String> applicationNames = Arrays.stream(hasPrivilegesRequest.applicationPrivileges())
175+
.map(RoleDescriptor.ApplicationResourcePrivileges::getApplication)
176+
.collect(Collectors.toSet());
177+
for (String applicationName : applicationNames) {
178+
final Map<String, ResourcePrivileges> appPrivilegesByResource = new LinkedHashMap<>();
179+
for (RoleDescriptor.ApplicationResourcePrivileges p : hasPrivilegesRequest.applicationPrivileges()) {
180+
if (applicationName.equals(p.getApplication())) {
181+
for (String resource : p.getResources()) {
182+
final Map<String, Boolean> privileges = new HashMap<>();
183+
final ResourcePrivileges existing = appPrivilegesByResource.get(resource);
184+
if (existing != null) {
185+
privileges.putAll(existing.getPrivileges());
186+
}
187+
for (String privilege : p.getPrivileges()) {
188+
privileges.put(privilege, authorized);
189+
}
190+
appPrivilegesByResource.put(resource, ResourcePrivileges.builder(resource).addPrivileges(privileges).build());
191+
}
192+
}
193+
}
194+
privilegesByApplication.put(applicationName, appPrivilegesByResource.values());
195+
}
196+
return new HasPrivilegesResponse(authentication.getUser().principal(), authorized, clusterPrivMap, indices.values(),
197+
privilegesByApplication);
198+
}
199+
200+
private GetUserPrivilegesResponse getUserPrivilegesResponse(boolean isSuperuser) {
201+
final Set<String> cluster = isSuperuser ? Collections.singleton("ALL") : Collections.emptySet();
202+
final Set<ConditionalClusterPrivilege> conditionalCluster = Collections.emptySet();
203+
final Set<GetUserPrivilegesResponse.Indices> indices = isSuperuser ? Collections.singleton(new Indices(Collections.singleton("*"),
204+
Collections.singleton("*"), Collections.emptySet(), Collections.emptySet(), true)) : Collections.emptySet();
205+
206+
final Set<RoleDescriptor.ApplicationResourcePrivileges> application = isSuperuser ?
207+
Collections.singleton(
208+
RoleDescriptor.ApplicationResourcePrivileges.builder().application("*").privileges("*").resources("*").build()) :
209+
Collections.emptySet();
210+
final Set<String> runAs = isSuperuser ? Collections.singleton("*") : Collections.emptySet();
211+
return new GetUserPrivilegesResponse(cluster, conditionalCluster, indices, application, runAs);
212+
}
213+
214+
public static class CustomAuthorizationInfo implements AuthorizationInfo {
215+
216+
private final String[] roles;
217+
private final CustomAuthorizationInfo authenticatedAuthzInfo;
218+
219+
CustomAuthorizationInfo(String[] roles, CustomAuthorizationInfo authenticatedAuthzInfo) {
220+
this.roles = roles;
221+
this.authenticatedAuthzInfo = authenticatedAuthzInfo;
222+
}
223+
224+
@Override
225+
public Map<String, Object> asMap() {
226+
return Collections.singletonMap("roles", roles);
227+
}
228+
229+
@Override
230+
public CustomAuthorizationInfo getAuthenticatedUserAuthorizationInfo() {
231+
return authenticatedAuthzInfo;
232+
}
233+
}
234+
235+
private boolean isSuperuser(User user) {
236+
return Arrays.asList(user.roles()).contains("custom_superuser");
237+
}
238+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.example;
21+
22+
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.xpack.core.security.SecurityExtension;
24+
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine;
25+
26+
/**
27+
* Security extension class that registers the custom authorization engine to be used
28+
*/
29+
public class ExampleAuthorizationEngineExtension implements SecurityExtension {
30+
31+
@Override
32+
public AuthorizationEngine getAuthorizationEngine(Settings settings) {
33+
return new CustomAuthorizationEngine();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.elasticsearch.example.ExampleAuthorizationEngineExtension

0 commit comments

Comments
 (0)