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.jupiter.api.Assertions.assertEquals; 024import static org.junit.jupiter.api.Assertions.assertThrows; 025import static org.mockito.Mockito.when; 026 027import java.io.IOException; 028import java.net.URI; 029import java.net.URISyntaxException; 030import org.apache.hadoop.fs.FileSystem; 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.jupiter.api.BeforeEach; 040import org.junit.jupiter.api.Tag; 041import org.junit.jupiter.api.Test; 042import org.mockito.Mockito; 043 044@Tag(SecurityTests.TAG) 045@Tag(SmallTests.TAG) 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 @BeforeEach 058 public void setup() throws IOException, URISyntaxException { 059 when(userProvider.getCurrent()).thenReturn(user); 060 when(userProvider.isHadoopSecurityEnabled()).thenReturn(true); 061 when(fileSystem.getCanonicalServiceName()).thenReturn("hdfs://"); 062 when(fileSystem.getUri()).thenReturn(new URI("hdfs://someUri")); 063 when(webHdfsFileSystem.getCanonicalServiceName()).thenReturn("webhdfs://"); 064 when(webHdfsFileSystem.getUri()).thenReturn(new URI("webhdfs://someUri")); 065 when(swebHdfsFileSystem.getCanonicalServiceName()).thenReturn("swebhdfs://"); 066 when(swebHdfsFileSystem.getUri()).thenReturn(new URI("swebhdfs://someUri")); 067 when(user.getToken(HDFS_DELEGATION_KIND.toString(), fileSystem.getCanonicalServiceName())) 068 .thenReturn(hdfsToken); 069 when(user.getToken(WEBHDFS_TOKEN_KIND.toString(), webHdfsFileSystem.getCanonicalServiceName())) 070 .thenReturn(webhdfsToken); 071 when( 072 user.getToken(SWEBHDFS_TOKEN_KIND.toString(), swebHdfsFileSystem.getCanonicalServiceName())) 073 .thenReturn(swebhdfsToken); 074 when(hdfsToken.getKind()).thenReturn(new Text("HDFS_DELEGATION_TOKEN")); 075 when(webhdfsToken.getKind()).thenReturn(WEBHDFS_TOKEN_KIND); 076 when(swebhdfsToken.getKind()).thenReturn(SWEBHDFS_TOKEN_KIND); 077 } 078 079 @Test 080 public void acquireDelegationToken_defaults_to_hdfsFileSystem() throws IOException { 081 fsDelegationToken.acquireDelegationToken(fileSystem); 082 assertEquals(fsDelegationToken.getUserToken().getKind(), HDFS_DELEGATION_KIND); 083 } 084 085 @Test 086 public void acquireDelegationToken_webhdfsFileSystem() throws IOException { 087 fsDelegationToken.acquireDelegationToken(webHdfsFileSystem); 088 assertEquals(fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND); 089 } 090 091 @Test 092 public void acquireDelegationToken_swebhdfsFileSystem() throws IOException { 093 fsDelegationToken.acquireDelegationToken(swebHdfsFileSystem); 094 assertEquals(fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND); 095 } 096 097 @Test 098 public void acquireDelegationTokenByTokenKind_rejects_null_token_kind() throws IOException { 099 assertThrows(NullPointerException.class, 100 () -> fsDelegationToken.acquireDelegationToken(null, fileSystem)); 101 } 102 103 @Test 104 public void acquireDelegationTokenByTokenKind_webhdfsFileSystem() throws IOException { 105 fsDelegationToken.acquireDelegationToken(WEBHDFS_TOKEN_KIND.toString(), webHdfsFileSystem); 106 assertEquals(fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND); 107 } 108 109 @Test 110 public void acquireDelegationTokenByTokenKind_swebhdfsFileSystem() throws IOException { 111 fsDelegationToken.acquireDelegationToken(SWEBHDFS_TOKEN_KIND.toString(), swebHdfsFileSystem); 112 assertEquals(fsDelegationToken.getUserToken().getKind(), SWEBHDFS_TOKEN_KIND); 113 } 114}