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.hbase.security.token;
019
020import static org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier.HDFS_DELEGATION_KIND;
021import static org.apache.hadoop.hdfs.web.WebHdfsConstants.SWEBHDFS_TOKEN_KIND;
022import static org.apache.hadoop.hdfs.web.WebHdfsConstants.WEBHDFS_TOKEN_KIND;
023import static org.junit.Assert.assertEquals;
024import static org.mockito.Mockito.when;
025
026import java.io.IOException;
027import java.net.URI;
028import java.net.URISyntaxException;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.security.User;
032import org.apache.hadoop.hbase.security.UserProvider;
033import org.apache.hadoop.hbase.testclassification.SecurityTests;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hdfs.web.SWebHdfsFileSystem;
036import org.apache.hadoop.hdfs.web.WebHdfsFileSystem;
037import org.apache.hadoop.io.Text;
038import org.apache.hadoop.security.token.Token;
039import org.junit.Before;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.mockito.Mockito;
044
045@Category({SecurityTests.class, SmallTests.class})
046public class TestFsDelegationToken {
047  private UserProvider userProvider = Mockito.mock(UserProvider.class);
048  private User user = Mockito.mock(User.class);
049  private FsDelegationToken fsDelegationToken = new FsDelegationToken(userProvider, "Renewer");
050  private Token hdfsToken = Mockito.mock(Token.class);
051  private Token webhdfsToken = Mockito.mock(Token.class);
052  private Token swebhdfsToken = Mockito.mock(Token.class);
053  private WebHdfsFileSystem webHdfsFileSystem = Mockito.mock(WebHdfsFileSystem.class);
054  private WebHdfsFileSystem swebHdfsFileSystem = Mockito.mock(SWebHdfsFileSystem.class);
055  private FileSystem fileSystem = Mockito.mock(FileSystem.class);
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059      HBaseClassTestRule.forClass(TestFsDelegationToken.class);
060
061  @Before
062  public void setup() throws IOException, URISyntaxException {
063    when(userProvider.getCurrent()).thenReturn(user);
064    when(userProvider.isHadoopSecurityEnabled()).thenReturn(true);
065    when(fileSystem.getCanonicalServiceName()).thenReturn("hdfs://");
066    when(fileSystem.getUri()).thenReturn(new URI("hdfs://someUri"));
067    when(webHdfsFileSystem.getCanonicalServiceName()).thenReturn("webhdfs://");
068    when(webHdfsFileSystem.getUri()).thenReturn(new URI("webhdfs://someUri"));
069    when(swebHdfsFileSystem.getCanonicalServiceName()).thenReturn("swebhdfs://");
070    when(swebHdfsFileSystem.getUri()).thenReturn(new URI("swebhdfs://someUri"));
071    when(user.getToken(
072        HDFS_DELEGATION_KIND.toString(),
073        fileSystem.getCanonicalServiceName()))
074        .thenReturn(hdfsToken);
075    when(user.getToken(
076        WEBHDFS_TOKEN_KIND.toString(),
077        webHdfsFileSystem.getCanonicalServiceName())).thenReturn(webhdfsToken);
078    when(user.getToken(
079        SWEBHDFS_TOKEN_KIND.toString(),
080        swebHdfsFileSystem.getCanonicalServiceName())).thenReturn(swebhdfsToken);
081    when(hdfsToken.getKind()).thenReturn(new Text("HDFS_DELEGATION_TOKEN"));
082    when(webhdfsToken.getKind()).thenReturn(WEBHDFS_TOKEN_KIND);
083    when(swebhdfsToken.getKind()).thenReturn(SWEBHDFS_TOKEN_KIND);
084  }
085
086  @Test
087  public void acquireDelegationToken_defaults_to_hdfsFileSystem() throws IOException {
088    fsDelegationToken.acquireDelegationToken(fileSystem);
089    assertEquals(
090        fsDelegationToken.getUserToken().getKind(), HDFS_DELEGATION_KIND);
091  }
092
093  @Test
094  public void acquireDelegationToken_webhdfsFileSystem() throws IOException {
095    fsDelegationToken.acquireDelegationToken(webHdfsFileSystem);
096    assertEquals(
097        fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
098  }
099
100  @Test
101  public void acquireDelegationToken_swebhdfsFileSystem() throws IOException {
102    fsDelegationToken.acquireDelegationToken(swebHdfsFileSystem);
103    assertEquals(
104        fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND);
105  }
106
107  @Test(expected = NullPointerException.class)
108  public void acquireDelegationTokenByTokenKind_rejects_null_token_kind() throws IOException {
109    fsDelegationToken.acquireDelegationToken(null, fileSystem);
110  }
111
112  @Test
113  public void acquireDelegationTokenByTokenKind_webhdfsFileSystem() throws IOException {
114    fsDelegationToken
115        .acquireDelegationToken(WEBHDFS_TOKEN_KIND.toString(), webHdfsFileSystem);
116    assertEquals(fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
117  }
118
119  @Test
120  public void acquireDelegationTokenByTokenKind_swebhdfsFileSystem() throws IOException {
121    fsDelegationToken
122        .acquireDelegationToken(
123            SWEBHDFS_TOKEN_KIND.toString(), swebHdfsFileSystem);
124    assertEquals(fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND);
125  }
126}