Skip to content

Commit 11f30de

Browse files
venki09petersomogyi
authored andcommitted
HBASE-22313 Add method to FsDelegation token to accept token kind (#199)
Signed-off-by: stack <[email protected]> Signed-off-by: Peter Somogyi <[email protected]>
1 parent 41490e7 commit 11f30de

File tree

2 files changed

+169
-9
lines changed

2 files changed

+169
-9
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/security/token/FsDelegationToken.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
@@ -15,18 +15,24 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
1918
package org.apache.hadoop.hbase.security.token;
2019

21-
import java.io.IOException;
20+
import static org.apache.hadoop.hdfs.protocol.HdfsConstants.HDFS_URI_SCHEME;
21+
import static org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier.HDFS_DELEGATION_KIND;
22+
import static org.apache.hadoop.hdfs.web.WebHdfsConstants.SWEBHDFS_SCHEME;
23+
import static org.apache.hadoop.hdfs.web.WebHdfsConstants.SWEBHDFS_TOKEN_KIND;
24+
import static org.apache.hadoop.hdfs.web.WebHdfsConstants.WEBHDFS_SCHEME;
25+
import static org.apache.hadoop.hdfs.web.WebHdfsConstants.WEBHDFS_TOKEN_KIND;
2226

27+
import java.io.IOException;
28+
import java.util.Objects;
29+
import org.apache.hadoop.fs.FileSystem;
30+
import org.apache.hadoop.hbase.security.UserProvider;
31+
import org.apache.hadoop.security.token.Token;
2332
import org.apache.yetus.audience.InterfaceAudience;
2433
import org.apache.yetus.audience.InterfaceStability;
2534
import org.slf4j.Logger;
2635
import org.slf4j.LoggerFactory;
27-
import org.apache.hadoop.fs.FileSystem;
28-
import org.apache.hadoop.hbase.security.UserProvider;
29-
import org.apache.hadoop.security.token.Token;
3036

3137
/**
3238
* Helper class to obtain a filesystem delegation token.
@@ -54,18 +60,46 @@ public FsDelegationToken(final UserProvider userProvider, final String renewer)
5460
}
5561

5662
/**
57-
* Acquire the delegation token for the specified filesytem.
63+
* Acquire the delegation token for the specified filesystem.
5864
* Before requesting a new delegation token, tries to find one already available.
65+
* Currently supports checking existing delegation tokens for swebhdfs, webhdfs and hdfs.
5966
*
6067
* @param fs the filesystem that requires the delegation token
6168
* @throws IOException on fs.getDelegationToken() failure
6269
*/
6370
public void acquireDelegationToken(final FileSystem fs)
6471
throws IOException {
72+
String tokenKind;
73+
String scheme = fs.getUri().getScheme();
74+
if (SWEBHDFS_SCHEME.equalsIgnoreCase(scheme)) {
75+
tokenKind = SWEBHDFS_TOKEN_KIND.toString();
76+
} else if (WEBHDFS_SCHEME.equalsIgnoreCase(scheme)) {
77+
tokenKind = WEBHDFS_TOKEN_KIND.toString();
78+
} else if (HDFS_URI_SCHEME.equalsIgnoreCase(scheme)) {
79+
tokenKind = HDFS_DELEGATION_KIND.toString();
80+
} else {
81+
LOG.warn("Unknown FS URI scheme: " + scheme);
82+
// Preserve default behavior
83+
tokenKind = HDFS_DELEGATION_KIND.toString();
84+
}
85+
86+
acquireDelegationToken(tokenKind, fs);
87+
}
88+
89+
/**
90+
* Acquire the delegation token for the specified filesystem and token kind.
91+
* Before requesting a new delegation token, tries to find one already available.
92+
*
93+
* @param tokenKind non-null token kind to get delegation token from the {@link UserProvider}
94+
* @param fs the filesystem that requires the delegation token
95+
* @throws IOException on fs.getDelegationToken() failure
96+
*/
97+
public void acquireDelegationToken(final String tokenKind, final FileSystem fs)
98+
throws IOException {
99+
Objects.requireNonNull(tokenKind, "tokenKind:null");
65100
if (userProvider.isHadoopSecurityEnabled()) {
66101
this.fs = fs;
67-
userToken = userProvider.getCurrent().getToken("HDFS_DELEGATION_TOKEN",
68-
fs.getCanonicalServiceName());
102+
userToken = userProvider.getCurrent().getToken(tokenKind, fs.getCanonicalServiceName());
69103
if (userToken == null) {
70104
hasForwardedToken = false;
71105
try {
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.HBaseClassTestRule;
31+
import org.apache.hadoop.hbase.security.User;
32+
import org.apache.hadoop.hbase.security.UserProvider;
33+
import org.apache.hadoop.hbase.testclassification.SecurityTests;
34+
import org.apache.hadoop.hbase.testclassification.SmallTests;
35+
import org.apache.hadoop.hdfs.web.SWebHdfsFileSystem;
36+
import org.apache.hadoop.hdfs.web.WebHdfsFileSystem;
37+
import org.apache.hadoop.io.Text;
38+
import org.apache.hadoop.security.token.Token;
39+
import org.junit.Before;
40+
import org.junit.ClassRule;
41+
import org.junit.Test;
42+
import org.junit.experimental.categories.Category;
43+
import org.mockito.Mockito;
44+
45+
@Category({SecurityTests.class, SmallTests.class})
46+
public class TestFsDelegationToken {
47+
private UserProvider userProvider = Mockito.mock(UserProvider.class);
48+
private User user = Mockito.mock(User.class);
49+
private FsDelegationToken fsDelegationToken = new FsDelegationToken(userProvider, "Renewer");
50+
private Token hdfsToken = Mockito.mock(Token.class);
51+
private Token webhdfsToken = Mockito.mock(Token.class);
52+
private Token swebhdfsToken = Mockito.mock(Token.class);
53+
private WebHdfsFileSystem webHdfsFileSystem = Mockito.mock(WebHdfsFileSystem.class);
54+
private WebHdfsFileSystem swebHdfsFileSystem = Mockito.mock(SWebHdfsFileSystem.class);
55+
private FileSystem fileSystem = Mockito.mock(FileSystem.class);
56+
57+
@ClassRule
58+
public static final HBaseClassTestRule CLASS_RULE =
59+
HBaseClassTestRule.forClass(TestFsDelegationToken.class);
60+
61+
@Before
62+
public void setup() throws IOException, URISyntaxException {
63+
when(userProvider.getCurrent()).thenReturn(user);
64+
when(userProvider.isHadoopSecurityEnabled()).thenReturn(true);
65+
when(fileSystem.getCanonicalServiceName()).thenReturn("hdfs://");
66+
when(fileSystem.getUri()).thenReturn(new URI("hdfs://someUri"));
67+
when(webHdfsFileSystem.getCanonicalServiceName()).thenReturn("webhdfs://");
68+
when(webHdfsFileSystem.getUri()).thenReturn(new URI("webhdfs://someUri"));
69+
when(swebHdfsFileSystem.getCanonicalServiceName()).thenReturn("swebhdfs://");
70+
when(swebHdfsFileSystem.getUri()).thenReturn(new URI("swebhdfs://someUri"));
71+
when(user.getToken(
72+
HDFS_DELEGATION_KIND.toString(),
73+
fileSystem.getCanonicalServiceName()))
74+
.thenReturn(hdfsToken);
75+
when(user.getToken(
76+
WEBHDFS_TOKEN_KIND.toString(),
77+
webHdfsFileSystem.getCanonicalServiceName())).thenReturn(webhdfsToken);
78+
when(user.getToken(
79+
SWEBHDFS_TOKEN_KIND.toString(),
80+
swebHdfsFileSystem.getCanonicalServiceName())).thenReturn(swebhdfsToken);
81+
when(hdfsToken.getKind()).thenReturn(new Text("HDFS_DELEGATION_TOKEN"));
82+
when(webhdfsToken.getKind()).thenReturn(WEBHDFS_TOKEN_KIND);
83+
when(swebhdfsToken.getKind()).thenReturn(SWEBHDFS_TOKEN_KIND);
84+
}
85+
86+
@Test
87+
public void acquireDelegationToken_defaults_to_hdfsFileSystem() throws IOException {
88+
fsDelegationToken.acquireDelegationToken(fileSystem);
89+
assertEquals(
90+
fsDelegationToken.getUserToken().getKind(), HDFS_DELEGATION_KIND);
91+
}
92+
93+
@Test
94+
public void acquireDelegationToken_webhdfsFileSystem() throws IOException {
95+
fsDelegationToken.acquireDelegationToken(webHdfsFileSystem);
96+
assertEquals(
97+
fsDelegationToken.getUserToken().getKind(), WEBHDFS_TOKEN_KIND);
98+
}
99+
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+
}

0 commit comments

Comments
 (0)