diff --git beeline/pom.xml beeline/pom.xml index 5503add..58ca92e 100644 --- beeline/pom.xml +++ beeline/pom.xml @@ -29,6 +29,7 @@ .. + 1.6.6 @@ -119,8 +120,15 @@ test - org.mockito - mockito-all + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} test diff --git beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java index a981bce..ea58776 100644 --- beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java +++ beeline/src/java/org/apache/hive/beeline/HiveSchemaTool.java @@ -815,38 +815,10 @@ private void runBeeLine(String scriptDir, String scriptFile) // Generate the beeline args per hive conf and execute the given script public void runBeeLine(String sqlScriptFile) throws IOException { - List argList = new ArrayList(); - argList.add("-u"); - argList.add(HiveSchemaHelper.getValidConfVar( - ConfVars.METASTORECONNECTURLKEY, hiveConf)); - argList.add("-d"); - argList.add(HiveSchemaHelper.getValidConfVar( - ConfVars.METASTORE_CONNECTION_DRIVER, hiveConf)); - argList.add("-n"); - argList.add(userName); - argList.add("-p"); - argList.add(passWord); - argList.add("-f"); - argList.add(sqlScriptFile); - - if (LOG.isDebugEnabled()) { - LOG.debug("Going to invoke file that contains:"); - BufferedReader reader = new BufferedReader(new FileReader(sqlScriptFile)); - try { - String line; - while ((line = reader.readLine()) != null) { - LOG.debug("script: " + line); - } - } finally { - if (reader != null) { - reader.close(); - } - } - } + CommandBuilder builder = new CommandBuilder(hiveConf, userName, passWord, sqlScriptFile); // run the script using Beeline - BeeLine beeLine = new BeeLine(); - try { + try (BeeLine beeLine = new BeeLine()) { if (!verbose) { beeLine.setOutputStream(new PrintStream(new NullOutputStream())); beeLine.getOpts().setSilent(true); @@ -856,13 +828,53 @@ public void runBeeLine(String sqlScriptFile) throws IOException { // We can be pretty sure that an entire line can be processed as a single command since // we always add a line separator at the end while calling dbCommandParser.buildCommand. beeLine.getOpts().setEntireLineAsCommand(true); - LOG.debug("Going to run command <" + StringUtils.join(argList, " ") + ">"); - int status = beeLine.begin(argList.toArray(new String[0]), null); + LOG.debug("Going to run command <" + builder.buildToLog() + ">"); + int status = beeLine.begin(builder.buildToRun(), null); if (status != 0) { throw new IOException("Schema script failed, errorcode " + status); } - } finally { - beeLine.close(); + } + } + + static class CommandBuilder { + private final HiveConf hiveConf; + private final String userName; + private final String password; + private final String sqlScriptFile; + + CommandBuilder(HiveConf hiveConf, String userName, String password, String sqlScriptFile) { + this.hiveConf = hiveConf; + this.userName = userName; + this.password = password; + this.sqlScriptFile = sqlScriptFile; + } + + String[] buildToRun() throws IOException { + return argsWith(password); + } + + String buildToLog() throws IOException { + logScript(); + return StringUtils.join(argsWith(BeeLine.PASSWD_MASK), " "); + } + + private String[] argsWith(String password) throws IOException { + return new String[] { "-u", + HiveSchemaHelper.getValidConfVar(ConfVars.METASTORECONNECTURLKEY, hiveConf), "-d", + HiveSchemaHelper.getValidConfVar(ConfVars.METASTORE_CONNECTION_DRIVER, hiveConf), "-n", + userName, "-p", password, "-f", sqlScriptFile }; + } + + private void logScript() throws IOException { + if (LOG.isDebugEnabled()) { + LOG.debug("Going to invoke file that contains:"); + try (BufferedReader reader = new BufferedReader(new FileReader(sqlScriptFile))) { + String line; + while ((line = reader.readLine()) != null) { + LOG.debug("script: " + line); + } + } + } } } diff --git beeline/src/test/org/apache/hive/beeline/TestHiveSchemaTool.java beeline/src/test/org/apache/hive/beeline/TestHiveSchemaTool.java new file mode 100644 index 0000000..8d386da --- /dev/null +++ beeline/src/test/org/apache/hive/beeline/TestHiveSchemaTool.java @@ -0,0 +1,73 @@ +package org.apache.hive.beeline; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.same; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; + +@RunWith(PowerMockRunner.class) +@PowerMockIgnore("javax.management.*") +@PrepareForTest({ HiveSchemaHelper.class, HiveSchemaTool.CommandBuilder.class }) +public class TestHiveSchemaTool { + + String scriptFile = System.getProperty("java.io.tmpdir") + File.separator + "someScript.sql"; + @Mock + private HiveConf hiveConf; + private HiveSchemaTool.CommandBuilder builder; + private String pasword = "reallySimplePassword"; + + @Before + public void setup() throws IOException { + mockStatic(HiveSchemaHelper.class); + when(HiveSchemaHelper + .getValidConfVar(eq(HiveConf.ConfVars.METASTORECONNECTURLKEY), same(hiveConf))) + .thenReturn("someURL"); + when(HiveSchemaHelper + .getValidConfVar(eq(HiveConf.ConfVars.METASTORE_CONNECTION_DRIVER), same(hiveConf))) + .thenReturn("someDriver"); + + File file = new File(scriptFile); + if (!file.exists()) { + file.createNewFile(); + } + builder = new HiveSchemaTool.CommandBuilder(hiveConf, "testUser", pasword, scriptFile); + } + + @After + public void globalAssert() throws IOException { + verifyStatic(); + HiveSchemaHelper.getValidConfVar(eq(HiveConf.ConfVars.METASTORECONNECTURLKEY), same(hiveConf)); + HiveSchemaHelper + .getValidConfVar(eq(HiveConf.ConfVars.METASTORE_CONNECTION_DRIVER), same(hiveConf)); + + new File(scriptFile).delete(); + } + + @Test + public void shouldReturnStrippedPassword() throws IOException { + assertFalse(builder.buildToLog().contains(pasword)); + } + + @Test + public void shouldReturnActualPassword() throws IOException { + String[] strings = builder.buildToRun(); + assertTrue(Arrays.asList(strings).contains(pasword)); + } +} \ No newline at end of file