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  package org.apache.hadoop.hbase.security.token;
19  
20  import static org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier.HDFS_DELEGATION_KIND;
21  import static org.apache.hadoop.hdfs.web.WebHdfsConstants.SWEBHDFS_TOKEN_KIND;
22  import static org.apache.hadoop.hdfs.web.WebHdfsConstants.WEBHDFS_TOKEN_KIND;
23  import static org.junit.Assert.assertEquals;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.IOException;
27  import java.net.URI;
28  import java.net.URISyntaxException;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.hbase.security.User;
31  import org.apache.hadoop.hbase.security.UserProvider;
32  import org.apache.hadoop.hbase.testclassification.SecurityTests;
33  import org.apache.hadoop.hbase.testclassification.SmallTests;
34  import org.apache.hadoop.hdfs.web.SWebHdfsFileSystem;
35  import org.apache.hadoop.hdfs.web.WebHdfsFileSystem;
36  import org.apache.hadoop.io.Text;
37  import org.apache.hadoop.security.token.Token;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  import org.mockito.Mockito;
42  
43  @Category({SecurityTests.class, SmallTests.class})
44  public class TestFsDelegationToken {
45    private UserProvider userProvider = Mockito.mock(UserProvider.class);
46    private User user = Mockito.mock(User.class);
47    private FsDelegationToken fsDelegationToken = new FsDelegationToken(userProvider, "Renewer");
48    private Token hdfsToken = Mockito.mock(Token.class);
49    private Token webhdfsToken = Mockito.mock(Token.class);
50    private Token swebhdfsToken = Mockito.mock(Token.class);
51    private WebHdfsFileSystem webHdfsFileSystem = Mockito.mock(WebHdfsFileSystem.class);
52    private WebHdfsFileSystem swebHdfsFileSystem = Mockito.mock(SWebHdfsFileSystem.class);
53    private FileSystem fileSystem = Mockito.mock(FileSystem.class);
54  
55    @Before
56    public void setup() throws IOException, URISyntaxException {
57      when(userProvider.getCurrent()).thenReturn(user);
58      when(userProvider.isHadoopSecurityEnabled()).thenReturn(true);
59      when(fileSystem.getCanonicalServiceName()).thenReturn("hdfs://");
60      when(fileSystem.getUri()).thenReturn(new URI("hdfs://someUri"));
61      when(webHdfsFileSystem.getCanonicalServiceName()).thenReturn("webhdfs://");
62      when(webHdfsFileSystem.getUri()).thenReturn(new URI("webhdfs://someUri"));
63      when(swebHdfsFileSystem.getCanonicalServiceName()).thenReturn("swebhdfs://");
64      when(swebHdfsFileSystem.getUri()).thenReturn(new URI("swebhdfs://someUri"));
65      when(user.getToken(
66          HDFS_DELEGATION_KIND.toString(),
67          fileSystem.getCanonicalServiceName()))
68          .thenReturn(hdfsToken);
69      when(user.getToken(
70          WEBHDFS_TOKEN_KIND.toString(),
71          webHdfsFileSystem.getCanonicalServiceName())).thenReturn(webhdfsToken);
72      when(user.getToken(
73          SWEBHDFS_TOKEN_KIND.toString(),
74          swebHdfsFileSystem.getCanonicalServiceName())).thenReturn(swebhdfsToken);
75      when(hdfsToken.getKind()).thenReturn(new Text("HDFS_DELEGATION_TOKEN"));
76      when(webhdfsToken.getKind()).thenReturn(WEBHDFS_TOKEN_KIND);
77      when(swebhdfsToken.getKind()).thenReturn(SWEBHDFS_TOKEN_KIND);
78    }
79  
80    @Test
81    public void acquireDelegationToken_defaults_to_hdfsFileSystem() throws IOException {
82      fsDelegationToken.acquireDelegationToken(fileSystem);
83      assertEquals(
84          fsDelegationToken.getUserToken().getKind(), HDFS_DELEGATION_KIND);
85    }
86  
87    @Test
88    public void acquireDelegationToken_webhdfsFileSystem() throws IOException {
89      fsDelegationToken.acquireDelegationToken(webHdfsFileSystem);
90      assertEquals(
91          fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
92    }
93  
94    @Test
95    public void acquireDelegationToken_swebhdfsFileSystem() throws IOException {
96      fsDelegationToken.acquireDelegationToken(swebHdfsFileSystem);
97      assertEquals(
98          fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND);
99    }
100 
101   @Test(expected = NullPointerException.class)
102   public void acquireDelegationTokenByTokenKind_rejects_null_token_kind() throws IOException {
103     fsDelegationToken.acquireDelegationToken(null, fileSystem);
104   }
105 
106   @Test
107   public void acquireDelegationTokenByTokenKind_webhdfsFileSystem() throws IOException {
108     fsDelegationToken
109         .acquireDelegationToken(WEBHDFS_TOKEN_KIND.toString(), webHdfsFileSystem);
110     assertEquals(fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
111   }
112 
113   @Test
114   public void acquireDelegationTokenByTokenKind_swebhdfsFileSystem() throws IOException {
115     fsDelegationToken
116         .acquireDelegationToken(
117             SWEBHDFS_TOKEN_KIND.toString(), swebHdfsFileSystem);
118     assertEquals(fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND);
119   }
120 }