|
| 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 | +package org.apache.hadoop.security.authorize; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import org.apache.hadoop.conf.Configuration; |
| 22 | +import org.apache.hadoop.util.StringUtils; |
| 23 | +import org.apache.hadoop.security.UserGroupInformation; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | +import static org.junit.Assert.*; |
| 27 | + |
| 28 | +public class TestProxyUsers { |
| 29 | + private static final String REAL_USER_NAME = "proxier"; |
| 30 | + private static final String PROXY_USER_NAME = "proxied_user"; |
| 31 | + private static final String[] GROUP_NAMES = |
| 32 | + new String[] { "foo_group" }; |
| 33 | + private static final String[] OTHER_GROUP_NAMES = |
| 34 | + new String[] { "bar_group" }; |
| 35 | + private static final String PROXY_IP = "1.2.3.4"; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testProxyUsers() throws Exception { |
| 39 | + Configuration conf = new Configuration(); |
| 40 | + conf.set( |
| 41 | + ProxyUsers.getProxySuperuserGroupConfKey(REAL_USER_NAME), |
| 42 | + StringUtils.join(",", Arrays.asList(GROUP_NAMES))); |
| 43 | + conf.set( |
| 44 | + ProxyUsers.getProxySuperuserIpConfKey(REAL_USER_NAME), |
| 45 | + PROXY_IP); |
| 46 | + ProxyUsers.refreshSuperUserGroupsConfiguration(conf); |
| 47 | + |
| 48 | + |
| 49 | + // First try proxying a group that's allowed |
| 50 | + UserGroupInformation realUserUgi = UserGroupInformation |
| 51 | + .createRemoteUser(REAL_USER_NAME); |
| 52 | + UserGroupInformation proxyUserUgi = UserGroupInformation.createProxyUserForTesting( |
| 53 | + PROXY_USER_NAME, realUserUgi, GROUP_NAMES); |
| 54 | + |
| 55 | + // From good IP |
| 56 | + assertAuthorized(proxyUserUgi, "1.2.3.4"); |
| 57 | + // From bad IP |
| 58 | + assertNotAuthorized(proxyUserUgi, "1.2.3.5"); |
| 59 | + |
| 60 | + // Now try proxying a group that's not allowed |
| 61 | + realUserUgi = UserGroupInformation.createRemoteUser(REAL_USER_NAME); |
| 62 | + proxyUserUgi = UserGroupInformation.createProxyUserForTesting( |
| 63 | + PROXY_USER_NAME, realUserUgi, OTHER_GROUP_NAMES); |
| 64 | + |
| 65 | + // From good IP |
| 66 | + assertNotAuthorized(proxyUserUgi, "1.2.3.4"); |
| 67 | + // From bad IP |
| 68 | + assertNotAuthorized(proxyUserUgi, "1.2.3.5"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testWildcardGroup() { |
| 73 | + Configuration conf = new Configuration(); |
| 74 | + conf.set( |
| 75 | + ProxyUsers.getProxySuperuserGroupConfKey(REAL_USER_NAME), |
| 76 | + "*"); |
| 77 | + conf.set( |
| 78 | + ProxyUsers.getProxySuperuserIpConfKey(REAL_USER_NAME), |
| 79 | + PROXY_IP); |
| 80 | + ProxyUsers.refreshSuperUserGroupsConfiguration(conf); |
| 81 | + |
| 82 | + // First try proxying a group that's allowed |
| 83 | + UserGroupInformation realUserUgi = UserGroupInformation |
| 84 | + .createRemoteUser(REAL_USER_NAME); |
| 85 | + UserGroupInformation proxyUserUgi = UserGroupInformation.createProxyUserForTesting( |
| 86 | + PROXY_USER_NAME, realUserUgi, GROUP_NAMES); |
| 87 | + |
| 88 | + // From good IP |
| 89 | + assertAuthorized(proxyUserUgi, "1.2.3.4"); |
| 90 | + // From bad IP |
| 91 | + assertNotAuthorized(proxyUserUgi, "1.2.3.5"); |
| 92 | + |
| 93 | + // Now try proxying a different group (just to make sure we aren't getting spill over |
| 94 | + // from the other test case!) |
| 95 | + realUserUgi = UserGroupInformation.createRemoteUser(REAL_USER_NAME); |
| 96 | + proxyUserUgi = UserGroupInformation.createProxyUserForTesting( |
| 97 | + PROXY_USER_NAME, realUserUgi, OTHER_GROUP_NAMES); |
| 98 | + |
| 99 | + // From good IP |
| 100 | + assertAuthorized(proxyUserUgi, "1.2.3.4"); |
| 101 | + // From bad IP |
| 102 | + assertNotAuthorized(proxyUserUgi, "1.2.3.5"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testWildcardIP() { |
| 107 | + Configuration conf = new Configuration(); |
| 108 | + conf.set( |
| 109 | + ProxyUsers.getProxySuperuserGroupConfKey(REAL_USER_NAME), |
| 110 | + StringUtils.join(",", Arrays.asList(GROUP_NAMES))); |
| 111 | + conf.set( |
| 112 | + ProxyUsers.getProxySuperuserIpConfKey(REAL_USER_NAME), |
| 113 | + "*"); |
| 114 | + ProxyUsers.refreshSuperUserGroupsConfiguration(conf); |
| 115 | + |
| 116 | + // First try proxying a group that's allowed |
| 117 | + UserGroupInformation realUserUgi = UserGroupInformation |
| 118 | + .createRemoteUser(REAL_USER_NAME); |
| 119 | + UserGroupInformation proxyUserUgi = UserGroupInformation.createProxyUserForTesting( |
| 120 | + PROXY_USER_NAME, realUserUgi, GROUP_NAMES); |
| 121 | + |
| 122 | + // From either IP should be fine |
| 123 | + assertAuthorized(proxyUserUgi, "1.2.3.4"); |
| 124 | + assertAuthorized(proxyUserUgi, "1.2.3.5"); |
| 125 | + |
| 126 | + // Now set up an unallowed group |
| 127 | + realUserUgi = UserGroupInformation.createRemoteUser(REAL_USER_NAME); |
| 128 | + proxyUserUgi = UserGroupInformation.createProxyUserForTesting( |
| 129 | + PROXY_USER_NAME, realUserUgi, OTHER_GROUP_NAMES); |
| 130 | + |
| 131 | + // Neither IP should be OK |
| 132 | + assertNotAuthorized(proxyUserUgi, "1.2.3.4"); |
| 133 | + assertNotAuthorized(proxyUserUgi, "1.2.3.5"); |
| 134 | + } |
| 135 | + |
| 136 | + private void assertNotAuthorized(UserGroupInformation proxyUgi, String host) { |
| 137 | + try { |
| 138 | + ProxyUsers.authorize(proxyUgi, host, null); |
| 139 | + fail("Allowed authorization of " + proxyUgi + " from " + host); |
| 140 | + } catch (AuthorizationException e) { |
| 141 | + // Expected |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void assertAuthorized(UserGroupInformation proxyUgi, String host) { |
| 146 | + try { |
| 147 | + ProxyUsers.authorize(proxyUgi, host, null); |
| 148 | + } catch (AuthorizationException e) { |
| 149 | + fail("Did not allowed authorization of " + proxyUgi + " from " + host); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments