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(HDFS_DELEGATION_KIND.toString(), fileSystem.getCanonicalServiceName()))
072      .thenReturn(hdfsToken);
073    when(user.getToken(WEBHDFS_TOKEN_KIND.toString(), webHdfsFileSystem.getCanonicalServiceName()))
074      .thenReturn(webhdfsToken);
075    when(
076      user.getToken(SWEBHDFS_TOKEN_KIND.toString(), swebHdfsFileSystem.getCanonicalServiceName()))
077        .thenReturn(swebhdfsToken);
078    when(hdfsToken.getKind()).thenReturn(new Text("HDFS_DELEGATION_TOKEN"));
079    when(webhdfsToken.getKind()).thenReturn(WEBHDFS_TOKEN_KIND);
080    when(swebhdfsToken.getKind()).thenReturn(SWEBHDFS_TOKEN_KIND);
081  }
082
083  @Test
084  public void acquireDelegationToken_defaults_to_hdfsFileSystem() throws IOException {
085    fsDelegationToken.acquireDelegationToken(fileSystem);
086    assertEquals(fsDelegationToken.getUserToken().getKind(), HDFS_DELEGATION_KIND);
087  }
088
089  @Test
090  public void acquireDelegationToken_webhdfsFileSystem() throws IOException {
091    fsDelegationToken.acquireDelegationToken(webHdfsFileSystem);
092    assertEquals(fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
093  }
094
095  @Test
096  public void acquireDelegationToken_swebhdfsFileSystem() throws IOException {
097    fsDelegationToken.acquireDelegationToken(swebHdfsFileSystem);
098    assertEquals(fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND);
099  }
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.acquireDelegationToken(WEBHDFS_TOKEN_KIND.toString(), webHdfsFileSystem);
109    assertEquals(fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
110  }
111
112  @Test
113  public void acquireDelegationTokenByTokenKind_swebhdfsFileSystem() throws IOException {
114    fsDelegationToken.acquireDelegationToken(SWEBHDFS_TOKEN_KIND.toString(), swebHdfsFileSystem);
115    assertEquals(fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND);
116  }
117}