|
| 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 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 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.ozone.s3.util; |
| 20 | + |
| 21 | +import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
| 22 | +import org.apache.hadoop.ozone.OmUtils; |
| 23 | +import org.apache.hadoop.ozone.om.OMConfigKeys; |
| 24 | +import org.apache.hadoop.security.SecurityUtil; |
| 25 | +import org.apache.hadoop.test.GenericTestUtils; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import java.util.Collection; |
| 31 | + |
| 32 | +import static org.apache.hadoop.fs.CommonConfigurationKeys.HADOOP_SECURITY_TOKEN_SERVICE_USE_IP; |
| 33 | +import static org.junit.Assert.fail; |
| 34 | + |
| 35 | +/** |
| 36 | + * Class used to test OzoneS3Util. |
| 37 | + */ |
| 38 | +public class TestOzoneS3Util { |
| 39 | + |
| 40 | + |
| 41 | + private OzoneConfiguration configuration; |
| 42 | + private String serviceID = "omService"; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setConf() { |
| 46 | + configuration = new OzoneConfiguration(); |
| 47 | + |
| 48 | + String nodeIDs = "om1,om2,om3"; |
| 49 | + configuration.set(OMConfigKeys.OZONE_OM_SERVICE_IDS_KEY, serviceID); |
| 50 | + configuration.set(OMConfigKeys.OZONE_OM_NODES_KEY + "." + serviceID, |
| 51 | + nodeIDs); |
| 52 | + configuration.setBoolean(HADOOP_SECURITY_TOKEN_SERVICE_USE_IP, false); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testBuildServiceNameForToken() { |
| 57 | + |
| 58 | + Collection<String> nodeIDList = OmUtils.getOMNodeIds(configuration, |
| 59 | + serviceID); |
| 60 | + |
| 61 | + configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY, |
| 62 | + serviceID, "om1"), "om1:9862"); |
| 63 | + configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY, |
| 64 | + serviceID, "om2"), "om2:9862"); |
| 65 | + configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY, |
| 66 | + serviceID, "om3"), "om3:9862"); |
| 67 | + |
| 68 | + String expectedOmServiceAddress = buildServiceAddress(nodeIDList); |
| 69 | + |
| 70 | + SecurityUtil.setConfiguration(configuration); |
| 71 | + String omserviceAddr = OzoneS3Util.buildServiceNameForToken(configuration, |
| 72 | + serviceID, nodeIDList); |
| 73 | + |
| 74 | + Assert.assertEquals(expectedOmServiceAddress, omserviceAddr); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testBuildServiceNameForTokenIncorrectConfig() { |
| 80 | + |
| 81 | + Collection<String> nodeIDList = OmUtils.getOMNodeIds(configuration, |
| 82 | + serviceID); |
| 83 | + |
| 84 | + // Don't set om3 node rpc address. Here we are skipping setting of one of |
| 85 | + // the OM address. So buildServiceNameForToken will fail. |
| 86 | + configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY, |
| 87 | + serviceID, "om1"), "om1:9862"); |
| 88 | + configuration.set(OmUtils.addKeySuffixes(OMConfigKeys.OZONE_OM_ADDRESS_KEY, |
| 89 | + serviceID, "om2"), "om2:9862"); |
| 90 | + |
| 91 | + |
| 92 | + SecurityUtil.setConfiguration(configuration); |
| 93 | + |
| 94 | + try { |
| 95 | + OzoneS3Util.buildServiceNameForToken(configuration, |
| 96 | + serviceID, nodeIDList); |
| 97 | + fail("testBuildServiceNameForTokenIncorrectConfig failed"); |
| 98 | + } catch (IllegalArgumentException ex) { |
| 99 | + GenericTestUtils.assertExceptionContains("Could not find rpcAddress " + |
| 100 | + "for", ex); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Build serviceName from list of node ids. |
| 108 | + * @param nodeIDList |
| 109 | + * @return service name for token. |
| 110 | + */ |
| 111 | + private String buildServiceAddress(Collection<String> nodeIDList) { |
| 112 | + StringBuilder omServiceAddrBuilder = new StringBuilder(); |
| 113 | + int nodesLength = nodeIDList.size(); |
| 114 | + int counter = 0; |
| 115 | + for (String nodeID : nodeIDList) { |
| 116 | + counter++; |
| 117 | + String addr = configuration.get(OmUtils.addKeySuffixes( |
| 118 | + OMConfigKeys.OZONE_OM_ADDRESS_KEY, serviceID, nodeID)); |
| 119 | + |
| 120 | + if (counter != nodesLength) { |
| 121 | + omServiceAddrBuilder.append(addr + ","); |
| 122 | + } else { |
| 123 | + omServiceAddrBuilder.append(addr); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return omServiceAddrBuilder.toString(); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments