Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.cli.ConfigurationException;
import org.apache.activemq.artemis.core.server.ActivateCallback;
import org.apache.activemq.artemis.dto.BrokerDTO;
import org.apache.activemq.artemis.dto.JaasSecurityDTO;
import org.apache.activemq.artemis.dto.PropertyDTO;
import org.apache.activemq.artemis.dto.SecurityManagerDTO;
import org.apache.activemq.artemis.dto.ServerDTO;
import org.apache.activemq.artemis.integration.Broker;
import org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager;
Expand All @@ -44,7 +52,38 @@ private static BrokerDTO createBrokerConfiguration(URI configURI,
} catch (IOException ioe) {
throw new ConfigurationException("Invalid configuration URI, can't find configuration scheme: " + configURI.getScheme());
}
return factory.createBroker(configURI, artemisHome, artemisInstance, artemisURIInstance);
BrokerDTO broker = factory.createBroker(configURI, artemisHome, artemisInstance, artemisURIInstance);

Properties systemProperties = System.getProperties();
String systemSecurityJaasPropertyPrefix = ActiveMQDefaultConfiguration.getDefaultSystemSecurityJaasPropertyPrefix();
String systemSecurityManagerPropertyPrefix = ActiveMQDefaultConfiguration.getDefaultSystemSecurityManagerPropertyPrefix();

if (systemProperties.containsKey(systemSecurityJaasPropertyPrefix + "domain")) {
JaasSecurityDTO security = broker.security instanceof JaasSecurityDTO ?
(JaasSecurityDTO) broker.security : new JaasSecurityDTO();

security.domain = (String)systemProperties.get(systemSecurityJaasPropertyPrefix + "domain");
security.certificateDomain = Optional.ofNullable((String)systemProperties.get(
systemSecurityJaasPropertyPrefix + "certificateDomain")).orElse(security.certificateDomain);

broker.security = security;
} else if (systemProperties.containsKey(systemSecurityManagerPropertyPrefix + "className")) {
SecurityManagerDTO security = broker.security instanceof SecurityManagerDTO ?
(SecurityManagerDTO) broker.security : new SecurityManagerDTO();

security.className = (String)systemProperties.get(systemSecurityManagerPropertyPrefix + "className");
security.properties = Objects.requireNonNullElse(security.properties, new ArrayList<>());
systemProperties.forEach((key, value) -> {
if (((String)key).startsWith(systemSecurityManagerPropertyPrefix + "properties.")) {
security.properties.add(new PropertyDTO(((String)key).substring(
systemSecurityManagerPropertyPrefix.length() + 11), (String)value));
}
});

broker.security = security;
}

return broker;
}

public static BrokerDTO createBrokerConfiguration(String configuration,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.cli.factory;

import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.dto.BrokerDTO;
import org.apache.activemq.artemis.dto.JaasSecurityDTO;
import org.apache.activemq.artemis.dto.PropertyDTO;
import org.apache.activemq.artemis.dto.SecurityDTO;
import org.apache.activemq.artemis.dto.SecurityManagerDTO;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BrokerFactoryTest {

private static final String testBrokerConfiguration = "test://config";

private final String securityJaasPropertyPrefix = ActiveMQDefaultConfiguration.getDefaultSystemSecurityJaasPropertyPrefix();
private final String securityManagerPropertyPrefix = ActiveMQDefaultConfiguration.getDefaultSystemSecurityManagerPropertyPrefix();
private final List<String> systemPropertiesToClear = new ArrayList<>();

@BeforeEach
public void setUp() {
TestBrokerFactoryHandler.clear();
}

@AfterEach
public void tearDown() {
TestBrokerFactoryHandler.clear();
for (String property : systemPropertiesToClear) {
System.clearProperty(property);
}
systemPropertiesToClear.clear();
}

private void setSystemProperty(String key, String value) {
System.setProperty(key, value);
systemPropertiesToClear.add(key);
}

@Test
public void testCreateBrokerConfiguration() throws Exception {
final String testArtemisHome = "test-home";
final String testArtemisInstance = "test-instance";
final URI testArtemisURIInstance = URI.create(testArtemisInstance);

TestBrokerFactoryHandler.setBroker(new BrokerDTO());

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration(
testBrokerConfiguration, testArtemisHome, testArtemisInstance, testArtemisURIInstance);

assertNotNull(createdBroker);
assertEquals(testBrokerConfiguration, TestBrokerFactoryHandler.getBrokerURI().toString());
assertEquals(testArtemisHome, TestBrokerFactoryHandler.getArtemisHome());
assertEquals(testArtemisInstance, TestBrokerFactoryHandler.getArtemisInstance());
assertEquals(testArtemisURIInstance, TestBrokerFactoryHandler.getArtemisURIInstance());
}

@Test
public void testCreateBrokerConfigurationWithJaasDomainFromSystemProperties() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "testDomain");

TestBrokerFactoryHandler.setBroker(new BrokerDTO());

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testJaasSecurity(createdBroker.security, "testDomain", null);
}

@Test
public void testCreateBrokerConfigurationWithJaasDomainAndCertificateDomainFromSystemProperties() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "testDomain");
setSystemProperty(securityJaasPropertyPrefix + "certificateDomain", "testCertificateDomain");

TestBrokerFactoryHandler.setBroker(new BrokerDTO());

BrokerDTO broker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(broker);
testJaasSecurity(broker.security, "testDomain", "testCertificateDomain");
}

@Test
public void testCreateBrokerConfigurationWithNewJaasDomainFromExistingJaasSecurityAndSystemProperties() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "newTestDomain");

JaasSecurityDTO security = new JaasSecurityDTO();
security.domain = "testDomain";
security.certificateDomain = "testCertificateDomain";
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testJaasSecurity(createdBroker.security, "newTestDomain", "testCertificateDomain");
}

@Test
public void testCreateBrokerConfigurationWithNewJaasDomainFromExistingSecurityManagerAndSystemProperties() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "newTestDomain");

SecurityManagerDTO security = new SecurityManagerDTO();
security.className = "testClass";
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testJaasSecurity(createdBroker.security, "newTestDomain", null);
}

@Test
public void testCreateBrokerConfigurationWithNewJaasDomainAndCertificateDomainFromExistingJaasSecurityAndSystemProperties() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "newTestDomain");
setSystemProperty(securityJaasPropertyPrefix + "certificateDomain", "newTestCertificateDomain");

JaasSecurityDTO security = new JaasSecurityDTO();
security.domain = "testDomain";
security.certificateDomain = "testCertificateDomain";
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testJaasSecurity(createdBroker.security, "newTestDomain", "newTestCertificateDomain");
}

@Test
public void testCreateBrokerConfigurationWithNewJaasDomainAndCertificateDomainFromExistingSecurityManagerAndSystemProperties() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "newTestDomain");
setSystemProperty(securityJaasPropertyPrefix + "certificateDomain", "newTestCertificateDomain");

SecurityManagerDTO security = new SecurityManagerDTO();
security.className = "testClassName";
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testJaasSecurity(createdBroker.security, "newTestDomain", "newTestCertificateDomain");
}

private void testJaasSecurity(SecurityDTO security, String expectedDomain, String expectedCertificateDomain) throws Exception {
assertNotNull(security);
assertInstanceOf(JaasSecurityDTO.class, security);
JaasSecurityDTO jaasSecurity = (JaasSecurityDTO) security;
assertEquals(expectedDomain, jaasSecurity.domain);
assertEquals(expectedCertificateDomain, jaasSecurity.certificateDomain);
}

@Test
public void testCreateBrokerConfigurationWithSecurityManagerClassNameFromSystemProperties() throws Exception {
setSystemProperty(securityManagerPropertyPrefix + "className", "testClassName");

TestBrokerFactoryHandler.setBroker(new BrokerDTO());

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testSecurityManager(createdBroker.security, "testClassName", Collections.emptyList());
}

@Test
public void testCreateBrokerConfigurationWithSecurityManagerClassNameAndPropertiesFromSystemProperties() throws Exception {
setSystemProperty(securityManagerPropertyPrefix + "className", "testClassName");
setSystemProperty(securityManagerPropertyPrefix + "properties.testKey1", "testValue1");
setSystemProperty(securityManagerPropertyPrefix + "properties.testKey2", "testValue2");

TestBrokerFactoryHandler.setBroker(new BrokerDTO());

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testSecurityManager(createdBroker.security, "testClassName", List.of(
new PropertyDTO("testKey1", "testValue1"), new PropertyDTO("testKey2", "testValue2")));
}

