1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.cli; 20 21 import java.io.File; 22 import java.io.IOException; 23 import java.security.NoSuchAlgorithmException; 24 import java.util.UUID; 25 26 import static org.junit.Assert.assertTrue; 27 28 import org.apache.hadoop.cli.util.CLICommand; 29 import org.apache.hadoop.cli.util.CLICommandCryptoAdmin; 30 import org.apache.hadoop.cli.util.CLICommandTypes; 31 import org.apache.hadoop.cli.util.CLITestCmd; 32 import org.apache.hadoop.cli.util.CryptoAdminCmdExecutor; 33 import org.apache.hadoop.cli.util.CommandExecutor; 34 import org.apache.hadoop.cli.util.CommandExecutor.Result; 35 import org.apache.hadoop.conf.Configuration; 36 import org.apache.hadoop.crypto.key.JavaKeyStoreProvider; 37 import org.apache.hadoop.crypto.key.KeyProvider; 38 import org.apache.hadoop.crypto.key.KeyProviderFactory; 39 import org.apache.hadoop.fs.FileSystem; 40 import org.apache.hadoop.fs.Path; 41 import org.apache.hadoop.hdfs.DFSConfigKeys; 42 import org.apache.hadoop.hdfs.DistributedFileSystem; 43 import org.apache.hadoop.hdfs.HDFSPolicyProvider; 44 import org.apache.hadoop.hdfs.MiniDFSCluster; 45 import org.apache.hadoop.hdfs.tools.CryptoAdmin; 46 import org.apache.hadoop.security.authorize.PolicyProvider; 47 import org.junit.After; 48 import org.junit.Before; 49 import org.junit.Test; 50 import org.xml.sax.SAXException; 51 52 public class TestCryptoAdminCLI extends CLITestHelperDFS { 53 protected MiniDFSCluster dfsCluster = null; 54 protected FileSystem fs = null; 55 protected String namenode = null; 56 private static File tmpDir; 57 58 @Before 59 @Override setUp()60 public void setUp() throws Exception { 61 super.setUp(); 62 conf.setClass(PolicyProvider.POLICY_PROVIDER_CONFIG, 63 HDFSPolicyProvider.class, PolicyProvider.class); 64 conf.setInt(DFSConfigKeys.DFS_REPLICATION_KEY, 1); 65 66 tmpDir = new File(System.getProperty("test.build.data", "target"), 67 UUID.randomUUID().toString()).getAbsoluteFile(); 68 final Path jksPath = new Path(tmpDir.toString(), "test.jks"); 69 conf.set(DFSConfigKeys.DFS_ENCRYPTION_KEY_PROVIDER_URI, 70 JavaKeyStoreProvider.SCHEME_NAME + "://file" + jksPath.toUri()); 71 72 dfsCluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build(); 73 dfsCluster.waitClusterUp(); 74 createAKey("mykey", conf); 75 namenode = conf.get(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "file:///"); 76 77 username = System.getProperty("user.name"); 78 79 fs = dfsCluster.getFileSystem(); 80 assertTrue("Not an HDFS: " + fs.getUri(), 81 fs instanceof DistributedFileSystem); 82 } 83 84 @After 85 @Override tearDown()86 public void tearDown() throws Exception { 87 if (fs != null) { 88 fs.close(); 89 } 90 if (dfsCluster != null) { 91 dfsCluster.shutdown(); 92 } 93 Thread.sleep(2000); 94 super.tearDown(); 95 } 96 97 /* Helper function to create a key in the Key Provider. */ createAKey(String keyName, Configuration conf)98 private void createAKey(String keyName, Configuration conf) 99 throws NoSuchAlgorithmException, IOException { 100 final KeyProvider provider = 101 dfsCluster.getNameNode().getNamesystem().getProvider(); 102 final KeyProvider.Options options = KeyProvider.options(conf); 103 provider.createKey(keyName, options); 104 provider.flush(); 105 } 106 107 @Override getTestFile()108 protected String getTestFile() { 109 return "testCryptoConf.xml"; 110 } 111 112 @Override expandCommand(final String cmd)113 protected String expandCommand(final String cmd) { 114 String expCmd = cmd; 115 expCmd = expCmd.replaceAll("NAMENODE", namenode); 116 expCmd = expCmd.replaceAll("#LF#", 117 System.getProperty("line.separator")); 118 expCmd = super.expandCommand(expCmd); 119 return expCmd; 120 } 121 122 @Override getConfigParser()123 protected TestConfigFileParser getConfigParser() { 124 return new TestConfigFileParserCryptoAdmin(); 125 } 126 127 private class TestConfigFileParserCryptoAdmin extends 128 CLITestHelper.TestConfigFileParser { 129 @Override endElement(String uri, String localName, String qName)130 public void endElement(String uri, String localName, String qName) 131 throws SAXException { 132 if (qName.equals("crypto-admin-command")) { 133 if (testCommands != null) { 134 testCommands.add(new CLITestCmdCryptoAdmin(charString, 135 new CLICommandCryptoAdmin())); 136 } else if (cleanupCommands != null) { 137 cleanupCommands.add(new CLITestCmdCryptoAdmin(charString, 138 new CLICommandCryptoAdmin())); 139 } 140 } else { 141 super.endElement(uri, localName, qName); 142 } 143 } 144 } 145 146 private class CLITestCmdCryptoAdmin extends CLITestCmd { CLITestCmdCryptoAdmin(String str, CLICommandTypes type)147 public CLITestCmdCryptoAdmin(String str, CLICommandTypes type) { 148 super(str, type); 149 } 150 151 @Override getExecutor(String tag)152 public CommandExecutor getExecutor(String tag) 153 throws IllegalArgumentException { 154 if (getType() instanceof CLICommandCryptoAdmin) { 155 return new CryptoAdminCmdExecutor(tag, new CryptoAdmin(conf)); 156 } 157 return super.getExecutor(tag); 158 } 159 } 160 161 @Override execute(CLICommand cmd)162 protected Result execute(CLICommand cmd) throws Exception { 163 return cmd.getExecutor(namenode).executeCommand(cmd.getCmd()); 164 } 165 166 @Test 167 @Override testAll()168 public void testAll () { 169 super.testAll(); 170 } 171 } 172