SecurityConfigurationFactory { public SecurityConfiguration createSecurityConfigurationFromRequest(Map<String, Object> properties, boolean persistEmbeddedDescriptor) { SecurityConfiguration securityConfiguration = null; LOGGER.debug("Creating security configuration from properties: {}", properties); Map<String, Object> securityProperties = (Map<String, Object>) properties.get(SECURITY_PROPERTY_ID); if (securityProperties == null) { LOGGER.debug("No security information properties provided, returning null"); return securityConfiguration; } String securityTypeString = Strings.emptyToNull((String) securityProperties.get(TYPE_PROPERTY_ID)); if (securityTypeString == null) { LOGGER.error("Type is missing from security block."); throw new IllegalArgumentException("Type missing from security block."); } SecurityType securityType = Enums.getIfPresent(SecurityType.class, securityTypeString).orNull(); if (securityType == null) { LOGGER.error("Unsupported security type specified: {}", securityType); throw new IllegalArgumentException("Invalid security type specified: " + securityTypeString); } if (securityType == SecurityType.KERBEROS) { String descriptorReference = Strings.emptyToNull((String) securityProperties.get(KERBEROS_DESCRIPTOR_REFERENCE_PROPERTY_ID)); Object descriptorJsonMap = securityProperties.get(KERBEROS_DESCRIPTOR_PROPERTY_ID); if (descriptorReference != null && descriptorJsonMap != null) { LOGGER.error("Both kerberos descriptor and kerberos descriptor reference are set in the security configuration!"); throw new IllegalArgumentException("Usage of properties : " + KERBEROS_DESCRIPTOR_PROPERTY_ID + " and " + KERBEROS_DESCRIPTOR_REFERENCE_PROPERTY_ID + " at the same time, is not allowed."); } String descriptorText = null; if (descriptorJsonMap != null) { LOGGER.debug("Found embedded descriptor: {}", descriptorJsonMap); descriptorText = jsonSerializer.<Map<String, Object>>toJson(descriptorJsonMap, Map.class); if (persistEmbeddedDescriptor) { descriptorReference = persistKerberosDescriptor(descriptorText); } securityConfiguration = new SecurityConfiguration(SecurityType.KERBEROS, descriptorReference, descriptorText); } else if (descriptorReference != null) { LOGGER.debug("Found descriptor reference: {}", descriptorReference); securityConfiguration = loadSecurityConfigurationByReference(descriptorReference); } else { LOGGER.debug("There is no security descriptor found in the request"); securityConfiguration = new SecurityConfiguration(SecurityType.KERBEROS); } } else { LOGGER.debug("There is no security configuration found in the request"); securityConfiguration = new SecurityConfiguration(SecurityType.NONE); } return securityConfiguration; } SecurityConfigurationFactory(); protected  SecurityConfigurationFactory(Gson jsonSerializer, KerberosDescriptorDAO kerberosDescriptorDAO, KerberosDescriptorFactory kerberosDescriptorFactory); SecurityConfiguration createSecurityConfigurationFromRequest(Map<String, Object> properties, boolean
    persistEmbeddedDescriptor); SecurityConfiguration loadSecurityConfigurationByReference(String reference); static final String SECURITY_PROPERTY_ID; static final String TYPE_PROPERTY_ID; static final String KERBEROS_DESCRIPTOR_PROPERTY_ID; static final String KERBEROS_DESCRIPTOR_REFERENCE_PROPERTY_ID; }
