[120] | 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.security.Permission; |
---|
| 21 | |
---|
| 22 | import javax.security.auth.Subject; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.conf.Configuration; |
---|
| 25 | import org.apache.hadoop.security.SecurityUtil; |
---|
| 26 | import org.apache.hadoop.security.UnixUserGroupInformation; |
---|
| 27 | import org.apache.hadoop.security.SecurityUtil.AccessControlList; |
---|
| 28 | |
---|
| 29 | import junit.framework.TestCase; |
---|
| 30 | |
---|
| 31 | public class TestConfiguredPolicy extends TestCase { |
---|
| 32 | private static final String USER1 = "drwho"; |
---|
| 33 | private static final String USER2 = "joe"; |
---|
| 34 | private static final String[] GROUPS1 = new String[]{"tardis"}; |
---|
| 35 | private static final String[] GROUPS2 = new String[]{"users"}; |
---|
| 36 | |
---|
| 37 | private static final String KEY_1 = "test.policy.1"; |
---|
| 38 | private static final String KEY_2 = "test.policy.2"; |
---|
| 39 | |
---|
| 40 | public static class Protocol1 { |
---|
| 41 | int i; |
---|
| 42 | } |
---|
| 43 | public static class Protocol2 { |
---|
| 44 | int j; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | private static class TestPolicyProvider extends PolicyProvider { |
---|
| 48 | @Override |
---|
| 49 | public Service[] getServices() { |
---|
| 50 | return new Service[] { |
---|
| 51 | new Service(KEY_1, Protocol1.class), |
---|
| 52 | new Service(KEY_2, Protocol2.class), |
---|
| 53 | }; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | public void testConfiguredPolicy() throws Exception { |
---|
| 58 | Configuration conf = new Configuration(); |
---|
| 59 | conf.set(KEY_1, AccessControlList.WILDCARD_ACL_VALUE); |
---|
| 60 | conf.set(KEY_2, USER1 + " " + GROUPS1[0]); |
---|
| 61 | |
---|
| 62 | ConfiguredPolicy policy = new ConfiguredPolicy(conf, new TestPolicyProvider()); |
---|
| 63 | SecurityUtil.setPolicy(policy); |
---|
| 64 | |
---|
| 65 | Subject user1 = |
---|
| 66 | SecurityUtil.getSubject(new UnixUserGroupInformation(USER1, GROUPS1)); |
---|
| 67 | |
---|
| 68 | // Should succeed |
---|
| 69 | ServiceAuthorizationManager.authorize(user1, Protocol1.class); |
---|
| 70 | |
---|
| 71 | // Should fail |
---|
| 72 | Subject user2 = |
---|
| 73 | SecurityUtil.getSubject(new UnixUserGroupInformation(USER2, GROUPS2)); |
---|
| 74 | boolean failed = false; |
---|
| 75 | try { |
---|
| 76 | ServiceAuthorizationManager.authorize(user2, Protocol2.class); |
---|
| 77 | } catch (AuthorizationException ae) { |
---|
| 78 | failed = true; |
---|
| 79 | } |
---|
| 80 | assertTrue(failed); |
---|
| 81 | } |
---|
| 82 | } |
---|