Skip to content

Commit 22430c1

Browse files
committed
HADOOP-16457. Fixed Kerberos activation in ServiceAuthorizationManager.
Contributed by Prabhu Joseph
1 parent f51702d commit 22430c1

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/authorize/ServiceAuthorizationManager.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,23 @@ public void authorize(UserGroupInformation user,
9797
throw new AuthorizationException("Protocol " + protocol +
9898
" is not known.");
9999
}
100-
101-
// get client principal key to verify (if available)
102-
KerberosInfo krbInfo = SecurityUtil.getKerberosInfo(protocol, conf);
103-
String clientPrincipal = null;
104-
if (krbInfo != null) {
105-
String clientKey = krbInfo.clientPrincipal();
106-
if (clientKey != null && !clientKey.isEmpty()) {
107-
try {
108-
clientPrincipal = SecurityUtil.getServerPrincipal(
109-
conf.get(clientKey), addr);
110-
} catch (IOException e) {
111-
throw (AuthorizationException) new AuthorizationException(
112-
"Can't figure out Kerberos principal name for connection from "
113-
+ addr + " for user=" + user + " protocol=" + protocol)
114-
.initCause(e);
100+
101+
String clientPrincipal = null;
102+
if (UserGroupInformation.isSecurityEnabled()) {
103+
// get client principal key to verify (if available)
104+
KerberosInfo krbInfo = SecurityUtil.getKerberosInfo(protocol, conf);
105+
if (krbInfo != null) {
106+
String clientKey = krbInfo.clientPrincipal();
107+
if (clientKey != null && !clientKey.isEmpty()) {
108+
try {
109+
clientPrincipal = SecurityUtil.getServerPrincipal(
110+
conf.get(clientKey), addr);
111+
} catch (IOException e) {
112+
throw (AuthorizationException) new AuthorizationException(
113+
"Can't figure out Kerberos principal name for connection from "
114+
+ addr + " for user=" + user + " protocol=" + protocol)
115+
.initCause(e);
116+
}
115117
}
116118
}
117119
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/authorize/TestServiceAuthorization.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.fail;
2222

23+
import java.lang.annotation.Annotation;
2324
import java.net.InetAddress;
2425
import java.net.UnknownHostException;
2526

2627
import org.apache.hadoop.conf.Configuration;
2728
import org.apache.hadoop.fs.CommonConfigurationKeys;
2829
import org.apache.hadoop.ipc.TestRPC.TestProtocol;
30+
import org.apache.hadoop.security.KerberosInfo;
31+
import org.apache.hadoop.security.SecurityInfo;
32+
import org.apache.hadoop.security.SecurityUtil;
2933
import org.apache.hadoop.security.UserGroupInformation;
34+
import org.apache.hadoop.security.token.TokenInfo;
3035
import org.junit.Test;
3136

3237
public class TestServiceAuthorization {
@@ -52,6 +57,53 @@ public Service[] getServices() {
5257
}
5358
}
5459

60+
private static class CustomSecurityInfo extends SecurityInfo {
61+
@Override
62+
public KerberosInfo getKerberosInfo(Class<?> protocol,
63+
Configuration conf) {
64+
return new KerberosInfo() {
65+
@Override
66+
public Class<? extends Annotation> annotationType() {
67+
return null;
68+
}
69+
@Override
70+
public String serverPrincipal() {
71+
return null;
72+
}
73+
@Override
74+
public String clientPrincipal() {
75+
return "dfs.datanode.kerberos.principal";
76+
}
77+
};
78+
}
79+
80+
@Override
81+
public TokenInfo getTokenInfo(Class<?> protocol, Configuration conf) {
82+
return null;
83+
}
84+
}
85+
86+
@Test
87+
public void testWithClientPrincipalOnUnsecureMode()
88+
throws UnknownHostException {
89+
UserGroupInformation hdfsUser = UserGroupInformation.createUserForTesting(
90+
"hdfs", new String[] {"hadoop"});
91+
ServiceAuthorizationManager serviceAuthorizationManager =
92+
new ServiceAuthorizationManager();
93+
SecurityUtil.setSecurityInfoProviders(new CustomSecurityInfo());
94+
95+
Configuration conf = new Configuration();
96+
conf.set("dfs.datanode.kerberos.principal", "dn/[email protected]");
97+
conf.set(ACL_CONFIG, "user1 hadoop");
98+
serviceAuthorizationManager.refresh(conf, new TestPolicyProvider());
99+
try {
100+
serviceAuthorizationManager.authorize(hdfsUser, TestProtocol.class, conf,
101+
InetAddress.getByName(ADDRESS));
102+
} catch (AuthorizationException e) {
103+
fail();
104+
}
105+
}
106+
55107
@Test
56108
public void testDefaultAcl() {
57109
ServiceAuthorizationManager serviceAuthorizationManager =

0 commit comments

Comments
 (0)