View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.security;
20  
21  import static org.junit.Assert.*;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import java.io.IOException;
32  import java.security.PrivilegedAction;
33  import java.security.PrivilegedExceptionAction;
34  
35  @Category(SmallTests.class)
36  public class TestUser {
37    private static Log LOG = LogFactory.getLog(TestUser.class);
38  
39    @Test
40    public void testBasicAttributes() throws Exception {
41      Configuration conf = HBaseConfiguration.create();
42      User user = User.createUserForTesting(conf, "simple", new String[]{"foo"});
43      assertEquals("Username should match", "simple", user.getName());
44      assertEquals("Short username should match", "simple", user.getShortName());
45      // don't test shortening of kerberos names because regular Hadoop doesn't support them
46    }
47  
48    @Test
49    public void testRunAs() throws Exception {
50      Configuration conf = HBaseConfiguration.create();
51      final User user = User.createUserForTesting(conf, "testuser", new String[]{"foo"});
52      final PrivilegedExceptionAction<String> action = new PrivilegedExceptionAction<String>(){
53        public String run() throws IOException {
54            User u = User.getCurrent();
55            return u.getName();
56        }
57      };
58  
59      String username = user.runAs(action);
60      assertEquals("Current user within runAs() should match",
61          "testuser", username);
62  
63      // ensure the next run is correctly set
64      User user2 = User.createUserForTesting(conf, "testuser2", new String[]{"foo"});
65      String username2 = user2.runAs(action);
66      assertEquals("Second username should match second user",
67          "testuser2", username2);
68  
69      // check the exception version
70      username = user.runAs(new PrivilegedExceptionAction<String>(){
71        public String run() throws Exception {
72          return User.getCurrent().getName();
73        }
74      });
75      assertEquals("User name in runAs() should match", "testuser", username);
76  
77      // verify that nested contexts work
78      user2.runAs(new PrivilegedExceptionAction(){
79        public Object run() throws IOException, InterruptedException{
80          String nestedName = user.runAs(action);
81          assertEquals("Nest name should match nested user", "testuser", nestedName);
82          assertEquals("Current name should match current user",
83              "testuser2", User.getCurrent().getName());
84          return null;
85        }
86      });
87    }
88  
89    /**
90     * Make sure that we're returning a result for the current user.
91     * Previously getCurrent() was returning null if not initialized on
92     * non-secure Hadoop variants.
93     */
94    @Test
95    public void testGetCurrent() throws Exception {
96      User user1 = User.getCurrent();
97      assertNotNull(user1.ugi);
98      LOG.debug("User1 is "+user1.getName());
99  
100     for (int i =0 ; i< 100; i++) {
101       User u = User.getCurrent();
102       assertNotNull(u);
103       assertEquals(user1.getName(), u.getName());
104       assertEquals(user1, u);
105       assertEquals(user1.hashCode(), u.hashCode());
106     }
107   }
108 
109 }
110