|
| 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.knox.gateway.pac4j.filter; |
| 19 | + |
| 20 | +import org.apache.knox.gateway.pac4j.session.KnoxSessionStore; |
| 21 | +import org.apache.knox.gateway.services.GatewayServices; |
| 22 | +import org.apache.knox.gateway.services.ServiceType; |
| 23 | +import org.apache.knox.gateway.services.security.AliasService; |
| 24 | +import org.apache.knox.gateway.services.security.CryptoService; |
| 25 | +import org.apache.knox.gateway.services.security.KeystoreService; |
| 26 | +import org.apache.knox.gateway.services.security.MasterService; |
| 27 | +import org.easymock.EasyMock; |
| 28 | +import org.junit.Assert; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import javax.servlet.FilterConfig; |
| 32 | +import javax.servlet.ServletContext; |
| 33 | +import java.security.KeyStore; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import static org.pac4j.config.client.PropertiesConstants.SAML_IDENTITY_PROVIDER_METADATA_PATH; |
| 40 | +import static org.pac4j.config.client.PropertiesConstants.SAML_KEYSTORE_PATH; |
| 41 | + |
| 42 | +public class Pac4jDispatcherFilterTest { |
| 43 | + |
| 44 | + private static final String TEST_CLUSTER_NAME = "test-cluster"; |
| 45 | + private static final String TEST_CALLBACK_URL = "https://localhost:8443/gateway/knoxsso/api/v1/websso"; |
| 46 | + |
| 47 | + |
| 48 | + /** |
| 49 | + * Test that verifies a custom value for PAC4J_COOKIE_MAX_AGE is properly set when provided in the configuration |
| 50 | + */ |
| 51 | + @Test |
| 52 | + public void testCustomCookieMaxAge() throws Exception { |
| 53 | + |
| 54 | + String COOKIE_MAX_AGE = "1800"; |
| 55 | + List<String> params = new ArrayList<>(); |
| 56 | + params.add(Pac4jDispatcherFilter.PAC4J_CALLBACK_URL); |
| 57 | + params.add(Pac4jDispatcherFilter.PAC4J_COOKIE_MAX_AGE); |
| 58 | + params.add("clientName"); |
| 59 | + params.add(SAML_KEYSTORE_PATH); |
| 60 | + params.add(SAML_IDENTITY_PROVIDER_METADATA_PATH); |
| 61 | + |
| 62 | + KeyStore ks = KeyStore.getInstance("JKS"); |
| 63 | + |
| 64 | + ServletContext context = EasyMock.createNiceMock(ServletContext.class); |
| 65 | + GatewayServices services = EasyMock.createNiceMock(GatewayServices.class); |
| 66 | + CryptoService cryptoService = EasyMock.createNiceMock(CryptoService.class); |
| 67 | + AliasService aliasService = EasyMock.createNiceMock(AliasService.class); |
| 68 | + KeystoreService keystoreService = EasyMock.createNiceMock(KeystoreService.class); |
| 69 | + MasterService masterService = EasyMock.createNiceMock(MasterService.class); |
| 70 | + FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class); |
| 71 | + |
| 72 | + EasyMock.expect(keystoreService.getKeystoreForGateway()).andReturn(ks).anyTimes(); |
| 73 | + EasyMock.expect(masterService.getMasterSecret()).andReturn("apacheknox".toCharArray()).anyTimes(); |
| 74 | + |
| 75 | + EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes(); |
| 76 | + EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(TEST_CLUSTER_NAME).anyTimes(); |
| 77 | + EasyMock.expect(services.getService(ServiceType.CRYPTO_SERVICE)).andReturn(cryptoService).anyTimes(); |
| 78 | + EasyMock.expect(services.getService(ServiceType.ALIAS_SERVICE)).andReturn(aliasService).anyTimes(); |
| 79 | + EasyMock.expect(services.getService(ServiceType.KEYSTORE_SERVICE)).andReturn(keystoreService).anyTimes(); |
| 80 | + EasyMock.expect(services.getService(ServiceType.MASTER_SERVICE)).andReturn(masterService).anyTimes(); |
| 81 | + EasyMock.expect(filterConfig.getServletContext()).andReturn(context).anyTimes(); |
| 82 | + EasyMock.expect(filterConfig.getInitParameterNames()).andReturn(Collections.enumeration(params)).anyTimes(); |
| 83 | + EasyMock.expect(filterConfig.getInitParameter(Pac4jDispatcherFilter.PAC4J_CALLBACK_URL)).andReturn(TEST_CALLBACK_URL).anyTimes(); |
| 84 | + EasyMock.expect(filterConfig.getInitParameter(SAML_KEYSTORE_PATH)).andReturn("/var/keystore").anyTimes(); |
| 85 | + EasyMock.expect(filterConfig.getInitParameter(SAML_IDENTITY_PROVIDER_METADATA_PATH)).andReturn("/tmp/sp-metadata.xml").anyTimes(); |
| 86 | + EasyMock.expect(filterConfig.getInitParameter("clientName")).andReturn("SAML2Client").anyTimes(); |
| 87 | + EasyMock.expect(filterConfig.getInitParameter(Pac4jDispatcherFilter.PAC4J_COOKIE_MAX_AGE)).andReturn(COOKIE_MAX_AGE).anyTimes(); |
| 88 | + EasyMock.expect(aliasService.getPasswordFromAliasForCluster(TEST_CLUSTER_NAME, KnoxSessionStore.PAC4J_PASSWORD, true)) |
| 89 | + .andReturn(KnoxSessionStore.PAC4J_PASSWORD.toCharArray()).anyTimes(); |
| 90 | + |
| 91 | + |
| 92 | + EasyMock.replay(context, services, cryptoService, aliasService, keystoreService, masterService, filterConfig); |
| 93 | + |
| 94 | + Pac4jDispatcherFilter filter = new Pac4jDispatcherFilter(); |
| 95 | + filter.init(filterConfig); |
| 96 | + |
| 97 | + |
| 98 | + java.lang.reflect.Field configField = filter.getClass().getDeclaredField("sessionStoreConfigs"); |
| 99 | + configField.setAccessible(true); |
| 100 | + Map<String, String> sessionStoreConfigs = (Map<String, String>) configField.get(filter); |
| 101 | + Assert.assertEquals(COOKIE_MAX_AGE, sessionStoreConfigs.get(Pac4jDispatcherFilter.PAC4J_COOKIE_MAX_AGE)); |
| 102 | + |
| 103 | + // Verify all mock interactions |
| 104 | + EasyMock.verify(context, services, cryptoService, aliasService, keystoreService, masterService, filterConfig); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Test that verifies the default value of PAC4J_COOKIE_MAX_AGE is set (-1) when no value is provided in the configuration |
| 109 | + */ |
| 110 | + @Test |
| 111 | + public void testDefaultCookieMaxAge() throws Exception { |
| 112 | + String COOKIE_MAX_AGE = "-1"; |
| 113 | + List<String> params = new ArrayList<>(); |
| 114 | + params.add(Pac4jDispatcherFilter.PAC4J_CALLBACK_URL); |
| 115 | + params.add("clientName"); |
| 116 | + params.add(SAML_KEYSTORE_PATH); |
| 117 | + params.add(SAML_IDENTITY_PROVIDER_METADATA_PATH); |
| 118 | + |
| 119 | + KeyStore ks = KeyStore.getInstance("JKS"); |
| 120 | + |
| 121 | + ServletContext context = EasyMock.createNiceMock(ServletContext.class); |
| 122 | + GatewayServices services = EasyMock.createNiceMock(GatewayServices.class); |
| 123 | + CryptoService cryptoService = EasyMock.createNiceMock(CryptoService.class); |
| 124 | + AliasService aliasService = EasyMock.createNiceMock(AliasService.class); |
| 125 | + KeystoreService keystoreService = EasyMock.createNiceMock(KeystoreService.class); |
| 126 | + MasterService masterService = EasyMock.createNiceMock(MasterService.class); |
| 127 | + FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class); |
| 128 | + |
| 129 | + EasyMock.expect(keystoreService.getKeystoreForGateway()).andReturn(ks).anyTimes(); |
| 130 | + EasyMock.expect(masterService.getMasterSecret()).andReturn("apacheknox".toCharArray()).anyTimes(); |
| 131 | + |
| 132 | + EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes(); |
| 133 | + EasyMock.expect(context.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(TEST_CLUSTER_NAME).anyTimes(); |
| 134 | + EasyMock.expect(services.getService(ServiceType.CRYPTO_SERVICE)).andReturn(cryptoService).anyTimes(); |
| 135 | + EasyMock.expect(services.getService(ServiceType.ALIAS_SERVICE)).andReturn(aliasService).anyTimes(); |
| 136 | + EasyMock.expect(services.getService(ServiceType.KEYSTORE_SERVICE)).andReturn(keystoreService).anyTimes(); |
| 137 | + EasyMock.expect(services.getService(ServiceType.MASTER_SERVICE)).andReturn(masterService).anyTimes(); |
| 138 | + EasyMock.expect(filterConfig.getServletContext()).andReturn(context).anyTimes(); |
| 139 | + EasyMock.expect(filterConfig.getInitParameterNames()).andReturn(Collections.enumeration(params)).anyTimes(); |
| 140 | + EasyMock.expect(filterConfig.getInitParameter(Pac4jDispatcherFilter.PAC4J_CALLBACK_URL)).andReturn(TEST_CALLBACK_URL).anyTimes(); |
| 141 | + EasyMock.expect(filterConfig.getInitParameter(SAML_KEYSTORE_PATH)).andReturn("/var/keystore").anyTimes(); |
| 142 | + EasyMock.expect(filterConfig.getInitParameter(SAML_IDENTITY_PROVIDER_METADATA_PATH)).andReturn("/tmp/sp-metadata.xml").anyTimes(); |
| 143 | + EasyMock.expect(filterConfig.getInitParameter("clientName")).andReturn("SAML2Client").anyTimes(); |
| 144 | + EasyMock.expect(aliasService.getPasswordFromAliasForCluster(TEST_CLUSTER_NAME, KnoxSessionStore.PAC4J_PASSWORD, true)) |
| 145 | + .andReturn(KnoxSessionStore.PAC4J_PASSWORD.toCharArray()).anyTimes(); |
| 146 | + |
| 147 | + |
| 148 | + EasyMock.replay(context, services, cryptoService, aliasService, keystoreService, masterService, filterConfig); |
| 149 | + |
| 150 | + Pac4jDispatcherFilter filter = new Pac4jDispatcherFilter(); |
| 151 | + filter.init(filterConfig); |
| 152 | + |
| 153 | + java.lang.reflect.Field configField = filter.getClass().getDeclaredField("sessionStoreConfigs"); |
| 154 | + configField.setAccessible(true); |
| 155 | + Map<String, String> sessionStoreConfigs = (Map<String, String>) configField.get(filter); |
| 156 | + Assert.assertEquals(COOKIE_MAX_AGE, sessionStoreConfigs.get(Pac4jDispatcherFilter.PAC4J_COOKIE_MAX_AGE)); |
| 157 | + |
| 158 | + // Verify all mock interactions |
| 159 | + EasyMock.verify(context, services, cryptoService, aliasService, keystoreService, masterService, filterConfig); |
| 160 | + } |
| 161 | +} |
0 commit comments