|
| 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.authc.Authentication; |
| 25 | +import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine; |
| 26 | +import org.elasticsearch.xpack.core.security.authz.ResolvedIndices; |
| 27 | +import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl; |
| 28 | +import org.elasticsearch.xpack.core.security.authz.accesscontrol.IndicesAccessControl.IndexAccessControl; |
| 29 | +import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissions; |
| 30 | +import org.elasticsearch.xpack.core.security.user.User; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.function.Function; |
| 39 | + |
| 40 | +/** |
| 41 | + * A custom implementation of an authorization engine. This engine is extremely basic in that it |
| 42 | + * authorizes based upon the name of a single role. If users have this role they are granted access. |
| 43 | + */ |
| 44 | +public class CustomAuthorizationEngine implements AuthorizationEngine { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void resolveAuthorizationInfo(RequestInfo requestInfo, ActionListener<AuthorizationInfo> listener) { |
| 48 | + final Authentication authentication = requestInfo.getAuthentication(); |
| 49 | + if (authentication.getUser().isRunAs()) { |
| 50 | + final CustomAuthorizationInfo authenticatedUserAuthzInfo = |
| 51 | + new CustomAuthorizationInfo(authentication.getUser().authenticatedUser().roles(), null); |
| 52 | + listener.onResponse(new CustomAuthorizationInfo(authentication.getUser().roles(), authenticatedUserAuthzInfo)); |
| 53 | + } else { |
| 54 | + listener.onResponse(new CustomAuthorizationInfo(authentication.getUser().roles(), null)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void authorizeRunAs(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, ActionListener<AuthorizationResult> listener) { |
| 60 | + if (isSuperuser(requestInfo.getAuthentication().getUser().authenticatedUser())) { |
| 61 | + listener.onResponse(AuthorizationResult.granted()); |
| 62 | + } else { |
| 63 | + listener.onResponse(AuthorizationResult.deny()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void authorizeClusterAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, |
| 69 | + ActionListener<AuthorizationResult> listener) { |
| 70 | + if (isSuperuser(requestInfo.getAuthentication().getUser())) { |
| 71 | + listener.onResponse(AuthorizationResult.granted()); |
| 72 | + } else { |
| 73 | + listener.onResponse(AuthorizationResult.deny()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void authorizeIndexAction(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, |
| 79 | + AsyncSupplier<ResolvedIndices> indicesAsyncSupplier, |
| 80 | + Function<String, AliasOrIndex> aliasOrIndexFunction, |
| 81 | + ActionListener<IndexAuthorizationResult> listener) { |
| 82 | + if (isSuperuser(requestInfo.getAuthentication().getUser())) { |
| 83 | + indicesAsyncSupplier.getAsync(ActionListener.wrap(resolvedIndices -> { |
| 84 | + Map<String, IndexAccessControl> indexAccessControlMap = new HashMap<>(); |
| 85 | + for (String name : resolvedIndices.getLocal()) { |
| 86 | + indexAccessControlMap.put(name, new IndexAccessControl(true, FieldPermissions.DEFAULT, null)); |
| 87 | + } |
| 88 | + IndicesAccessControl indicesAccessControl = |
| 89 | + new IndicesAccessControl(true, Collections.unmodifiableMap(indexAccessControlMap)); |
| 90 | + listener.onResponse(new IndexAuthorizationResult(true, indicesAccessControl)); |
| 91 | + }, listener::onFailure)); |
| 92 | + } else { |
| 93 | + listener.onResponse(new IndexAuthorizationResult(true, IndicesAccessControl.DENIED)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void loadAuthorizedIndices(RequestInfo requestInfo, AuthorizationInfo authorizationInfo, |
| 99 | + Map<String, AliasOrIndex> aliasAndIndexLookup, ActionListener<List<String>> listener) { |
| 100 | + if (isSuperuser(requestInfo.getAuthentication().getUser())) { |
| 101 | + listener.onResponse(new ArrayList<>(aliasAndIndexLookup.keySet())); |
| 102 | + } else { |
| 103 | + listener.onResponse(Collections.emptyList()); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public static class CustomAuthorizationInfo implements AuthorizationInfo { |
| 108 | + |
| 109 | + private final String[] roles; |
| 110 | + private final CustomAuthorizationInfo authenticatedAuthzInfo; |
| 111 | + |
| 112 | + CustomAuthorizationInfo(String[] roles, CustomAuthorizationInfo authenticatedAuthzInfo) { |
| 113 | + this.roles = roles; |
| 114 | + this.authenticatedAuthzInfo = authenticatedAuthzInfo; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public Map<String, Object> asMap() { |
| 119 | + return Collections.singletonMap("roles", roles); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public CustomAuthorizationInfo getAuthenticatedUserAuthorizationInfo() { |
| 124 | + return authenticatedAuthzInfo; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private boolean isSuperuser(User user) { |
| 129 | + return Arrays.binarySearch(user.roles(), "custom_superuser") > -1; |
| 130 | + } |
| 131 | +} |
0 commit comments