@Test
public void testCreateBrokerConfigurationWithNewSecurityManagerClassNameFromExistingSecurityManagerAndSystemProperties() throws Exception {
setSystemProperty(securityManagerPropertyPrefix + "className", "newTestClassName");

SecurityManagerDTO security = new SecurityManagerDTO();
security.className = "testClassName";
security.properties = new ArrayList<>(List.of(
new PropertyDTO("testKey1", "testValue1"),
new PropertyDTO("testKey2", "testValue2")));
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testSecurityManager(createdBroker.security, "newTestClassName", List.of(
new PropertyDTO("testKey1", "testValue1"), new PropertyDTO("testKey2", "testValue2")));
}

@Test
public void testCreateBrokerConfigurationWithNewSecurityManagerClassNameAndPropertiesFromExistingSecurityManagerAndSystemProperties() throws Exception {
setSystemProperty(securityManagerPropertyPrefix + "className", "newTestClassName");
setSystemProperty(securityManagerPropertyPrefix + "properties.testKey1", "newTestValue1");
setSystemProperty(securityManagerPropertyPrefix + "properties.newTestKey2", "newTestValue2");

SecurityManagerDTO security = new SecurityManagerDTO();
security.className = "testClassName";
security.properties = new ArrayList<>(List.of(
new PropertyDTO("testKey1", "testValue1"),
new PropertyDTO("testKey2", "testValue2")));
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testSecurityManager(createdBroker.security, "newTestClassName", List.of(
new PropertyDTO("testKey1", "testValue1"), new PropertyDTO("testKey2", "testValue2"),
new PropertyDTO("testKey1", "newTestValue1"), new PropertyDTO("newTestKey2", "newTestValue2")));
}


@Test
public void testCreateBrokerConfigurationWithNewSecurityManagerClassNameFromExistingJaasSecurityAndSystemProperties() throws Exception {
setSystemProperty(securityManagerPropertyPrefix + "className", "newTestClassName");

JaasSecurityDTO security = new JaasSecurityDTO();
security.domain = "testDomain";
security.certificateDomain = "testCertificateDomain";
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testSecurityManager(createdBroker.security, "newTestClassName", Collections.emptyList());
}

@Test
public void testCreateBrokerConfigurationWithNewSecurityManagerClassNameAndPropertiesFromExistingJaasSecurityAndSystemProperties() throws Exception {
setSystemProperty(securityManagerPropertyPrefix + "className", "newTestClassName");
setSystemProperty(securityManagerPropertyPrefix + "properties.testKey1", "newTestValue1");
setSystemProperty(securityManagerPropertyPrefix + "properties.newTestKey2", "newTestValue2");

JaasSecurityDTO security = new JaasSecurityDTO();
security.domain = "testDomain";
security.certificateDomain = "testCertificateDomain";
BrokerDTO broker = new BrokerDTO();
broker.security = security;
TestBrokerFactoryHandler.setBroker(broker);

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testSecurityManager(createdBroker.security, "newTestClassName", List.of(
new PropertyDTO("testKey1", "newTestValue1"), new PropertyDTO("newTestKey2", "newTestValue2")));
}

private void testSecurityManager(SecurityDTO security, String expectedClassName, List<PropertyDTO> expectedProperties) throws Exception {
assertNotNull(security);
assertInstanceOf(SecurityManagerDTO.class, security);
SecurityManagerDTO securityManager = (SecurityManagerDTO)security;
assertEquals(expectedClassName, securityManager.className);

if (expectedProperties != null) {
assertEquals(expectedProperties.size(), securityManager.properties.size());
assertTrue(expectedProperties.stream().allMatch(expectedProperty ->
securityManager.properties.stream().anyMatch(property ->
Objects.equals(expectedProperty.key, property.key) &&
Objects.equals(expectedProperty.value, property.value))));
} else {
assertNull(securityManager.properties);
}
}

@Test
public void testJaasSecurityTakesPrecedenceOverSecurityManager() throws Exception {
setSystemProperty(securityJaasPropertyPrefix + "domain", "testDomain");
setSystemProperty(securityManagerPropertyPrefix + "className", "testClassName");

TestBrokerFactoryHandler.setBroker(new BrokerDTO());

BrokerDTO createdBroker = BrokerFactory.createBrokerConfiguration("test://config", null, null, null);

assertNotNull(createdBroker);
testJaasSecurity(createdBroker.security, "testDomain", null);
}
}
Loading