Repository: incubator-slider Updated Branches: refs/heads/develop 48975be4e -> f95ba34a4
SLIDER-1140: KDiag can't handle a % sign in a krb5.conf file Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/f95ba34a Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/f95ba34a Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/f95ba34a Branch: refs/heads/develop Commit: f95ba34a4692b4d6f596cb05a6a23929c74b31ef Parents: 48975be Author: Steve Loughran <ste...@apache.org> Authored: Thu Jun 9 14:17:14 2016 +0100 Committer: Steve Loughran <ste...@apache.org> Committed: Thu Jun 9 14:17:14 2016 +0100 ---------------------------------------------------------------------- .../apache/hadoop/security/KerberosDiags.java | 47 ++++++++++++++---- .../apache/slider/client/TestDiagnostics.groovy | 52 ++++++++++++++++++-- 2 files changed, 87 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/f95ba34a/slider-core/src/main/java/org/apache/hadoop/security/KerberosDiags.java ---------------------------------------------------------------------- diff --git a/slider-core/src/main/java/org/apache/hadoop/security/KerberosDiags.java b/slider-core/src/main/java/org/apache/hadoop/security/KerberosDiags.java index 1747a2b..8c572b3 100644 --- a/slider-core/src/main/java/org/apache/hadoop/security/KerberosDiags.java +++ b/slider-core/src/main/java/org/apache/hadoop/security/KerberosDiags.java @@ -18,6 +18,7 @@ package org.apache.hadoop.security; +import com.google.common.annotations.VisibleForTesting; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; @@ -316,11 +317,11 @@ public class KerberosDiags implements Closeable { } /** - * Dump a keytab: list all principals + * Dump a keytab: list all principals. * @param keytabFile the keytab file * @throws IOException IO problems */ - private void dumpKeytab(File keytabFile) throws IOException { + public void dumpKeytab(File keytabFile) throws IOException { title("Examining keytab %s", keytabFile); File kt = keytabFile.getCanonicalFile(); failif(!kt.exists(), CAT_CONFIG, "Keytab not found: %s", kt); @@ -513,15 +514,27 @@ public class KerberosDiags implements Closeable { } /** - * Print a line of output. This goes to any output file, or + * Format and print a line of output. + * This goes to any output file, or * is logged at info. The output is flushed before and after, to * try and stay in sync with JRE logging. * @param format format string * @param args any arguments */ - private void println(String format, Object... args) { + @VisibleForTesting + public void println(String format, Object... args) { + println(format(format, args)); + } + + /** + * Print a line of output. This goes to any output file, or + * is logged at info. The output is flushed before and after, to + * try and stay in sync with JRE logging. + * @param msg message string + */ + @VisibleForTesting + private void println(String msg) { flush(); - String msg = String.format(format, args); if (out != null) { out.println(msg); } else { @@ -538,7 +551,7 @@ public class KerberosDiags implements Closeable { private void title(String format, Object... args) { println(""); println(""); - String msg = "== " + String.format(format, args) + " =="; + String msg = "== " + format(format, args) + " =="; println(msg); println(""); } @@ -575,10 +588,10 @@ public class KerberosDiags implements Closeable { * @param file file to dump * @throws IOException IO problems */ - private void dump(File file) throws IOException { + public void dump(File file) throws IOException { try (FileInputStream in = new FileInputStream(file)) { for (String line : IOUtils.readLines(in)) { - println(line); + println("%s", line); } } println(""); @@ -617,6 +630,22 @@ public class KerberosDiags implements Closeable { } /** + * Format a string, treating a call where there are no varags values + * as a string to pass through unformatted. + * @param message message, which is either a format string + args, or + * a general string + * @param args argument array + * @return a string for printing. + */ + public static String format(String message, Object... args) { + if (args.length == 0) { + return message; + } else { + return String.format(message, args); + } + } + + /** * Diagnostics failures return the exit code 41, "unauthorized". * * They have a category, initially for testing: the category can be @@ -631,7 +660,7 @@ public class KerberosDiags implements Closeable { } public KerberosDiagsFailure(String category, String message, Object... args) { - this(category, String.format(message, args)); + this(category, format(message, args)); } public KerberosDiagsFailure(String category, Throwable throwable, http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/f95ba34a/slider-core/src/test/groovy/org/apache/slider/client/TestDiagnostics.groovy ---------------------------------------------------------------------- diff --git a/slider-core/src/test/groovy/org/apache/slider/client/TestDiagnostics.groovy b/slider-core/src/test/groovy/org/apache/slider/client/TestDiagnostics.groovy index a20ab23..81c4daf 100644 --- a/slider-core/src/test/groovy/org/apache/slider/client/TestDiagnostics.groovy +++ b/slider-core/src/test/groovy/org/apache/slider/client/TestDiagnostics.groovy @@ -18,8 +18,10 @@ package org.apache.slider.client -import groovy.transform.CompileStatic import groovy.util.logging.Slf4j +import org.apache.hadoop.conf.Configuration +import org.apache.hadoop.fs.FileUtil +import org.apache.hadoop.security.KerberosDiags import org.apache.hadoop.yarn.conf.YarnConfiguration import static org.apache.slider.common.Constants.SUN_SECURITY_KRB5_DEBUG import org.apache.slider.common.params.ActionDiagnosticArgs @@ -28,8 +30,6 @@ import org.apache.slider.common.params.ClientArgs import org.apache.slider.common.params.SliderActions import org.apache.slider.common.tools.SliderUtils import org.apache.slider.core.main.ServiceLauncher -import org.apache.slider.test.SliderTestBase -import org.apache.slider.test.YarnMiniClusterTestBase import org.apache.slider.test.YarnZKMiniClusterTestBase import org.junit.Test @@ -72,4 +72,50 @@ class TestDiagnostics extends YarnZKMiniClusterTestBase { assert 0 == launcher.serviceExitCode } + @Test + public void testKDiagExceptionConstruction() throws Throwable { + assert new KerberosDiags.KerberosDiagsFailure("CAT", "%02d", 3).toString().contains("03") + assert new KerberosDiags.KerberosDiagsFailure("CAT", "%w").toString().contains("%w") + assert new KerberosDiags.KerberosDiagsFailure("CAT", new Exception(), "%w") + .toString().contains("%w") + } + + @Test + public void testKDiagPrintln() throws Throwable { + assert "%w" == KerberosDiags.format("%w") + assert "%s" == KerberosDiags.format("%s") + assert "false" == KerberosDiags.format("%s", false) + def sw = new StringWriter() + def kdiag = new KerberosDiags(new Configuration(), + new PrintWriter(sw), [], null, "self", 16, false) + try { + kdiag.println("%02d", 3) + kdiag.println("%s") + kdiag.println("%w") + } finally { + kdiag.close() + } + def output = sw.toString() + assert output.contains("03") + assert output.contains("%s") + assert output.contains("%w") + } + + @Test + public void testKDiagDumpFile() throws Throwable { + def file1 = new File("./target/kdiaginput.txt") + + def s = 'invalid %w string %s' + file1 << s + def sw = new StringWriter() + def kdiag = new KerberosDiags(new Configuration(), + new PrintWriter(sw), [], null, "self", 16, false) + try { + kdiag.dump(file1) + } finally { + kdiag.close() + } + def output = sw.toString() + assert output.contains(s) + } }