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