|
21 | 21 | import java.io.File; |
22 | 22 | import java.io.IOException; |
23 | 23 | import java.net.URI; |
24 | | -import java.nio.charset.StandardCharsets; |
25 | | -import java.security.PrivilegedExceptionAction; |
26 | 24 | import java.text.MessageFormat; |
27 | 25 | import java.util.List; |
28 | 26 | import java.util.Map; |
|
40 | 38 | import org.apache.hadoop.fs.FileSystem; |
41 | 39 | import org.apache.hadoop.fs.Path; |
42 | 40 | import org.apache.hadoop.security.UserGroupInformation; |
43 | | -import org.apache.hadoop.security.authentication.client.AuthenticationException; |
44 | | -import org.apache.hadoop.security.authentication.util.KerberosUtil; |
45 | 41 | import org.apache.hadoop.yarn.api.ApplicationConstants; |
46 | 42 | import org.apache.hadoop.yarn.api.records.ApplicationId; |
47 | 43 | import org.apache.hadoop.yarn.api.records.ApplicationReport; |
48 | 44 | import org.apache.hadoop.yarn.client.api.AppAdminClient; |
49 | 45 | import org.apache.hadoop.yarn.client.api.YarnClient; |
| 46 | +import org.apache.hadoop.yarn.client.util.YarnClientUtils; |
50 | 47 | import org.apache.hadoop.yarn.conf.YarnConfiguration; |
51 | 48 | import org.apache.hadoop.yarn.exceptions.YarnException; |
52 | 49 | import org.apache.hadoop.yarn.service.api.records.Component; |
|
60 | 57 | import org.apache.hadoop.yarn.service.utils.ServiceApiUtil; |
61 | 58 | import org.apache.hadoop.yarn.util.RMHAUtils; |
62 | 59 | import org.eclipse.jetty.util.UrlEncoded; |
63 | | -import org.ietf.jgss.GSSContext; |
64 | | -import org.ietf.jgss.GSSException; |
65 | | -import org.ietf.jgss.GSSManager; |
66 | | -import org.ietf.jgss.GSSName; |
67 | | -import org.ietf.jgss.Oid; |
68 | 60 | import org.slf4j.Logger; |
69 | 61 | import org.slf4j.LoggerFactory; |
70 | 62 |
|
@@ -92,54 +84,6 @@ public class ApiServiceClient extends AppAdminClient { |
92 | 84 | super.serviceInit(configuration); |
93 | 85 | } |
94 | 86 |
|
95 | | - /** |
96 | | - * Generate SPNEGO challenge request token. |
97 | | - * |
98 | | - * @param server - hostname to contact |
99 | | - * @throws IOException |
100 | | - * @throws InterruptedException |
101 | | - */ |
102 | | - String generateToken(String server) throws IOException, InterruptedException { |
103 | | - UserGroupInformation currentUser = UserGroupInformation.getCurrentUser(); |
104 | | - LOG.debug("The user credential is {}", currentUser); |
105 | | - String challenge = currentUser |
106 | | - .doAs(new PrivilegedExceptionAction<String>() { |
107 | | - @Override |
108 | | - public String run() throws Exception { |
109 | | - try { |
110 | | - // This Oid for Kerberos GSS-API mechanism. |
111 | | - Oid mechOid = KerberosUtil.getOidInstance("GSS_KRB5_MECH_OID"); |
112 | | - GSSManager manager = GSSManager.getInstance(); |
113 | | - // GSS name for server |
114 | | - GSSName serverName = manager.createName("HTTP@" + server, |
115 | | - GSSName.NT_HOSTBASED_SERVICE); |
116 | | - // Create a GSSContext for authentication with the service. |
117 | | - // We're passing client credentials as null since we want them to |
118 | | - // be read from the Subject. |
119 | | - GSSContext gssContext = manager.createContext( |
120 | | - serverName.canonicalize(mechOid), mechOid, null, |
121 | | - GSSContext.DEFAULT_LIFETIME); |
122 | | - gssContext.requestMutualAuth(true); |
123 | | - gssContext.requestCredDeleg(true); |
124 | | - // Establish context |
125 | | - byte[] inToken = new byte[0]; |
126 | | - byte[] outToken = gssContext.initSecContext(inToken, 0, |
127 | | - inToken.length); |
128 | | - gssContext.dispose(); |
129 | | - // Base64 encoded and stringified token for server |
130 | | - LOG.debug("Got valid challenge for host {}", serverName); |
131 | | - return new String(BASE_64_CODEC.encode(outToken), |
132 | | - StandardCharsets.US_ASCII); |
133 | | - } catch (GSSException | IllegalAccessException |
134 | | - | NoSuchFieldException | ClassNotFoundException e) { |
135 | | - LOG.error("Error: {}", e); |
136 | | - throw new AuthenticationException(e); |
137 | | - } |
138 | | - } |
139 | | - }); |
140 | | - return challenge; |
141 | | - } |
142 | | - |
143 | 87 | /** |
144 | 88 | * Calculate Resource Manager address base on working REST API. |
145 | 89 | */ |
@@ -177,7 +121,7 @@ String getRMWebAddress() { |
177 | 121 | .resource(sb.toString()).type(MediaType.APPLICATION_JSON); |
178 | 122 | if (useKerberos) { |
179 | 123 | String[] server = host.split(":"); |
180 | | - String challenge = generateToken(server[0]); |
| 124 | + String challenge = YarnClientUtils.generateToken(server[0]); |
181 | 125 | builder.header(HttpHeaders.AUTHORIZATION, "Negotiate " + |
182 | 126 | challenge); |
183 | 127 | LOG.debug("Authorization: Negotiate {}", challenge); |
@@ -289,7 +233,7 @@ private Builder getApiClient(String requestPath) |
289 | 233 | if (conf.get("hadoop.http.authentication.type").equals("kerberos")) { |
290 | 234 | try { |
291 | 235 | URI url = new URI(requestPath); |
292 | | - String challenge = generateToken(url.getHost()); |
| 236 | + String challenge = YarnClientUtils.generateToken(url.getHost()); |
293 | 237 | builder.header(HttpHeaders.AUTHORIZATION, "Negotiate " + challenge); |
294 | 238 | } catch (Exception e) { |
295 | 239 | throw new IOException(e); |
|
0 commit comments