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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.security.access;
20  
21  import java.io.IOException;
22  import java.net.InetAddress;
23  import java.util.Collection;
24  import java.util.Map;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.classification.InterfaceStability;
31  import org.apache.hadoop.hbase.ipc.RpcServer;
32  import org.apache.hadoop.hbase.security.AccessDeniedException;
33  import org.apache.hadoop.hbase.security.User;
34  import org.apache.hadoop.hbase.security.access.Permission.Action;
35  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
40  @InterfaceStability.Evolving
41  public final class AccessChecker {
42    private static final Logger AUDITLOG =
43        LoggerFactory.getLogger("SecurityLogger." + AccessChecker.class.getName());
44    private TableAuthManager authManager;
45    /**
46     * if we are active, usually false, only true if "hbase.security.authorization"
47     * has been set to true in site configuration.see HBASE-19483.
48     */
49    private boolean authorizationEnabled;
50  
51    public static boolean isAuthorizationSupported(Configuration conf) {
52      return conf.getBoolean(User.HBASE_SECURITY_AUTHORIZATION_CONF_KEY, false);
53    }
54  
55    /**
56     * Constructor with existing configuration
57     *
58     * @param conf Existing configuration to use
59     * @param zkw reference to the {@link ZooKeeperWatcher}
60     */
61    public AccessChecker(final Configuration conf, final ZooKeeperWatcher zkw)
62        throws RuntimeException {
63      // If zk is null or IOException while obtaining auth manager,
64      // throw RuntimeException so that the coprocessor is unloaded.
65      if (zkw != null) {
66        try {
67          this.authManager = TableAuthManager.getOrCreate(zkw, conf);
68        } catch (IOException ioe) {
69          throw new RuntimeException("Error obtaining AccessChecker", ioe);
70        }
71      } else {
72        throw new NullPointerException("Error obtaining AccessChecker, zk found null.");
73      }
74      authorizationEnabled = isAuthorizationSupported(conf);
75    }
76  
77    public TableAuthManager getAuthManager() {
78      return authManager;
79    }
80  
81    public void logResult(AuthResult result) {
82      if (AUDITLOG.isTraceEnabled()) {
83        InetAddress remoteAddr = RpcServer.getRemoteAddress();
84        AUDITLOG.trace("Access " + (result.isAllowed() ? "allowed" : "denied") +
85            " for user " + (result.getUser() != null ? result.getUser().getShortName() : "UNKNOWN") +
86            "; reason: " + result.getReason() +
87            "; remote address: " + (remoteAddr != null ? remoteAddr : "") +
88            "; request: " + result.getRequest() +
89            "; context: " + result.toContextString());
90      }
91    }
92  
93    /**
94     * Authorizes that the current user has any of the given permissions for the
95     * given table, column family and column qualifier.
96     * @param tableName Table requested
97     * @param family Column family requested
98     * @param qualifier Column qualifier requested
99     * @throws IOException if obtaining the current user fails
100    * @throws AccessDeniedException if user has no authorization
101    */
102   public void requirePermission(User user, String request, TableName tableName, byte[] family,
103       byte[] qualifier, Action... permissions) throws IOException {
104     AuthResult result = null;
105 
106     for (Action permission : permissions) {
107       if (authManager.authorize(user, tableName, family, qualifier, permission)) {
108         result = AuthResult.allow(request, "Table permission granted", user,
109             permission, tableName, family, qualifier);
110         break;
111       } else {
112         // rest of the world
113         result = AuthResult.deny(request, "Insufficient permissions", user,
114             permission, tableName, family, qualifier);
115       }
116     }
117     logResult(result);
118     if (authorizationEnabled && !result.isAllowed()) {
119       throw new AccessDeniedException("Insufficient permissions " + result.toContextString());
120     }
121   }
122 
123   /**
124    * Authorizes that the current user has any of the given permissions for the
125    * given table, column family and column qualifier.
126    * @param tableName Table requested
127    * @param family Column family param
128    * @param qualifier Column qualifier param
129    * @throws IOException if obtaining the current user fails
130    * @throws AccessDeniedException if user has no authorization
131    */
132   public void requireTablePermission(User user, String request, TableName tableName, byte[] family,
133       byte[] qualifier, Action... permissions) throws IOException {
134     AuthResult result = null;
135 
136     for (Action permission : permissions) {
137       if (authManager.authorize(user, tableName, null, null, permission)) {
138         result = AuthResult.allow(request, "Table permission granted", user,
139             permission, tableName, null, null);
140         result.getParams().setFamily(family).setQualifier(qualifier);
141         break;
142       } else {
143         // rest of the world
144         result = AuthResult.deny(request, "Insufficient permissions", user,
145             permission, tableName, family, qualifier);
146         result.getParams().setFamily(family).setQualifier(qualifier);
147       }
148     }
149     logResult(result);
150     if (authorizationEnabled && !result.isAllowed()) {
151       throw new AccessDeniedException("Insufficient permissions " + result.toContextString());
152     }
153   }
154 
155   /**
156    * Authorizes that the current user has any of the given permissions to access the table.
157    *
158    * @param tableName Table requested
159    * @param permissions Actions being requested
160    * @throws IOException if obtaining the current user fails
161    * @throws AccessDeniedException if user has no authorization
162    */
163   public void requireAccess(User user, String request, TableName tableName,
164       Action... permissions) throws IOException {
165     AuthResult result = null;
166 
167     for (Action permission : permissions) {
168       if (authManager.hasAccess(user, tableName, permission)) {
169         result = AuthResult.allow(request, "Table permission granted", user,
170             permission, tableName, null, null);
171         break;
172       } else {
173         // rest of the world
174         result = AuthResult.deny(request, "Insufficient permissions", user,
175             permission, tableName, null, null);
176       }
177     }
178     logResult(result);
179     if (authorizationEnabled && !result.isAllowed()) {
180       throw new AccessDeniedException("Insufficient permissions " + result.toContextString());
181     }
182   }
183 
184   /**
185    * Authorizes that the current user has global privileges for the given action.
186    * @param perm The action being requested
187    * @throws IOException if obtaining the current user fails
188    * @throws AccessDeniedException if authorization is denied
189    */
190   public void requirePermission(User user, String request, Action perm) throws IOException {
191     requireGlobalPermission(user, request, perm, null, null);
192   }
193 
194   /**
195    * Checks that the user has the given global permission. The generated
196    * audit log message will contain context information for the operation
197    * being authorized, based on the given parameters.
198    * @param perm Action being requested
199    * @param tableName Affected table name.
200    * @param familyMap Affected column families.
201    */
202   public void requireGlobalPermission(User user, String request, Action perm, TableName tableName,
203       Map<byte[], ? extends Collection<byte[]>> familyMap) throws IOException {
204     AuthResult result = null;
205     if (authManager.authorize(user, perm)) {
206       result = AuthResult.allow(request, "Global check allowed", user, perm, tableName, familyMap);
207       result.getParams().setTableName(tableName).setFamilies(familyMap);
208       logResult(result);
209     } else {
210       result = AuthResult.deny(request, "Global check failed", user, perm, tableName, familyMap);
211       result.getParams().setTableName(tableName).setFamilies(familyMap);
212       logResult(result);
213       if (authorizationEnabled) {
214         throw new AccessDeniedException("Insufficient permissions for user '" +
215             (user != null ? user.getShortName() : "null") +"' (global, action=" +
216             perm.toString() + ")");
217       }
218     }
219   }
220 
221   /**
222    * Checks that the user has the given global permission. The generated
223    * audit log message will contain context information for the operation
224    * being authorized, based on the given parameters.
225    * @param perm Action being requested
226    * @param namespace  The given namespace
227    */
228   public void requireGlobalPermission(User user, String request, Action perm,
229       String namespace) throws IOException {
230     AuthResult authResult = null;
231     if (authManager.authorize(user, perm)) {
232       authResult = AuthResult.allow(request, "Global check allowed", user, perm, null);
233       authResult.getParams().setNamespace(namespace);
234       logResult(authResult);
235     } else {
236       authResult = AuthResult.deny(request, "Global check failed", user, perm, null);
237       authResult.getParams().setNamespace(namespace);
238       logResult(authResult);
239       if (authorizationEnabled) {
240         throw new AccessDeniedException("Insufficient permissions for user '" +
241             (user != null ? user.getShortName() : "null") +"' (global, action=" +
242             perm.toString() + ")");
243       }
244     }
245   }
246 
247   /**
248    * Checks that the user has the given global or namespace permission.
249    * @param namespace  The given namespace
250    * @param permissions Actions being requested
251    */
252   public void requireNamespacePermission(User user, String request, String namespace,
253       Action... permissions) throws IOException {
254     AuthResult result = null;
255 
256     for (Action permission : permissions) {
257       if (authManager.authorize(user, namespace, permission)) {
258         result = AuthResult.allow(request, "Namespace permission granted",
259             user, permission, namespace);
260         break;
261       } else {
262         // rest of the world
263         result = AuthResult.deny(request, "Insufficient permissions", user,
264             permission, namespace);
265       }
266     }
267     logResult(result);
268     if (authorizationEnabled && !result.isAllowed()) {
269       throw new AccessDeniedException("Insufficient permissions "
270           + result.toContextString());
271     }
272   }
273 
274   /**
275    * Checks that the user has the given global or namespace permission.
276    * @param namespace   The given namespace
277    * @param permissions Actions being requested
278    */
279   public void requireNamespacePermission(User user, String request, String namespace, TableName tableName,
280       Map<byte[], ? extends Collection<byte[]>> familyMap, Action... permissions)
281       throws IOException {
282     AuthResult result = null;
283 
284     for (Action permission : permissions) {
285       if (authManager.authorize(user, namespace, permission)) {
286         result = AuthResult.allow(request, "Namespace permission granted",
287             user, permission, namespace);
288         result.getParams().setTableName(tableName).setFamilies(familyMap);
289         break;
290       } else {
291         // rest of the world
292         result = AuthResult.deny(request, "Insufficient permissions", user,
293             permission, namespace);
294         result.getParams().setTableName(tableName).setFamilies(familyMap);
295       }
296     }
297     logResult(result);
298     if (authorizationEnabled && !result.isAllowed()) {
299       throw new AccessDeniedException("Insufficient permissions "
300           + result.toContextString());
301     }
302   }
303 }