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.hdfs.server.namenode; 019 020import org.apache.hadoop.classification.InterfaceAudience; 021import org.apache.hadoop.classification.InterfaceStability; 022import org.apache.hadoop.fs.UnresolvedLinkException; 023import org.apache.hadoop.fs.permission.FsAction; 024import org.apache.hadoop.fs.permission.FsPermission; 025import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; 026import org.apache.hadoop.security.AccessControlException; 027 028import java.io.IOException; 029import java.util.Map; 030import java.util.Set; 031 032/** 033 * Implementations of this interface are called from within an 034 * <code>inode</code> to set or return authorization related information. 035 * <p/> 036 * The HDFS default implementation, {@link DefaultAuthorizationProvider} uses 037 * the <code>inode</code> itself to retrieve and store information. 038 * <p/> 039 * A custom implementation may use a different authorization store and enforce 040 * the permission check using alternate logic. 041 * <p/> 042 * It is expected that an implementation of the provider will not call external 043 * systems or realize expensive computations on any of the methods defined by 044 * the provider interface as they are typically invoked within remote client 045 * filesystem operations. 046 * <p/> 047 * If calls to external systems are required, they should be done 048 * asynchronously from the provider methods. 049 */ 050@InterfaceAudience.Public 051@InterfaceStability.Unstable 052public abstract class AuthorizationProvider { 053 054 private static final ThreadLocal<Boolean> CLIENT_OP_TL = 055 new ThreadLocal<Boolean>() { 056 @Override 057 protected Boolean initialValue() { 058 return Boolean.FALSE; 059 } 060 }; 061 062 static void beginClientOp() { 063 CLIENT_OP_TL.set(Boolean.TRUE); 064 } 065 066 static void endClientOp() { 067 CLIENT_OP_TL.set(Boolean.FALSE); 068 } 069 070 private static AuthorizationProvider provider = 071 new DefaultAuthorizationProvider(); 072 073 /** 074 * Return the authorization provider singleton for the NameNode. 075 * 076 * @return the authorization provider 077 */ 078 public static AuthorizationProvider get() { 079 return provider; 080 } 081 082 /** 083 * Set the authorization provider singleton for the NameNode. The 084 * provider must be started (before being set) and stopped by the setter. 085 * 086 * @param authzProvider the authorization provider 087 */ 088 static void set(AuthorizationProvider authzProvider) { 089 provider = (authzProvider != null) ? authzProvider 090 : new DefaultAuthorizationProvider(); 091 } 092 093 /** 094 * Constant that indicates current state (as opposed to a particular snapshot 095 * ID) when retrieving authorization information from the provider. 096 */ 097 public static final int CURRENT_STATE_ID = Snapshot.CURRENT_STATE_ID; 098 099 /** 100 * This interface exposes INode read-only information relevant for 101 * authorization decisions. 102 * 103 * @see AuthorizationProvider 104 */ 105 @InterfaceAudience.Public 106 @InterfaceStability.Unstable 107 public interface INodeAuthorizationInfo { 108 109 /** 110 * Return the inode unique ID. This value never changes. 111 * 112 * @return the inode unique ID. 113 */ 114 public long getId(); 115 116 /** 117 * Return the inode path element name. This value may change. 118 * @return the inode path element name. 119 */ 120 public String getLocalName(); 121 122 /** 123 * Return the parent inode. This value may change. 124 * 125 * @return the parent inode. 126 */ 127 public INodeAuthorizationInfo getParent(); 128 129 /** 130 * Return the inode full path. This value may change. 131 * 132 * @return the inode full path 133 */ 134 public String getFullPathName(); 135 136 /** 137 * Return if the inode is a directory or not. 138 * 139 * @return <code>TRUE</code> if the inode is a directory, 140 * <code>FALSE</code> otherwise. 141 */ 142 public boolean isDirectory(); 143 144 /** 145 * Return the inode user for the specified snapshot. 146 * 147 * @param snapshotId a snapshot ID or {@link #CURRENT_STATE_ID} for latest 148 * value. 149 * @return the inode user for the specified snapshot. 150 */ 151 public String getUserName(int snapshotId); 152 153 /** 154 * Return the inode group for the specified snapshot. 155 * 156 * @param snapshotId a snapshot ID or {@link #CURRENT_STATE_ID} for latest 157 * value. 158 * @return the inode group for the specified snapshot. 159 */ 160 public String getGroupName(int snapshotId); 161 162 /** 163 * Return the inode permission for the specified snapshot. 164 * 165 * @param snapshotId a snapshot ID or {@link #CURRENT_STATE_ID} for latest 166 * value. 167 * @return the inode permission for the specified snapshot. 168 */ 169 public FsPermission getFsPermission(int snapshotId); 170 171 /** 172 * Return the inode ACL feature for the specified snapshot. 173 * 174 * @param snapshotId a snapshot ID or {@link #CURRENT_STATE_ID} for latest 175 * value. 176 * @return the inode ACL feature for the specified snapshot. 177 */ 178 public AclFeature getAclFeature(int snapshotId); 179 180 } 181 182 /** 183 * Indicates if the current provider method invocation is part of a client 184 * operation or it is an internal NameNode call (i.e. a FS image or an edit 185 * log operation). 186 * 187 * @return <code>TRUE</code> if the provider method invocation is being 188 * done as part of a client operation, <code>FALSE</code> otherwise. 189 */ 190 protected final boolean isClientOp() { 191 return CLIENT_OP_TL.get() == Boolean.TRUE; 192 } 193 194 /** 195 * Initialize the provider. This method is called at NameNode startup 196 * time. 197 */ 198 public void start() { 199 } 200 201 /** 202 * Shutdown the provider. This method is called at NameNode shutdown time. 203 */ 204 public void stop() { 205 } 206 207 /** 208 * Set all currently snapshot-able directories and their corresponding last 209 * snapshot ID. This method is called at NameNode startup. 210 * <p/> 211 * A provider implementation that keeps authorization information on per 212 * snapshot basis can use this call to initialize/re-sync its information with 213 * the NameNode snapshot-able directories information. 214 * 215 * @param snapshotableDirs a map with all the currently snapshot-able 216 * directories and their corresponding last snapshot ID 217 */ 218 public void setSnaphottableDirs(Map<INodeAuthorizationInfo, Integer> 219 snapshotableDirs) { 220 } 221 222 /** 223 * Add a directory as snapshot-able. 224 * <p/> 225 * A provider implementation that keeps authorization information on per 226 * snapshot basis can use this call to prepare itself for snapshots on the 227 * specified directory. 228 * 229 * @param dir snapshot-able directory to add 230 */ 231 public void addSnapshottable(INodeAuthorizationInfo dir) { 232 } 233 234 /** 235 * Remove a directory as snapshot-able. 236 * <p/> 237 * A provider implementation that keeps authorization information on per 238 * snapshot basis can use this call to clean up any snapshot on the 239 * specified directory. 240 * 241 * @param dir snapshot-able directory to remove 242 */ 243 public void removeSnapshottable(INodeAuthorizationInfo dir) { 244 } 245 246 /** 247 * Create a snapshot for snapshot-able directory. 248 * <p/> 249 * A provider implementation that keeps authorization information on per 250 * snapshot basis can use this call to perform any snapshot related 251 * bookkeeping on the specified directory because of the snapshot creation. 252 * 253 * @param dir directory to make a snapshot of 254 * @param snapshotId the snapshot ID to create 255 */ 256 public void createSnapshot(INodeAuthorizationInfo dir, int snapshotId) 257 throws IOException { 258 } 259 260 /** 261 * Remove a snapshot for snapshot-able directory. 262 * <p/> 263 * A provider implementation that keeps authorization information on per 264 * snapshot basis can use this call to perform any snapshot related 265 * bookkeeping on the specified directory because of the snapshot removal. 266 * 267 * @param dir directory to remove a snapshot from 268 * @param snapshotId the snapshot ID to remove 269 */ 270 public void removeSnapshot(INodeAuthorizationInfo dir, int snapshotId) 271 throws IOException { 272 } 273 274 /** 275 * Set the user for an inode. 276 * <p/> 277 * This method is always call within a Filesystem LOCK. 278 * 279 * @param node inode 280 * @param user user name 281 */ 282 public abstract void setUser(INodeAuthorizationInfo node, String user); 283 284 /** 285 * Get the user of an inode. 286 * <p/> 287 * This method is always call within a Filesystem LOCK. 288 * 289 * @param node inode 290 * @param snapshotId snapshot ID of the inode to get the user from 291 * @return the user of the inode 292 */ 293 public abstract String getUser(INodeAuthorizationInfo node, int snapshotId); 294 295 /** 296 * Set teh group of an inode. 297 * <p/> 298 * This method is always call within a Filesystem LOCK. 299 * 300 * @param node inode 301 * @param group group name 302 */ 303 public abstract void setGroup(INodeAuthorizationInfo node, String group); 304 305 /** 306 * Get the group of an inode. 307 * <p/> 308 * This method is always call within a Filesystem LOCK. 309 * 310 * @param node inode 311 * @param snapshotId snapshot ID of the inode to get the group from 312 * @return the group of the inode 313 */ 314 public abstract String getGroup(INodeAuthorizationInfo node, int snapshotId); 315 316 /** 317 * Set the permission of an inode. 318 * <p/> 319 * This method is always call within a Filesystem LOCK. 320 * 321 * @param node inode 322 * @param permission the permission to set 323 */ 324 public abstract void setPermission(INodeAuthorizationInfo node, 325 FsPermission permission); 326 327 /** 328 * Get the permission of an inode. 329 * <p/> 330 * This method is always call within a Filesystem LOCK. 331 * 332 * @param node inode 333 * @param snapshotId snapshot ID of the inode to get the permission from 334 * @return the permission of the inode 335 */ 336 public abstract FsPermission getFsPermission(INodeAuthorizationInfo node, 337 int snapshotId); 338 339 /** 340 * Get the ACLs of an inode. 341 * <p/> 342 * This method is always call within a Filesystem LOCK. 343 * 344 * @param node inode 345 * @param snapshotId snapshot ID of the inode to get the ACLs from 346 * @return the ACLs of the inode 347 */ 348 public abstract AclFeature getAclFeature(INodeAuthorizationInfo node, 349 int snapshotId); 350 351 /** 352 * Remove the ACLs of an inode. 353 * <p/> 354 * This method is always call within a Filesystem LOCK. 355 * 356 * @param node inode 357 */ 358 public abstract void removeAclFeature(INodeAuthorizationInfo node); 359 360 /** 361 * Add ACLs to an inode. 362 * <p/> 363 * This method is always call within a Filesystem LOCK. 364 * 365 * @param node inode 366 * @param f the ACLs of the inode 367 */ 368 public abstract void addAclFeature(INodeAuthorizationInfo node, AclFeature f); 369 370 /** 371 * Check whether current user have permissions to access the path. 372 * Traverse is always checked. 373 * <p/> 374 * This method is always call within a Filesystem LOCK. 375 * <p/> 376 * Parent path means the parent directory for the path. 377 * Ancestor path means the last (the closest) existing ancestor directory 378 * of the path. 379 * <p/> 380 * Note that if the parent path exists, 381 * then the parent path and the ancestor path are the same. 382 * <p/> 383 * For example, suppose the path is "/foo/bar/baz". 384 * No matter baz is a file or a directory, 385 * the parent path is "/foo/bar". 386 * If bar exists, then the ancestor path is also "/foo/bar". 387 * If bar does not exist and foo exists, 388 * then the ancestor path is "/foo". 389 * Further, if both foo and bar do not exist, 390 * then the ancestor path is "/". 391 * 392 * @param user user ot check permissions against 393 * @param groups groups of the user to check permissions against 394 * @param inodes inodes of the path to check permissions 395 * @param snapshotId snapshot ID to check permissions 396 * @param doCheckOwner Require user to be the owner of the path? 397 * @param ancestorAccess The access required by the ancestor of the path. 398 * @param parentAccess The access required by the parent of the path. 399 * @param access The access required by the path. 400 * @param subAccess If path is a directory, 401 * it is the access required of the path and all the sub-directories. 402 * If path is not a directory, there is no effect. 403 * @param ignoreEmptyDir Ignore permission checking for empty directory? 404 * @throws AccessControlException 405 * @throws UnresolvedLinkException 406 */ 407 public abstract void checkPermission(String user, Set<String> groups, 408 INodeAuthorizationInfo[] inodes, int snapshotId, 409 boolean doCheckOwner, FsAction ancestorAccess, FsAction parentAccess, 410 FsAction access, FsAction subAccess, boolean ignoreEmptyDir) 411 throws AccessControlException, UnresolvedLinkException; 412 413}