|
| 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.fs.s3a.auth; |
| 20 | + |
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.PrintStream; |
| 24 | +import java.net.URI; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | + |
| 27 | +import org.assertj.core.api.Assertions; |
| 28 | +import org.junit.AfterClass; |
| 29 | +import org.junit.Test; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import org.apache.hadoop.conf.Configuration; |
| 34 | +import org.apache.hadoop.fs.FileSystem; |
| 35 | +import org.apache.hadoop.fs.Path; |
| 36 | +import org.apache.hadoop.fs.s3a.AbstractS3ATestBase; |
| 37 | +import org.apache.hadoop.io.IOUtils; |
| 38 | +import org.apache.hadoop.security.UserGroupInformation; |
| 39 | +import org.apache.hadoop.security.alias.CredentialShell; |
| 40 | + |
| 41 | +import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_CREDENTIAL_PROVIDER_PATH; |
| 42 | +import static org.apache.hadoop.fs.s3a.Constants.S3A_SECURITY_CREDENTIAL_PROVIDER_PATH; |
| 43 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.disableFilesystemCaching; |
| 44 | +import static org.apache.hadoop.fs.s3a.S3ATestUtils.removeBaseAndBucketOverrides; |
| 45 | + |
| 46 | +/** |
| 47 | + * Test JCEKS file load/save on S3a. |
| 48 | + * Uses CredentialShell to better replicate the CLI. |
| 49 | + * |
| 50 | + * See HADOOP-17894. |
| 51 | + * This test is at risk of leaking FS instances in the JCEKS providers; |
| 52 | + * this is handled in an AfterClass operation. |
| 53 | + * |
| 54 | + */ |
| 55 | +public class ITestJceksIO extends AbstractS3ATestBase { |
| 56 | + private static final Logger LOG = LoggerFactory.getLogger( |
| 57 | + ITestJceksIO.class); |
| 58 | + private static final String UTF8 = StandardCharsets.UTF_8.name(); |
| 59 | + private PrintStream oldStdout, oldStderr; |
| 60 | + private ByteArrayOutputStream stdout, stderr; |
| 61 | + private PrintStream printStdout, printStderr; |
| 62 | + |
| 63 | + @Override |
| 64 | + public void setup() throws Exception { |
| 65 | + super.setup(); |
| 66 | + oldStdout = System.out; |
| 67 | + oldStderr = System.err; |
| 68 | + stdout = new ByteArrayOutputStream(); |
| 69 | + printStdout = new PrintStream(stdout); |
| 70 | + System.setOut(printStdout); |
| 71 | + |
| 72 | + stderr = new ByteArrayOutputStream(); |
| 73 | + printStderr = new PrintStream(stderr); |
| 74 | + System.setErr(printStderr); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void teardown() throws Exception { |
| 79 | + System.setOut(oldStdout); |
| 80 | + System.setErr(oldStderr); |
| 81 | + IOUtils.cleanupWithLogger(LOG, printStdout, printStderr); |
| 82 | + super.teardown(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Shut down all filesystems for this user to avoid |
| 87 | + * leaking those used by credential providers. |
| 88 | + */ |
| 89 | + @AfterClass |
| 90 | + public static void closeAllFilesystems() { |
| 91 | + try { |
| 92 | + LOG.info("Closing down all filesystems for current user"); |
| 93 | + FileSystem.closeAllForUGI(UserGroupInformation.getCurrentUser()); |
| 94 | + } catch (IOException e) { |
| 95 | + LOG.warn("UGI.getCurrentUser()", e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * FS config with no providers. FS caching is disabled. |
| 101 | + * @return a new configuration. |
| 102 | + */ |
| 103 | + private Configuration createNewConfiguration() { |
| 104 | + final Configuration conf = new Configuration(getConfiguration()); |
| 105 | + removeBaseAndBucketOverrides(conf, |
| 106 | + HADOOP_SECURITY_CREDENTIAL_PROVIDER_PATH, |
| 107 | + S3A_SECURITY_CREDENTIAL_PROVIDER_PATH); |
| 108 | + disableFilesystemCaching(conf); |
| 109 | + return conf; |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + /* |
| 114 | + * List credentials; expect the file to be missing. |
| 115 | + * hadoop credential list -provider jceks://s3a@bucket/s3.jceks |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testListMissingJceksFile() throws Throwable { |
| 119 | + final Path dir = path("jceks"); |
| 120 | + Path keystore = new Path(dir, "keystore.jceks"); |
| 121 | + String jceksProvider = toJceksProvider(keystore); |
| 122 | + |
| 123 | + CredentialShell cs = new CredentialShell(); |
| 124 | + |
| 125 | + cs.setConf(createNewConfiguration()); |
| 126 | + run(cs, null, |
| 127 | + "list", "-provider", jceksProvider); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testCredentialSuccessfulLifecycle() throws Exception { |
| 132 | + final Path dir = path("jceks"); |
| 133 | + Path keystore = new Path(dir, "keystore.jceks"); |
| 134 | + String jceksProvider = toJceksProvider(keystore); |
| 135 | + CredentialShell cs = new CredentialShell(); |
| 136 | + cs.setConf(createNewConfiguration()); |
| 137 | + run(cs, "credential1 has been successfully created.", "create", "credential1", "-value", |
| 138 | + "p@ssw0rd", "-provider", |
| 139 | + jceksProvider); |
| 140 | + |
| 141 | + assertIsFile(keystore); |
| 142 | + run(cs, "credential1", |
| 143 | + "list", "-provider", jceksProvider); |
| 144 | + |
| 145 | + run(cs, "credential1 has been successfully deleted.", |
| 146 | + "delete", "credential1", "-f", "-provider", |
| 147 | + jceksProvider); |
| 148 | + |
| 149 | + String[] args5 = { |
| 150 | + "list", "-provider", |
| 151 | + jceksProvider |
| 152 | + }; |
| 153 | + String out = run(cs, null, args5); |
| 154 | + Assertions.assertThat(out) |
| 155 | + .describedAs("Command result of list") |
| 156 | + .doesNotContain("credential1"); |
| 157 | + } |
| 158 | + |
| 159 | + private String run(CredentialShell cs, String expected, String... args) |
| 160 | + throws Exception { |
| 161 | + stdout.reset(); |
| 162 | + int rc = cs.run(args); |
| 163 | + final String out = stdout.toString(UTF8); |
| 164 | + LOG.error("{}", stderr.toString(UTF8)); |
| 165 | + LOG.info("{}", out); |
| 166 | + |
| 167 | + Assertions.assertThat(rc) |
| 168 | + .describedAs("Command result of %s with output %s", |
| 169 | + args[0], out) |
| 170 | + .isEqualTo(0); |
| 171 | + if (expected != null) { |
| 172 | + Assertions.assertThat(out) |
| 173 | + .describedAs("Command result of %s", args[0]) |
| 174 | + .contains(expected); |
| 175 | + } |
| 176 | + return out; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Convert a path to a jceks URI. |
| 181 | + * @param keystore store |
| 182 | + * @return string for the command line |
| 183 | + */ |
| 184 | + private String toJceksProvider(Path keystore) { |
| 185 | + final URI uri = keystore.toUri(); |
| 186 | + return String.format("jceks://%s@%s%s", |
| 187 | + uri.getScheme(), uri.getHost(), uri.getPath()); |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments