|
|
@@ -15,23 +15,30 @@
|
|
|
// limitations under the License.
|
|
|
package com.cloudera.hue;
|
|
|
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.File;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
import org.apache.hadoop.fs.Path;
|
|
|
+import org.apache.hadoop.io.Text;
|
|
|
import org.apache.hadoop.security.Credentials;
|
|
|
+import org.apache.hadoop.security.token.Token;
|
|
|
|
|
|
/**
|
|
|
* A tool to merge the credentials of multiple distinct files containing Hadoop
|
|
|
* delegation tokens into a single file.
|
|
|
*/
|
|
|
public class CredentialsMerger {
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Merge several credentials files into one. Give the desired output file
|
|
|
* first, followed by all of the input files.
|
|
|
*
|
|
|
+ * <p>File formats are tried in this order: TokenStorageFile, urlEncodedString.
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
* @param args <out> <in1> ...
|
|
|
* @throws IOException in the event of an error reading or writing files.
|
|
|
*/
|
|
|
@@ -44,15 +51,29 @@ public class CredentialsMerger {
|
|
|
Path outputFile = new Path("file://" + new File(args[0]).getAbsolutePath());
|
|
|
Configuration conf = new Configuration();
|
|
|
Credentials credentials = new Credentials();
|
|
|
+
|
|
|
for (int i = 1; i < args.length; i++) {
|
|
|
- Credentials singleFileCredentials = Credentials.readTokenStorageFile(
|
|
|
- new Path("file://" + new File(args[i]).getAbsolutePath()), conf);
|
|
|
- credentials.addAll(singleFileCredentials);
|
|
|
+ try {
|
|
|
+ Credentials singleFileCredentials = Credentials.readTokenStorageFile(
|
|
|
+ new Path("file://" + new File(args[i]).getAbsolutePath()), conf);
|
|
|
+ credentials.addAll(singleFileCredentials);
|
|
|
+ } catch (IOException e) {
|
|
|
+ BufferedReader reader = new BufferedReader(new FileReader(args[i]));
|
|
|
+ try {
|
|
|
+ // Retry to read the token with an encodedUrl format
|
|
|
+ Token<?> token = new Token();
|
|
|
+ String encodedtoken = reader.readLine();
|
|
|
+ token.decodeFromUrlString(encodedtoken);
|
|
|
+ credentials.addToken(new Text(args[i]), token);
|
|
|
+ } finally {
|
|
|
+ reader.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
credentials.writeTokenStorageFile(outputFile, conf);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Show command usage.
|
|
|
*/
|
|
|
@@ -60,5 +81,4 @@ public class CredentialsMerger {
|
|
|
System.err.println("Usage: " + CredentialsMerger.class.getCanonicalName()
|
|
|
+ " <dst> <src> ...");
|
|
|
}
|
|
|
-
|
|
|
}
|