This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
CredentialUtils.java
379 lines (353 loc) · 13.6 KB
/
CredentialUtils.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* 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.slider.core.launch;
import com.google.common.base.Preconditions;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.security.Credentials;
import org.apache.hadoop.security.SecurityUtil;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
import org.apache.hadoop.yarn.client.ClientRMProxy;
import org.apache.hadoop.yarn.client.api.TimelineClient;
import org.apache.hadoop.yarn.client.api.YarnClient;
import org.apache.hadoop.yarn.conf.HAUtil;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.hadoop.yarn.security.client.TimelineDelegationTokenIdentifier;
import org.apache.hadoop.yarn.util.ConverterUtils;
import org.apache.slider.common.SliderXmlConfKeys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.apache.hadoop.security.UserGroupInformation.HADOOP_TOKEN_FILE_LOCATION;
import static org.apache.hadoop.yarn.conf.YarnConfiguration.*;
/**
* Utils to work with credentials and tokens.
*
* Designed to be movable to Hadoop core
*/
public final class CredentialUtils {
public static final String JOB_CREDENTIALS_BINARY
= SliderXmlConfKeys.MAPREDUCE_JOB_CREDENTIALS_BINARY;
private CredentialUtils() {
}
private static final Logger LOG =
LoggerFactory.getLogger(CredentialUtils.class);
/**
* Save credentials to a byte buffer. Returns null if there were no
* credentials to save
* @param credentials credential set
* @return a byte buffer of serialized tokens
* @throws IOException if the credentials could not be written to the stream
*/
public static ByteBuffer marshallCredentials(Credentials credentials) throws IOException {
ByteBuffer buffer = null;
if (!credentials.getAllTokens().isEmpty()) {
DataOutputBuffer dob = new DataOutputBuffer();
try {
credentials.writeTokenStorageToStream(dob);
} finally {
dob.close();
}
buffer = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
}
return buffer;
}
public static File locateEnvCredentials(Map<String, String> env,
Configuration conf,
StringBuffer sourceTextOut) throws FileNotFoundException {
String tokenFilename = env.get(HADOOP_TOKEN_FILE_LOCATION);
String source = "environment variable " + HADOOP_TOKEN_FILE_LOCATION;
if (tokenFilename == null) {
tokenFilename = conf.get(JOB_CREDENTIALS_BINARY);
source = "configuration option " + JOB_CREDENTIALS_BINARY;
}
if (tokenFilename != null) {
// use delegation tokens, i.e. from Oozie
File file = new File(tokenFilename.trim());
String details = String.format(
"Token File %s from %s",
file,
source);
if (!file.exists()) {
throw new FileNotFoundException("No " + details);
}
if (!file.isFile() && !file.canRead()) {
throw new FileNotFoundException("Cannot read " + details);
}
sourceTextOut.append(details);
return file;
} else {
return null;
}
}
/**
* Load the credentials from the environment. This looks at
* the value of {@link UserGroupInformation#HADOOP_TOKEN_FILE_LOCATION}
* and attempts to read in the value
* @param env environment to resolve the variable from
* @param conf configuration use when reading the tokens
* @return a set of credentials, or null if the environment did not
* specify any
* @throws IOException if a location for credentials was defined, but
* the credentials could not be loaded.
*/
public static Credentials loadTokensFromEnvironment(Map<String, String> env,
Configuration conf)
throws IOException {
StringBuffer origin = new StringBuffer();
File file = locateEnvCredentials(env, conf, origin);
if (file != null) {
LOG.debug("Using {}", origin);
return Credentials.readTokenStorageFile(file, conf);
} else {
return null;
}
}
/**
* Save credentials to a file
* @param file file to save to (will be overwritten)
* @param credentials credentials to write
* @throws IOException
*/
public static void saveTokens(File file,
Credentials credentials) throws IOException {
try(DataOutputStream daos = new DataOutputStream(
new FileOutputStream(file))) {
credentials.writeTokenStorageToStream(daos);
}
}
/**
* Look up and return the resource manager's principal. This method
* automatically does the <code>_HOST</code> replacement in the principal and
* correctly handles HA resource manager configurations.
*
* From: YARN-4629
* @param conf the {@link Configuration} file from which to read the
* principal
* @return the resource manager's principal string
* @throws IOException thrown if there's an error replacing the host name
*/
public static String getRMPrincipal(Configuration conf) throws IOException {
String principal = conf.get(RM_PRINCIPAL, "");
String hostname;
Preconditions.checkState(!principal.isEmpty(), "Not set: " + RM_PRINCIPAL);
if (HAUtil.isHAEnabled(conf)) {
YarnConfiguration yarnConf = new YarnConfiguration(conf);
if (yarnConf.get(RM_HA_ID) == null) {
// If RM_HA_ID is not configured, use the first of RM_HA_IDS.
// Any valid RM HA ID should work.
String[] rmIds = yarnConf.getStrings(RM_HA_IDS);
Preconditions.checkState((rmIds != null) && (rmIds.length > 0),
"Not set " + RM_HA_IDS);
yarnConf.set(RM_HA_ID, rmIds[0]);
}
hostname = yarnConf.getSocketAddr(
RM_ADDRESS,
DEFAULT_RM_ADDRESS,
DEFAULT_RM_PORT).getHostName();
} else {
hostname = conf.getSocketAddr(
RM_ADDRESS,
DEFAULT_RM_ADDRESS,
DEFAULT_RM_PORT).getHostName();
}
return SecurityUtil.getServerPrincipal(principal, hostname);
}
/**
* Create and add any filesystem delegation tokens with
* the RM(s) configured to be able to renew them. Returns null
* on an insecure cluster (i.e. harmless)
* @param conf configuration
* @param fs filesystem
* @param credentials credentials to update
* @return a list of all added tokens.
* @throws IOException
*/
public static Token<?>[] addRMRenewableFSDelegationTokens(Configuration conf,
FileSystem fs,
Credentials credentials) throws IOException {
Preconditions.checkArgument(conf != null);
Preconditions.checkArgument(credentials != null);
if (UserGroupInformation.isSecurityEnabled()) {
return fs.addDelegationTokens(CredentialUtils.getRMPrincipal(conf),
credentials);
}
return null;
}
/**
* Add an FS delegation token which can be renewed by the current user
* @param fs filesystem
* @param credentials credentials to update
* @throws IOException problems.
*/
public static void addSelfRenewableFSDelegationTokens(
FileSystem fs,
Credentials credentials) throws IOException {
Preconditions.checkArgument(fs != null);
Preconditions.checkArgument(credentials != null);
fs.addDelegationTokens(
getSelfRenewer(),
credentials);
}
public static String getSelfRenewer() throws IOException {
return UserGroupInformation.getLoginUser().getShortUserName();
}
/**
* Create and add an RM delegation token to the credentials
* @param yarnClient Yarn Client
* @param credentials to add token to
* @return the token which was added
* @throws IOException
* @throws YarnException
*/
public static Token<TokenIdentifier> addRMDelegationToken(YarnClient yarnClient,
Credentials credentials)
throws IOException, YarnException {
Configuration conf = yarnClient.getConfig();
Text rmPrincipal = new Text(CredentialUtils.getRMPrincipal(conf));
Text rmDTService = ClientRMProxy.getRMDelegationTokenService(conf);
Token<TokenIdentifier> rmDelegationToken =
ConverterUtils.convertFromYarn(
yarnClient.getRMDelegationToken(rmPrincipal),
rmDTService);
credentials.addToken(rmDelegationToken.getService(), rmDelegationToken);
return rmDelegationToken;
}
public static Token<TimelineDelegationTokenIdentifier> maybeAddTimelineToken(
Configuration conf,
Credentials credentials)
throws IOException, YarnException {
if (conf.getBoolean(YarnConfiguration.TIMELINE_SERVICE_ENABLED, false)) {
LOG.debug("Timeline service enabled -fetching token");
try(TimelineClient timelineClient = TimelineClient.createTimelineClient()) {
timelineClient.init(conf);
timelineClient.start();
Token<TimelineDelegationTokenIdentifier> token =
timelineClient.getDelegationToken(
CredentialUtils.getRMPrincipal(conf));
credentials.addToken(token.getService(), token);
return token;
}
} else {
LOG.debug("Timeline service is disabled");
return null;
}
}
/**
* Filter a list of tokens from a set of credentials
* @param credentials credential source (a new credential set os re
* @param filter List of tokens to strip out
* @return a new, filtered, set of credentials
*/
public static Credentials filterTokens(Credentials credentials,
List<Text> filter) {
Credentials result = new Credentials(credentials);
Iterator<Token<? extends TokenIdentifier>> iter =
result.getAllTokens().iterator();
while (iter.hasNext()) {
Token<? extends TokenIdentifier> token = iter.next();
LOG.debug("Token {}", token.getKind());
if (filter.contains(token.getKind())) {
LOG.debug("Filtering token {}", token.getKind());
iter.remove();
}
}
return result;
}
public static String dumpTokens(Credentials credentials, String separator) {
ArrayList<Token<? extends TokenIdentifier>> sorted =
new ArrayList<>(credentials.getAllTokens());
Collections.sort(sorted, new TokenComparator());
StringBuilder buffer = new StringBuilder(sorted.size()* 128);
for (Token<? extends TokenIdentifier> token : sorted) {
buffer.append(tokenToString(token)).append(separator);
}
return buffer.toString();
}
/**
* Create a string for people to look at
* @param token token to convert to a string form
* @return a printable view of the token
*/
public static String tokenToString(Token<? extends TokenIdentifier> token) {
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT);
StringBuilder buffer = new StringBuilder(128);
buffer.append(token.toString());
try {
TokenIdentifier ti = token.decodeIdentifier();
buffer.append("; ").append(ti);
if (ti instanceof AbstractDelegationTokenIdentifier) {
// details in human readable form, and compensate for information HDFS DT omits
AbstractDelegationTokenIdentifier dt = (AbstractDelegationTokenIdentifier) ti;
buffer.append("; Renewer: ").append(dt.getRenewer());
buffer.append("; Issued: ")
.append(df.format(new Date(dt.getIssueDate())));
buffer.append("; Max Date: ")
.append(df.format(new Date(dt.getMaxDate())));
}
} catch (IOException e) {
//marshall problem; not ours
LOG.debug("Failed to decode {}: {}", token, e, e);
}
return buffer.toString();
}
/**
* Get the expiry time of a token.
* @param token token to examine
* @return the time in milliseconds after which the token is invalid.
* @throws IOException
*/
public static long getTokenExpiryTime(Token token) throws IOException {
TokenIdentifier identifier = token.decodeIdentifier();
Preconditions.checkState(identifier instanceof AbstractDelegationTokenIdentifier,
"Token %s of type: %s has an identifier which cannot be examined: %s",
token, token.getClass(), identifier);
AbstractDelegationTokenIdentifier id =
(AbstractDelegationTokenIdentifier) identifier;
return id.getMaxDate();
}
private static class TokenComparator
implements Comparator<Token<? extends TokenIdentifier>>, Serializable {
@Override
public int compare(Token<? extends TokenIdentifier> left,
Token<? extends TokenIdentifier> right) {
return left.getKind().toString().compareTo(right.getKind().toString());
}
}
}