|
| 1 | +/* |
| 2 | + * Copyright 2002-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.support; |
| 18 | + |
| 19 | +import static java.lang.String.format; |
| 20 | +import static org.hamcrest.CoreMatchers.is; |
| 21 | +import static org.junit.Assert.assertThat; |
| 22 | + |
| 23 | +import java.security.AccessControlException; |
| 24 | +import java.security.Permission; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +import org.junit.After; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 32 | +import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; |
| 33 | +import org.springframework.context.annotation.Profile; |
| 34 | +import org.springframework.core.env.AbstractEnvironment; |
| 35 | +import org.springframework.core.env.StandardEnvironmentTests; |
| 36 | +import org.springframework.stereotype.Component; |
| 37 | + |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests integration between Environment and SecurityManagers. See SPR-9970. |
| 41 | + * |
| 42 | + * @author Chris Beams |
| 43 | + */ |
| 44 | +public class EnvironmentSecurityManagerIntegrationTests { |
| 45 | + |
| 46 | + private SecurityManager originalSecurityManager; |
| 47 | + private Map<String, String> env; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() { |
| 51 | + originalSecurityManager = System.getSecurityManager(); |
| 52 | + env = StandardEnvironmentTests.getModifiableSystemEnvironment(); |
| 53 | + env.put(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "p1"); |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + public void tearDown() { |
| 58 | + env.remove(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME); |
| 59 | + System.setSecurityManager(originalSecurityManager); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() { |
| 64 | + SecurityManager securityManager = new SecurityManager() { |
| 65 | + @Override |
| 66 | + public void checkPermission(Permission perm) { |
| 67 | + // disallowing access to System#getenv means that our |
| 68 | + // ReadOnlySystemAttributesMap will come into play. |
| 69 | + if ("getenv.*".equals(perm.getName())) { |
| 70 | + throw new AccessControlException( |
| 71 | + "Accessing the system environment is disallowed"); |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + System.setSecurityManager(securityManager); |
| 76 | + |
| 77 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 78 | + AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf); |
| 79 | + reader.register(C1.class); |
| 80 | + assertThat(bf.containsBean("c1"), is(true)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void securityManagerDisallowsAccessToSystemEnvironmentAndDisallowsAccessToIndividualKey() { |
| 85 | + SecurityManager securityManager = new SecurityManager() { |
| 86 | + @Override |
| 87 | + public void checkPermission(Permission perm) { |
| 88 | + // disallowing access to System#getenv means that our |
| 89 | + // ReadOnlySystemAttributesMap will come into play. |
| 90 | + if ("getenv.*".equals(perm.getName())) { |
| 91 | + throw new AccessControlException( |
| 92 | + "Accessing the system environment is disallowed"); |
| 93 | + } |
| 94 | + // disallowing access to the spring.profiles.active property means that |
| 95 | + // the BeanDefinitionReader won't be able to determine which profiles are |
| 96 | + // active. We should see an INFO-level message in the console about this |
| 97 | + // and as a result, any components marked with a non-default profile will |
| 98 | + // be ignored. |
| 99 | + if (("getenv."+AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) { |
| 100 | + throw new AccessControlException( |
| 101 | + format("Accessing system environment variable [%s] is disallowed", |
| 102 | + AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)); |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + System.setSecurityManager(securityManager); |
| 107 | + |
| 108 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 109 | + AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf); |
| 110 | + reader.register(C1.class); |
| 111 | + assertThat(bf.containsBean("c1"), is(false)); |
| 112 | + } |
| 113 | + |
| 114 | + @Component("c1") |
| 115 | + @Profile("p1") |
| 116 | + static class C1 { |
| 117 | + } |
| 118 | +} |
0 commit comments