From 297e916f4187713384782fb5c6a6a0f7fbb8d4e2 Mon Sep 17 00:00:00 2001 From: Vinod Kumar Vavilapalli Date: Fri, 31 May 2013 19:48:30 +0000 Subject: [PATCH] MAPREDUCE-5275. Bring back a couple of APIs in mapreduce.security.TokenCache for binary compatibility with 1.x mapreduce APIs. Contributed by Mayank Bansal. svn merge --ignore-ancestry -c 1488369 ../../trunk/ git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-2@1488370 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-mapreduce-project/CHANGES.txt | 3 +++ .../hadoop/mapreduce/security/TokenCache.java | 25 +++++++++++++++++++ .../mapreduce/security/TestTokenCache.java | 25 +++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/hadoop-mapreduce-project/CHANGES.txt b/hadoop-mapreduce-project/CHANGES.txt index 7852ee693d9..0a705e10748 100644 --- a/hadoop-mapreduce-project/CHANGES.txt +++ b/hadoop-mapreduce-project/CHANGES.txt @@ -115,6 +115,9 @@ Release 2.0.5-beta - UNRELEASED MAPREDUCE-5176. Add annotation for tagging tasks as responsive to preemption. (Carlo Curino, cdouglas) + MAPREDUCE-5275. Bring back a couple of APIs in mapreduce.security.TokenCache + for binary compatibility with 1.x mapreduce APIs. (Mayank Bansal via vinodkv) + OPTIMIZATIONS MAPREDUCE-4974. Optimising the LineRecordReader initialize() method diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/security/TokenCache.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/security/TokenCache.java index bada8a1b4d7..47421f246fe 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/security/TokenCache.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/security/TokenCache.java @@ -177,6 +177,19 @@ public class TokenCache { } return ts; } + + /** + * load job token from a file + * + * @param conf + * @throws IOException + */ + @InterfaceAudience.Private + public static Credentials loadTokens(String jobTokenFile, Configuration conf) + throws IOException { + return loadTokens(jobTokenFile, new JobConf(conf)); + } + /** * store job token * @param t @@ -205,4 +218,16 @@ public class TokenCache { public static byte[] getShuffleSecretKey(Credentials credentials) { return getSecretKey(credentials, SHUFFLE_TOKEN); } + + /** + * + * @param namenode + * @return delegation token + */ + public static + Token getDelegationToken( + Credentials credentials, String namenode) { + return (Token) credentials.getToken(new Text( + namenode)); + } } diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapreduce/security/TestTokenCache.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapreduce/security/TestTokenCache.java index 8a84c293caa..14af7aa214d 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapreduce/security/TestTokenCache.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapreduce/security/TestTokenCache.java @@ -24,6 +24,7 @@ import static org.mockito.Mockito.*; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; @@ -168,4 +169,28 @@ public class TestTokenCache { TokenCache.cleanUpTokenReferral(conf); assertNull(conf.get(MRJobConfig.MAPREDUCE_JOB_CREDENTIALS_BINARY)); } + + @Test + public void testGetTokensForNamenodes() throws IOException, + URISyntaxException { + Path TEST_ROOT_DIR = + new Path(System.getProperty("test.build.data", "test/build/data")); + // ick, but need fq path minus file:/ + String binaryTokenFile = + FileSystem.getLocal(conf) + .makeQualified(new Path(TEST_ROOT_DIR, "tokenFile")).toUri() + .getPath(); + + MockFileSystem fs1 = createFileSystemForServiceName("service1"); + Credentials creds = new Credentials(); + Token token1 = fs1.getDelegationToken(renewer); + creds.addToken(token1.getService(), token1); + // wait to set, else the obtain tokens call above will fail with FNF + conf.set(MRJobConfig.MAPREDUCE_JOB_CREDENTIALS_BINARY, binaryTokenFile); + creds.writeTokenStorageFile(new Path(binaryTokenFile), conf); + TokenCache.obtainTokensForNamenodesInternal(fs1, creds, conf); + String fs_addr = fs1.getCanonicalServiceName(); + Token nnt = TokenCache.getDelegationToken(creds, fs_addr); + assertNotNull("Token for nn is null", nnt); + } } \ No newline at end of file