Updated Branches: refs/heads/master de24f8322 -> f0fcd6d11
http://git-wip-us.apache.org/repos/asf/accumulo/blob/f0fcd6d1/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBase.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBase.java b/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBase.java index e65079f..73405c5 100644 --- a/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBase.java +++ b/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBase.java @@ -43,16 +43,30 @@ import org.apache.log4j.Logger; * @since 1.5.0 */ public class ConfiguratorBase { - + /** * Configuration keys for {@link Instance#getConnector(String, AuthenticationToken)}. * * @since 1.5.0 */ public static enum ConnectorInfo { - IS_CONFIGURED, PRINCIPAL, TOKEN, TOKEN_CLASS, TOKEN_FILE + IS_CONFIGURED, PRINCIPAL, TOKEN, + } + + private static enum TokenSource { + FILE, INLINE; + + private String prefix; + + private TokenSource() { + prefix = name().toLowerCase() + ":"; + } + + public String prefix() { + return prefix; + } } - + /** * Configuration keys for {@link Instance}, {@link ZooKeeperInstance}, and {@link MockInstance}. * @@ -61,7 +75,7 @@ public class ConfiguratorBase { protected static enum InstanceOpts { TYPE, NAME, ZOO_KEEPERS; } - + /** * Configuration keys for general configuration options. * @@ -70,7 +84,7 @@ public class ConfiguratorBase { protected static enum GeneralOpts { LOG_LEVEL } - + /** * Provides a configuration key for a given feature enum, prefixed by the implementingClass * @@ -84,7 +98,7 @@ public class ConfiguratorBase { protected static String enumToConfKey(Class<?> implementingClass, Enum<?> e) { return implementingClass.getSimpleName() + "." + e.getDeclaringClass().getSimpleName() + "." + StringUtils.camelize(e.name().toLowerCase()); } - + /** * Sets the connector information needed to communicate with Accumulo in this job. * @@ -107,14 +121,14 @@ public class ConfiguratorBase { throws AccumuloSecurityException { if (isConnectorInfoSet(implementingClass, conf)) throw new IllegalStateException("Connector info for " + implementingClass.getSimpleName() + " can only be set once per job"); - + ArgumentChecker.notNull(principal, token); conf.setBoolean(enumToConfKey(implementingClass, ConnectorInfo.IS_CONFIGURED), true); conf.set(enumToConfKey(implementingClass, ConnectorInfo.PRINCIPAL), principal); - conf.set(enumToConfKey(implementingClass, ConnectorInfo.TOKEN_CLASS), token.getClass().getName()); - conf.set(enumToConfKey(implementingClass, ConnectorInfo.TOKEN), Base64.encodeBase64String(AuthenticationTokenSerializer.serialize(token))); + conf.set(enumToConfKey(implementingClass, ConnectorInfo.TOKEN), + TokenSource.INLINE.prefix() + token.getClass().getName() + ":" + Base64.encodeBase64String(AuthenticationTokenSerializer.serialize(token))); } - + /** * Sets the connector information needed to communicate with Accumulo in this job. * @@ -129,27 +143,27 @@ public class ConfiguratorBase { * @param principal * a valid Accumulo user name * @param tokenFile - * the path to the token file + * the path to the token file in DFS * @throws AccumuloSecurityException * @since 1.6.0 */ public static void setConnectorInfo(Class<?> implementingClass, Configuration conf, String principal, String tokenFile) throws AccumuloSecurityException { if (isConnectorInfoSet(implementingClass, conf)) throw new IllegalStateException("Connector info for " + implementingClass.getSimpleName() + " can only be set once per job"); - + ArgumentChecker.notNull(principal, tokenFile); - + try { DistributedCache.addCacheFile(new URI(tokenFile), conf); } catch (URISyntaxException e) { throw new IllegalStateException("Unable to add tokenFile \"" + tokenFile + "\" to distributed cache."); } - + conf.setBoolean(enumToConfKey(implementingClass, ConnectorInfo.IS_CONFIGURED), true); - conf.set(enumToConfKey(implementingClass, ConnectorInfo.TOKEN_FILE), tokenFile); conf.set(enumToConfKey(implementingClass, ConnectorInfo.PRINCIPAL), principal); + conf.set(enumToConfKey(implementingClass, ConnectorInfo.TOKEN), TokenSource.FILE.prefix() + tokenFile); } - + /** * Determines if the connector info has already been set for this instance. * @@ -164,7 +178,7 @@ public class ConfiguratorBase { public static Boolean isConnectorInfoSet(Class<?> implementingClass, Configuration conf) { return conf.getBoolean(enumToConfKey(implementingClass, ConnectorInfo.IS_CONFIGURED), false); } - + /** * Gets the user name from the configuration. * @@ -179,63 +193,35 @@ public class ConfiguratorBase { public static String getPrincipal(Class<?> implementingClass, Configuration conf) { return conf.get(enumToConfKey(implementingClass, ConnectorInfo.PRINCIPAL)); } - - /** - * Gets the serialized token class from either the configuration or the token file. - * - * @param implementingClass - * the class whose name will be used as a prefix for the property configuration key - * @param conf - * the Hadoop configuration object to configure - * @return the principal - * @since 1.5.0 - * @see #setConnectorInfo(Class, Configuration, String, AuthenticationToken) - */ - public static String getTokenClass(Class<?> implementingClass, Configuration conf) { - String tokenFile = getTokenFile(implementingClass, conf); - if (tokenFile.isEmpty()) { - return conf.get(enumToConfKey(implementingClass, ConnectorInfo.TOKEN_CLASS)); - } else { - return readTokenFile(implementingClass, conf).getToken().getClass().getName(); - } - } - + /** - * Gets the password from either the configuration or the token file. WARNING: If no token file is specified, the password is stored in the Configuration and - * shared with all MapReduce tasks; It is BASE64 encoded to provide a charset safe conversion to a string, and is not intended to be secure. + * Gets the authenticated token from either the specified token file or directly from the configuration, whichever was used when the job was configured. * * @param implementingClass * the class whose name will be used as a prefix for the property configuration key * @param conf * the Hadoop configuration object to configure - * @return the decoded principal's authentication token - * @since 1.5.0 - * @see #setConnectorInfo(Class, Configuration, String, AuthenticationToken) - */ - public static byte[] getToken(Class<?> implementingClass, Configuration conf) { - String tokenFile = getTokenFile(implementingClass, conf); - String token = null; - if (tokenFile.isEmpty()) { - token = conf.get(enumToConfKey(implementingClass, ConnectorInfo.TOKEN)); - return Base64.decodeBase64(token.getBytes(Constants.UTF8)); - } - - return AuthenticationTokenSerializer.serialize(readTokenFile(implementingClass, conf).getToken()); - } - - /** - * Grabs the token file's path out of the Configuration. - * - * @param conf - * the Hadoop context for the configured job - * @return path to the token file as a String + * @return the principal's authentication token * @since 1.6.0 * @see #setConnectorInfo(Class, Configuration, String, AuthenticationToken) + * @see #setConnectorInfo(Class, Configuration, String, String) */ - public static String getTokenFile(Class<?> implementingClass, Configuration conf) { - return conf.get(enumToConfKey(implementingClass, ConnectorInfo.TOKEN_FILE), ""); + public static AuthenticationToken getAuthenticationToken(Class<?> implementingClass, Configuration conf) { + String token = conf.get(enumToConfKey(implementingClass, ConnectorInfo.TOKEN)); + if (token == null || token.isEmpty()) + return null; + if (token.startsWith(TokenSource.INLINE.prefix())) { + String[] args = token.substring(TokenSource.INLINE.prefix().length()).split(":", 2); + if (args.length == 2) + return AuthenticationTokenSerializer.deserialize(args[0], Base64.decodeBase64(args[1].getBytes(Constants.UTF8))); + } else if (token.startsWith(TokenSource.FILE.prefix())) { + String tokenFileName = token.substring(TokenSource.FILE.prefix().length()); + return getTokenFromFile(conf, getPrincipal(implementingClass, conf), tokenFileName); + } + + throw new IllegalStateException("Token was not properly serialized into the configuration"); } - + /** * Reads from the token file in distributed cache. Currently, the token file stores data separated by colons e.g. principal:token_class:token * @@ -245,8 +231,7 @@ public class ConfiguratorBase { * @since 1.6.0 * @see #setConnectorInfo(Class, Configuration, String, AuthenticationToken) */ - private static Credentials readTokenFile(Class<?> implementingClass, Configuration conf) { - String tokenFile = getTokenFile(implementingClass, conf); + private static AuthenticationToken getTokenFromFile(Configuration conf, String principal, String tokenFile) { FSDataInputStream in = null; try { URI[] uris = DistributedCache.getCacheFiles(conf); @@ -266,11 +251,10 @@ public class ConfiguratorBase { } java.util.Scanner fileScanner = new java.util.Scanner(in); try { - String principal = getPrincipal(implementingClass, conf); while (fileScanner.hasNextLine()) { Credentials creds = Credentials.deserialize(fileScanner.nextLine()); if (principal.equals(creds.getPrincipal())) { - return creds; + return creds.getToken(); } } throw new IllegalArgumentException("Couldn't find token for user \"" + principal + "\" in file \"" + tokenFile + "\""); @@ -281,7 +265,7 @@ public class ConfiguratorBase { throw new RuntimeException(fileScanner.ioException()); } } - + /** * Configures a {@link ZooKeeperInstance} for this job. * @@ -300,12 +284,12 @@ public class ConfiguratorBase { if (!conf.get(key, "").isEmpty()) throw new IllegalStateException("Instance info can only be set once per job; it has already been configured with " + conf.get(key)); conf.set(key, "ZooKeeperInstance"); - + ArgumentChecker.notNull(instanceName, zooKeepers); conf.set(enumToConfKey(implementingClass, InstanceOpts.NAME), instanceName); conf.set(enumToConfKey(implementingClass, InstanceOpts.ZOO_KEEPERS), zooKeepers); } - + /** * Configures a {@link MockInstance} for this job. * @@ -322,11 +306,11 @@ public class ConfiguratorBase { if (!conf.get(key, "").isEmpty()) throw new IllegalStateException("Instance info can only be set once per job; it has already been configured with " + conf.get(key)); conf.set(key, "MockInstance"); - + ArgumentChecker.notNull(instanceName); conf.set(enumToConfKey(implementingClass, InstanceOpts.NAME), instanceName); } - + /** * Initializes an Accumulo {@link Instance} based on the configuration. * @@ -351,7 +335,7 @@ public class ConfiguratorBase { else throw new IllegalStateException("Unrecognized instance type " + instanceType); } - + /** * Sets the log level for this job. * @@ -368,7 +352,7 @@ public class ConfiguratorBase { Logger.getLogger(implementingClass).setLevel(level); conf.setInt(enumToConfKey(implementingClass, GeneralOpts.LOG_LEVEL), level.toInt()); } - + /** * Gets the log level from this configuration. * @@ -383,5 +367,5 @@ public class ConfiguratorBase { public static Level getLogLevel(Class<?> implementingClass, Configuration conf) { return Level.toLevel(conf.getInt(enumToConfKey(implementingClass, GeneralOpts.LOG_LEVEL), Level.INFO.toInt())); } - + } http://git-wip-us.apache.org/repos/asf/accumulo/blob/f0fcd6d1/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/InputConfigurator.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/InputConfigurator.java b/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/InputConfigurator.java index 9d7c878..a1c3f70 100644 --- a/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/InputConfigurator.java +++ b/core/src/main/java/org/apache/accumulo/core/client/mapreduce/lib/util/InputConfigurator.java @@ -41,7 +41,7 @@ import org.apache.accumulo.core.client.TableNotFoundException; import org.apache.accumulo.core.client.impl.Tables; import org.apache.accumulo.core.client.impl.TabletLocator; import org.apache.accumulo.core.client.mock.MockTabletLocator; -import org.apache.accumulo.core.client.security.tokens.AuthenticationToken.AuthenticationTokenSerializer; +import org.apache.accumulo.core.client.security.tokens.AuthenticationToken; import org.apache.accumulo.core.data.Range; import org.apache.accumulo.core.iterators.SortedKeyValueIterator; import org.apache.accumulo.core.security.Authorizations; @@ -58,7 +58,7 @@ import org.apache.hadoop.util.StringUtils; * @since 1.5.0 */ public class InputConfigurator extends ConfiguratorBase { - + /** * Configuration keys for {@link Scanner}. * @@ -67,7 +67,7 @@ public class InputConfigurator extends ConfiguratorBase { public static enum ScanOpts { TABLE_NAME, AUTHORIZATIONS, RANGES, COLUMNS, ITERATORS } - + /** * Configuration keys for various features. * @@ -76,7 +76,7 @@ public class InputConfigurator extends ConfiguratorBase { public static enum Features { AUTO_ADJUST_RANGES, SCAN_ISOLATION, USE_LOCAL_ITERATORS, SCAN_OFFLINE } - + /** * Sets the name of the input table, over which this job will scan. * @@ -92,7 +92,7 @@ public class InputConfigurator extends ConfiguratorBase { ArgumentChecker.notNull(tableName); conf.set(enumToConfKey(implementingClass, ScanOpts.TABLE_NAME), tableName); } - + /** * Gets the table name from the configuration. * @@ -107,7 +107,7 @@ public class InputConfigurator extends ConfiguratorBase { public static String getInputTableName(Class<?> implementingClass, Configuration conf) { return conf.get(enumToConfKey(implementingClass, ScanOpts.TABLE_NAME)); } - + /** * Sets the {@link Authorizations} used to scan. Must be a subset of the user's authorization. Defaults to the empty set. * @@ -123,7 +123,7 @@ public class InputConfigurator extends ConfiguratorBase { if (auths != null && !auths.isEmpty()) conf.set(enumToConfKey(implementingClass, ScanOpts.AUTHORIZATIONS), auths.serialize()); } - + /** * Gets the authorizations to set for the scans from the configuration. * @@ -139,7 +139,7 @@ public class InputConfigurator extends ConfiguratorBase { String authString = conf.get(enumToConfKey(implementingClass, ScanOpts.AUTHORIZATIONS)); return authString == null ? Authorizations.EMPTY : new Authorizations(authString.getBytes()); } - + /** * Sets the input ranges to scan for this job. If not set, the entire table will be scanned. * @@ -165,7 +165,7 @@ public class InputConfigurator extends ConfiguratorBase { } conf.setStrings(enumToConfKey(implementingClass, ScanOpts.RANGES), rangeStrings.toArray(new String[0])); } - + /** * Gets the ranges to scan over from a job. * @@ -189,7 +189,7 @@ public class InputConfigurator extends ConfiguratorBase { } return ranges; } - + /** * Restricts the columns that will be mapped over for this job. * @@ -208,7 +208,7 @@ public class InputConfigurator extends ConfiguratorBase { for (Pair<Text,Text> column : columnFamilyColumnQualifierPairs) { if (column.getFirst() == null) throw new IllegalArgumentException("Column family can not be null"); - + String col = new String(Base64.encodeBase64(TextUtil.getBytes(column.getFirst())), Constants.UTF8); if (column.getSecond() != null) col += ":" + new String(Base64.encodeBase64(TextUtil.getBytes(column.getSecond())), Constants.UTF8); @@ -216,7 +216,7 @@ public class InputConfigurator extends ConfiguratorBase { } conf.setStrings(enumToConfKey(implementingClass, ScanOpts.COLUMNS), columnStrings.toArray(new String[0])); } - + /** * Gets the columns to be mapped over from this job. * @@ -238,7 +238,7 @@ public class InputConfigurator extends ConfiguratorBase { } return columns; } - + /** * Encode an iterator on the input for this job. * @@ -260,7 +260,7 @@ public class InputConfigurator extends ConfiguratorBase { } catch (IOException e) { throw new IllegalArgumentException("unable to serialize IteratorSetting"); } - + String iterators = conf.get(enumToConfKey(implementingClass, ScanOpts.ITERATORS)); // No iterators specified yet, create a new string if (iterators == null || iterators.isEmpty()) { @@ -272,7 +272,7 @@ public class InputConfigurator extends ConfiguratorBase { // Store the iterators w/ the job conf.set(enumToConfKey(implementingClass, ScanOpts.ITERATORS), iterators); } - + /** * Gets a list of the iterator settings (for iterators to apply to a scanner) from this configuration. * @@ -286,11 +286,11 @@ public class InputConfigurator extends ConfiguratorBase { */ public static List<IteratorSetting> getIterators(Class<?> implementingClass, Configuration conf) { String iterators = conf.get(enumToConfKey(implementingClass, ScanOpts.ITERATORS)); - + // If no iterators are present, return an empty list if (iterators == null || iterators.isEmpty()) return new ArrayList<IteratorSetting>(); - + // Compose the set of iterators encoded in the job configuration StringTokenizer tokens = new StringTokenizer(iterators, StringUtils.COMMA_STR); List<IteratorSetting> list = new ArrayList<IteratorSetting>(); @@ -306,7 +306,7 @@ public class InputConfigurator extends ConfiguratorBase { } return list; } - + /** * Controls the automatic adjustment of ranges for this job. This feature merges overlapping ranges, then splits them to align with tablet boundaries. * Disabling this feature will cause exactly one Map task to be created for each specified range. The default setting is enabled. * @@ -326,7 +326,7 @@ public class InputConfigurator extends ConfiguratorBase { public static void setAutoAdjustRanges(Class<?> implementingClass, Configuration conf, boolean enableFeature) { conf.setBoolean(enumToConfKey(implementingClass, Features.AUTO_ADJUST_RANGES), enableFeature); } - + /** * Determines whether a configuration has auto-adjust ranges enabled. * @@ -341,7 +341,7 @@ public class InputConfigurator extends ConfiguratorBase { public static Boolean getAutoAdjustRanges(Class<?> implementingClass, Configuration conf) { return conf.getBoolean(enumToConfKey(implementingClass, Features.AUTO_ADJUST_RANGES), true); } - + /** * Controls the use of the {@link IsolatedScanner} in this job. * @@ -359,7 +359,7 @@ public class InputConfigurator extends ConfiguratorBase { public static void setScanIsolation(Class<?> implementingClass, Configuration conf, boolean enableFeature) { conf.setBoolean(enumToConfKey(implementingClass, Features.SCAN_ISOLATION), enableFeature); } - + /** * Determines whether a configuration has isolation enabled. * @@ -374,7 +374,7 @@ public class InputConfigurator extends ConfiguratorBase { public static Boolean isIsolated(Class<?> implementingClass, Configuration conf) { return conf.getBoolean(enumToConfKey(implementingClass, Features.SCAN_ISOLATION), false); } - + /** * Controls the use of the {@link ClientSideIteratorScanner} in this job. Enabling this feature will cause the iterator stack to be constructed within the Map * task, rather than within the Accumulo TServer. To use this feature, all classes needed for those iterators must be available on the classpath for the task. @@ -393,7 +393,7 @@ public class InputConfigurator extends ConfiguratorBase { public static void setLocalIterators(Class<?> implementingClass, Configuration conf, boolean enableFeature) { conf.setBoolean(enumToConfKey(implementingClass, Features.USE_LOCAL_ITERATORS), enableFeature); } - + /** * Determines whether a configuration uses local iterators. * @@ -408,7 +408,7 @@ public class InputConfigurator extends ConfiguratorBase { public static Boolean usesLocalIterators(Class<?> implementingClass, Configuration conf) { return conf.getBoolean(enumToConfKey(implementingClass, Features.USE_LOCAL_ITERATORS), false); } - + /** * <p> * Enable reading offline tables. By default, this feature is disabled and only online tables are scanned. This will make the map reduce job directly read the @@ -445,7 +445,7 @@ public class InputConfigurator extends ConfiguratorBase { public static void setOfflineTableScan(Class<?> implementingClass, Configuration conf, boolean enableFeature) { conf.setBoolean(enumToConfKey(implementingClass, Features.SCAN_OFFLINE), enableFeature); } - + /** * Determines whether a configuration has the offline table scan feature enabled. * @@ -460,7 +460,7 @@ public class InputConfigurator extends ConfiguratorBase { public static Boolean isOfflineScan(Class<?> implementingClass, Configuration conf) { return conf.getBoolean(enumToConfKey(implementingClass, Features.SCAN_OFFLINE), false); } - + /** * Initializes an Accumulo {@link TabletLocator} based on the configuration. * @@ -481,7 +481,7 @@ public class InputConfigurator extends ConfiguratorBase { String tableName = getInputTableName(implementingClass, conf); return TabletLocator.getLocator(instance, new Text(Tables.getTableId(instance, tableName))); } - + // InputFormat doesn't have the equivalent of OutputFormat's checkOutputSpecs(JobContext job) /** * Check whether a configuration is fully configured to be used with an Accumulo {@link org.apache.hadoop.mapreduce.InputFormat}. @@ -502,14 +502,14 @@ public class InputConfigurator extends ConfiguratorBase { throw new IOException("Instance info has not been set."); // validate that we can connect as configured try { - Connector c = getInstance(implementingClass, conf).getConnector(getPrincipal(implementingClass, conf), - AuthenticationTokenSerializer.deserialize(getTokenClass(implementingClass, conf), getToken(implementingClass, conf))); - if (!c.securityOperations().authenticateUser(getPrincipal(implementingClass, conf), - AuthenticationTokenSerializer.deserialize(getTokenClass(implementingClass, conf), getToken(implementingClass, conf)))) + String principal = getPrincipal(implementingClass, conf); + AuthenticationToken token = getAuthenticationToken(implementingClass, conf); + Connector c = getInstance(implementingClass, conf).getConnector(principal, token); + if (!c.securityOperations().authenticateUser(principal, token)) throw new IOException("Unable to authenticate user"); - if (!c.securityOperations().hasTablePermission(getPrincipal(implementingClass, conf), getInputTableName(implementingClass, conf), TablePermission.READ)) + if (!c.securityOperations().hasTablePermission(principal, getInputTableName(implementingClass, conf), TablePermission.READ)) throw new IOException("Unable to access table"); - + if (!conf.getBoolean(enumToConfKey(implementingClass, Features.USE_LOCAL_ITERATORS), false)) { // validate that any scan-time iterators can be loaded by the the tablet servers for (IteratorSetting iter : getIterators(implementingClass, conf)) { @@ -517,7 +517,7 @@ public class InputConfigurator extends ConfiguratorBase { throw new AccumuloException("Servers are unable to load " + iter.getIteratorClass() + " as a " + SortedKeyValueIterator.class.getName()); } } - + } catch (AccumuloException e) { throw new IOException(e); } catch (AccumuloSecurityException e) { @@ -526,5 +526,5 @@ public class InputConfigurator extends ConfiguratorBase { throw new IOException(e); } } - + } http://git-wip-us.apache.org/repos/asf/accumulo/blob/f0fcd6d1/core/src/test/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBaseTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBaseTest.java b/core/src/test/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBaseTest.java new file mode 100644 index 0000000..62564fa --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/client/mapreduce/lib/util/ConfiguratorBaseTest.java @@ -0,0 +1,119 @@ +/* + * 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.accumulo.core.client.mapreduce.lib.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.apache.accumulo.core.client.AccumuloSecurityException; +import org.apache.accumulo.core.client.Instance; +import org.apache.accumulo.core.client.ZooKeeperInstance; +import org.apache.accumulo.core.client.mock.MockInstance; +import org.apache.accumulo.core.client.security.tokens.AuthenticationToken; +import org.apache.accumulo.core.client.security.tokens.AuthenticationToken.AuthenticationTokenSerializer; +import org.apache.accumulo.core.client.security.tokens.PasswordToken; +import org.apache.commons.codec.binary.Base64; +import org.apache.hadoop.conf.Configuration; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.junit.Test; + +/** + * + */ +public class ConfiguratorBaseTest { + + private static enum PrivateTestingEnum { + SOMETHING, SOMETHING_ELSE + } + + @Test + public void testEnumToConfKey() { + assertEquals(this.getClass().getSimpleName() + ".PrivateTestingEnum.Something", + ConfiguratorBase.enumToConfKey(this.getClass(), PrivateTestingEnum.SOMETHING)); + assertEquals(this.getClass().getSimpleName() + ".PrivateTestingEnum.SomethingElse", + ConfiguratorBase.enumToConfKey(this.getClass(), PrivateTestingEnum.SOMETHING_ELSE)); + } + + @Test + public void testSetConnectorInfoClassOfQConfigurationStringAuthenticationToken() throws AccumuloSecurityException { + Configuration conf = new Configuration(); + assertFalse(ConfiguratorBase.isConnectorInfoSet(this.getClass(), conf)); + ConfiguratorBase.setConnectorInfo(this.getClass(), conf, "testUser", new PasswordToken("testPassword")); + assertTrue(ConfiguratorBase.isConnectorInfoSet(this.getClass(), conf)); + assertEquals("testUser", ConfiguratorBase.getPrincipal(this.getClass(), conf)); + AuthenticationToken token = ConfiguratorBase.getAuthenticationToken(this.getClass(), conf); + assertEquals(PasswordToken.class, token.getClass()); + assertEquals(new PasswordToken("testPassword"), token); + assertEquals( + "inline:" + PasswordToken.class.getName() + ":" + Base64.encodeBase64String(AuthenticationTokenSerializer.serialize(new PasswordToken("testPassword"))), + conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.ConnectorInfo.TOKEN))); + } + + @Test + public void testSetConnectorInfoClassOfQConfigurationStringString() throws AccumuloSecurityException { + Configuration conf = new Configuration(); + assertFalse(ConfiguratorBase.isConnectorInfoSet(this.getClass(), conf)); + ConfiguratorBase.setConnectorInfo(this.getClass(), conf, "testUser", "testFile"); + assertTrue(ConfiguratorBase.isConnectorInfoSet(this.getClass(), conf)); + assertEquals("testUser", ConfiguratorBase.getPrincipal(this.getClass(), conf)); + assertEquals("file:testFile", conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.ConnectorInfo.TOKEN))); + } + + @Test + public void testSetZooKeeperInstance() { + Configuration conf = new Configuration(); + ConfiguratorBase.setZooKeeperInstance(this.getClass(), conf, "testInstanceName", "testZooKeepers"); + assertEquals("testInstanceName", conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.InstanceOpts.NAME))); + assertEquals("testZooKeepers", conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.InstanceOpts.ZOO_KEEPERS))); + assertEquals(ZooKeeperInstance.class.getSimpleName(), conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.InstanceOpts.TYPE))); + // TODO uncomment this after ACCUMULO-1699 + // Instance instance = ConfiguratorBase.getInstance(this.getClass(), conf); + // assertEquals(ZooKeeperInstance.class.getName(), instance.getClass().getName()); + } + + @Test + public void testSetMockInstance() { + Configuration conf = new Configuration(); + ConfiguratorBase.setMockInstance(this.getClass(), conf, "testInstanceName"); + assertEquals("testInstanceName", conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.InstanceOpts.NAME))); + assertEquals(null, conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.InstanceOpts.ZOO_KEEPERS))); + assertEquals(MockInstance.class.getSimpleName(), conf.get(ConfiguratorBase.enumToConfKey(this.getClass(), ConfiguratorBase.InstanceOpts.TYPE))); + Instance instance = ConfiguratorBase.getInstance(this.getClass(), conf); + assertEquals(MockInstance.class.getName(), instance.getClass().getName()); + } + + @Test + public void testSetLogLevel() { + Configuration conf = new Configuration(); + Level currentLevel = Logger.getLogger(this.getClass()).getLevel(); + + ConfiguratorBase.setLogLevel(this.getClass(), conf, Level.DEBUG); + Logger.getLogger(this.getClass()).setLevel(currentLevel); + assertEquals(Level.DEBUG, ConfiguratorBase.getLogLevel(this.getClass(), conf)); + + ConfiguratorBase.setLogLevel(this.getClass(), conf, Level.INFO); + Logger.getLogger(this.getClass()).setLevel(currentLevel); + assertEquals(Level.INFO, ConfiguratorBase.getLogLevel(this.getClass(), conf)); + + ConfiguratorBase.setLogLevel(this.getClass(), conf, Level.FATAL); + Logger.getLogger(this.getClass()).setLevel(currentLevel); + assertEquals(Level.FATAL, ConfiguratorBase.getLogLevel(this.getClass(), conf)); + } + +}
