001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.security.access; 019 020import java.io.IOException; 021import java.security.PrivilegedExceptionAction; 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.Collections; 025import java.util.HashMap; 026import java.util.Iterator; 027import java.util.List; 028import java.util.Map; 029import java.util.Map.Entry; 030import java.util.Optional; 031import java.util.Set; 032import java.util.TreeMap; 033import java.util.TreeSet; 034import java.util.stream.Collectors; 035import org.apache.hadoop.conf.Configuration; 036import org.apache.hadoop.hbase.ArrayBackedTag; 037import org.apache.hadoop.hbase.Cell; 038import org.apache.hadoop.hbase.CellUtil; 039import org.apache.hadoop.hbase.CompareOperator; 040import org.apache.hadoop.hbase.CompoundConfiguration; 041import org.apache.hadoop.hbase.CoprocessorEnvironment; 042import org.apache.hadoop.hbase.DoNotRetryIOException; 043import org.apache.hadoop.hbase.ExtendedCell; 044import org.apache.hadoop.hbase.ExtendedCellScanner; 045import org.apache.hadoop.hbase.HBaseInterfaceAudience; 046import org.apache.hadoop.hbase.HConstants; 047import org.apache.hadoop.hbase.KeyValue; 048import org.apache.hadoop.hbase.KeyValue.Type; 049import org.apache.hadoop.hbase.NamespaceDescriptor; 050import org.apache.hadoop.hbase.PrivateCellUtil; 051import org.apache.hadoop.hbase.ServerName; 052import org.apache.hadoop.hbase.TableName; 053import org.apache.hadoop.hbase.Tag; 054import org.apache.hadoop.hbase.client.Admin; 055import org.apache.hadoop.hbase.client.Append; 056import org.apache.hadoop.hbase.client.BalanceRequest; 057import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 058import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 059import org.apache.hadoop.hbase.client.Delete; 060import org.apache.hadoop.hbase.client.Durability; 061import org.apache.hadoop.hbase.client.Get; 062import org.apache.hadoop.hbase.client.Increment; 063import org.apache.hadoop.hbase.client.MasterSwitchType; 064import org.apache.hadoop.hbase.client.Mutation; 065import org.apache.hadoop.hbase.client.Put; 066import org.apache.hadoop.hbase.client.Query; 067import org.apache.hadoop.hbase.client.RegionInfo; 068import org.apache.hadoop.hbase.client.Result; 069import org.apache.hadoop.hbase.client.Scan; 070import org.apache.hadoop.hbase.client.SnapshotDescription; 071import org.apache.hadoop.hbase.client.TableDescriptor; 072import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 073import org.apache.hadoop.hbase.coprocessor.BulkLoadObserver; 074import org.apache.hadoop.hbase.coprocessor.CoprocessorException; 075import org.apache.hadoop.hbase.coprocessor.CoreCoprocessor; 076import org.apache.hadoop.hbase.coprocessor.EndpointObserver; 077import org.apache.hadoop.hbase.coprocessor.HasMasterServices; 078import org.apache.hadoop.hbase.coprocessor.HasRegionServerServices; 079import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor; 080import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment; 081import org.apache.hadoop.hbase.coprocessor.MasterObserver; 082import org.apache.hadoop.hbase.coprocessor.ObserverContext; 083import org.apache.hadoop.hbase.coprocessor.ObserverRpcCallContext; 084import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 085import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 086import org.apache.hadoop.hbase.coprocessor.RegionObserver; 087import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessor; 088import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment; 089import org.apache.hadoop.hbase.coprocessor.RegionServerObserver; 090import org.apache.hadoop.hbase.filter.ByteArrayComparable; 091import org.apache.hadoop.hbase.filter.Filter; 092import org.apache.hadoop.hbase.filter.FilterList; 093import org.apache.hadoop.hbase.io.hfile.HFile; 094import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils; 095import org.apache.hadoop.hbase.ipc.RpcServer; 096import org.apache.hadoop.hbase.master.MasterServices; 097import org.apache.hadoop.hbase.net.Address; 098import org.apache.hadoop.hbase.quotas.GlobalQuotaSettings; 099import org.apache.hadoop.hbase.regionserver.BloomType; 100import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker; 101import org.apache.hadoop.hbase.regionserver.InternalScanner; 102import org.apache.hadoop.hbase.regionserver.MiniBatchOperationInProgress; 103import org.apache.hadoop.hbase.regionserver.Region; 104import org.apache.hadoop.hbase.regionserver.RegionScanner; 105import org.apache.hadoop.hbase.regionserver.RegionServerServices; 106import org.apache.hadoop.hbase.regionserver.ScanType; 107import org.apache.hadoop.hbase.regionserver.ScannerContext; 108import org.apache.hadoop.hbase.regionserver.Store; 109import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker; 110import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest; 111import org.apache.hadoop.hbase.replication.ReplicationEndpoint; 112import org.apache.hadoop.hbase.replication.ReplicationPeerConfig; 113import org.apache.hadoop.hbase.replication.SyncReplicationState; 114import org.apache.hadoop.hbase.security.AccessDeniedException; 115import org.apache.hadoop.hbase.security.Superusers; 116import org.apache.hadoop.hbase.security.User; 117import org.apache.hadoop.hbase.security.UserProvider; 118import org.apache.hadoop.hbase.security.access.Permission.Action; 119import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils; 120import org.apache.hadoop.hbase.util.ByteRange; 121import org.apache.hadoop.hbase.util.Bytes; 122import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 123import org.apache.hadoop.hbase.util.Pair; 124import org.apache.hadoop.hbase.util.SimpleMutableByteRange; 125import org.apache.hadoop.hbase.wal.WALEdit; 126import org.apache.yetus.audience.InterfaceAudience; 127import org.slf4j.Logger; 128import org.slf4j.LoggerFactory; 129 130import org.apache.hbase.thirdparty.com.google.common.base.Preconditions; 131import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet; 132import org.apache.hbase.thirdparty.com.google.common.collect.ListMultimap; 133import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 134import org.apache.hbase.thirdparty.com.google.common.collect.MapMaker; 135import org.apache.hbase.thirdparty.com.google.common.collect.Maps; 136import org.apache.hbase.thirdparty.com.google.protobuf.Message; 137import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; 138import org.apache.hbase.thirdparty.com.google.protobuf.RpcController; 139import org.apache.hbase.thirdparty.com.google.protobuf.Service; 140 141import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 142import org.apache.hadoop.hbase.shaded.protobuf.ResponseConverter; 143import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos; 144import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos.AccessControlService; 145import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos.HasPermissionRequest; 146import org.apache.hadoop.hbase.shaded.protobuf.generated.AccessControlProtos.HasPermissionResponse; 147 148/** 149 * Provides basic authorization checks for data access and administrative operations. 150 * <p> 151 * {@code AccessController} performs authorization checks for HBase operations based on: 152 * </p> 153 * <ul> 154 * <li>the identity of the user performing the operation</li> 155 * <li>the scope over which the operation is performed, in increasing specificity: global, table, 156 * column family, or qualifier</li> 157 * <li>the type of action being performed (as mapped to {@link Permission.Action} values)</li> 158 * </ul> 159 * <p> 160 * If the authorization check fails, an {@link AccessDeniedException} will be thrown for the 161 * operation. 162 * </p> 163 * <p> 164 * To perform authorization checks, {@code AccessController} relies on the RpcServerEngine being 165 * loaded to provide the user identities for remote requests. 166 * </p> 167 * <p> 168 * The access control lists used for authorization can be manipulated via the exposed 169 * {@link AccessControlService} Interface implementation, and the associated {@code grant}, 170 * {@code revoke}, and {@code user_permission} HBase shell commands. 171 * </p> 172 */ 173@CoreCoprocessor 174@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG) 175public class AccessController implements MasterCoprocessor, RegionCoprocessor, 176 RegionServerCoprocessor, AccessControlService.Interface, MasterObserver, RegionObserver, 177 RegionServerObserver, EndpointObserver, BulkLoadObserver { 178 // TODO: encapsulate observer functions into separate class/sub-class. 179 180 private static final Logger LOG = LoggerFactory.getLogger(AccessController.class); 181 182 private static final Logger AUDITLOG = 183 LoggerFactory.getLogger("SecurityLogger." + AccessController.class.getName()); 184 private static final String CHECK_COVERING_PERM = "check_covering_perm"; 185 private static final String TAG_CHECK_PASSED = "tag_check_passed"; 186 private static final byte[] TRUE = Bytes.toBytes(true); 187 188 private AccessChecker accessChecker; 189 private ZKPermissionWatcher zkPermissionWatcher; 190 191 /** flags if we are running on a region of the _acl_ table */ 192 private boolean aclRegion = false; 193 194 /** 195 * defined only for Endpoint implementation, so it can have way to access region services 196 */ 197 private RegionCoprocessorEnvironment regionEnv; 198 199 /** Mapping of scanner instances to the user who created them */ 200 private Map<InternalScanner, String> scannerOwners = new MapMaker().weakKeys().makeMap(); 201 202 private Map<TableName, List<UserPermission>> tableAcls; 203 204 /** Provider for mapping principal names to Users */ 205 private UserProvider userProvider; 206 207 /** 208 * if we are active, usually false, only true if "hbase.security.authorization" has been set to 209 * true in site configuration 210 */ 211 private boolean authorizationEnabled; 212 213 /** if we are able to support cell ACLs */ 214 private boolean cellFeaturesEnabled; 215 216 /** if we should check EXEC permissions */ 217 private boolean shouldCheckExecPermission; 218 219 /** 220 * if we should terminate access checks early as soon as table or CF grants allow access; pre-0.98 221 * compatible behavior 222 */ 223 private boolean compatibleEarlyTermination; 224 225 /** if we have been successfully initialized */ 226 private volatile boolean initialized = false; 227 228 /** if the ACL table is available, only relevant in the master */ 229 private volatile boolean aclTabAvailable = false; 230 231 public static boolean isCellAuthorizationSupported(Configuration conf) { 232 return AccessChecker.isAuthorizationSupported(conf) 233 && (HFile.getFormatVersion(conf) >= HFile.MIN_FORMAT_VERSION_WITH_TAGS); 234 } 235 236 public Region getRegion() { 237 return regionEnv != null ? regionEnv.getRegion() : null; 238 } 239 240 public AuthManager getAuthManager() { 241 return accessChecker.getAuthManager(); 242 } 243 244 private void initialize(RegionCoprocessorEnvironment e) throws IOException { 245 final Region region = e.getRegion(); 246 Configuration conf = e.getConfiguration(); 247 Map<byte[], ListMultimap<String, UserPermission>> tables = PermissionStorage.loadAll(region); 248 // For each table, write out the table's permissions to the respective 249 // znode for that table. 250 for (Map.Entry<byte[], ListMultimap<String, UserPermission>> t : tables.entrySet()) { 251 byte[] entry = t.getKey(); 252 ListMultimap<String, UserPermission> perms = t.getValue(); 253 byte[] serialized = PermissionStorage.writePermissionsAsBytes(perms, conf); 254 zkPermissionWatcher.writeToZookeeper(entry, serialized); 255 } 256 initialized = true; 257 } 258 259 /** 260 * Writes all table ACLs for the tables in the given Map up into ZooKeeper znodes. This is called 261 * to synchronize ACL changes following {@code _acl_} table updates. 262 */ 263 private void updateACL(RegionCoprocessorEnvironment e, final Map<byte[], List<Cell>> familyMap) { 264 Set<byte[]> entries = new TreeSet<>(Bytes.BYTES_RAWCOMPARATOR); 265 for (Map.Entry<byte[], List<Cell>> f : familyMap.entrySet()) { 266 List<Cell> cells = f.getValue(); 267 for (Cell cell : cells) { 268 if (CellUtil.matchingFamily(cell, PermissionStorage.ACL_LIST_FAMILY)) { 269 entries.add(CellUtil.cloneRow(cell)); 270 } 271 } 272 } 273 Configuration conf = regionEnv.getConfiguration(); 274 byte[] currentEntry = null; 275 // TODO: Here we are already on the ACL region. (And it is single 276 // region) We can even just get the region from the env and do get 277 // directly. The short circuit connection would avoid the RPC overhead 278 // so no socket communication, req write/read .. But we have the PB 279 // to and fro conversion overhead. get req is converted to PB req 280 // and results are converted to PB results 1st and then to POJOs 281 // again. We could have avoided such at least in ACL table context.. 282 try { 283 for (byte[] entry : entries) { 284 currentEntry = entry; 285 ListMultimap<String, UserPermission> perms = 286 PermissionStorage.getPermissions(e.getConnection(), entry); 287 byte[] serialized = PermissionStorage.writePermissionsAsBytes(perms, conf); 288 zkPermissionWatcher.writeToZookeeper(entry, serialized); 289 } 290 } catch (IOException ex) { 291 LOG.error("Failed updating permissions mirror for '" 292 + (currentEntry == null ? "null" : Bytes.toString(currentEntry)) + "'", ex); 293 } 294 } 295 296 /** 297 * Check the current user for authorization to perform a specific action against the given set of 298 * row data. 299 * @param opType the operation type 300 * @param user the user 301 * @param e the coprocessor environment 302 * @param families the map of column families to qualifiers present in the request 303 * @param actions the desired actions 304 * @return an authorization result 305 */ 306 private AuthResult permissionGranted(OpType opType, User user, RegionCoprocessorEnvironment e, 307 Map<byte[], ? extends Collection<?>> families, Action... actions) { 308 AuthResult result = null; 309 for (Action action : actions) { 310 result = accessChecker.permissionGranted(opType.toString(), user, action, 311 e.getRegion().getRegionInfo().getTable(), families); 312 if (!result.isAllowed()) { 313 return result; 314 } 315 } 316 return result; 317 } 318 319 public void requireAccess(ObserverContext<?> ctx, String request, TableName tableName, 320 Action... permissions) throws IOException { 321 accessChecker.requireAccess(getActiveUser(ctx), request, tableName, permissions); 322 } 323 324 public void requirePermission(ObserverContext<?> ctx, String request, Action perm) 325 throws IOException { 326 accessChecker.requirePermission(getActiveUser(ctx), request, null, perm); 327 } 328 329 public void requireGlobalPermission(ObserverContext<?> ctx, String request, Action perm, 330 TableName tableName, Map<byte[], ? extends Collection<byte[]>> familyMap) throws IOException { 331 accessChecker.requireGlobalPermission(getActiveUser(ctx), request, perm, tableName, familyMap, 332 null); 333 } 334 335 public void requireGlobalPermission(ObserverContext<?> ctx, String request, Action perm, 336 String namespace) throws IOException { 337 accessChecker.requireGlobalPermission(getActiveUser(ctx), request, perm, namespace); 338 } 339 340 public void requireNamespacePermission(ObserverContext<?> ctx, String request, String namespace, 341 Action... permissions) throws IOException { 342 accessChecker.requireNamespacePermission(getActiveUser(ctx), request, namespace, null, 343 permissions); 344 } 345 346 public void requireNamespacePermission(ObserverContext<?> ctx, String request, String namespace, 347 TableName tableName, Map<byte[], ? extends Collection<byte[]>> familyMap, Action... permissions) 348 throws IOException { 349 accessChecker.requireNamespacePermission(getActiveUser(ctx), request, namespace, tableName, 350 familyMap, permissions); 351 } 352 353 public void requirePermission(ObserverContext<?> ctx, String request, TableName tableName, 354 byte[] family, byte[] qualifier, Action... permissions) throws IOException { 355 accessChecker.requirePermission(getActiveUser(ctx), request, tableName, family, qualifier, null, 356 permissions); 357 } 358 359 public void requireTablePermission(ObserverContext<?> ctx, String request, TableName tableName, 360 byte[] family, byte[] qualifier, Action... permissions) throws IOException { 361 accessChecker.requireTablePermission(getActiveUser(ctx), request, tableName, family, qualifier, 362 permissions); 363 } 364 365 public void checkLockPermissions(ObserverContext<?> ctx, String namespace, TableName tableName, 366 RegionInfo[] regionInfos, String reason) throws IOException { 367 accessChecker.checkLockPermissions(getActiveUser(ctx), namespace, tableName, regionInfos, 368 reason); 369 } 370 371 /** 372 * Returns <code>true</code> if the current user is allowed the given action over at least one of 373 * the column qualifiers in the given column families. 374 */ 375 private boolean hasFamilyQualifierPermission(User user, Action perm, 376 RegionCoprocessorEnvironment env, Map<byte[], ? extends Collection<byte[]>> familyMap) 377 throws IOException { 378 RegionInfo hri = env.getRegion().getRegionInfo(); 379 TableName tableName = hri.getTable(); 380 381 if (user == null) { 382 return false; 383 } 384 385 if (familyMap != null && familyMap.size() > 0) { 386 // at least one family must be allowed 387 for (Map.Entry<byte[], ? extends Collection<byte[]>> family : familyMap.entrySet()) { 388 if (family.getValue() != null && !family.getValue().isEmpty()) { 389 for (byte[] qualifier : family.getValue()) { 390 if ( 391 getAuthManager().authorizeUserTable(user, tableName, family.getKey(), qualifier, perm) 392 ) { 393 return true; 394 } 395 } 396 } else { 397 if (getAuthManager().authorizeUserFamily(user, tableName, family.getKey(), perm)) { 398 return true; 399 } 400 } 401 } 402 } else if (LOG.isDebugEnabled()) { 403 LOG.debug("Empty family map passed for permission check"); 404 } 405 406 return false; 407 } 408 409 private enum OpType { 410 GET("get"), 411 EXISTS("exists"), 412 SCAN("scan"), 413 PUT("put"), 414 DELETE("delete"), 415 CHECK_AND_PUT("checkAndPut"), 416 CHECK_AND_DELETE("checkAndDelete"), 417 APPEND("append"), 418 INCREMENT("increment"); 419 420 private String type; 421 422 private OpType(String type) { 423 this.type = type; 424 } 425 426 @Override 427 public String toString() { 428 return type; 429 } 430 } 431 432 /** 433 * Determine if cell ACLs covered by the operation grant access. This is expensive. 434 * @return false if cell ACLs failed to grant access, true otherwise 435 */ 436 private boolean checkCoveringPermission(User user, OpType request, RegionCoprocessorEnvironment e, 437 byte[] row, Map<byte[], ? extends Collection<?>> familyMap, long opTs, Action... actions) 438 throws IOException { 439 if (!cellFeaturesEnabled) { 440 return false; 441 } 442 long cellGrants = 0; 443 long latestCellTs = 0; 444 Get get = new Get(row); 445 // Only in case of Put/Delete op, consider TS within cell (if set for individual cells). 446 // When every cell, within a Mutation, can be linked with diff TS we can not rely on only one 447 // version. We have to get every cell version and check its TS against the TS asked for in 448 // Mutation and skip those Cells which is outside this Mutation TS.In case of Put, we have to 449 // consider only one such passing cell. In case of Delete we have to consider all the cell 450 // versions under this passing version. When Delete Mutation contains columns which are a 451 // version delete just consider only one version for those column cells. 452 boolean considerCellTs = (request == OpType.PUT || request == OpType.DELETE); 453 if (considerCellTs) { 454 get.readAllVersions(); 455 } else { 456 get.readVersions(1); 457 } 458 boolean diffCellTsFromOpTs = false; 459 for (Map.Entry<byte[], ? extends Collection<?>> entry : familyMap.entrySet()) { 460 byte[] col = entry.getKey(); 461 // TODO: HBASE-7114 could possibly unify the collection type in family 462 // maps so we would not need to do this 463 if (entry.getValue() instanceof Set) { 464 Set<byte[]> set = (Set<byte[]>) entry.getValue(); 465 if (set == null || set.isEmpty()) { 466 get.addFamily(col); 467 } else { 468 for (byte[] qual : set) { 469 get.addColumn(col, qual); 470 } 471 } 472 } else if (entry.getValue() instanceof List) { 473 List<ExtendedCell> list = (List<ExtendedCell>) entry.getValue(); 474 if (list == null || list.isEmpty()) { 475 get.addFamily(col); 476 } else { 477 // In case of family delete, a Cell will be added into the list with Qualifier as null. 478 for (ExtendedCell cell : list) { 479 if ( 480 cell.getQualifierLength() == 0 && (cell.getTypeByte() == Type.DeleteFamily.getCode() 481 || cell.getTypeByte() == Type.DeleteFamilyVersion.getCode()) 482 ) { 483 get.addFamily(col); 484 } else { 485 get.addColumn(col, CellUtil.cloneQualifier(cell)); 486 } 487 if (considerCellTs) { 488 long cellTs = cell.getTimestamp(); 489 latestCellTs = Math.max(latestCellTs, cellTs); 490 diffCellTsFromOpTs = diffCellTsFromOpTs || (opTs != cellTs); 491 } 492 } 493 } 494 } else if (entry.getValue() == null) { 495 get.addFamily(col); 496 } else { 497 throw new RuntimeException( 498 "Unhandled collection type " + entry.getValue().getClass().getName()); 499 } 500 } 501 // We want to avoid looking into the future. So, if the cells of the 502 // operation specify a timestamp, or the operation itself specifies a 503 // timestamp, then we use the maximum ts found. Otherwise, we bound 504 // the Get to the current server time. We add 1 to the timerange since 505 // the upper bound of a timerange is exclusive yet we need to examine 506 // any cells found there inclusively. 507 long latestTs = Math.max(opTs, latestCellTs); 508 if (latestTs == 0 || latestTs == HConstants.LATEST_TIMESTAMP) { 509 latestTs = EnvironmentEdgeManager.currentTime(); 510 } 511 get.setTimeRange(0, latestTs + 1); 512 // In case of Put operation we set to read all versions. This was done to consider the case 513 // where columns are added with TS other than the Mutation TS. But normally this wont be the 514 // case with Put. There no need to get all versions but get latest version only. 515 if (!diffCellTsFromOpTs && request == OpType.PUT) { 516 get.readVersions(1); 517 } 518 if (LOG.isTraceEnabled()) { 519 LOG.trace("Scanning for cells with " + get); 520 } 521 // This Map is identical to familyMap. The key is a BR rather than byte[]. 522 // It will be easy to do gets over this new Map as we can create get keys over the Cell cf by 523 // new SimpleByteRange(cell.familyArray, cell.familyOffset, cell.familyLen) 524 Map<ByteRange, List<Cell>> familyMap1 = new HashMap<>(); 525 for (Entry<byte[], ? extends Collection<?>> entry : familyMap.entrySet()) { 526 if (entry.getValue() instanceof List) { 527 familyMap1.put(new SimpleMutableByteRange(entry.getKey()), (List<Cell>) entry.getValue()); 528 } 529 } 530 RegionScanner scanner = getRegion(e).getScanner(new Scan(get)); 531 List<Cell> cells = Lists.newArrayList(); 532 Cell prevCell = null; 533 ByteRange curFam = new SimpleMutableByteRange(); 534 boolean curColAllVersions = (request == OpType.DELETE); 535 long curColCheckTs = opTs; 536 boolean foundColumn = false; 537 try { 538 boolean more = false; 539 ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(1).build(); 540 541 do { 542 cells.clear(); 543 // scan with limit as 1 to hold down memory use on wide rows 544 more = scanner.next(cells, scannerContext); 545 for (Cell cell : cells) { 546 if (LOG.isTraceEnabled()) { 547 LOG.trace("Found cell " + cell); 548 } 549 boolean colChange = prevCell == null || !CellUtil.matchingColumn(prevCell, cell); 550 if (colChange) foundColumn = false; 551 prevCell = cell; 552 if (!curColAllVersions && foundColumn) { 553 continue; 554 } 555 if (colChange && considerCellTs) { 556 curFam.set(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength()); 557 List<Cell> cols = familyMap1.get(curFam); 558 for (Cell col : cols) { 559 // null/empty qualifier is used to denote a Family delete. The TS and delete type 560 // associated with this is applicable for all columns within the family. That is 561 // why the below (col.getQualifierLength() == 0) check. 562 if ( 563 (col.getQualifierLength() == 0 && request == OpType.DELETE) 564 || CellUtil.matchingQualifier(cell, col) 565 ) { 566 byte type = PrivateCellUtil.getTypeByte(col); 567 if (considerCellTs) { 568 curColCheckTs = col.getTimestamp(); 569 } 570 // For a Delete op we pass allVersions as true. When a Delete Mutation contains 571 // a version delete for a column no need to check all the covering cells within 572 // that column. Check all versions when Type is DeleteColumn or DeleteFamily 573 // One version delete types are Delete/DeleteFamilyVersion 574 curColAllVersions = (KeyValue.Type.DeleteColumn.getCode() == type) 575 || (KeyValue.Type.DeleteFamily.getCode() == type); 576 break; 577 } 578 } 579 } 580 if (cell.getTimestamp() > curColCheckTs) { 581 // Just ignore this cell. This is not a covering cell. 582 continue; 583 } 584 foundColumn = true; 585 for (Action action : actions) { 586 // Are there permissions for this user for the cell? 587 if (!getAuthManager().authorizeCell(user, getTableName(e), cell, action)) { 588 // We can stop if the cell ACL denies access 589 return false; 590 } 591 } 592 cellGrants++; 593 } 594 } while (more); 595 } catch (AccessDeniedException ex) { 596 throw ex; 597 } catch (IOException ex) { 598 LOG.error("Exception while getting cells to calculate covering permission", ex); 599 } finally { 600 scanner.close(); 601 } 602 // We should not authorize unless we have found one or more cell ACLs that 603 // grant access. This code is used to check for additional permissions 604 // after no table or CF grants are found. 605 return cellGrants > 0; 606 } 607 608 private static void addCellPermissions(final byte[] perms, Map<byte[], List<Cell>> familyMap) { 609 // Iterate over the entries in the familyMap, replacing the cells therein 610 // with new cells including the ACL data 611 for (Map.Entry<byte[], List<Cell>> e : familyMap.entrySet()) { 612 List<Cell> newCells = Lists.newArrayList(); 613 for (Cell c : e.getValue()) { 614 assert c instanceof ExtendedCell; 615 ExtendedCell cell = (ExtendedCell) c; 616 // Prepend the supplied perms in a new ACL tag to an update list of tags for the cell 617 List<Tag> tags = new ArrayList<>(); 618 tags.add(new ArrayBackedTag(PermissionStorage.ACL_TAG_TYPE, perms)); 619 Iterator<Tag> tagIterator = PrivateCellUtil.tagsIterator(cell); 620 while (tagIterator.hasNext()) { 621 tags.add(tagIterator.next()); 622 } 623 newCells.add(PrivateCellUtil.createCell(cell, tags)); 624 } 625 // This is supposed to be safe, won't CME 626 e.setValue(newCells); 627 } 628 } 629 630 // Checks whether incoming cells contain any tag with type as ACL_TAG_TYPE. This tag 631 // type is reserved and should not be explicitly set by user. 632 private void checkForReservedTagPresence(User user, Mutation m) throws IOException { 633 // No need to check if we're not going to throw 634 if (!authorizationEnabled) { 635 m.setAttribute(TAG_CHECK_PASSED, TRUE); 636 return; 637 } 638 // Superusers are allowed to store cells unconditionally. 639 if (Superusers.isSuperUser(user)) { 640 m.setAttribute(TAG_CHECK_PASSED, TRUE); 641 return; 642 } 643 // We already checked (prePut vs preBatchMutation) 644 if (m.getAttribute(TAG_CHECK_PASSED) != null) { 645 return; 646 } 647 for (ExtendedCellScanner cellScanner = m.cellScanner(); cellScanner.advance();) { 648 Iterator<Tag> tagsItr = PrivateCellUtil.tagsIterator(cellScanner.current()); 649 while (tagsItr.hasNext()) { 650 if (tagsItr.next().getType() == PermissionStorage.ACL_TAG_TYPE) { 651 throw new AccessDeniedException("Mutation contains cell with reserved type tag"); 652 } 653 } 654 } 655 m.setAttribute(TAG_CHECK_PASSED, TRUE); 656 } 657 658 /* ---- MasterObserver implementation ---- */ 659 @Override 660 public void start(CoprocessorEnvironment env) throws IOException { 661 CompoundConfiguration conf = new CompoundConfiguration(); 662 conf.add(env.getConfiguration()); 663 664 authorizationEnabled = AccessChecker.isAuthorizationSupported(conf); 665 if (!authorizationEnabled) { 666 LOG.warn("AccessController has been loaded with authorization checks DISABLED!"); 667 } 668 669 shouldCheckExecPermission = conf.getBoolean(AccessControlConstants.EXEC_PERMISSION_CHECKS_KEY, 670 AccessControlConstants.DEFAULT_EXEC_PERMISSION_CHECKS); 671 672 cellFeaturesEnabled = (HFile.getFormatVersion(conf) >= HFile.MIN_FORMAT_VERSION_WITH_TAGS); 673 if (!cellFeaturesEnabled) { 674 LOG.info("A minimum HFile version of " + HFile.MIN_FORMAT_VERSION_WITH_TAGS 675 + " is required to persist cell ACLs. Consider setting " + HFile.FORMAT_VERSION_KEY 676 + " accordingly."); 677 } 678 679 if (env instanceof MasterCoprocessorEnvironment) { 680 // if running on HMaster 681 MasterCoprocessorEnvironment mEnv = (MasterCoprocessorEnvironment) env; 682 if (mEnv instanceof HasMasterServices) { 683 MasterServices masterServices = ((HasMasterServices) mEnv).getMasterServices(); 684 zkPermissionWatcher = masterServices.getZKPermissionWatcher(); 685 accessChecker = masterServices.getAccessChecker(); 686 } 687 } else if (env instanceof RegionServerCoprocessorEnvironment) { 688 RegionServerCoprocessorEnvironment rsEnv = (RegionServerCoprocessorEnvironment) env; 689 if (rsEnv instanceof HasRegionServerServices) { 690 RegionServerServices rsServices = 691 ((HasRegionServerServices) rsEnv).getRegionServerServices(); 692 zkPermissionWatcher = rsServices.getZKPermissionWatcher(); 693 accessChecker = rsServices.getAccessChecker(); 694 } 695 } else if (env instanceof RegionCoprocessorEnvironment) { 696 // if running at region 697 regionEnv = (RegionCoprocessorEnvironment) env; 698 conf.addBytesMap(regionEnv.getRegion().getTableDescriptor().getValues()); 699 compatibleEarlyTermination = conf.getBoolean(AccessControlConstants.CF_ATTRIBUTE_EARLY_OUT, 700 AccessControlConstants.DEFAULT_ATTRIBUTE_EARLY_OUT); 701 if (regionEnv instanceof HasRegionServerServices) { 702 RegionServerServices rsServices = 703 ((HasRegionServerServices) regionEnv).getRegionServerServices(); 704 zkPermissionWatcher = rsServices.getZKPermissionWatcher(); 705 accessChecker = rsServices.getAccessChecker(); 706 } 707 } 708 709 Preconditions.checkState(zkPermissionWatcher != null, "ZKPermissionWatcher is null"); 710 Preconditions.checkState(accessChecker != null, "AccessChecker is null"); 711 712 // set the user-provider. 713 this.userProvider = UserProvider.instantiate(env.getConfiguration()); 714 tableAcls = new MapMaker().weakValues().makeMap(); 715 } 716 717 @Override 718 public void stop(CoprocessorEnvironment env) { 719 } 720 721 /*********************************** Observer/Service Getters ***********************************/ 722 @Override 723 public Optional<RegionObserver> getRegionObserver() { 724 return Optional.of(this); 725 } 726 727 @Override 728 public Optional<MasterObserver> getMasterObserver() { 729 return Optional.of(this); 730 } 731 732 @Override 733 public Optional<EndpointObserver> getEndpointObserver() { 734 return Optional.of(this); 735 } 736 737 @Override 738 public Optional<BulkLoadObserver> getBulkLoadObserver() { 739 return Optional.of(this); 740 } 741 742 @Override 743 public Optional<RegionServerObserver> getRegionServerObserver() { 744 return Optional.of(this); 745 } 746 747 @Override 748 public Iterable<Service> getServices() { 749 return Collections 750 .singleton(AccessControlProtos.AccessControlService.newReflectiveService(this)); 751 } 752 753 /*********************************** Observer implementations ***********************************/ 754 755 @Override 756 public void preCreateTable(ObserverContext<MasterCoprocessorEnvironment> c, TableDescriptor desc, 757 RegionInfo[] regions) throws IOException { 758 Set<byte[]> families = desc.getColumnFamilyNames(); 759 Map<byte[], Set<byte[]>> familyMap = new TreeMap<>(Bytes.BYTES_COMPARATOR); 760 for (byte[] family : families) { 761 familyMap.put(family, null); 762 } 763 requireNamespacePermission(c, "createTable", desc.getTableName().getNamespaceAsString(), 764 desc.getTableName(), familyMap, Action.ADMIN, Action.CREATE); 765 } 766 767 @Override 768 public void postCompletedCreateTableAction(final ObserverContext<MasterCoprocessorEnvironment> c, 769 final TableDescriptor desc, final RegionInfo[] regions) throws IOException { 770 // When AC is used, it should be configured as the 1st CP. 771 // In Master, the table operations like create, are handled by a Thread pool but the max size 772 // for this pool is 1. So if multiple CPs create tables on startup, these creations will happen 773 // sequentially only. 774 // Related code in HMaster#startServiceThreads 775 // {code} 776 // // We depend on there being only one instance of this executor running 777 // // at a time. To do concurrency, would need fencing of enable/disable of 778 // // tables. 779 // this.service.startExecutorService(ExecutorType.MASTER_TABLE_OPERATIONS, 1); 780 // {code} 781 // In future if we change this pool to have more threads, then there is a chance for thread, 782 // creating acl table, getting delayed and by that time another table creation got over and 783 // this hook is getting called. In such a case, we will need a wait logic here which will 784 // wait till the acl table is created. 785 if (PermissionStorage.isAclTable(desc)) { 786 this.aclTabAvailable = true; 787 } else { 788 if (!aclTabAvailable) { 789 LOG.warn("Not adding owner permission for table " + desc.getTableName() + ". " 790 + PermissionStorage.ACL_TABLE_NAME + " is not yet created. " + getClass().getSimpleName() 791 + " should be configured as the first Coprocessor"); 792 } else { 793 String owner = getActiveUser(c).getShortName(); 794 final UserPermission userPermission = new UserPermission(owner, 795 Permission.newBuilder(desc.getTableName()).withActions(Action.values()).build()); 796 // switch to the real hbase master user for doing the RPC on the ACL table 797 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 798 @Override 799 public Void run() throws Exception { 800 PermissionStorage.addUserPermission(c.getEnvironment().getConnection(), userPermission, 801 false); 802 return null; 803 } 804 }); 805 } 806 } 807 } 808 809 @Override 810 public void preDeleteTable(ObserverContext<MasterCoprocessorEnvironment> c, TableName tableName) 811 throws IOException { 812 requirePermission(c, "deleteTable", tableName, null, null, Action.ADMIN, Action.CREATE); 813 } 814 815 @Override 816 public void postDeleteTable(ObserverContext<MasterCoprocessorEnvironment> c, 817 final TableName tableName) throws IOException { 818 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 819 @Override 820 public Void run() throws Exception { 821 PermissionStorage.removeTablePermissions(c.getEnvironment().getConnection(), tableName); 822 return null; 823 } 824 }); 825 zkPermissionWatcher.deleteTableACLNode(tableName); 826 } 827 828 @Override 829 public void preTruncateTable(ObserverContext<MasterCoprocessorEnvironment> c, 830 final TableName tableName) throws IOException { 831 requirePermission(c, "truncateTable", tableName, null, null, Action.ADMIN, Action.CREATE); 832 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 833 @Override 834 public Void run() throws Exception { 835 List<UserPermission> acls = 836 PermissionStorage.getTablePermissions(c.getEnvironment().getConnection(), tableName); 837 if (acls != null) { 838 tableAcls.put(tableName, acls); 839 } 840 return null; 841 } 842 }); 843 } 844 845 @Override 846 public void postTruncateTable(ObserverContext<MasterCoprocessorEnvironment> ctx, 847 final TableName tableName) throws IOException { 848 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 849 @Override 850 public Void run() throws Exception { 851 List<UserPermission> perms = tableAcls.get(tableName); 852 if (perms != null) { 853 for (UserPermission perm : perms) { 854 PermissionStorage.addUserPermission(ctx.getEnvironment().getConnection(), perm, false); 855 } 856 } 857 tableAcls.remove(tableName); 858 return null; 859 } 860 }); 861 } 862 863 @Override 864 public void preTruncateRegion(final ObserverContext<MasterCoprocessorEnvironment> ctx, 865 final RegionInfo regionInfo) throws IOException { 866 requirePermission(ctx, "truncateRegion", regionInfo.getTable(), null, null, Action.ADMIN, 867 Action.CREATE); 868 } 869 870 @Override 871 public TableDescriptor preModifyTable(ObserverContext<MasterCoprocessorEnvironment> c, 872 TableName tableName, TableDescriptor currentDesc, TableDescriptor newDesc) throws IOException { 873 // TODO: potentially check if this is a add/modify/delete column operation 874 requirePermission(c, "modifyTable", tableName, null, null, Action.ADMIN, Action.CREATE); 875 return newDesc; 876 } 877 878 @Override 879 public String preModifyTableStoreFileTracker(ObserverContext<MasterCoprocessorEnvironment> c, 880 TableName tableName, String dstSFT) throws IOException { 881 requirePermission(c, "modifyTableStoreFileTracker", tableName, null, null, Action.ADMIN, 882 Action.CREATE); 883 return dstSFT; 884 } 885 886 @Override 887 public String preModifyColumnFamilyStoreFileTracker( 888 ObserverContext<MasterCoprocessorEnvironment> c, TableName tableName, byte[] family, 889 String dstSFT) throws IOException { 890 requirePermission(c, "modifyColumnFamilyStoreFileTracker", tableName, family, null, 891 Action.ADMIN, Action.CREATE); 892 return dstSFT; 893 } 894 895 @Override 896 public void postModifyTable(ObserverContext<MasterCoprocessorEnvironment> c, TableName tableName, 897 TableDescriptor oldDesc, TableDescriptor currentDesc) throws IOException { 898 // default the table owner to current user, if not specified. 899 final String owner = getActiveUser(c).getShortName(); 900 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 901 @Override 902 public Void run() throws Exception { 903 UserPermission userperm = new UserPermission(owner, 904 Permission.newBuilder(currentDesc.getTableName()).withActions(Action.values()).build()); 905 PermissionStorage.addUserPermission(c.getEnvironment().getConnection(), userperm, false); 906 return null; 907 } 908 }); 909 // Remove the permissions of the column families dropped from the table. Otherwise, if a 910 // column family with the same name is added back later, the stale permissions would be 911 // effective again. 912 final Set<byte[]> removedFamilies = new TreeSet<>(Bytes.BYTES_COMPARATOR); 913 if (oldDesc != null) { 914 removedFamilies.addAll(oldDesc.getColumnFamilyNames()); 915 removedFamilies.removeAll(currentDesc.getColumnFamilyNames()); 916 } 917 if (!removedFamilies.isEmpty()) { 918 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 919 @Override 920 public Void run() throws Exception { 921 PermissionStorage.removeTableColumnFamiliesPermissions(c.getEnvironment().getConnection(), 922 tableName, removedFamilies); 923 return null; 924 } 925 }); 926 } 927 } 928 929 @Override 930 public void preEnableTable(ObserverContext<MasterCoprocessorEnvironment> c, TableName tableName) 931 throws IOException { 932 requirePermission(c, "enableTable", tableName, null, null, Action.ADMIN, Action.CREATE); 933 } 934 935 @Override 936 public void preDisableTable(ObserverContext<MasterCoprocessorEnvironment> c, TableName tableName) 937 throws IOException { 938 if (Bytes.equals(tableName.getName(), PermissionStorage.ACL_GLOBAL_NAME)) { 939 // We have to unconditionally disallow disable of the ACL table when we are installed, 940 // even if not enforcing authorizations. We are still allowing grants and revocations, 941 // checking permissions and logging audit messages, etc. If the ACL table is not 942 // available we will fail random actions all over the place. 943 throw new AccessDeniedException("Not allowed to disable " + PermissionStorage.ACL_TABLE_NAME 944 + " table with AccessController installed"); 945 } 946 requirePermission(c, "disableTable", tableName, null, null, Action.ADMIN, Action.CREATE); 947 } 948 949 @Override 950 public void preAbortProcedure(ObserverContext<MasterCoprocessorEnvironment> ctx, 951 final long procId) throws IOException { 952 requirePermission(ctx, "abortProcedure", Action.ADMIN); 953 } 954 955 @Override 956 public void postAbortProcedure(ObserverContext<MasterCoprocessorEnvironment> ctx) 957 throws IOException { 958 // There is nothing to do at this time after the procedure abort request was sent. 959 } 960 961 @Override 962 public void preGetProcedures(ObserverContext<MasterCoprocessorEnvironment> ctx) 963 throws IOException { 964 requirePermission(ctx, "getProcedure", Action.ADMIN); 965 } 966 967 @Override 968 public void preGetLocks(ObserverContext<MasterCoprocessorEnvironment> ctx) throws IOException { 969 User user = getActiveUser(ctx); 970 accessChecker.requirePermission(user, "getLocks", null, Action.ADMIN); 971 } 972 973 @Override 974 public void preMove(ObserverContext<MasterCoprocessorEnvironment> c, RegionInfo region, 975 ServerName srcServer, ServerName destServer) throws IOException { 976 requirePermission(c, "move", region.getTable(), null, null, Action.ADMIN); 977 } 978 979 @Override 980 public void preAssign(ObserverContext<MasterCoprocessorEnvironment> c, RegionInfo regionInfo) 981 throws IOException { 982 requirePermission(c, "assign", regionInfo.getTable(), null, null, Action.ADMIN); 983 } 984 985 @Override 986 public void preUnassign(ObserverContext<MasterCoprocessorEnvironment> c, RegionInfo regionInfo) 987 throws IOException { 988 requirePermission(c, "unassign", regionInfo.getTable(), null, null, Action.ADMIN); 989 } 990 991 @Override 992 public void preRegionOffline(ObserverContext<MasterCoprocessorEnvironment> c, 993 RegionInfo regionInfo) throws IOException { 994 requirePermission(c, "regionOffline", regionInfo.getTable(), null, null, Action.ADMIN); 995 } 996 997 @Override 998 public void preSetSplitOrMergeEnabled(final ObserverContext<MasterCoprocessorEnvironment> ctx, 999 final boolean newValue, final MasterSwitchType switchType) throws IOException { 1000 requirePermission(ctx, "setSplitOrMergeEnabled", Action.ADMIN); 1001 } 1002 1003 @Override 1004 public void preBalance(ObserverContext<MasterCoprocessorEnvironment> c, BalanceRequest request) 1005 throws IOException { 1006 requirePermission(c, "balance", Action.ADMIN); 1007 } 1008 1009 @Override 1010 public void preBalanceSwitch(ObserverContext<MasterCoprocessorEnvironment> c, boolean newValue) 1011 throws IOException { 1012 requirePermission(c, "balanceSwitch", Action.ADMIN); 1013 } 1014 1015 @Override 1016 public void preShutdown(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException { 1017 requirePermission(c, "shutdown", Action.ADMIN); 1018 } 1019 1020 @Override 1021 public void preStopMaster(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException { 1022 requirePermission(c, "stopMaster", Action.ADMIN); 1023 } 1024 1025 @Override 1026 public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx) 1027 throws IOException { 1028 try (Admin admin = ctx.getEnvironment().getConnection().getAdmin()) { 1029 if (!admin.tableExists(PermissionStorage.ACL_TABLE_NAME)) { 1030 createACLTable(admin); 1031 } else { 1032 this.aclTabAvailable = true; 1033 } 1034 } 1035 } 1036 1037 /** 1038 * Create the ACL table 1039 */ 1040 private static void createACLTable(Admin admin) throws IOException { 1041 /** Table descriptor for ACL table */ 1042 ColumnFamilyDescriptor cfd = 1043 ColumnFamilyDescriptorBuilder.newBuilder(PermissionStorage.ACL_LIST_FAMILY).setMaxVersions(1) 1044 .setInMemory(true).setBlockCacheEnabled(true).setBlocksize(8 * 1024) 1045 .setBloomFilterType(BloomType.NONE).setScope(HConstants.REPLICATION_SCOPE_LOCAL).build(); 1046 TableDescriptor td = TableDescriptorBuilder.newBuilder(PermissionStorage.ACL_TABLE_NAME) 1047 .setColumnFamily(cfd).build(); 1048 admin.createTable(td); 1049 } 1050 1051 @Override 1052 public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx, 1053 final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor) throws IOException { 1054 // Move this ACL check to SnapshotManager#checkPermissions as part of AC deprecation. 1055 requirePermission(ctx, "snapshot " + snapshot.getName(), hTableDescriptor.getTableName(), null, 1056 null, Permission.Action.ADMIN); 1057 } 1058 1059 @Override 1060 public void preListSnapshot(ObserverContext<MasterCoprocessorEnvironment> ctx, 1061 final SnapshotDescription snapshot) throws IOException { 1062 User user = getActiveUser(ctx); 1063 if (SnapshotDescriptionUtils.isSnapshotOwner(snapshot, user)) { 1064 // list it, if user is the owner of snapshot 1065 AuthResult result = AuthResult.allow("listSnapshot " + snapshot.getName(), 1066 "Snapshot owner check allowed", user, null, null, null); 1067 AccessChecker.logResult(result); 1068 } else { 1069 accessChecker.requirePermission(user, "listSnapshot " + snapshot.getName(), null, 1070 Action.ADMIN); 1071 } 1072 } 1073 1074 @Override 1075 public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx, 1076 final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor) throws IOException { 1077 User user = getActiveUser(ctx); 1078 if ( 1079 SnapshotDescriptionUtils.isSnapshotOwner(snapshot, user) 1080 && hTableDescriptor.getTableName().getNameAsString().equals(snapshot.getTableNameAsString()) 1081 ) { 1082 // Snapshot owner is allowed to create a table with the same name as the snapshot he took 1083 AuthResult result = AuthResult.allow("cloneSnapshot " + snapshot.getName(), 1084 "Snapshot owner check allowed", user, null, hTableDescriptor.getTableName(), null); 1085 AccessChecker.logResult(result); 1086 } else if (SnapshotDescriptionUtils.isSnapshotOwner(snapshot, user)) { 1087 requireNamespacePermission(ctx, "cloneSnapshot", 1088 hTableDescriptor.getTableName().getNamespaceAsString(), Action.ADMIN); 1089 } else { 1090 accessChecker.requirePermission(user, "cloneSnapshot " + snapshot.getName(), null, 1091 Action.ADMIN); 1092 } 1093 } 1094 1095 @Override 1096 public void preRestoreSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx, 1097 final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor) throws IOException { 1098 User user = getActiveUser(ctx); 1099 if (SnapshotDescriptionUtils.isSnapshotOwner(snapshot, user)) { 1100 accessChecker.requirePermission(user, "restoreSnapshot " + snapshot.getName(), 1101 hTableDescriptor.getTableName(), null, null, null, Permission.Action.ADMIN); 1102 } else { 1103 accessChecker.requirePermission(user, "restoreSnapshot " + snapshot.getName(), null, 1104 Action.ADMIN); 1105 } 1106 } 1107 1108 @Override 1109 public void preDeleteSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx, 1110 final SnapshotDescription snapshot) throws IOException { 1111 User user = getActiveUser(ctx); 1112 if (SnapshotDescriptionUtils.isSnapshotOwner(snapshot, user)) { 1113 // Snapshot owner is allowed to delete the snapshot 1114 AuthResult result = AuthResult.allow("deleteSnapshot " + snapshot.getName(), 1115 "Snapshot owner check allowed", user, null, null, null); 1116 AccessChecker.logResult(result); 1117 } else { 1118 accessChecker.requirePermission(user, "deleteSnapshot " + snapshot.getName(), null, 1119 Action.ADMIN); 1120 } 1121 } 1122 1123 @Override 1124 public void preCreateNamespace(ObserverContext<MasterCoprocessorEnvironment> ctx, 1125 NamespaceDescriptor ns) throws IOException { 1126 requireGlobalPermission(ctx, "createNamespace", Action.ADMIN, ns.getName()); 1127 } 1128 1129 @Override 1130 public void preDeleteNamespace(ObserverContext<MasterCoprocessorEnvironment> ctx, 1131 String namespace) throws IOException { 1132 requireGlobalPermission(ctx, "deleteNamespace", Action.ADMIN, namespace); 1133 } 1134 1135 @Override 1136 public void postDeleteNamespace(ObserverContext<MasterCoprocessorEnvironment> ctx, 1137 final String namespace) throws IOException { 1138 User.runAsLoginUser(new PrivilegedExceptionAction<Void>() { 1139 @Override 1140 public Void run() throws Exception { 1141 PermissionStorage.removeNamespacePermissions(ctx.getEnvironment().getConnection(), 1142 namespace); 1143 return null; 1144 } 1145 }); 1146 zkPermissionWatcher.deleteNamespaceACLNode(namespace); 1147 LOG.info(namespace + " entry deleted in " + PermissionStorage.ACL_TABLE_NAME + " table."); 1148 } 1149 1150 @Override 1151 public void preModifyNamespace(ObserverContext<MasterCoprocessorEnvironment> ctx, 1152 NamespaceDescriptor currentNsDesc, NamespaceDescriptor newNsDesc) throws IOException { 1153 // We require only global permission so that 1154 // a user with NS admin cannot altering namespace configurations. i.e. namespace quota 1155 requireGlobalPermission(ctx, "modifyNamespace", Action.ADMIN, newNsDesc.getName()); 1156 } 1157 1158 @Override 1159 public void preGetNamespaceDescriptor(ObserverContext<MasterCoprocessorEnvironment> ctx, 1160 String namespace) throws IOException { 1161 requireNamespacePermission(ctx, "getNamespaceDescriptor", namespace, Action.ADMIN); 1162 } 1163 1164 @Override 1165 public void postListNamespaces(ObserverContext<MasterCoprocessorEnvironment> ctx, 1166 List<String> namespaces) throws IOException { 1167 /* always allow namespace listing */ 1168 } 1169 1170 @Override 1171 public void postListNamespaceDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx, 1172 List<NamespaceDescriptor> descriptors) throws IOException { 1173 // Retains only those which passes authorization checks, as the checks weren't done as part 1174 // of preGetTableDescriptors. 1175 Iterator<NamespaceDescriptor> itr = descriptors.iterator(); 1176 User user = getActiveUser(ctx); 1177 while (itr.hasNext()) { 1178 NamespaceDescriptor desc = itr.next(); 1179 try { 1180 accessChecker.requireNamespacePermission(user, "listNamespaces", desc.getName(), null, 1181 Action.ADMIN); 1182 } catch (AccessDeniedException e) { 1183 itr.remove(); 1184 } 1185 } 1186 } 1187 1188 @Override 1189 public void preTableFlush(final ObserverContext<MasterCoprocessorEnvironment> ctx, 1190 final TableName tableName) throws IOException { 1191 // Move this ACL check to MasterFlushTableProcedureManager#checkPermissions as part of AC 1192 // deprecation. 1193 requirePermission(ctx, "flushTable", tableName, null, null, Action.ADMIN, Action.CREATE); 1194 } 1195 1196 @Override 1197 public void preSplitRegion(final ObserverContext<MasterCoprocessorEnvironment> ctx, 1198 final TableName tableName, final byte[] splitRow) throws IOException { 1199 requirePermission(ctx, "split", tableName, null, null, Action.ADMIN); 1200 } 1201 1202 @Override 1203 public void preClearDeadServers(ObserverContext<MasterCoprocessorEnvironment> ctx) 1204 throws IOException { 1205 requirePermission(ctx, "clearDeadServers", Action.ADMIN); 1206 } 1207 1208 @Override 1209 public void preDecommissionRegionServers(ObserverContext<MasterCoprocessorEnvironment> ctx, 1210 List<ServerName> servers, boolean offload) throws IOException { 1211 requirePermission(ctx, "decommissionRegionServers", Action.ADMIN); 1212 } 1213 1214 @Override 1215 public void preListDecommissionedRegionServers(ObserverContext<MasterCoprocessorEnvironment> ctx) 1216 throws IOException { 1217 requirePermission(ctx, "listDecommissionedRegionServers", Action.READ); 1218 } 1219 1220 @Override 1221 public void preRecommissionRegionServer(ObserverContext<MasterCoprocessorEnvironment> ctx, 1222 ServerName server, List<byte[]> encodedRegionNames) throws IOException { 1223 requirePermission(ctx, "recommissionRegionServers", Action.ADMIN); 1224 } 1225 1226 /* ---- RegionObserver implementation ---- */ 1227 1228 @Override 1229 public void preOpen(ObserverContext<? extends RegionCoprocessorEnvironment> c) 1230 throws IOException { 1231 RegionCoprocessorEnvironment env = c.getEnvironment(); 1232 final Region region = env.getRegion(); 1233 if (region == null) { 1234 LOG.error("NULL region from RegionCoprocessorEnvironment in preOpen()"); 1235 } else { 1236 RegionInfo regionInfo = region.getRegionInfo(); 1237 if (regionInfo.getTable().isSystemTable()) { 1238 checkSystemOrSuperUser(getActiveUser(c)); 1239 } else { 1240 requirePermission(c, "preOpen", Action.ADMIN); 1241 } 1242 } 1243 } 1244 1245 @Override 1246 public void postOpen(ObserverContext<? extends RegionCoprocessorEnvironment> c) { 1247 RegionCoprocessorEnvironment env = c.getEnvironment(); 1248 final Region region = env.getRegion(); 1249 if (region == null) { 1250 LOG.error("NULL region from RegionCoprocessorEnvironment in postOpen()"); 1251 return; 1252 } 1253 if (PermissionStorage.isAclRegion(region)) { 1254 aclRegion = true; 1255 try { 1256 initialize(env); 1257 } catch (IOException ex) { 1258 // if we can't obtain permissions, it's better to fail 1259 // than perform checks incorrectly 1260 throw new RuntimeException("Failed to initialize permissions cache", ex); 1261 } 1262 } else { 1263 initialized = true; 1264 } 1265 } 1266 1267 @Override 1268 public void preFlush(ObserverContext<? extends RegionCoprocessorEnvironment> c, 1269 FlushLifeCycleTracker tracker) throws IOException { 1270 requirePermission(c, "flush", getTableName(c.getEnvironment()), null, null, Action.ADMIN, 1271 Action.CREATE); 1272 } 1273 1274 @Override 1275 public InternalScanner preCompact(ObserverContext<? extends RegionCoprocessorEnvironment> c, 1276 Store store, InternalScanner scanner, ScanType scanType, CompactionLifeCycleTracker tracker, 1277 CompactionRequest request) throws IOException { 1278 requirePermission(c, "compact", getTableName(c.getEnvironment()), null, null, Action.ADMIN, 1279 Action.CREATE); 1280 return scanner; 1281 } 1282 1283 private void internalPreRead(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1284 final Query query, OpType opType) throws IOException { 1285 Filter filter = query.getFilter(); 1286 // Don't wrap an AccessControlFilter 1287 if (filter != null && filter instanceof AccessControlFilter) { 1288 return; 1289 } 1290 User user = getActiveUser(c); 1291 RegionCoprocessorEnvironment env = c.getEnvironment(); 1292 Map<byte[], ? extends Collection<byte[]>> families = null; 1293 switch (opType) { 1294 case GET: 1295 case EXISTS: 1296 families = ((Get) query).getFamilyMap(); 1297 break; 1298 case SCAN: 1299 families = ((Scan) query).getFamilyMap(); 1300 break; 1301 default: 1302 throw new RuntimeException("Unhandled operation " + opType); 1303 } 1304 AuthResult authResult = permissionGranted(opType, user, env, families, Action.READ); 1305 Region region = getRegion(env); 1306 TableName table = getTableName(region); 1307 Map<ByteRange, Integer> cfVsMaxVersions = Maps.newHashMap(); 1308 for (ColumnFamilyDescriptor hcd : region.getTableDescriptor().getColumnFamilies()) { 1309 cfVsMaxVersions.put(new SimpleMutableByteRange(hcd.getName()), hcd.getMaxVersions()); 1310 } 1311 if (!authResult.isAllowed()) { 1312 if (!cellFeaturesEnabled || compatibleEarlyTermination) { 1313 // Old behavior: Scan with only qualifier checks if we have partial 1314 // permission. Backwards compatible behavior is to throw an 1315 // AccessDeniedException immediately if there are no grants for table 1316 // or CF or CF+qual. Only proceed with an injected filter if there are 1317 // grants for qualifiers. Otherwise we will fall through below and log 1318 // the result and throw an ADE. We may end up checking qualifier 1319 // grants three times (permissionGranted above, here, and in the 1320 // filter) but that's the price of backwards compatibility. 1321 if (hasFamilyQualifierPermission(user, Action.READ, env, families)) { 1322 authResult.setAllowed(true); 1323 authResult.setReason("Access allowed with filter"); 1324 // Only wrap the filter if we are enforcing authorizations 1325 if (authorizationEnabled) { 1326 Filter ourFilter = new AccessControlFilter(getAuthManager(), user, table, 1327 AccessControlFilter.Strategy.CHECK_TABLE_AND_CF_ONLY, cfVsMaxVersions); 1328 // wrap any existing filter 1329 if (filter != null) { 1330 ourFilter = new FilterList(FilterList.Operator.MUST_PASS_ALL, 1331 Lists.newArrayList(ourFilter, filter)); 1332 } 1333 switch (opType) { 1334 case GET: 1335 case EXISTS: 1336 ((Get) query).setFilter(ourFilter); 1337 break; 1338 case SCAN: 1339 ((Scan) query).setFilter(ourFilter); 1340 break; 1341 default: 1342 throw new RuntimeException("Unhandled operation " + opType); 1343 } 1344 } 1345 } 1346 } else { 1347 // New behavior: Any access we might be granted is more fine-grained 1348 // than whole table or CF. Simply inject a filter and return what is 1349 // allowed. We will not throw an AccessDeniedException. This is a 1350 // behavioral change since 0.96. 1351 authResult.setAllowed(true); 1352 authResult.setReason("Access allowed with filter"); 1353 // Only wrap the filter if we are enforcing authorizations 1354 if (authorizationEnabled) { 1355 Filter ourFilter = new AccessControlFilter(getAuthManager(), user, table, 1356 AccessControlFilter.Strategy.CHECK_CELL_DEFAULT, cfVsMaxVersions); 1357 // wrap any existing filter 1358 if (filter != null) { 1359 ourFilter = new FilterList(FilterList.Operator.MUST_PASS_ALL, 1360 Lists.newArrayList(ourFilter, filter)); 1361 } 1362 switch (opType) { 1363 case GET: 1364 case EXISTS: 1365 ((Get) query).setFilter(ourFilter); 1366 break; 1367 case SCAN: 1368 ((Scan) query).setFilter(ourFilter); 1369 break; 1370 default: 1371 throw new RuntimeException("Unhandled operation " + opType); 1372 } 1373 } 1374 } 1375 } 1376 1377 AccessChecker.logResult(authResult); 1378 if (authorizationEnabled && !authResult.isAllowed()) { 1379 throw new AccessDeniedException("Insufficient permissions for user '" 1380 + (user != null ? user.getShortName() : "null") + "' (table=" + table + ", action=READ)"); 1381 } 1382 } 1383 1384 @Override 1385 public void preGetOp(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1386 final Get get, final List<Cell> result) throws IOException { 1387 internalPreRead(c, get, OpType.GET); 1388 } 1389 1390 @Override 1391 public boolean preExists(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1392 final Get get, final boolean exists) throws IOException { 1393 internalPreRead(c, get, OpType.EXISTS); 1394 return exists; 1395 } 1396 1397 @Override 1398 public void prePut(final ObserverContext<? extends RegionCoprocessorEnvironment> c, final Put put, 1399 final WALEdit edit, final Durability durability) throws IOException { 1400 User user = getActiveUser(c); 1401 checkForReservedTagPresence(user, put); 1402 1403 // Require WRITE permission to the table, CF, or top visible value, if any. 1404 // NOTE: We don't need to check the permissions for any earlier Puts 1405 // because we treat the ACLs in each Put as timestamped like any other 1406 // HBase value. A new ACL in a new Put applies to that Put. It doesn't 1407 // change the ACL of any previous Put. This allows simple evolution of 1408 // security policy over time without requiring expensive updates. 1409 RegionCoprocessorEnvironment env = c.getEnvironment(); 1410 Map<byte[], ? extends Collection<Cell>> families = put.getFamilyCellMap(); 1411 AuthResult authResult = permissionGranted(OpType.PUT, user, env, families, Action.WRITE); 1412 AccessChecker.logResult(authResult); 1413 if (!authResult.isAllowed()) { 1414 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1415 put.setAttribute(CHECK_COVERING_PERM, TRUE); 1416 } else if (authorizationEnabled) { 1417 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1418 } 1419 } 1420 1421 // Add cell ACLs from the operation to the cells themselves 1422 byte[] bytes = put.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL); 1423 if (bytes != null) { 1424 if (cellFeaturesEnabled) { 1425 addCellPermissions(bytes, put.getFamilyCellMap()); 1426 } else { 1427 throw new DoNotRetryIOException("Cell ACLs cannot be persisted"); 1428 } 1429 } 1430 } 1431 1432 @Override 1433 public void postPut(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1434 final Put put, final WALEdit edit, final Durability durability) { 1435 if (aclRegion) { 1436 updateACL(c.getEnvironment(), put.getFamilyCellMap()); 1437 } 1438 } 1439 1440 @Override 1441 public void preDelete(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1442 final Delete delete, final WALEdit edit, final Durability durability) throws IOException { 1443 // An ACL on a delete is useless, we shouldn't allow it 1444 if (delete.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL) != null) { 1445 throw new DoNotRetryIOException("ACL on delete has no effect: " + delete.toString()); 1446 } 1447 // Require WRITE permissions on all cells covered by the delete. Unlike 1448 // for Puts we need to check all visible prior versions, because a major 1449 // compaction could remove them. If the user doesn't have permission to 1450 // overwrite any of the visible versions ('visible' defined as not covered 1451 // by a tombstone already) then we have to disallow this operation. 1452 RegionCoprocessorEnvironment env = c.getEnvironment(); 1453 Map<byte[], ? extends Collection<Cell>> families = delete.getFamilyCellMap(); 1454 User user = getActiveUser(c); 1455 AuthResult authResult = permissionGranted(OpType.DELETE, user, env, families, Action.WRITE); 1456 AccessChecker.logResult(authResult); 1457 if (!authResult.isAllowed()) { 1458 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1459 delete.setAttribute(CHECK_COVERING_PERM, TRUE); 1460 } else if (authorizationEnabled) { 1461 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1462 } 1463 } 1464 } 1465 1466 @Override 1467 public void preBatchMutate(ObserverContext<? extends RegionCoprocessorEnvironment> c, 1468 MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException { 1469 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1470 TableName table = c.getEnvironment().getRegion().getRegionInfo().getTable(); 1471 User user = getActiveUser(c); 1472 for (int i = 0; i < miniBatchOp.size(); i++) { 1473 Mutation m = miniBatchOp.getOperation(i); 1474 if (m.getAttribute(CHECK_COVERING_PERM) != null) { 1475 // We have a failure with table, cf and q perm checks and now giving a chance for cell 1476 // perm check 1477 OpType opType; 1478 long timestamp; 1479 if (m instanceof Put) { 1480 checkForReservedTagPresence(user, m); 1481 opType = OpType.PUT; 1482 timestamp = m.getTimestamp(); 1483 } else if (m instanceof Delete) { 1484 opType = OpType.DELETE; 1485 timestamp = m.getTimestamp(); 1486 } else if (m instanceof Increment) { 1487 opType = OpType.INCREMENT; 1488 timestamp = ((Increment) m).getTimeRange().getMax(); 1489 } else if (m instanceof Append) { 1490 opType = OpType.APPEND; 1491 timestamp = ((Append) m).getTimeRange().getMax(); 1492 } else { 1493 // If the operation type is not Put/Delete/Increment/Append, do nothing 1494 continue; 1495 } 1496 AuthResult authResult = null; 1497 if ( 1498 checkCoveringPermission(user, opType, c.getEnvironment(), m.getRow(), 1499 m.getFamilyCellMap(), timestamp, Action.WRITE) 1500 ) { 1501 authResult = AuthResult.allow(opType.toString(), "Covering cell set", user, 1502 Action.WRITE, table, m.getFamilyCellMap()); 1503 } else { 1504 authResult = AuthResult.deny(opType.toString(), "Covering cell set", user, Action.WRITE, 1505 table, m.getFamilyCellMap()); 1506 } 1507 AccessChecker.logResult(authResult); 1508 if (authorizationEnabled && !authResult.isAllowed()) { 1509 throw new AccessDeniedException( 1510 "Insufficient permissions " + authResult.toContextString()); 1511 } 1512 } 1513 } 1514 } 1515 } 1516 1517 @Override 1518 public void postDelete(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1519 final Delete delete, final WALEdit edit, final Durability durability) throws IOException { 1520 if (aclRegion) { 1521 updateACL(c.getEnvironment(), delete.getFamilyCellMap()); 1522 } 1523 } 1524 1525 @Override 1526 public boolean preCheckAndPut(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1527 final byte[] row, final byte[] family, final byte[] qualifier, final CompareOperator op, 1528 final ByteArrayComparable comparator, final Put put, final boolean result) throws IOException { 1529 User user = getActiveUser(c); 1530 checkForReservedTagPresence(user, put); 1531 1532 // Require READ and WRITE permissions on the table, CF, and KV to update 1533 RegionCoprocessorEnvironment env = c.getEnvironment(); 1534 Map<byte[], ? extends Collection<byte[]>> families = makeFamilyMap(family, qualifier); 1535 AuthResult authResult = 1536 permissionGranted(OpType.CHECK_AND_PUT, user, env, families, Action.READ, Action.WRITE); 1537 AccessChecker.logResult(authResult); 1538 if (!authResult.isAllowed()) { 1539 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1540 put.setAttribute(CHECK_COVERING_PERM, TRUE); 1541 } else if (authorizationEnabled) { 1542 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1543 } 1544 } 1545 1546 byte[] bytes = put.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL); 1547 if (bytes != null) { 1548 if (cellFeaturesEnabled) { 1549 addCellPermissions(bytes, put.getFamilyCellMap()); 1550 } else { 1551 throw new DoNotRetryIOException("Cell ACLs cannot be persisted"); 1552 } 1553 } 1554 return result; 1555 } 1556 1557 @Override 1558 public boolean preCheckAndPutAfterRowLock( 1559 final ObserverContext<? extends RegionCoprocessorEnvironment> c, final byte[] row, 1560 final byte[] family, final byte[] qualifier, final CompareOperator opp, 1561 final ByteArrayComparable comparator, final Put put, final boolean result) throws IOException { 1562 if (put.getAttribute(CHECK_COVERING_PERM) != null) { 1563 // We had failure with table, cf and q perm checks and now giving a chance for cell 1564 // perm check 1565 TableName table = c.getEnvironment().getRegion().getRegionInfo().getTable(); 1566 Map<byte[], ? extends Collection<byte[]>> families = makeFamilyMap(family, qualifier); 1567 AuthResult authResult = null; 1568 User user = getActiveUser(c); 1569 if ( 1570 checkCoveringPermission(user, OpType.CHECK_AND_PUT, c.getEnvironment(), row, families, 1571 HConstants.LATEST_TIMESTAMP, Action.READ) 1572 ) { 1573 authResult = AuthResult.allow(OpType.CHECK_AND_PUT.toString(), "Covering cell set", user, 1574 Action.READ, table, families); 1575 } else { 1576 authResult = AuthResult.deny(OpType.CHECK_AND_PUT.toString(), "Covering cell set", user, 1577 Action.READ, table, families); 1578 } 1579 AccessChecker.logResult(authResult); 1580 if (authorizationEnabled && !authResult.isAllowed()) { 1581 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1582 } 1583 } 1584 return result; 1585 } 1586 1587 @Override 1588 public boolean preCheckAndDelete(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1589 final byte[] row, final byte[] family, final byte[] qualifier, final CompareOperator op, 1590 final ByteArrayComparable comparator, final Delete delete, final boolean result) 1591 throws IOException { 1592 // An ACL on a delete is useless, we shouldn't allow it 1593 if (delete.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL) != null) { 1594 throw new DoNotRetryIOException("ACL on checkAndDelete has no effect: " + delete.toString()); 1595 } 1596 // Require READ and WRITE permissions on the table, CF, and the KV covered 1597 // by the delete 1598 RegionCoprocessorEnvironment env = c.getEnvironment(); 1599 Map<byte[], ? extends Collection<byte[]>> families = makeFamilyMap(family, qualifier); 1600 User user = getActiveUser(c); 1601 AuthResult authResult = 1602 permissionGranted(OpType.CHECK_AND_DELETE, user, env, families, Action.READ, Action.WRITE); 1603 AccessChecker.logResult(authResult); 1604 if (!authResult.isAllowed()) { 1605 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1606 delete.setAttribute(CHECK_COVERING_PERM, TRUE); 1607 } else if (authorizationEnabled) { 1608 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1609 } 1610 } 1611 return result; 1612 } 1613 1614 @Override 1615 public boolean preCheckAndDeleteAfterRowLock( 1616 final ObserverContext<? extends RegionCoprocessorEnvironment> c, final byte[] row, 1617 final byte[] family, final byte[] qualifier, final CompareOperator op, 1618 final ByteArrayComparable comparator, final Delete delete, final boolean result) 1619 throws IOException { 1620 if (delete.getAttribute(CHECK_COVERING_PERM) != null) { 1621 // We had failure with table, cf and q perm checks and now giving a chance for cell 1622 // perm check 1623 TableName table = c.getEnvironment().getRegion().getRegionInfo().getTable(); 1624 Map<byte[], ? extends Collection<byte[]>> families = makeFamilyMap(family, qualifier); 1625 AuthResult authResult = null; 1626 User user = getActiveUser(c); 1627 if ( 1628 checkCoveringPermission(user, OpType.CHECK_AND_DELETE, c.getEnvironment(), row, families, 1629 HConstants.LATEST_TIMESTAMP, Action.READ) 1630 ) { 1631 authResult = AuthResult.allow(OpType.CHECK_AND_DELETE.toString(), "Covering cell set", user, 1632 Action.READ, table, families); 1633 } else { 1634 authResult = AuthResult.deny(OpType.CHECK_AND_DELETE.toString(), "Covering cell set", user, 1635 Action.READ, table, families); 1636 } 1637 AccessChecker.logResult(authResult); 1638 if (authorizationEnabled && !authResult.isAllowed()) { 1639 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1640 } 1641 } 1642 return result; 1643 } 1644 1645 @Override 1646 public Result preAppend(ObserverContext<? extends RegionCoprocessorEnvironment> c, Append append) 1647 throws IOException { 1648 User user = getActiveUser(c); 1649 checkForReservedTagPresence(user, append); 1650 1651 // Require WRITE permission to the table, CF, and the KV to be appended 1652 RegionCoprocessorEnvironment env = c.getEnvironment(); 1653 Map<byte[], ? extends Collection<Cell>> families = append.getFamilyCellMap(); 1654 AuthResult authResult = permissionGranted(OpType.APPEND, user, env, families, Action.WRITE); 1655 AccessChecker.logResult(authResult); 1656 if (!authResult.isAllowed()) { 1657 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1658 append.setAttribute(CHECK_COVERING_PERM, TRUE); 1659 } else if (authorizationEnabled) { 1660 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1661 } 1662 } 1663 1664 byte[] bytes = append.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL); 1665 if (bytes != null) { 1666 if (cellFeaturesEnabled) { 1667 addCellPermissions(bytes, append.getFamilyCellMap()); 1668 } else { 1669 throw new DoNotRetryIOException("Cell ACLs cannot be persisted"); 1670 } 1671 } 1672 1673 return null; 1674 } 1675 1676 @Override 1677 public Result preIncrement(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1678 final Increment increment) throws IOException { 1679 User user = getActiveUser(c); 1680 checkForReservedTagPresence(user, increment); 1681 1682 // Require WRITE permission to the table, CF, and the KV to be replaced by 1683 // the incremented value 1684 RegionCoprocessorEnvironment env = c.getEnvironment(); 1685 Map<byte[], ? extends Collection<Cell>> families = increment.getFamilyCellMap(); 1686 AuthResult authResult = permissionGranted(OpType.INCREMENT, user, env, families, Action.WRITE); 1687 AccessChecker.logResult(authResult); 1688 if (!authResult.isAllowed()) { 1689 if (cellFeaturesEnabled && !compatibleEarlyTermination) { 1690 increment.setAttribute(CHECK_COVERING_PERM, TRUE); 1691 } else if (authorizationEnabled) { 1692 throw new AccessDeniedException("Insufficient permissions " + authResult.toContextString()); 1693 } 1694 } 1695 1696 byte[] bytes = increment.getAttribute(AccessControlConstants.OP_ATTRIBUTE_ACL); 1697 if (bytes != null) { 1698 if (cellFeaturesEnabled) { 1699 addCellPermissions(bytes, increment.getFamilyCellMap()); 1700 } else { 1701 throw new DoNotRetryIOException("Cell ACLs cannot be persisted"); 1702 } 1703 } 1704 1705 return null; 1706 } 1707 1708 @Override 1709 public List<Pair<Cell, Cell>> postIncrementBeforeWAL( 1710 ObserverContext<? extends RegionCoprocessorEnvironment> ctx, Mutation mutation, 1711 List<Pair<Cell, Cell>> cellPairs) throws IOException { 1712 // If the HFile version is insufficient to persist tags, we won't have any 1713 // work to do here 1714 if (!cellFeaturesEnabled || mutation.getACL() == null) { 1715 return cellPairs; 1716 } 1717 return cellPairs.stream() 1718 .map(pair -> new Pair<>(pair.getFirst(), 1719 createNewCellWithTags(mutation, pair.getFirst(), pair.getSecond()))) 1720 .collect(Collectors.toList()); 1721 } 1722 1723 @Override 1724 public List<Pair<Cell, Cell>> postAppendBeforeWAL( 1725 ObserverContext<? extends RegionCoprocessorEnvironment> ctx, Mutation mutation, 1726 List<Pair<Cell, Cell>> cellPairs) throws IOException { 1727 // If the HFile version is insufficient to persist tags, we won't have any 1728 // work to do here 1729 if (!cellFeaturesEnabled || mutation.getACL() == null) { 1730 return cellPairs; 1731 } 1732 return cellPairs.stream() 1733 .map(pair -> new Pair<>(pair.getFirst(), 1734 createNewCellWithTags(mutation, pair.getFirst(), pair.getSecond()))) 1735 .collect(Collectors.toList()); 1736 } 1737 1738 private Cell createNewCellWithTags(Mutation mutation, Cell oldCell, Cell newCell) { 1739 // As Increment and Append operations have already copied the tags of oldCell to the newCell, 1740 // there is no need to rewrite them again. Just extract non-acl tags of newCell if we need to 1741 // add a new acl tag for the cell. Actually, oldCell is useless here. 1742 List<Tag> tags = Lists.newArrayList(); 1743 ExtendedCell newExtendedCell = (ExtendedCell) newCell; 1744 if (newExtendedCell != null) { 1745 Iterator<Tag> tagIterator = PrivateCellUtil.tagsIterator(newExtendedCell); 1746 while (tagIterator.hasNext()) { 1747 Tag tag = tagIterator.next(); 1748 if (tag.getType() != PermissionStorage.ACL_TAG_TYPE) { 1749 // Not an ACL tag, just carry it through 1750 if (LOG.isTraceEnabled()) { 1751 LOG.trace("Carrying forward tag from " + newCell + ": type " + tag.getType() 1752 + " length " + tag.getValueLength()); 1753 } 1754 tags.add(tag); 1755 } 1756 } 1757 } 1758 1759 // We have checked the ACL tag of mutation is not null. 1760 // So that the tags could not be empty. 1761 tags.add(new ArrayBackedTag(PermissionStorage.ACL_TAG_TYPE, mutation.getACL())); 1762 return PrivateCellUtil.createCell(newExtendedCell, tags); 1763 } 1764 1765 @Override 1766 public void preScannerOpen(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1767 final Scan scan) throws IOException { 1768 internalPreRead(c, scan, OpType.SCAN); 1769 } 1770 1771 @Override 1772 public RegionScanner postScannerOpen( 1773 final ObserverContext<? extends RegionCoprocessorEnvironment> c, final Scan scan, 1774 final RegionScanner s) throws IOException { 1775 User user = getActiveUser(c); 1776 if (user != null && user.getShortName() != null) { 1777 // store reference to scanner owner for later checks 1778 scannerOwners.put(s, user.getShortName()); 1779 } 1780 return s; 1781 } 1782 1783 @Override 1784 public boolean preScannerNext(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1785 final InternalScanner s, final List<Result> result, final int limit, final boolean hasNext) 1786 throws IOException { 1787 requireScannerOwner(s); 1788 return hasNext; 1789 } 1790 1791 @Override 1792 public void preScannerClose(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1793 final InternalScanner s) throws IOException { 1794 requireScannerOwner(s); 1795 } 1796 1797 @Override 1798 public void postScannerClose(final ObserverContext<? extends RegionCoprocessorEnvironment> c, 1799 final InternalScanner s) throws IOException { 1800 // clean up any associated owner mapping 1801 scannerOwners.remove(s); 1802 } 1803 1804 /** 1805 * Verify, when servicing an RPC, that the caller is the scanner owner. If so, we assume that 1806 * access control is correctly enforced based on the checks performed in preScannerOpen() 1807 */ 1808 private void requireScannerOwner(InternalScanner s) throws AccessDeniedException { 1809 if (!RpcServer.isInRpcCallContext()) { 1810 return; 1811 } 1812 String requestUserName = RpcServer.getRequestUserName().orElse(null); 1813 String owner = scannerOwners.get(s); 1814 if (authorizationEnabled && owner != null && !owner.equals(requestUserName)) { 1815 throw new AccessDeniedException("User '" + requestUserName + "' is not the scanner owner!"); 1816 } 1817 } 1818 1819 /** 1820 * Verifies user has CREATE or ADMIN privileges on the Column Families involved in the 1821 * bulkLoadHFile request. Specific Column Write privileges are presently ignored. 1822 */ 1823 @Override 1824 public void preBulkLoadHFile(ObserverContext<? extends RegionCoprocessorEnvironment> ctx, 1825 List<Pair<byte[], String>> familyPaths) throws IOException { 1826 User user = getActiveUser(ctx); 1827 for (Pair<byte[], String> el : familyPaths) { 1828 accessChecker.requirePermission(user, "preBulkLoadHFile", 1829 ctx.getEnvironment().getRegion().getTableDescriptor().getTableName(), el.getFirst(), null, 1830 null, Action.ADMIN, Action.CREATE); 1831 } 1832 } 1833 1834 /** 1835 * Authorization check for SecureBulkLoadProtocol.prepareBulkLoad() 1836 * @param ctx the context 1837 */ 1838 @Override 1839 public void prePrepareBulkLoad(ObserverContext<RegionCoprocessorEnvironment> ctx) 1840 throws IOException { 1841 requireAccess(ctx, "prePrepareBulkLoad", 1842 ctx.getEnvironment().getRegion().getTableDescriptor().getTableName(), Action.ADMIN, 1843 Action.CREATE); 1844 } 1845 1846 /** 1847 * Authorization security check for SecureBulkLoadProtocol.cleanupBulkLoad() 1848 * @param ctx the context 1849 */ 1850 @Override 1851 public void preCleanupBulkLoad(ObserverContext<RegionCoprocessorEnvironment> ctx) 1852 throws IOException { 1853 requireAccess(ctx, "preCleanupBulkLoad", 1854 ctx.getEnvironment().getRegion().getTableDescriptor().getTableName(), Action.ADMIN, 1855 Action.CREATE); 1856 } 1857 1858 /* ---- EndpointObserver implementation ---- */ 1859 1860 @Override 1861 public Message preEndpointInvocation(ObserverContext<? extends RegionCoprocessorEnvironment> ctx, 1862 Service service, String methodName, Message request) throws IOException { 1863 // Don't intercept calls to our own AccessControlService, we check for 1864 // appropriate permissions in the service handlers 1865 if (shouldCheckExecPermission && !(service instanceof AccessControlService)) { 1866 requirePermission(ctx, 1867 "invoke(" + service.getDescriptorForType().getName() + "." + methodName + ")", 1868 getTableName(ctx.getEnvironment()), null, null, Action.EXEC); 1869 } 1870 return request; 1871 } 1872 1873 @Override 1874 public void postEndpointInvocation(ObserverContext<? extends RegionCoprocessorEnvironment> ctx, 1875 Service service, String methodName, Message request, Message.Builder responseBuilder) 1876 throws IOException { 1877 } 1878 1879 /* ---- Protobuf AccessControlService implementation ---- */ 1880 1881 /** 1882 * @deprecated since 2.2.0 and will be removed in 4.0.0. Use 1883 * {@link Admin#grant(UserPermission, boolean)} instead. 1884 * @see Admin#grant(UserPermission, boolean) 1885 * @see <a href="https://issues.apache.org/jira/browse/HBASE-21739">HBASE-21739</a> 1886 */ 1887 @Deprecated 1888 @Override 1889 public void grant(RpcController controller, AccessControlProtos.GrantRequest request, 1890 RpcCallback<AccessControlProtos.GrantResponse> done) { 1891 final UserPermission perm = AccessControlUtil.toUserPermission(request.getUserPermission()); 1892 AccessControlProtos.GrantResponse response = null; 1893 try { 1894 // verify it's only running at .acl. 1895 if (aclRegion) { 1896 if (!initialized) { 1897 throw new CoprocessorException("AccessController not yet initialized"); 1898 } 1899 User caller = RpcServer.getRequestUser().orElse(null); 1900 if (LOG.isDebugEnabled()) { 1901 LOG.debug("Received request from {} to grant access permission {}", caller.getName(), 1902 perm.toString()); 1903 } 1904 preGrantOrRevoke(caller, "grant", perm); 1905 1906 // regionEnv is set at #start. Hopefully not null at this point. 1907 regionEnv.getConnection().getAdmin().grant( 1908 new UserPermission(perm.getUser(), perm.getPermission()), 1909 request.getMergeExistingPermissions()); 1910 if (AUDITLOG.isTraceEnabled()) { 1911 // audit log should store permission changes in addition to auth results 1912 AUDITLOG.trace("Granted permission " + perm.toString()); 1913 } 1914 } else { 1915 throw new CoprocessorException(AccessController.class, 1916 "This method " + "can only execute at " + PermissionStorage.ACL_TABLE_NAME + " table."); 1917 } 1918 response = AccessControlProtos.GrantResponse.getDefaultInstance(); 1919 } catch (IOException ioe) { 1920 // pass exception back up 1921 CoprocessorRpcUtils.setControllerException(controller, ioe); 1922 } 1923 done.run(response); 1924 } 1925 1926 /** 1927 * @deprecated since 2.2.0 and will be removed in 4.0.0. Use {@link Admin#revoke(UserPermission)} 1928 * instead. 1929 * @see Admin#revoke(UserPermission) 1930 * @see <a href="https://issues.apache.org/jira/browse/HBASE-21739">HBASE-21739</a> 1931 */ 1932 @Deprecated 1933 @Override 1934 public void revoke(RpcController controller, AccessControlProtos.RevokeRequest request, 1935 RpcCallback<AccessControlProtos.RevokeResponse> done) { 1936 final UserPermission perm = AccessControlUtil.toUserPermission(request.getUserPermission()); 1937 AccessControlProtos.RevokeResponse response = null; 1938 try { 1939 // only allowed to be called on _acl_ region 1940 if (aclRegion) { 1941 if (!initialized) { 1942 throw new CoprocessorException("AccessController not yet initialized"); 1943 } 1944 User caller = RpcServer.getRequestUser().orElse(null); 1945 if (LOG.isDebugEnabled()) { 1946 LOG.debug("Received request from {} to revoke access permission {}", 1947 caller.getShortName(), perm.toString()); 1948 } 1949 preGrantOrRevoke(caller, "revoke", perm); 1950 // regionEnv is set at #start. Hopefully not null here. 1951 regionEnv.getConnection().getAdmin() 1952 .revoke(new UserPermission(perm.getUser(), perm.getPermission())); 1953 if (AUDITLOG.isTraceEnabled()) { 1954 // audit log should record all permission changes 1955 AUDITLOG.trace("Revoked permission " + perm.toString()); 1956 } 1957 } else { 1958 throw new CoprocessorException(AccessController.class, 1959 "This method " + "can only execute at " + PermissionStorage.ACL_TABLE_NAME + " table."); 1960 } 1961 response = AccessControlProtos.RevokeResponse.getDefaultInstance(); 1962 } catch (IOException ioe) { 1963 // pass exception back up 1964 CoprocessorRpcUtils.setControllerException(controller, ioe); 1965 } 1966 done.run(response); 1967 } 1968 1969 /** 1970 * @deprecated since 2.2.0 and will be removed in 4.0.0. Use 1971 * {@link Admin#getUserPermissions(GetUserPermissionsRequest)} instead. 1972 * @see Admin#getUserPermissions(GetUserPermissionsRequest) 1973 * @see <a href="https://issues.apache.org/jira/browse/HBASE-21911">HBASE-21911</a> 1974 */ 1975 @Deprecated 1976 @Override 1977 public void getUserPermissions(RpcController controller, 1978 AccessControlProtos.GetUserPermissionsRequest request, 1979 RpcCallback<AccessControlProtos.GetUserPermissionsResponse> done) { 1980 AccessControlProtos.GetUserPermissionsResponse response = null; 1981 try { 1982 // only allowed to be called on _acl_ region 1983 if (aclRegion) { 1984 if (!initialized) { 1985 throw new CoprocessorException("AccessController not yet initialized"); 1986 } 1987 User caller = RpcServer.getRequestUser().orElse(null); 1988 final String userName = request.hasUserName() ? request.getUserName().toStringUtf8() : null; 1989 final String namespace = 1990 request.hasNamespaceName() ? request.getNamespaceName().toStringUtf8() : null; 1991 final TableName table = 1992 request.hasTableName() ? ProtobufUtil.toTableName(request.getTableName()) : null; 1993 final byte[] cf = 1994 request.hasColumnFamily() ? request.getColumnFamily().toByteArray() : null; 1995 final byte[] cq = 1996 request.hasColumnQualifier() ? request.getColumnQualifier().toByteArray() : null; 1997 AccessControlProtos.Permission.Type protoType = 1998 request.hasType() ? request.getType() : null; 1999 Permission.Scope permissionScope = ShadedAccessControlUtil.toPermissionScope(protoType); 2000 preGetUserPermissions(caller, userName, namespace, table, cf, cq, permissionScope); 2001 GetUserPermissionsRequest getUserPermissionsRequest = null; 2002 if (request.getType() == AccessControlProtos.Permission.Type.Table) { 2003 getUserPermissionsRequest = GetUserPermissionsRequest.newBuilder(table).withFamily(cf) 2004 .withQualifier(cq).withUserName(userName).build(); 2005 } else if (request.getType() == AccessControlProtos.Permission.Type.Namespace) { 2006 getUserPermissionsRequest = 2007 GetUserPermissionsRequest.newBuilder(namespace).withUserName(userName).build(); 2008 } else { 2009 getUserPermissionsRequest = 2010 GetUserPermissionsRequest.newBuilder().withUserName(userName).build(); 2011 } 2012 List<UserPermission> perms = 2013 regionEnv.getConnection().getAdmin().getUserPermissions(getUserPermissionsRequest); 2014 response = AccessControlUtil.buildGetUserPermissionsResponse(perms); 2015 } else { 2016 throw new CoprocessorException(AccessController.class, 2017 "This method " + "can only execute at " + PermissionStorage.ACL_TABLE_NAME + " table."); 2018 } 2019 } catch (IOException ioe) { 2020 // pass exception back up 2021 CoprocessorRpcUtils.setControllerException(controller, ioe); 2022 } 2023 done.run(response); 2024 } 2025 2026 /** 2027 * @deprecated since 2.2.0 and will be removed 4.0.0. Use {@link Admin#hasUserPermissions(List)} 2028 * instead. 2029 * @see Admin#hasUserPermissions(List) 2030 * @see <a href="https://issues.apache.org/jira/browse/HBASE-22117">HBASE-22117</a> 2031 */ 2032 @Deprecated 2033 @Override 2034 public void checkPermissions(RpcController controller, 2035 AccessControlProtos.CheckPermissionsRequest request, 2036 RpcCallback<AccessControlProtos.CheckPermissionsResponse> done) { 2037 AccessControlProtos.CheckPermissionsResponse response = null; 2038 try { 2039 User user = RpcServer.getRequestUser().orElse(null); 2040 TableName tableName = regionEnv.getRegion().getTableDescriptor().getTableName(); 2041 List<Permission> permissions = new ArrayList<>(); 2042 for (int i = 0; i < request.getPermissionCount(); i++) { 2043 Permission permission = AccessControlUtil.toPermission(request.getPermission(i)); 2044 permissions.add(permission); 2045 if (permission instanceof TablePermission) { 2046 TablePermission tperm = (TablePermission) permission; 2047 if (!tperm.getTableName().equals(tableName)) { 2048 throw new CoprocessorException(AccessController.class, 2049 String.format( 2050 "This method can only execute at the table specified in " 2051 + "TablePermission. Table of the region:%s , requested table:%s", 2052 tableName, tperm.getTableName())); 2053 } 2054 } 2055 } 2056 for (Permission permission : permissions) { 2057 boolean hasPermission = 2058 accessChecker.hasUserPermission(user, "checkPermissions", permission); 2059 if (!hasPermission) { 2060 throw new AccessDeniedException("Insufficient permissions " + permission.toString()); 2061 } 2062 } 2063 response = AccessControlProtos.CheckPermissionsResponse.getDefaultInstance(); 2064 } catch (IOException ioe) { 2065 CoprocessorRpcUtils.setControllerException(controller, ioe); 2066 } 2067 done.run(response); 2068 } 2069 2070 private Region getRegion(RegionCoprocessorEnvironment e) { 2071 return e.getRegion(); 2072 } 2073 2074 private TableName getTableName(RegionCoprocessorEnvironment e) { 2075 Region region = e.getRegion(); 2076 if (region != null) { 2077 return getTableName(region); 2078 } 2079 return null; 2080 } 2081 2082 private TableName getTableName(Region region) { 2083 RegionInfo regionInfo = region.getRegionInfo(); 2084 if (regionInfo != null) { 2085 return regionInfo.getTable(); 2086 } 2087 return null; 2088 } 2089 2090 @Override 2091 public void preClose(ObserverContext<? extends RegionCoprocessorEnvironment> c, 2092 boolean abortRequested) throws IOException { 2093 requirePermission(c, "preClose", Action.ADMIN); 2094 } 2095 2096 private void checkSystemOrSuperUser(User activeUser) throws IOException { 2097 // No need to check if we're not going to throw 2098 if (!authorizationEnabled) { 2099 return; 2100 } 2101 if (!Superusers.isSuperUser(activeUser)) { 2102 throw new AccessDeniedException( 2103 "User '" + (activeUser != null ? activeUser.getShortName() : "null") 2104 + "' is not system or super user."); 2105 } 2106 } 2107 2108 @Override 2109 public void preStopRegionServer(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2110 throws IOException { 2111 requirePermission(ctx, "preStopRegionServer", Action.ADMIN); 2112 } 2113 2114 private Map<byte[], ? extends Collection<byte[]>> makeFamilyMap(byte[] family, byte[] qualifier) { 2115 if (family == null) { 2116 return null; 2117 } 2118 2119 Map<byte[], Collection<byte[]>> familyMap = new TreeMap<>(Bytes.BYTES_COMPARATOR); 2120 familyMap.put(family, qualifier != null ? ImmutableSet.of(qualifier) : null); 2121 return familyMap; 2122 } 2123 2124 @Override 2125 public void preGetTableDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx, 2126 List<TableName> tableNamesList, List<TableDescriptor> descriptors, String regex) 2127 throws IOException { 2128 // We are delegating the authorization check to postGetTableDescriptors as we don't have 2129 // any concrete set of table names when a regex is present or the full list is requested. 2130 if (regex == null && tableNamesList != null && !tableNamesList.isEmpty()) { 2131 // Otherwise, if the requestor has ADMIN or CREATE privs for all listed tables, the 2132 // request can be granted. 2133 try (Admin admin = ctx.getEnvironment().getConnection().getAdmin()) { 2134 for (TableName tableName : tableNamesList) { 2135 // Skip checks for a table that does not exist 2136 if (!admin.tableExists(tableName)) { 2137 continue; 2138 } 2139 requirePermission(ctx, "getTableDescriptors", tableName, null, null, Action.ADMIN, 2140 Action.CREATE); 2141 } 2142 } 2143 } 2144 } 2145 2146 @Override 2147 public void postGetTableDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx, 2148 List<TableName> tableNamesList, List<TableDescriptor> descriptors, String regex) 2149 throws IOException { 2150 // Skipping as checks in this case are already done by preGetTableDescriptors. 2151 if (regex == null && tableNamesList != null && !tableNamesList.isEmpty()) { 2152 return; 2153 } 2154 2155 // Retains only those which passes authorization checks, as the checks weren't done as part 2156 // of preGetTableDescriptors. 2157 Iterator<TableDescriptor> itr = descriptors.iterator(); 2158 while (itr.hasNext()) { 2159 TableDescriptor htd = itr.next(); 2160 try { 2161 requirePermission(ctx, "getTableDescriptors", htd.getTableName(), null, null, Action.ADMIN, 2162 Action.CREATE); 2163 } catch (AccessDeniedException e) { 2164 itr.remove(); 2165 } 2166 } 2167 } 2168 2169 @Override 2170 public void postGetTableNames(ObserverContext<MasterCoprocessorEnvironment> ctx, 2171 List<TableDescriptor> descriptors, String regex) throws IOException { 2172 // Retains only those which passes authorization checks. 2173 Iterator<TableDescriptor> itr = descriptors.iterator(); 2174 while (itr.hasNext()) { 2175 TableDescriptor htd = itr.next(); 2176 try { 2177 requireAccess(ctx, "getTableNames", htd.getTableName(), Action.values()); 2178 } catch (AccessDeniedException e) { 2179 itr.remove(); 2180 } 2181 } 2182 } 2183 2184 @Override 2185 public void preMergeRegions(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2186 final RegionInfo[] regionsToMerge) throws IOException { 2187 requirePermission(ctx, "mergeRegions", regionsToMerge[0].getTable(), null, null, Action.ADMIN); 2188 } 2189 2190 @Override 2191 public void preRollWALWriterRequest(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2192 throws IOException { 2193 requirePermission(ctx, "preRollLogWriterRequest", Permission.Action.ADMIN); 2194 } 2195 2196 @Override 2197 public void postRollWALWriterRequest(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2198 throws IOException { 2199 } 2200 2201 @Override 2202 public void preSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2203 final String userName, final GlobalQuotaSettings quotas) throws IOException { 2204 requirePermission(ctx, "setUserQuota", Action.ADMIN); 2205 } 2206 2207 @Override 2208 public void preSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2209 final String userName, final TableName tableName, final GlobalQuotaSettings quotas) 2210 throws IOException { 2211 requirePermission(ctx, "setUserTableQuota", tableName, null, null, Action.ADMIN); 2212 } 2213 2214 @Override 2215 public void preSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2216 final String userName, final String namespace, final GlobalQuotaSettings quotas) 2217 throws IOException { 2218 requirePermission(ctx, "setUserNamespaceQuota", Action.ADMIN); 2219 } 2220 2221 @Override 2222 public void preSetTableQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2223 final TableName tableName, final GlobalQuotaSettings quotas) throws IOException { 2224 requirePermission(ctx, "setTableQuota", tableName, null, null, Action.ADMIN); 2225 } 2226 2227 @Override 2228 public void preSetNamespaceQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2229 final String namespace, final GlobalQuotaSettings quotas) throws IOException { 2230 requirePermission(ctx, "setNamespaceQuota", Action.ADMIN); 2231 } 2232 2233 @Override 2234 public void preSetRegionServerQuota(ObserverContext<MasterCoprocessorEnvironment> ctx, 2235 final String regionServer, GlobalQuotaSettings quotas) throws IOException { 2236 requirePermission(ctx, "setRegionServerQuota", Action.ADMIN); 2237 } 2238 2239 @Override 2240 public ReplicationEndpoint postCreateReplicationEndPoint( 2241 ObserverContext<RegionServerCoprocessorEnvironment> ctx, ReplicationEndpoint endpoint) { 2242 return endpoint; 2243 } 2244 2245 @Override 2246 public void preReplicateLogEntries(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2247 throws IOException { 2248 requirePermission(ctx, "replicateLogEntries", Action.WRITE); 2249 } 2250 2251 @Override 2252 public void preClearCompactionQueues(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2253 throws IOException { 2254 requirePermission(ctx, "preClearCompactionQueues", Permission.Action.ADMIN); 2255 } 2256 2257 @Override 2258 public void preAddReplicationPeer(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2259 String peerId, ReplicationPeerConfig peerConfig) throws IOException { 2260 requirePermission(ctx, "addReplicationPeer", Action.ADMIN); 2261 } 2262 2263 @Override 2264 public void preRemoveReplicationPeer(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2265 String peerId) throws IOException { 2266 requirePermission(ctx, "removeReplicationPeer", Action.ADMIN); 2267 } 2268 2269 @Override 2270 public void preEnableReplicationPeer(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2271 String peerId) throws IOException { 2272 requirePermission(ctx, "enableReplicationPeer", Action.ADMIN); 2273 } 2274 2275 @Override 2276 public void preDisableReplicationPeer(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2277 String peerId) throws IOException { 2278 requirePermission(ctx, "disableReplicationPeer", Action.ADMIN); 2279 } 2280 2281 @Override 2282 public void preGetReplicationPeerConfig(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2283 String peerId) throws IOException { 2284 requirePermission(ctx, "getReplicationPeerConfig", Action.ADMIN); 2285 } 2286 2287 @Override 2288 public void preUpdateReplicationPeerConfig( 2289 final ObserverContext<MasterCoprocessorEnvironment> ctx, String peerId, 2290 ReplicationPeerConfig peerConfig) throws IOException { 2291 requirePermission(ctx, "updateReplicationPeerConfig", Action.ADMIN); 2292 } 2293 2294 @Override 2295 public void preTransitReplicationPeerSyncReplicationState( 2296 final ObserverContext<MasterCoprocessorEnvironment> ctx, String peerId, 2297 SyncReplicationState clusterState) throws IOException { 2298 requirePermission(ctx, "transitSyncReplicationPeerState", Action.ADMIN); 2299 } 2300 2301 @Override 2302 public void preListReplicationPeers(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2303 String regex) throws IOException { 2304 requirePermission(ctx, "listReplicationPeers", Action.ADMIN); 2305 } 2306 2307 @Override 2308 public void preRequestLock(ObserverContext<MasterCoprocessorEnvironment> ctx, String namespace, 2309 TableName tableName, RegionInfo[] regionInfos, String description) throws IOException { 2310 // There are operations in the CREATE and ADMIN domain which may require lock, READ 2311 // or WRITE. So for any lock request, we check for these two perms irrespective of lock type. 2312 String reason = String.format("Description=%s", description); 2313 checkLockPermissions(ctx, namespace, tableName, regionInfos, reason); 2314 } 2315 2316 @Override 2317 public void preLockHeartbeat(ObserverContext<MasterCoprocessorEnvironment> ctx, 2318 TableName tableName, String description) throws IOException { 2319 checkLockPermissions(ctx, null, tableName, null, description); 2320 } 2321 2322 @Override 2323 public void preExecuteProcedures(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2324 throws IOException { 2325 checkSystemOrSuperUser(getActiveUser(ctx)); 2326 } 2327 2328 @Override 2329 public void preSwitchRpcThrottle(ObserverContext<MasterCoprocessorEnvironment> ctx, 2330 boolean enable) throws IOException { 2331 requirePermission(ctx, "switchRpcThrottle", Action.ADMIN); 2332 } 2333 2334 @Override 2335 public void preIsRpcThrottleEnabled(ObserverContext<MasterCoprocessorEnvironment> ctx) 2336 throws IOException { 2337 requirePermission(ctx, "isRpcThrottleEnabled", Action.ADMIN); 2338 } 2339 2340 @Override 2341 public void preSwitchExceedThrottleQuota(ObserverContext<MasterCoprocessorEnvironment> ctx, 2342 boolean enable) throws IOException { 2343 requirePermission(ctx, "switchExceedThrottleQuota", Action.ADMIN); 2344 } 2345 2346 /** 2347 * Returns the active user to which authorization checks should be applied. If we are in the 2348 * context of an RPC call, the remote user is used, otherwise the currently logged in user is 2349 * used. 2350 */ 2351 private User getActiveUser(ObserverContext<?> ctx) throws IOException { 2352 // for non-rpc handling, fallback to system user 2353 Optional<User> optionalUser = ctx.getRpcCallContext().map(ObserverRpcCallContext::getUser); 2354 if (optionalUser.isPresent()) { 2355 return optionalUser.get(); 2356 } 2357 return userProvider.getCurrent(); 2358 } 2359 2360 /** 2361 * @deprecated since 2.2.0 and will be removed in 4.0.0. Use 2362 * {@link Admin#hasUserPermissions(String, List)} instead. 2363 * @see Admin#hasUserPermissions(String, List) 2364 * @see <a href="https://issues.apache.org/jira/browse/HBASE-22117">HBASE-22117</a> 2365 */ 2366 @Deprecated 2367 @Override 2368 public void hasPermission(RpcController controller, HasPermissionRequest request, 2369 RpcCallback<HasPermissionResponse> done) { 2370 // Converts proto to a TablePermission object. 2371 TablePermission tPerm = AccessControlUtil.toTablePermission(request.getTablePermission()); 2372 // Check input user name 2373 if (!request.hasUserName()) { 2374 throw new IllegalStateException("Input username cannot be empty"); 2375 } 2376 final String inputUserName = request.getUserName().toStringUtf8(); 2377 AccessControlProtos.HasPermissionResponse response = null; 2378 try { 2379 User caller = RpcServer.getRequestUser().orElse(null); 2380 List<Permission> permissions = Lists.newArrayList(tPerm); 2381 preHasUserPermissions(caller, inputUserName, permissions); 2382 boolean hasPermission = 2383 regionEnv.getConnection().getAdmin().hasUserPermissions(inputUserName, permissions).get(0); 2384 response = ResponseConverter.buildHasPermissionResponse(hasPermission); 2385 } catch (IOException ioe) { 2386 ResponseConverter.setControllerException(controller, ioe); 2387 } 2388 done.run(response); 2389 } 2390 2391 @Override 2392 public void preGrant(ObserverContext<MasterCoprocessorEnvironment> ctx, 2393 UserPermission userPermission, boolean mergeExistingPermissions) throws IOException { 2394 preGrantOrRevoke(getActiveUser(ctx), "grant", userPermission); 2395 } 2396 2397 @Override 2398 public void preRevoke(ObserverContext<MasterCoprocessorEnvironment> ctx, 2399 UserPermission userPermission) throws IOException { 2400 preGrantOrRevoke(getActiveUser(ctx), "revoke", userPermission); 2401 } 2402 2403 private void preGrantOrRevoke(User caller, String request, UserPermission userPermission) 2404 throws IOException { 2405 switch (userPermission.getPermission().scope) { 2406 case GLOBAL: 2407 accessChecker.requireGlobalPermission(caller, request, Action.ADMIN, ""); 2408 break; 2409 case NAMESPACE: 2410 NamespacePermission namespacePerm = (NamespacePermission) userPermission.getPermission(); 2411 accessChecker.requireNamespacePermission(caller, request, namespacePerm.getNamespace(), 2412 null, Action.ADMIN); 2413 break; 2414 case TABLE: 2415 TablePermission tablePerm = (TablePermission) userPermission.getPermission(); 2416 accessChecker.requirePermission(caller, request, tablePerm.getTableName(), 2417 tablePerm.getFamily(), tablePerm.getQualifier(), null, Action.ADMIN); 2418 break; 2419 default: 2420 } 2421 if (!Superusers.isSuperUser(caller)) { 2422 accessChecker.performOnSuperuser(request, caller, userPermission.getUser()); 2423 } 2424 } 2425 2426 @Override 2427 public void preGetUserPermissions(ObserverContext<MasterCoprocessorEnvironment> ctx, 2428 String userName, String namespace, TableName tableName, byte[] family, byte[] qualifier, 2429 Permission.Scope permissionScope) throws IOException { 2430 preGetUserPermissions(getActiveUser(ctx), userName, namespace, tableName, family, qualifier, 2431 permissionScope); 2432 } 2433 2434 private void preGetUserPermissions(User caller, String userName, String namespace, 2435 TableName tableName, byte[] family, byte[] qualifier, Permission.Scope permissionScope) 2436 throws IOException { 2437 if (permissionScope == Permission.Scope.TABLE) { 2438 accessChecker.requirePermission(caller, "getUserPermissions", tableName, family, qualifier, 2439 userName, Action.ADMIN); 2440 } else if (permissionScope == Permission.Scope.NAMESPACE) { 2441 accessChecker.requireNamespacePermission(caller, "getUserPermissions", namespace, userName, 2442 Action.ADMIN); 2443 } else if (permissionScope == Permission.Scope.GLOBAL) { 2444 accessChecker.requirePermission(caller, "getUserPermissions", userName, Action.ADMIN); 2445 } else { 2446 preGetUserPermissions(caller, userName, namespace, tableName, family, qualifier); 2447 } 2448 } 2449 2450 private void preGetUserPermissions(User caller, String userName, String namespace, 2451 TableName tableName, byte[] family, byte[] qualifier) throws IOException { 2452 if (tableName != null) { 2453 accessChecker.requirePermission(caller, "getUserPermissions", tableName, family, qualifier, 2454 userName, Action.ADMIN); 2455 } else if (namespace != null) { 2456 accessChecker.requireNamespacePermission(caller, "getUserPermissions", namespace, userName, 2457 Action.ADMIN); 2458 } else { 2459 accessChecker.requirePermission(caller, "getUserPermissions", userName, Action.ADMIN); 2460 } 2461 } 2462 2463 @Override 2464 public void preHasUserPermissions(ObserverContext<MasterCoprocessorEnvironment> ctx, 2465 String userName, List<Permission> permissions) throws IOException { 2466 preHasUserPermissions(getActiveUser(ctx), userName, permissions); 2467 } 2468 2469 private void preHasUserPermissions(User caller, String userName, List<Permission> permissions) 2470 throws IOException { 2471 String request = "hasUserPermissions"; 2472 for (Permission permission : permissions) { 2473 if (!caller.getShortName().equals(userName)) { 2474 // User should have admin privilege if checking permission for other users 2475 if (permission instanceof TablePermission) { 2476 TablePermission tPerm = (TablePermission) permission; 2477 accessChecker.requirePermission(caller, request, tPerm.getTableName(), tPerm.getFamily(), 2478 tPerm.getQualifier(), userName, Action.ADMIN); 2479 } else if (permission instanceof NamespacePermission) { 2480 NamespacePermission nsPerm = (NamespacePermission) permission; 2481 accessChecker.requireNamespacePermission(caller, request, nsPerm.getNamespace(), userName, 2482 Action.ADMIN); 2483 } else { 2484 accessChecker.requirePermission(caller, request, userName, Action.ADMIN); 2485 } 2486 } else { 2487 // User don't need ADMIN privilege for self check. 2488 // Setting action as null in AuthResult to display empty action in audit log 2489 AuthResult result; 2490 if (permission instanceof TablePermission) { 2491 TablePermission tPerm = (TablePermission) permission; 2492 result = AuthResult.allow(request, "Self user validation allowed", caller, null, 2493 tPerm.getTableName(), tPerm.getFamily(), tPerm.getQualifier()); 2494 } else if (permission instanceof NamespacePermission) { 2495 NamespacePermission nsPerm = (NamespacePermission) permission; 2496 result = AuthResult.allow(request, "Self user validation allowed", caller, null, 2497 nsPerm.getNamespace()); 2498 } else { 2499 result = AuthResult.allow(request, "Self user validation allowed", caller, null, null, 2500 null, null); 2501 } 2502 AccessChecker.logResult(result); 2503 } 2504 } 2505 } 2506 2507 @Override 2508 public void preMoveServersAndTables(ObserverContext<MasterCoprocessorEnvironment> ctx, 2509 Set<Address> servers, Set<TableName> tables, String targetGroup) throws IOException { 2510 accessChecker.requirePermission(getActiveUser(ctx), "moveServersAndTables", null, 2511 Permission.Action.ADMIN); 2512 } 2513 2514 @Override 2515 public void preMoveServers(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2516 Set<Address> servers, String targetGroup) throws IOException { 2517 accessChecker.requirePermission(getActiveUser(ctx), "moveServers", null, 2518 Permission.Action.ADMIN); 2519 } 2520 2521 @Override 2522 public void preMoveTables(ObserverContext<MasterCoprocessorEnvironment> ctx, 2523 Set<TableName> tables, String targetGroup) throws IOException { 2524 accessChecker.requirePermission(getActiveUser(ctx), "moveTables", null, 2525 Permission.Action.ADMIN); 2526 } 2527 2528 @Override 2529 public void preAddRSGroup(ObserverContext<MasterCoprocessorEnvironment> ctx, String name) 2530 throws IOException { 2531 accessChecker.requirePermission(getActiveUser(ctx), "addRSGroup", null, 2532 Permission.Action.ADMIN); 2533 } 2534 2535 @Override 2536 public void preRemoveRSGroup(ObserverContext<MasterCoprocessorEnvironment> ctx, String name) 2537 throws IOException { 2538 accessChecker.requirePermission(getActiveUser(ctx), "removeRSGroup", null, 2539 Permission.Action.ADMIN); 2540 } 2541 2542 @Override 2543 public void preBalanceRSGroup(ObserverContext<MasterCoprocessorEnvironment> ctx, String groupName, 2544 BalanceRequest request) throws IOException { 2545 accessChecker.requirePermission(getActiveUser(ctx), "balanceRSGroup", null, 2546 Permission.Action.ADMIN); 2547 } 2548 2549 @Override 2550 public void preRemoveServers(ObserverContext<MasterCoprocessorEnvironment> ctx, 2551 Set<Address> servers) throws IOException { 2552 accessChecker.requirePermission(getActiveUser(ctx), "removeServers", null, 2553 Permission.Action.ADMIN); 2554 } 2555 2556 @Override 2557 public void preGetRSGroupInfo(ObserverContext<MasterCoprocessorEnvironment> ctx, String groupName) 2558 throws IOException { 2559 accessChecker.requirePermission(getActiveUser(ctx), "getRSGroupInfo", null, 2560 Permission.Action.ADMIN); 2561 } 2562 2563 @Override 2564 public void preGetRSGroupInfoOfTable(ObserverContext<MasterCoprocessorEnvironment> ctx, 2565 TableName tableName) throws IOException { 2566 accessChecker.requirePermission(getActiveUser(ctx), "getRSGroupInfoOfTable", null, 2567 Permission.Action.ADMIN); 2568 // todo: should add check for table existence 2569 } 2570 2571 @Override 2572 public void preListRSGroups(ObserverContext<MasterCoprocessorEnvironment> ctx) 2573 throws IOException { 2574 accessChecker.requirePermission(getActiveUser(ctx), "listRSGroups", null, 2575 Permission.Action.ADMIN); 2576 } 2577 2578 @Override 2579 public void preListTablesInRSGroup(ObserverContext<MasterCoprocessorEnvironment> ctx, 2580 String groupName) throws IOException { 2581 accessChecker.requirePermission(getActiveUser(ctx), "listTablesInRSGroup", null, 2582 Permission.Action.ADMIN); 2583 } 2584 2585 @Override 2586 public void preGetConfiguredNamespacesAndTablesInRSGroup( 2587 ObserverContext<MasterCoprocessorEnvironment> ctx, String groupName) throws IOException { 2588 accessChecker.requirePermission(getActiveUser(ctx), "getConfiguredNamespacesAndTablesInRSGroup", 2589 null, Permission.Action.ADMIN); 2590 } 2591 2592 @Override 2593 public void preGetRSGroupInfoOfServer(ObserverContext<MasterCoprocessorEnvironment> ctx, 2594 Address server) throws IOException { 2595 accessChecker.requirePermission(getActiveUser(ctx), "getRSGroupInfoOfServer", null, 2596 Permission.Action.ADMIN); 2597 } 2598 2599 @Override 2600 public void preRenameRSGroup(ObserverContext<MasterCoprocessorEnvironment> ctx, String oldName, 2601 String newName) throws IOException { 2602 accessChecker.requirePermission(getActiveUser(ctx), "renameRSGroup", null, 2603 Permission.Action.ADMIN); 2604 } 2605 2606 @Override 2607 public void preUpdateRSGroupConfig(final ObserverContext<MasterCoprocessorEnvironment> ctx, 2608 final String groupName, final Map<String, String> configuration) throws IOException { 2609 accessChecker.requirePermission(getActiveUser(ctx), "updateRSGroupConfig", null, 2610 Permission.Action.ADMIN); 2611 } 2612 2613 @Override 2614 public void preClearRegionBlockCache(ObserverContext<RegionServerCoprocessorEnvironment> ctx) 2615 throws IOException { 2616 accessChecker.requirePermission(getActiveUser(ctx), "clearRegionBlockCache", null, 2617 Permission.Action.ADMIN); 2618 } 2619 2620 @Override 2621 public void preUpdateRegionServerConfiguration( 2622 ObserverContext<RegionServerCoprocessorEnvironment> ctx, Configuration preReloadConf) 2623 throws IOException { 2624 accessChecker.requirePermission(getActiveUser(ctx), "updateConfiguration", null, 2625 Permission.Action.ADMIN); 2626 } 2627 2628 @Override 2629 public void preUpdateMasterConfiguration(ObserverContext<MasterCoprocessorEnvironment> ctx, 2630 Configuration preReloadConf) throws IOException { 2631 accessChecker.requirePermission(getActiveUser(ctx), "updateConfiguration", null, 2632 Permission.Action.ADMIN); 2633 } 2634 2635}