|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.kudu.subprocess.ranger.authorization; |
| 19 | + |
| 20 | +import com.google.common.annotations.VisibleForTesting; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import org.apache.hadoop.security.UserGroupInformation; |
| 23 | +import org.apache.kudu.ranger.Ranger.RangerRequestListPB; |
| 24 | +import org.apache.kudu.ranger.Ranger.RangerRequestPB; |
| 25 | +import org.apache.ranger.plugin.audit.RangerDefaultAuditHandler; |
| 26 | +import org.apache.ranger.plugin.model.RangerServiceDef; |
| 27 | +import org.apache.ranger.plugin.policyengine.RangerAccessRequest; |
| 28 | +import org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl; |
| 29 | +import org.apache.ranger.plugin.policyengine.RangerAccessResourceImpl; |
| 30 | +import org.apache.ranger.plugin.policyengine.RangerAccessResult; |
| 31 | +import org.apache.ranger.plugin.service.RangerBasePlugin; |
| 32 | +import org.apache.yetus.audience.InterfaceAudience; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import javax.annotation.Nullable; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Collection; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Set; |
| 42 | + |
| 43 | +public class RangerKuduAuthorizer { |
| 44 | + private static final Logger LOG = LoggerFactory.getLogger(RangerKuduAuthorizer.class); |
| 45 | + // The following properties need to match the Kudu service def in Ranger |
| 46 | + // (https://github.com/apache/ranger/blob/master/agents-common/src/main/resources/service-defs/ranger-servicedef-kudu.json). |
| 47 | + private static final String APP_ID = "kudu"; |
| 48 | + private static final String RANGER_DB_RESOURCE_NAME = "database"; |
| 49 | + private static final String RANGER_TABLE_RESOURCE_NAME = "table"; |
| 50 | + private static final String RANGER_COLUMN_RESOURCE_NAME = "column"; |
| 51 | + private static final String SERVICE_TYPE = "kudu"; |
| 52 | + |
| 53 | + // The Ranger Kudu plugin. This field is not final as it is used in the |
| 54 | + // mock test. |
| 55 | + @InterfaceAudience.LimitedPrivate("Test") |
| 56 | + RangerBasePlugin plugin; |
| 57 | + |
| 58 | + public RangerKuduAuthorizer() { |
| 59 | + plugin = new RangerBasePlugin(SERVICE_TYPE, APP_ID); |
| 60 | + plugin.setResultProcessor(new RangerDefaultAuditHandler()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Initializes the Ranger Kudu plugin, which has to be called explicitly |
| 65 | + * before doing any authorizations. |
| 66 | + */ |
| 67 | + public void init() { |
| 68 | + LOG.info("Initializing Ranger Kudu plugin"); |
| 69 | + plugin.init(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Authorizes a given <code>RangerRequestListPB</code> in Ranger and returns |
| 74 | + * a list of <code>RangerAccessResult</code> which contains the authorization |
| 75 | + * decisions. Note that the order of results is determined by the order of |
| 76 | + * requests. |
| 77 | + * |
| 78 | + * @param requests a RangerRequestListPB |
| 79 | + * @return a list of RangerAccessResult |
| 80 | + */ |
| 81 | + @VisibleForTesting |
| 82 | + public Collection<RangerAccessResult> authorize(RangerRequestListPB requests) { |
| 83 | + Collection<RangerAccessRequest> rangerRequests = createRequests(requests); |
| 84 | + // Reject requests if user field is empty. |
| 85 | + if (!requests.hasUser() || requests.getUser().isEmpty()) { |
| 86 | + Collection<RangerAccessResult> results = new ArrayList<>(); |
| 87 | + for (RangerAccessRequest request : rangerRequests) { |
| 88 | + // Create a 'dummy' RangerAccessResult that denies the request (to have |
| 89 | + // a short cut), instead of sending the request to Ranger. |
| 90 | + RangerAccessResult result = new RangerAccessResult( |
| 91 | + /* policyType= */1, APP_ID, |
| 92 | + new RangerServiceDef(), request); |
| 93 | + result.setIsAllowed(false); |
| 94 | + results.add(result); |
| 95 | + } |
| 96 | + return results; |
| 97 | + } |
| 98 | + return plugin.isAccessAllowed(rangerRequests); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Gets a list of authorization decision from Ranger with the specified list |
| 103 | + * of ranger access request. |
| 104 | + * |
| 105 | + * @param requests a list of RangerAccessRequest |
| 106 | + * @return a list of RangerAccessResult |
| 107 | + */ |
| 108 | + @VisibleForTesting |
| 109 | + Collection<RangerAccessResult> authorize(Collection<RangerAccessRequest> requests) { |
| 110 | + return plugin.isAccessAllowed(requests); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Creates a Ranger access request for the specified user who performs |
| 115 | + * the given action on the resource. |
| 116 | + * |
| 117 | + * @param action action to be authorized on the resource. Note that when it |
| 118 | + * is null, Ranger will match to any valid actions |
| 119 | + * @param user user who is performing the action |
| 120 | + * @param groups the set of groups the user belongs to |
| 121 | + * @param db the database name the action is to be performed on |
| 122 | + * @param table the table name the action is to be performed on |
| 123 | + * @param col the column name the action is to be performed on |
| 124 | + * @return the ranger access request |
| 125 | + */ |
| 126 | + private static RangerAccessRequestImpl createRequest( |
| 127 | + @Nullable String action, String user, |
| 128 | + @Nullable Set<String> groups, @Nullable String db, |
| 129 | + @Nullable String table, @Nullable String col) { |
| 130 | + final RangerAccessResourceImpl resource = new RangerAccessResourceImpl(); |
| 131 | + resource.setValue(RANGER_DB_RESOURCE_NAME, db); |
| 132 | + resource.setValue(RANGER_TABLE_RESOURCE_NAME, table); |
| 133 | + resource.setValue(RANGER_COLUMN_RESOURCE_NAME, col); |
| 134 | + |
| 135 | + final RangerAccessRequestImpl request = new RangerAccessRequestImpl(); |
| 136 | + request.setResource(resource); |
| 137 | + request.setAccessType(action); |
| 138 | + // Add action as it is used for auditing in Ranger. |
| 139 | + request.setAction(action); |
| 140 | + request.setUser(user); |
| 141 | + request.setUserGroups(groups); |
| 142 | + return request; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Creates a list of <code>RangerAccessRequest</code> for the given |
| 147 | + * <code>RangerRequestListPB</code>. |
| 148 | + * |
| 149 | + * @param requests the given RangerRequestListPB |
| 150 | + * @return a list of RangerAccessRequest |
| 151 | + */ |
| 152 | + private static List<RangerAccessRequest> createRequests(RangerRequestListPB requests) { |
| 153 | + List<RangerAccessRequest> rangerRequests = new ArrayList<>(); |
| 154 | + Preconditions.checkArgument(requests.hasUser()); |
| 155 | + Preconditions.checkArgument(!requests.getUser().isEmpty()); |
| 156 | + final String user = requests.getUser(); |
| 157 | + Set<String> groups = getUserGroups(user); |
| 158 | + for (RangerRequestPB request : requests.getRequestsList()) { |
| 159 | + // Action should be lower case to match the Kudu service def in Ranger. |
| 160 | + String action = request.getAction().name().toLowerCase(); |
| 161 | + String db = request.hasDatabase() ? request.getDatabase() : null; |
| 162 | + String table = request.hasTable() ? request.getTable() : null; |
| 163 | + String column = request.hasColumn() ? request.getColumn() : null; |
| 164 | + rangerRequests.add(createRequest(action, user, groups, |
| 165 | + db, table, column)); |
| 166 | + } |
| 167 | + return rangerRequests; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Gets the user group mapping from Hadoop. The groups of a user is determined by a |
| 172 | + * group mapping service provider. See more detail at |
| 173 | + * https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/GroupsMapping.html. |
| 174 | + * |
| 175 | + * @param user the user name |
| 176 | + * @return the set of groups the user belongs to |
| 177 | + */ |
| 178 | + private static Set<String> getUserGroups(String user) { |
| 179 | + Preconditions.checkNotNull(user); |
| 180 | + Preconditions.checkArgument(!user.isEmpty()); |
| 181 | + UserGroupInformation ugi; |
| 182 | + ugi = UserGroupInformation.createRemoteUser(user); |
| 183 | + return new HashSet<>(ugi.getGroups()); |
| 184 | + } |
| 185 | +} |
0 commit comments