|
| 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; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | + |
| 23 | +import com.amazonaws.ClientConfiguration; |
| 24 | +import com.amazonaws.Protocol; |
| 25 | +import org.assertj.core.api.Assertions; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import org.apache.hadoop.conf.Configuration; |
| 29 | +import org.apache.hadoop.test.AbstractHadoopTestBase; |
| 30 | + |
| 31 | +import static org.apache.hadoop.fs.s3a.Constants.PROXY_HOST; |
| 32 | +import static org.apache.hadoop.fs.s3a.Constants.PROXY_PORT; |
| 33 | +import static org.apache.hadoop.fs.s3a.Constants.PROXY_SECURED; |
| 34 | +import static org.apache.hadoop.fs.s3a.S3AUtils.initProxySupport; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests to verify {@link S3AUtils} translates the proxy configurations |
| 38 | + * are set correctly to Client configurations which are later used to construct |
| 39 | + * the proxy in AWS SDK. |
| 40 | + */ |
| 41 | +public class TestS3AProxy extends AbstractHadoopTestBase { |
| 42 | + |
| 43 | + /** |
| 44 | + * Verify Http proxy protocol. |
| 45 | + */ |
| 46 | + @Test |
| 47 | + public void testProxyHttp() throws IOException { |
| 48 | + Configuration proxyConfigForHttp = createProxyConfig(false); |
| 49 | + verifyProxy(proxyConfigForHttp, false); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Verify Https proxy protocol. |
| 54 | + */ |
| 55 | + @Test |
| 56 | + public void testProxyHttps() throws IOException { |
| 57 | + Configuration proxyConfigForHttps = createProxyConfig(true); |
| 58 | + verifyProxy(proxyConfigForHttps, true); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Verify default proxy protocol. |
| 63 | + */ |
| 64 | + @Test |
| 65 | + public void testProxyDefault() throws IOException { |
| 66 | + Configuration proxyConfigDefault = new Configuration(); |
| 67 | + proxyConfigDefault.set(PROXY_HOST, "testProxyDefault"); |
| 68 | + verifyProxy(proxyConfigDefault, false); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Assert that the configuration set for a proxy gets translated to Client |
| 73 | + * configuration with the correct protocol to be used by AWS SDK. |
| 74 | + * @param proxyConfig Configuration used to set the proxy configs. |
| 75 | + * @param isExpectedSecured What is the expected protocol for the proxy to |
| 76 | + * be? true for https, and false for http. |
| 77 | + * @throws IOException |
| 78 | + */ |
| 79 | + private void verifyProxy(Configuration proxyConfig, |
| 80 | + boolean isExpectedSecured) |
| 81 | + throws IOException { |
| 82 | + ClientConfiguration awsConf = new ClientConfiguration(); |
| 83 | + initProxySupport(proxyConfig, "test-bucket", awsConf); |
| 84 | + Assertions.assertThat(awsConf.getProxyProtocol()) |
| 85 | + .describedAs("Proxy protocol not as expected") |
| 86 | + .isEqualTo(isExpectedSecured ? Protocol.HTTPS : Protocol.HTTP); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Create a configuration file with proxy configs. |
| 91 | + * @param isSecured Should the configured proxy be secured or not? |
| 92 | + * @return configuration. |
| 93 | + */ |
| 94 | + private Configuration createProxyConfig(boolean isSecured) { |
| 95 | + Configuration conf = new Configuration(); |
| 96 | + conf.set(PROXY_HOST, "testProxy"); |
| 97 | + conf.set(PROXY_PORT, "1234"); |
| 98 | + conf.setBoolean(PROXY_SECURED, isSecured); |
| 99 | + return conf; |
| 100 | + } |
| 101 | +} |
0 commit comments