Skip to content

Commit b294f7b

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

File tree

2 files changed

+161
-6
lines changed

2 files changed

+161
-6
lines changed

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

Lines changed: 41 additions & 6 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,16 +15,23 @@
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

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;
26+
2127
import java.io.IOException;
28+
import java.util.Objects;
2229

2330
import org.apache.commons.logging.Log;
2431
import org.apache.commons.logging.LogFactory;
32+
import org.apache.hadoop.fs.FileSystem;
2533
import org.apache.hadoop.hbase.classification.InterfaceAudience;
2634
import org.apache.hadoop.hbase.classification.InterfaceStability;
27-
import org.apache.hadoop.fs.FileSystem;
2835
import org.apache.hadoop.hbase.security.UserProvider;
2936
import org.apache.hadoop.security.token.Token;
3037

@@ -54,18 +61,46 @@ public FsDelegationToken(final UserProvider userProvider, final String renewer)
5461
}
5562

5663
/**
57-
* Acquire the delegation token for the specified filesytem.
64+
* Acquire the delegation token for the specified filesystem.
5865
* Before requesting a new delegation token, tries to find one already available.
66+
* Currently supports checking existing delegation tokens for swebhdfs, webhdfs and hdfs.
5967
*
6068
* @param fs the filesystem that requires the delegation token
6169
* @throws IOException on fs.getDelegationToken() failure
6270
*/
6371
public void acquireDelegationToken(final FileSystem fs)
6472
throws IOException {
73+
String tokenKind;
74+
String scheme = fs.getUri().getScheme();
75+
if (SWEBHDFS_SCHEME.equalsIgnoreCase(scheme)) {
76+
tokenKind = SWEBHDFS_TOKEN_KIND.toString();
77+
} else if (WEBHDFS_SCHEME.equalsIgnoreCase(scheme)) {
78+
tokenKind = WEBHDFS_TOKEN_KIND.toString();
79+
} else if (HDFS_URI_SCHEME.equalsIgnoreCase(scheme)) {
80+
tokenKind = HDFS_DELEGATION_KIND.toString();
81+
} else {
82+
LOG.warn("Unknown FS URI scheme: " + scheme);
83+
// Preserve default behavior
84+
tokenKind = HDFS_DELEGATION_KIND.toString();
85+
}
86+
87+
acquireDelegationToken(tokenKind, fs);
88+
}
89+
90+
/**
91+
* Acquire the delegation token for the specified filesystem and token kind.
92+
* Before requesting a new delegation token, tries to find one already available.
93+
*
94+
* @param tokenKind non-null token kind to get delegation token from the {@link UserProvider}
95+
* @param fs the filesystem that requires the delegation token
96+
* @throws IOException on fs.getDelegationToken() failure
97+
*/
98+
public void acquireDelegationToken(final String tokenKind, final FileSystem fs)
99+
throws IOException {
100+
Objects.requireNonNull(tokenKind, "tokenKind:null");
65101
if (userProvider.isHadoopSecurityEnabled()) {
66102
this.fs = fs;
67-
userToken = userProvider.getCurrent().getToken("HDFS_DELEGATION_TOKEN",
68-
fs.getCanonicalServiceName());
103+
userToken = userProvider.getCurrent().getToken(tokenKind, fs.getCanonicalServiceName());
69104
if (userToken == null) {
70105
hasForwardedToken = false;
71106
try {
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
}

0 commit comments

Comments
 (0)