|
| 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 | +} |
0 commit comments