@Test public void testShouldLoadKerberosDescriptorWhenKDReferenceFoundInRequest() throws Exception { EasyMock.expect(kerberosDescriptorDAO.findByName(TEST_KERBEROS_DESCRIPTOR_REFERENCE)).andReturn(testKDEntity()); Map<String, Object> reuqestMap = new HashMap<>(); Map<String, Object> security = new HashMap<>(); security.put(SecurityConfigurationFactory.TYPE_PROPERTY_ID, SecurityType.KERBEROS.toString()); security.put(SecurityConfigurationFactory.KERBEROS_DESCRIPTOR_REFERENCE_PROPERTY_ID, TEST_KERBEROS_DESCRIPTOR_REFERENCE); reuqestMap.put(SecurityConfigurationFactory.SECURITY_PROPERTY_ID, security); EasyMock.replay(kerberosDescriptorDAO); SecurityConfiguration securityConfiguration = testSubject.createSecurityConfigurationFromRequest(reuqestMap, false); EasyMock.verify(kerberosDescriptorDAO); assertTrue(securityConfiguration.getType() == SecurityType.KERBEROS); }
@Test public void testShouldPersistKDWhenKDFoundInRequest() throws Exception { Capture<KerberosDescriptorEntity> kdEntityCaptor = EasyMock.newCapture(); kerberosDescriptorDAO.create(capture(kdEntityCaptor)); EasyMock.replay(kerberosDescriptorDAO); Map<String, Object> reuqestMap = new HashMap<>(); Map<String, Object> security = new HashMap<>(); security.put(SecurityConfigurationFactory.TYPE_PROPERTY_ID, SecurityType.KERBEROS.toString()); security.put(SecurityConfigurationFactory.KERBEROS_DESCRIPTOR_PROPERTY_ID, testKDReqPropertyMap()); reuqestMap.put(SecurityConfigurationFactory.SECURITY_PROPERTY_ID, security); testSubject.createSecurityConfigurationFromRequest(reuqestMap, true); EasyMock.verify(kerberosDescriptorDAO); Assert.assertEquals("The persisted descriptortext is not as expected", "{\"test\":\"{\\\"test\\\":\\\"test json\\\"}\"}", kdEntityCaptor.getValue().getKerberosDescriptorText()); Assert.assertNotNull("There is no generated kerberos descriptor reference in the persisting entity!", kdEntityCaptor.getValue().getName()); }
@Test public void testCreateKerberosSecurityWithoutDescriptor() throws Exception { Map<String, Object> reuqestMap = new HashMap<>(); Map<String, Object> security = new HashMap<>(); security.put(SecurityConfigurationFactory.TYPE_PROPERTY_ID, SecurityType.KERBEROS.toString()); reuqestMap.put(SecurityConfigurationFactory.SECURITY_PROPERTY_ID, security); SecurityConfiguration securityConfiguration = testSubject.createSecurityConfigurationFromRequest(reuqestMap, false); assertTrue(securityConfiguration.getType() == SecurityType.KERBEROS); }
@Test public void testCreateEmpty() throws Exception { Map<String, Object> reuqestMap = new HashMap<>(); SecurityConfiguration securityConfiguration = testSubject.createSecurityConfigurationFromRequest(reuqestMap, false); assertTrue(securityConfiguration == null); }
@Test(expected = IllegalArgumentException.class) public void testCreateInvalidSecurityType() throws Exception { Map<String, Object> reuqestMap = new HashMap<>(); Map<String, Object> security = new HashMap<>(); security.put(SecurityConfigurationFactory.TYPE_PROPERTY_ID, "INVALID_SECURITY_TYPE"); reuqestMap.put(SecurityConfigurationFactory.SECURITY_PROPERTY_ID, security); SecurityConfiguration securityConfiguration = testSubject.createSecurityConfigurationFromRequest(reuqestMap, false); assertTrue(securityConfiguration.getType() == SecurityType.KERBEROS); }
@Test public void testCreateKerberosSecurityTypeNone() throws Exception { Map<String, Object> reuqestMap = new HashMap<>(); Map<String, Object> security = new HashMap<>(); security.put(SecurityConfigurationFactory.TYPE_PROPERTY_ID, SecurityType.NONE.toString()); reuqestMap.put(SecurityConfigurationFactory.SECURITY_PROPERTY_ID, security); SecurityConfiguration securityConfiguration = testSubject.createSecurityConfigurationFromRequest(reuqestMap, false); assertTrue(securityConfiguration.getType() == SecurityType.NONE); }