View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.security.access;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.concurrent.ConcurrentSkipListMap;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.classification.InterfaceAudience;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Cell;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.exceptions.DeserializationException;
34  import org.apache.hadoop.hbase.security.User;
35  import org.apache.hadoop.hbase.security.UserProvider;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
38  import org.apache.zookeeper.KeeperException;
39  
40  import com.google.common.collect.ArrayListMultimap;
41  import com.google.common.collect.ListMultimap;
42  import com.google.common.collect.Lists;
43  
44  /**
45   * Performs authorization checks for a given user's assigned permissions
46   */
47  @InterfaceAudience.Private
48  public class TableAuthManager {
49    private static class PermissionCache<T extends Permission> {
50      /** Cache of user permissions */
51      private ListMultimap<String,T> userCache = ArrayListMultimap.create();
52      /** Cache of group permissions */
53      private ListMultimap<String,T> groupCache = ArrayListMultimap.create();
54  
55      public List<T> getUser(String user) {
56        return userCache.get(user);
57      }
58  
59      public void putUser(String user, T perm) {
60        userCache.put(user, perm);
61      }
62  
63      public List<T> replaceUser(String user, Iterable<? extends T> perms) {
64        return userCache.replaceValues(user, perms);
65      }
66  
67      public List<T> getGroup(String group) {
68        return groupCache.get(group);
69      }
70  
71      public void putGroup(String group, T perm) {
72        groupCache.put(group, perm);
73      }
74  
75      public List<T> replaceGroup(String group, Iterable<? extends T> perms) {
76        return groupCache.replaceValues(group, perms);
77      }
78  
79      /**
80       * Returns a combined map of user and group permissions, with group names prefixed by
81       * {@link AccessControlLists#GROUP_PREFIX}.
82       */
83      public ListMultimap<String,T> getAllPermissions() {
84        ListMultimap<String,T> tmp = ArrayListMultimap.create();
85        tmp.putAll(userCache);
86        for (String group : groupCache.keySet()) {
87          tmp.putAll(AccessControlLists.GROUP_PREFIX + group, groupCache.get(group));
88        }
89        return tmp;
90      }
91    }
92  
93    private static Log LOG = LogFactory.getLog(TableAuthManager.class);
94  
95    private static TableAuthManager instance;
96  
97    /** Cache of global permissions */
98    private volatile PermissionCache<Permission> globalCache;
99  
100   private ConcurrentSkipListMap<TableName, PermissionCache<TablePermission>> tableCache =
101       new ConcurrentSkipListMap<TableName, PermissionCache<TablePermission>>();
102 
103   private ConcurrentSkipListMap<String, PermissionCache<TablePermission>> nsCache =
104     new ConcurrentSkipListMap<String, PermissionCache<TablePermission>>();
105 
106   private Configuration conf;
107   private ZKPermissionWatcher zkperms;
108   private volatile long mtime;
109 
110   private TableAuthManager(ZooKeeperWatcher watcher, Configuration conf)
111       throws IOException {
112     this.conf = conf;
113 
114     // initialize global permissions based on configuration
115     globalCache = initGlobal(conf);
116 
117     this.zkperms = new ZKPermissionWatcher(watcher, this, conf);
118     try {
119       this.zkperms.start();
120     } catch (KeeperException ke) {
121       LOG.error("ZooKeeper initialization failed", ke);
122     }
123   }
124 
125   /**
126    * Returns a new {@code PermissionCache} initialized with permission assignments
127    * from the {@code hbase.superuser} configuration key.
128    */
129   private PermissionCache<Permission> initGlobal(Configuration conf) throws IOException {
130     UserProvider userProvider = UserProvider.instantiate(conf);
131     User user = userProvider.getCurrent();
132     if (user == null) {
133       throw new IOException("Unable to obtain the current user, " +
134           "authorization checks for internal operations will not work correctly!");
135     }
136     PermissionCache<Permission> newCache = new PermissionCache<Permission>();
137     String currentUser = user.getShortName();
138 
139     // the system user is always included
140     List<String> superusers = Lists.asList(currentUser, conf.getStrings(
141         AccessControlLists.SUPERUSER_CONF_KEY, new String[0]));
142     if (superusers != null) {
143       for (String name : superusers) {
144         if (AccessControlLists.isGroupPrincipal(name)) {
145           newCache.putGroup(AccessControlLists.getGroupName(name),
146               new Permission(Permission.Action.values()));
147         } else {
148           newCache.putUser(name, new Permission(Permission.Action.values()));
149         }
150       }
151     }
152     return newCache;
153   }
154 
155   public ZKPermissionWatcher getZKPermissionWatcher() {
156     return this.zkperms;
157   }
158 
159   public void refreshTableCacheFromWritable(TableName table,
160                                        byte[] data) throws IOException {
161     if (data != null && data.length > 0) {
162       ListMultimap<String,TablePermission> perms;
163       try {
164         perms = AccessControlLists.readPermissions(data, conf);
165       } catch (DeserializationException e) {
166         throw new IOException(e);
167       }
168 
169       if (perms != null) {
170         if (Bytes.equals(table.getName(), AccessControlLists.ACL_GLOBAL_NAME)) {
171           updateGlobalCache(perms);
172         } else {
173           updateTableCache(table, perms);
174         }
175       }
176     } else {
177       LOG.debug("Skipping permission cache refresh because writable data is empty");
178     }
179   }
180 
181   public void refreshNamespaceCacheFromWritable(String namespace, byte[] data) throws IOException {
182     if (data != null && data.length > 0) {
183       ListMultimap<String,TablePermission> perms;
184       try {
185         perms = AccessControlLists.readPermissions(data, conf);
186       } catch (DeserializationException e) {
187         throw new IOException(e);
188       }
189       if (perms != null) {
190         updateNsCache(namespace, perms);
191       }
192     } else {
193       LOG.debug("Skipping permission cache refresh because writable data is empty");
194     }
195   }
196 
197   /**
198    * Updates the internal global permissions cache
199    *
200    * @param userPerms
201    */
202   private void updateGlobalCache(ListMultimap<String,TablePermission> userPerms) {
203     PermissionCache<Permission> newCache = null;
204     try {
205       newCache = initGlobal(conf);
206       for (Map.Entry<String,TablePermission> entry : userPerms.entries()) {
207         if (AccessControlLists.isGroupPrincipal(entry.getKey())) {
208           newCache.putGroup(AccessControlLists.getGroupName(entry.getKey()),
209               new Permission(entry.getValue().getActions()));
210         } else {
211           newCache.putUser(entry.getKey(), new Permission(entry.getValue().getActions()));
212         }
213       }
214       globalCache = newCache;
215       mtime++;
216     } catch (IOException e) {
217       // Never happens
218       LOG.error("Error occured while updating the global cache", e);
219     }
220   }
221 
222   /**
223    * Updates the internal permissions cache for a single table, splitting
224    * the permissions listed into separate caches for users and groups to optimize
225    * group lookups.
226    * 
227    * @param table
228    * @param tablePerms
229    */
230   private void updateTableCache(TableName table,
231                                 ListMultimap<String,TablePermission> tablePerms) {
232     PermissionCache<TablePermission> newTablePerms = new PermissionCache<TablePermission>();
233 
234     for (Map.Entry<String,TablePermission> entry : tablePerms.entries()) {
235       if (AccessControlLists.isGroupPrincipal(entry.getKey())) {
236         newTablePerms.putGroup(AccessControlLists.getGroupName(entry.getKey()), entry.getValue());
237       } else {
238         newTablePerms.putUser(entry.getKey(), entry.getValue());
239       }
240     }
241 
242     tableCache.put(table, newTablePerms);
243     mtime++;
244   }
245 
246   /**
247    * Updates the internal permissions cache for a single table, splitting
248    * the permissions listed into separate caches for users and groups to optimize
249    * group lookups.
250    *
251    * @param namespace
252    * @param tablePerms
253    */
254   private void updateNsCache(String namespace,
255                              ListMultimap<String, TablePermission> tablePerms) {
256     PermissionCache<TablePermission> newTablePerms = new PermissionCache<TablePermission>();
257 
258     for (Map.Entry<String, TablePermission> entry : tablePerms.entries()) {
259       if (AccessControlLists.isGroupPrincipal(entry.getKey())) {
260         newTablePerms.putGroup(AccessControlLists.getGroupName(entry.getKey()), entry.getValue());
261       } else {
262         newTablePerms.putUser(entry.getKey(), entry.getValue());
263       }
264     }
265 
266     nsCache.put(namespace, newTablePerms);
267     mtime++;
268   }
269 
270   private PermissionCache<TablePermission> getTablePermissions(TableName table) {
271     if (!tableCache.containsKey(table)) {
272       tableCache.putIfAbsent(table, new PermissionCache<TablePermission>());
273     }
274     return tableCache.get(table);
275   }
276 
277   private PermissionCache<TablePermission> getNamespacePermissions(String namespace) {
278     if (!nsCache.containsKey(namespace)) {
279       nsCache.putIfAbsent(namespace, new PermissionCache<TablePermission>());
280     }
281     return nsCache.get(namespace);
282   }
283 
284   /**
285    * Authorizes a global permission
286    * @param perms
287    * @param action
288    * @return true if authorized, false otherwise
289    */
290   private boolean authorize(List<Permission> perms, Permission.Action action) {
291     if (perms != null) {
292       for (Permission p : perms) {
293         if (p.implies(action)) {
294           return true;
295         }
296       }
297     } else if (LOG.isDebugEnabled()) {
298       LOG.debug("No permissions found");
299     }
300 
301     return false;
302   }
303 
304   /**
305    * Authorize a global permission based on ACLs for the given user and the
306    * user's groups.
307    * @param user
308    * @param action
309    * @return true if known and authorized, false otherwise
310    */
311   public boolean authorize(User user, Permission.Action action) {
312     if (user == null) {
313       return false;
314     }
315 
316     if (authorize(globalCache.getUser(user.getShortName()), action)) {
317       return true;
318     }
319 
320     String[] groups = user.getGroupNames();
321     if (groups != null) {
322       for (String group : groups) {
323         if (authorize(globalCache.getGroup(group), action)) {
324           return true;
325         }
326       }
327     }
328     return false;
329   }
330 
331   private boolean authorize(List<TablePermission> perms,
332                             TableName table, byte[] family,
333                             Permission.Action action) {
334     return authorize(perms, table, family, null, action);
335   }
336 
337   private boolean authorize(List<TablePermission> perms,
338                             TableName table, byte[] family,
339                             byte[] qualifier, Permission.Action action) {
340     if (perms != null) {
341       for (TablePermission p : perms) {
342         if (p.implies(table, family, qualifier, action)) {
343           return true;
344         }
345       }
346     } else if (LOG.isDebugEnabled()) {
347       LOG.debug("No permissions found for table="+table);
348     }
349     return false;
350   }
351 
352   /**
353    * Authorize a user for a given KV. This is called from AccessControlFilter.
354    */
355   public boolean authorize(User user, TableName table, Cell cell, Permission.Action action) {
356     try {
357       List<Permission> perms = AccessControlLists.getCellPermissionsForUser(user, cell);
358       if (LOG.isTraceEnabled()) {
359         LOG.trace("Perms for user " + user.getShortName() + " in cell " + cell + ": " + perms);
360       }
361       for (Permission p: perms) {
362         if (p.implies(action)) {
363           return true;
364         }
365       }
366     } catch (IOException e) {
367       // We failed to parse the KV tag
368       LOG.error("Failed parse of ACL tag in cell " + cell);
369       // Fall through to check with the table and CF perms we were able
370       // to collect regardless
371     }
372     return false;
373   }
374 
375   public boolean authorize(User user, String namespace, Permission.Action action) {
376     // Global authorizations supercede namespace level
377     if (authorizeUser(user, action)) {
378       return true;
379     }
380     // Check namespace permissions
381     PermissionCache<TablePermission> tablePerms = nsCache.get(namespace);
382     if (tablePerms != null) {
383       List<TablePermission> userPerms = tablePerms.getUser(user.getShortName());
384       if (authorize(userPerms, namespace, action)) {
385         return true;
386       }
387       String[] groupNames = user.getGroupNames();
388       if (groupNames != null) {
389         for (String group : groupNames) {
390           List<TablePermission> groupPerms = tablePerms.getGroup(group);
391           if (authorize(groupPerms, namespace, action)) {
392             return true;
393           }
394         }
395       }
396     }
397     return false;
398   }
399 
400   private boolean authorize(List<TablePermission> perms, String namespace,
401                             Permission.Action action) {
402     if (perms != null) {
403       for (TablePermission p : perms) {
404         if (p.implies(namespace, action)) {
405           return true;
406         }
407       }
408     } else if (LOG.isDebugEnabled()) {
409       LOG.debug("No permissions for authorize() check, table=" + namespace);
410     }
411 
412     return false;
413   }
414 
415   /**
416    * Checks global authorization for a specific action for a user, based on the
417    * stored user permissions.
418    */
419   public boolean authorizeUser(User user, Permission.Action action) {
420     return authorize(globalCache.getUser(user.getShortName()), action);
421   }
422 
423   /**
424    * Checks authorization to a given table and column family for a user, based on the
425    * stored user permissions.
426    *
427    * @param user
428    * @param table
429    * @param family
430    * @param action
431    * @return true if known and authorized, false otherwise
432    */
433   public boolean authorizeUser(User user, TableName table, byte[] family,
434       Permission.Action action) {
435     return authorizeUser(user, table, family, null, action);
436   }
437 
438   public boolean authorizeUser(User user, TableName table, byte[] family,
439       byte[] qualifier, Permission.Action action) {
440     if (table == null) table = AccessControlLists.ACL_TABLE_NAME;
441     // Global and namespace authorizations supercede table level
442     if (authorize(user, table.getNamespaceAsString(), action)) {    
443       return true;
444     }
445     // Check table permissions
446     return authorize(getTablePermissions(table).getUser(user.getShortName()), table, family,
447         qualifier, action);
448   }
449 
450   /**
451    * Checks global authorization for a given action for a group, based on the stored
452    * permissions.
453    */
454   public boolean authorizeGroup(String groupName, Permission.Action action) {
455     return authorize(globalCache.getGroup(groupName), action);
456   }
457 
458   /**
459    * Checks authorization to a given table and column family for a group, based
460    * on the stored permissions. 
461    * @param groupName
462    * @param table
463    * @param family
464    * @param action
465    * @return true if known and authorized, false otherwise
466    */
467   public boolean authorizeGroup(String groupName, TableName table, byte[] family,
468       Permission.Action action) {
469     // Global authorization supercedes table level
470     if (authorizeGroup(groupName, action)) {
471       return true;
472     }
473     if (table == null) table = AccessControlLists.ACL_TABLE_NAME;
474     // Namespace authorization supercedes table level
475     if (authorize(getNamespacePermissions(table.getNamespaceAsString()).getGroup(groupName),
476         table, family, action)) {
477       return true;
478     }
479     // Check table level
480     return authorize(getTablePermissions(table).getGroup(groupName), table, family, action);
481   }
482 
483   public boolean authorize(User user, TableName table, byte[] family,
484       byte[] qualifier, Permission.Action action) {
485     if (authorizeUser(user, table, family, qualifier, action)) {
486       return true;
487     }
488 
489     String[] groups = user.getGroupNames();
490     if (groups != null) {
491       for (String group : groups) {
492         if (authorizeGroup(group, table, family, action)) {
493           return true;
494         }
495       }
496     }
497     return false;
498   }
499 
500   public boolean authorize(User user, TableName table, byte[] family,
501       Permission.Action action) {
502     return authorize(user, table, family, null, action);
503   }
504 
505   /**
506    * Returns true if the given user has a {@link TablePermission} matching up
507    * to the column family portion of a permission.  Note that this permission
508    * may be scoped to a given column qualifier and does not guarantee that
509    * authorize() on the same column family would return true.
510    */
511   public boolean matchPermission(User user,
512       TableName table, byte[] family, Permission.Action action) {
513     PermissionCache<TablePermission> tablePerms = tableCache.get(table);
514     if (tablePerms != null) {
515       List<TablePermission> userPerms = tablePerms.getUser(user.getShortName());
516       if (userPerms != null) {
517         for (TablePermission p : userPerms) {
518           if (p.matchesFamily(table, family, action)) {
519             return true;
520           }
521         }
522       }
523 
524       String[] groups = user.getGroupNames();
525       if (groups != null) {
526         for (String group : groups) {
527           List<TablePermission> groupPerms = tablePerms.getGroup(group);
528           if (groupPerms != null) {
529             for (TablePermission p : groupPerms) {
530               if (p.matchesFamily(table, family, action)) {
531                 return true;
532               }
533             }
534           }
535         }
536       }
537     }
538 
539     return false;
540   }
541 
542   public boolean matchPermission(User user,
543       TableName table, byte[] family, byte[] qualifier,
544       Permission.Action action) {
545     PermissionCache<TablePermission> tablePerms = tableCache.get(table);
546     if (tablePerms != null) {
547       List<TablePermission> userPerms = tablePerms.getUser(user.getShortName());
548       if (userPerms != null) {
549         for (TablePermission p : userPerms) {
550           if (p.matchesFamilyQualifier(table, family, qualifier, action)) {
551             return true;
552           }
553         }
554       }
555 
556       String[] groups = user.getGroupNames();
557       if (groups != null) {
558         for (String group : groups) {
559           List<TablePermission> groupPerms = tablePerms.getGroup(group);
560           if (groupPerms != null) {
561             for (TablePermission p : groupPerms) {
562               if (p.matchesFamilyQualifier(table, family, qualifier, action)) {
563                 return true;
564               }
565             }
566           }
567         }
568       }
569     }
570     return false;
571   }
572 
573   public void removeNamespace(byte[] ns) {
574     nsCache.remove(Bytes.toString(ns));
575   }
576 
577   public void removeTable(TableName table) {
578     tableCache.remove(table);
579   }
580 
581   /**
582    * Overwrites the existing permission set for a given user for a table, and
583    * triggers an update for zookeeper synchronization.
584    * @param username
585    * @param table
586    * @param perms
587    */
588   public void setTableUserPermissions(String username, TableName table,
589       List<TablePermission> perms) {
590     PermissionCache<TablePermission> tablePerms = getTablePermissions(table);
591     tablePerms.replaceUser(username, perms);
592     writeTableToZooKeeper(table, tablePerms);
593   }
594 
595   /**
596    * Overwrites the existing permission set for a group and triggers an update
597    * for zookeeper synchronization.
598    * @param group
599    * @param table
600    * @param perms
601    */
602   public void setTableGroupPermissions(String group, TableName table,
603       List<TablePermission> perms) {
604     PermissionCache<TablePermission> tablePerms = getTablePermissions(table);
605     tablePerms.replaceGroup(group, perms);
606     writeTableToZooKeeper(table, tablePerms);
607   }
608 
609   /**
610    * Overwrites the existing permission set for a given user for a table, and
611    * triggers an update for zookeeper synchronization.
612    * @param username
613    * @param namespace
614    * @param perms
615    */
616   public void setNamespaceUserPermissions(String username, String namespace,
617       List<TablePermission> perms) {
618     PermissionCache<TablePermission> tablePerms = getNamespacePermissions(namespace);
619     tablePerms.replaceUser(username, perms);
620     writeNamespaceToZooKeeper(namespace, tablePerms);
621   }
622 
623   /**
624    * Overwrites the existing permission set for a group and triggers an update
625    * for zookeeper synchronization.
626    * @param group
627    * @param namespace
628    * @param perms
629    */
630   public void setNamespaceGroupPermissions(String group, String namespace,
631       List<TablePermission> perms) {
632     PermissionCache<TablePermission> tablePerms = getNamespacePermissions(namespace);
633     tablePerms.replaceGroup(group, perms);
634     writeNamespaceToZooKeeper(namespace, tablePerms);
635   }
636 
637   public void writeTableToZooKeeper(TableName table,
638       PermissionCache<TablePermission> tablePerms) {
639     byte[] serialized = new byte[0];
640     if (tablePerms != null) {
641       serialized = AccessControlLists.writePermissionsAsBytes(tablePerms.getAllPermissions(), conf);
642     }
643     zkperms.writeToZookeeper(table.getName(), serialized);
644   }
645 
646   public void writeNamespaceToZooKeeper(String namespace,
647       PermissionCache<TablePermission> tablePerms) {
648     byte[] serialized = new byte[0];
649     if (tablePerms != null) {
650       serialized = AccessControlLists.writePermissionsAsBytes(tablePerms.getAllPermissions(), conf);
651     }
652     zkperms.writeToZookeeper(Bytes.toBytes(AccessControlLists.toNamespaceEntry(namespace)),
653         serialized);
654   }
655 
656   public long getMTime() {
657     return mtime;
658   }
659 
660   static Map<ZooKeeperWatcher,TableAuthManager> managerMap =
661     new HashMap<ZooKeeperWatcher,TableAuthManager>();
662 
663   public synchronized static TableAuthManager get(
664       ZooKeeperWatcher watcher, Configuration conf) throws IOException {
665     instance = managerMap.get(watcher);
666     if (instance == null) {
667       instance = new TableAuthManager(watcher, conf);
668       managerMap.put(watcher, instance);
669     }
670     return instance;
671   }
672 }