Skip to content

Commit d546b8a

Browse files
committed
Get the hbase-example working
1 parent d9da2bf commit d546b8a

File tree

12 files changed

+127
-24
lines changed

12 files changed

+127
-24
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/ipc/RpcConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected RpcConnection(Configuration conf, HashedWheelTimer timeoutTimer, Conne
122122
this.provider = pair.getFirst();
123123
this.token = pair.getSecond();
124124

125-
LOG.debug("Using {} authentication for service{}, sasl={}",
125+
LOG.debug("Using {} authentication for service={}, sasl={}",
126126
provider.getSaslAuthMethod().getName(), remoteId.serviceName, useSasl);
127127
reloginMaxBackoff = conf.getInt("hbase.security.relogin.maxbackoff", 5000);
128128
this.remoteId = remoteId;

hbase-examples/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@
185185
</exclusion>
186186
</exclusions>
187187
</dependency>
188+
<dependency>
189+
<groupId>org.bouncycastle</groupId>
190+
<artifactId>bcprov-jdk15on</artifactId>
191+
<scope>test</scope>
192+
</dependency>
193+
<dependency>
194+
<groupId>org.apache.hbase</groupId>
195+
<artifactId>hbase-http</artifactId>
196+
<scope>test</scope>
197+
<type>test-jar</type>
198+
</dependency>
188199
</dependencies>
189200
<profiles>
190201
<!-- Skip the tests in this module -->

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/SaslPlainServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import javax.security.sasl.SaslServer;
3030
import javax.security.sasl.SaslServerFactory;
3131

32-
import org.apache.hadoop.classification.InterfaceAudience;
32+
import org.apache.yetus.audience.InterfaceAudience;
3333

3434
/**
3535
* This class was copied from Hadoop Common (3.1.2) and subsequently modified.

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeClientTokenUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import org.apache.hadoop.io.Text;
2525
import org.apache.hadoop.security.token.Token;
2626
import org.apache.hadoop.security.token.TokenIdentifier;
27+
import org.apache.yetus.audience.InterfaceAudience;
2728

2829
/**
2930
* Used to acquire tokens for the ShadeSaslAuthenticationProvider.
3031
*/
32+
@InterfaceAudience.Private
3133
public class ShadeClientTokenUtil {
3234

3335
private ShadeClientTokenUtil() {}

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeProviderSelector.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
*/
118
package org.apache.hadoop.hbase.security.provider.example;
219

320
import java.util.Collection;
@@ -11,7 +28,9 @@
1128
import org.apache.hadoop.security.UserGroupInformation;
1229
import org.apache.hadoop.security.token.Token;
1330
import org.apache.hadoop.security.token.TokenIdentifier;
31+
import org.apache.yetus.audience.InterfaceAudience;
1432

33+
@InterfaceAudience.Private
1534
public class ShadeProviderSelector extends BuiltInProviderSelector {
1635

1736
private ShadeSaslClientAuthenticationProvider shade;

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeSaslAuthenticationProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import org.apache.hadoop.hbase.security.provider.SaslAuthenticationProvider;
2222
import org.apache.hadoop.io.Text;
2323
import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
24+
import org.apache.yetus.audience.InterfaceAudience;
2425

26+
@InterfaceAudience.Private
2527
public abstract class ShadeSaslAuthenticationProvider implements SaslAuthenticationProvider {
2628
public static final SaslAuthMethod METHOD = new SaslAuthMethod(
2729
"SHADE", (byte) 15, "PLAIN", AuthenticationMethod.TOKEN);
28-
public static final Text TOKEN_KIND = new Text("SHADE_TOKEN");
30+
public static final Text TOKEN_KIND = new Text("HBASE_EXAMPLE_SHADE_TOKEN");
2931

3032
@Override public SaslAuthMethod getSaslAuthMethod() {
3133
return METHOD;

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeSaslClientAuthenticationProvider.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.apache.hadoop.security.UserGroupInformation;
4141
import org.apache.hadoop.security.token.Token;
4242
import org.apache.hadoop.security.token.TokenIdentifier;
43+
import org.apache.yetus.audience.InterfaceAudience;
4344

45+
@InterfaceAudience.Private
4446
public class ShadeSaslClientAuthenticationProvider extends ShadeSaslAuthenticationProvider
4547
implements SaslClientAuthenticationProvider {
4648

@@ -54,15 +56,23 @@ public SaslClient createClient(Configuration conf, InetAddress serverAddr,
5456

5557
@Override
5658
public UserInformation getUserInfo(UserGroupInformation user) {
57-
return null;
59+
UserInformation.Builder userInfoPB = UserInformation.newBuilder();
60+
userInfoPB.setEffectiveUser(user.getUserName());
61+
return userInfoPB.build();
5862
}
5963

6064
static class ShadeSaslClientCallbackHandler implements CallbackHandler {
6165
private final String username;
6266
private final char[] password;
6367
public ShadeSaslClientCallbackHandler(
6468
Token<? extends TokenIdentifier> token) throws IOException {
65-
this.username = token.decodeIdentifier().getUser().getUserName();
69+
TokenIdentifier id = token.decodeIdentifier();
70+
if (id == null) {
71+
// Something is wrong with the environment if we can't get our Identifier back out.
72+
throw new IllegalStateException("Could not extract Identifier from Token");
73+
}
74+
UserGroupInformation ugi = id.getUser();
75+
this.username = ugi.getUserName();
6676
this.password = Bytes.toString(token.getPassword()).toCharArray();
6777
}
6878

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeSaslServerAuthenticationProvider.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.BufferedReader;
2121
import java.io.IOException;
2222
import java.io.InputStreamReader;
23+
import java.util.Arrays;
2324
import java.util.HashMap;
2425
import java.util.Map;
2526
import java.util.concurrent.atomic.AtomicReference;
@@ -40,11 +41,14 @@
4041
import org.apache.hadoop.hbase.security.provider.SaslServerAuthenticationProvider;
4142
import org.apache.hadoop.security.UserGroupInformation;
4243
import org.apache.hadoop.security.token.SecretManager;
44+
import org.apache.hadoop.security.token.SecretManager.InvalidToken;
4345
import org.apache.hadoop.security.token.TokenIdentifier;
4446
import org.apache.hadoop.util.StringUtils;
47+
import org.apache.yetus.audience.InterfaceAudience;
4548
import org.slf4j.Logger;
4649
import org.slf4j.LoggerFactory;
4750

51+
@InterfaceAudience.Private
4852
public class ShadeSaslServerAuthenticationProvider extends ShadeSaslAuthenticationProvider
4953
implements SaslServerAuthenticationProvider {
5054
private static final Logger LOG = LoggerFactory.getLogger(
@@ -131,7 +135,9 @@ public ShadeSaslServerCallbackHandler(AtomicReference<UserGroupInformation> atte
131135
this.passwordDatabase = passwordDatabase;
132136
}
133137

134-
@Override public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
138+
@Override public void handle(Callback[] callbacks)
139+
throws InvalidToken, UnsupportedCallbackException {
140+
LOG.info("SaslServerCallbackHandler called", new Exception());
135141
NameCallback nc = null;
136142
PasswordCallback pc = null;
137143
AuthorizeCallback ac = null;
@@ -155,13 +161,11 @@ public ShadeSaslServerCallbackHandler(AtomicReference<UserGroupInformation> atte
155161
UserGroupInformation ugi = createUgiForRemoteUser(username);
156162
attemptingUser.set(ugi);
157163

164+
char[] clientPassword = pc.getPassword();
158165
char[] actualPassword = passwordDatabase.get(username);
159-
if (actualPassword == null) {
160-
// How should we gracefully fail the authentication?
161-
throw new RuntimeException("Could not obtain password for user");
166+
if (!Arrays.equals(clientPassword, actualPassword)) {
167+
throw new InvalidToken("Authentication failed for " + username);
162168
}
163-
164-
pc.setPassword(actualPassword);
165169
}
166170

167171
if (ac != null) {

hbase-examples/src/main/java/org/apache/hadoop/hbase/security/provider/example/ShadeTokenIdentifier.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
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+
*/
118
package org.apache.hadoop.hbase.security.provider.example;
219

20+
import static java.util.Objects.requireNonNull;
21+
322
import java.io.DataInput;
423
import java.io.DataOutput;
524
import java.io.IOException;
625

726
import org.apache.hadoop.io.Text;
827
import org.apache.hadoop.security.UserGroupInformation;
928
import org.apache.hadoop.security.token.TokenIdentifier;
29+
import org.apache.yetus.audience.InterfaceAudience;
1030

31+
@InterfaceAudience.Private
1132
public class ShadeTokenIdentifier extends TokenIdentifier {
1233
private String username;
1334

35+
public ShadeTokenIdentifier() {
36+
// for ServiceLoader
37+
}
38+
1439
public ShadeTokenIdentifier(String username) {
15-
this.username = username;
40+
this.username = requireNonNull(username);
1641
}
1742

1843
@Override
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
#
18+
org.apache.hadoop.hbase.security.provider.example.ShadeTokenIdentifier

hbase-examples/src/test/java/org/apache/hadoop/hbase/security/provider/example/TestShadeSaslAuthenticationProvider.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
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+
*/
118
package org.apache.hadoop.hbase.security.provider.example;
219

320
import static org.junit.Assert.assertFalse;
@@ -20,6 +37,7 @@
2037
import org.apache.hadoop.fs.Path;
2138
import org.apache.hadoop.hbase.Cell;
2239
import org.apache.hadoop.hbase.CellUtil;
40+
import org.apache.hadoop.hbase.DoNotRetryIOException;
2341
import org.apache.hadoop.hbase.HBaseClassTestRule;
2442
import org.apache.hadoop.hbase.HBaseTestingUtility;
2543
import org.apache.hadoop.hbase.HConstants;
@@ -32,19 +50,16 @@
3250
import org.apache.hadoop.hbase.client.Get;
3351
import org.apache.hadoop.hbase.client.Put;
3452
import org.apache.hadoop.hbase.client.Result;
35-
import org.apache.hadoop.hbase.client.RetriesExhaustedException;
3653
import org.apache.hadoop.hbase.client.Table;
3754
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
3855
import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
39-
import org.apache.hadoop.hbase.ipc.BlockingRpcClient;
40-
import org.apache.hadoop.hbase.ipc.RpcClientFactory;
41-
import org.apache.hadoop.hbase.ipc.RpcServerFactory;
42-
import org.apache.hadoop.hbase.ipc.SimpleRpcServer;
4356
import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
4457
import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProviders;
4558
import org.apache.hadoop.hbase.security.provider.SaslServerAuthenticationProviders;
4659
import org.apache.hadoop.hbase.security.token.SecureTestCluster;
4760
import org.apache.hadoop.hbase.security.token.TokenProvider;
61+
import org.apache.hadoop.hbase.testclassification.MediumTests;
62+
import org.apache.hadoop.hbase.testclassification.SecurityTests;
4863
import org.apache.hadoop.hbase.util.Bytes;
4964
import org.apache.hadoop.hbase.util.FSUtils;
5065
import org.apache.hadoop.minikdc.MiniKdc;
@@ -55,10 +70,10 @@
5570
import org.junit.ClassRule;
5671
import org.junit.Rule;
5772
import org.junit.Test;
73+
import org.junit.experimental.categories.Category;
5874
import org.junit.rules.TestName;
59-
import org.slf4j.Logger;
60-
import org.slf4j.LoggerFactory;
6175

76+
@Category({MediumTests.class, SecurityTests.class})
6277
public class TestShadeSaslAuthenticationProvider {
6378

6479
@ClassRule
@@ -123,10 +138,6 @@ public static void setupCluster() throws Exception {
123138
UTIL.getDataTestDir("keytab").toUri().getPath());
124139
final MiniKdc kdc = UTIL.setupMiniKdc(KEYTAB_FILE);
125140

126-
// Switch back to NIO for now.
127-
CONF.set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY, BlockingRpcClient.class.getName());
128-
CONF.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, SimpleRpcServer.class.getName());
129-
130141
// Adds our test impls instead of creating service loader entries which
131142
// might inadvertently get them loaded on a real cluster.
132143
CONF.setStrings(SaslClientAuthenticationProviders.EXTRA_PROVIDERS_KEY,
@@ -210,7 +221,7 @@ public void testPositiveAuthentication() throws Exception {
210221
}
211222
}
212223

213-
@Test(expected = RetriesExhaustedException.class)
224+
@Test(expected = DoNotRetryIOException.class)
214225
public void testNegativeAuthentication() throws Exception {
215226
// Validate that we can read that record back out as the user with our custom auth'n
216227
final Configuration clientConf = new Configuration(CONF);

hbase-examples/src/test/resources/log4j.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ log4j.logger.org.apache.hadoop.metrics2.impl.MetricsSystemImpl=WARN
6666
log4j.logger.org.apache.hadoop.metrics2.util.MBeans=WARN
6767
# Enable this to get detailed connection error/retry logging.
6868
# log4j.logger.org.apache.hadoop.hbase.client.ConnectionImplementation=TRACE
69+
log4j.logger.org.apache.directory=WARN

0 commit comments

Comments
 (0)