/
TestGenerateDelegationToken.java
151 lines (137 loc) · 6.15 KB
/
TestGenerateDelegationToken.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.security.token;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import java.io.IOException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.Collection;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.AsyncConnection;
import org.apache.hadoop.hbase.client.AsyncTable;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.ipc.NettyRpcClient;
import org.apache.hadoop.hbase.ipc.RpcClientFactory;
import org.apache.hadoop.hbase.security.AccessDeniedException;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.SecurityTests;
import org.apache.hadoop.hbase.util.FutureUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.security.token.TokenIdentifier;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AuthenticationProtos.AuthenticationService;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AuthenticationProtos.GetAuthenticationTokenRequest;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AuthenticationProtos.GetAuthenticationTokenResponse;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AuthenticationProtos.WhoAmIRequest;
import org.apache.hadoop.hbase.shaded.protobuf.generated.AuthenticationProtos.WhoAmIResponse;
@RunWith(Parameterized.class)
@Category({ SecurityTests.class, MediumTests.class })
public class TestGenerateDelegationToken extends SecureTestCluster {
@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestGenerateDelegationToken.class);
@BeforeClass
public static void setUp() throws Exception {
SecureTestCluster.setUp();
try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
Token<? extends TokenIdentifier> token = ClientTokenUtil.obtainToken(conn);
UserGroupInformation.getCurrentUser().addToken(token);
}
}
@Parameters(name = "{index}: rpcClientImpl={0}")
public static Collection<Object> parameters() {
// Client connection supports only non-blocking RPCs (due to master registry restriction), hence
// we only test NettyRpcClient.
return Arrays.asList(new Object[] { NettyRpcClient.class.getName() });
}
@Parameter
public String rpcClientImpl;
@Before
public void setUpBeforeMethod() {
TEST_UTIL.getConfiguration().set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
rpcClientImpl);
}
private void testToken() throws Exception {
try (AsyncConnection conn =
ConnectionFactory.createAsyncConnection(TEST_UTIL.getConfiguration()).get()) {
AsyncTable<?> table = conn.getTable(TableName.META_TABLE_NAME);
WhoAmIResponse response =
table.<AuthenticationService.Interface, WhoAmIResponse> coprocessorService(
AuthenticationService::newStub,
(s, c, r) -> s.whoAmI(c, WhoAmIRequest.getDefaultInstance(), r),
HConstants.EMPTY_START_ROW).get();
assertEquals(USERNAME, response.getUsername());
assertEquals(AuthenticationMethod.TOKEN.name(), response.getAuthMethod());
IOException ioe =
assertThrows(IOException.class,
() -> FutureUtils.get(table.<AuthenticationService.Interface,
GetAuthenticationTokenResponse> coprocessorService(AuthenticationService::newStub,
(s, c, r) -> s.getAuthenticationToken(c,
GetAuthenticationTokenRequest.getDefaultInstance(), r),
HConstants.EMPTY_START_ROW)));
assertThat(ioe, instanceOf(AccessDeniedException.class));
assertThat(ioe.getMessage(),
containsString("Token generation only allowed for Kerberos authenticated clients"));
}
}
/**
* Confirm that we will use delegation token first if token and kerberos tickets are both present
*/
@Test
public void testTokenFirst() throws Exception {
testToken();
}
/**
* Confirm that we can connect to cluster successfully when there is only token present, i.e, no
* kerberos ticket
*/
@Test
public void testOnlyToken() throws Exception {
User user =
User.createUserForTesting(TEST_UTIL.getConfiguration(), "no_krb_user", new String[0]);
for (Token<? extends TokenIdentifier> token : User.getCurrent().getUGI().getCredentials()
.getAllTokens()) {
user.getUGI().addToken(token);
}
user.getUGI().doAs(new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
testToken();
return null;
}
});
}
}