From bb945d388ebf04fee3583680e5ee86988b131e26 Mon Sep 17 00:00:00 2001 From: Yuta Mochizuki <41100271+yumochiz@users.noreply.github.com> Date: Fri, 5 Oct 2018 01:59:08 +0900 Subject: [PATCH] Add System Property Option for Athenz (#2707) --- .../AuthenticationProviderAthenz.java | 12 ++++++++++-- .../AuthenticationProviderAthenzTest.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pulsar-broker-auth-athenz/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenz.java b/pulsar-broker-auth-athenz/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenz.java index 3a00c2022a33a..c2cf45da15953 100644 --- a/pulsar-broker-auth-athenz/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenz.java +++ b/pulsar-broker-auth-athenz/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenz.java @@ -25,6 +25,7 @@ import javax.naming.AuthenticationException; +import org.apache.commons.lang3.StringUtils; import org.apache.pulsar.broker.authentication.AuthenticationDataSource; import org.apache.pulsar.broker.authentication.AuthenticationProvider; import org.slf4j.Logger; @@ -39,14 +40,21 @@ public class AuthenticationProviderAthenz implements AuthenticationProvider { private static final String DOMAIN_NAME_LIST = "athenzDomainNames"; + private static final String SYS_PROP_DOMAIN_NAME_LIST = "pulsar.athenz.domain.names"; + private List domainNameList = null; @Override public void initialize(ServiceConfiguration config) throws IOException { - if (config.getProperty(DOMAIN_NAME_LIST) == null) { + String domainNames; + if (config.getProperty(DOMAIN_NAME_LIST) != null) { + domainNames = (String) config.getProperty(DOMAIN_NAME_LIST); + } else if (!StringUtils.isEmpty(System.getProperty(SYS_PROP_DOMAIN_NAME_LIST))) { + domainNames = System.getProperty(SYS_PROP_DOMAIN_NAME_LIST); + } else { throw new IOException("No athenz domain name specified"); } - String domainNames = (String) config.getProperty(DOMAIN_NAME_LIST); + domainNameList = Lists.newArrayList(domainNames.split(",")); log.info("Supported domain names for athenz: {}", domainNameList); } diff --git a/pulsar-broker-auth-athenz/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenzTest.java b/pulsar-broker-auth-athenz/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenzTest.java index 75934b1e980da..1946a014e78f2 100644 --- a/pulsar-broker-auth-athenz/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenzTest.java +++ b/pulsar-broker-auth-athenz/src/test/java/org/apache/pulsar/broker/authentication/AuthenticationProviderAthenzTest.java @@ -20,6 +20,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; + import org.testng.annotations.Test; import org.testng.annotations.BeforeClass; @@ -63,6 +64,20 @@ public void setup() throws Exception { System.setProperty(ZpeConsts.ZPE_PROP_ATHENZ_CONF, "./src/test/resources/athenz.conf.test"); } + @Test + public void testInitilizeFromSystemPropeties() { + System.setProperty("pulsar.athenz.domain.names", "test_provider"); + ServiceConfiguration emptyConf = new ServiceConfiguration(); + Properties emptyProp = new Properties(); + emptyConf.setProperties(emptyProp); + AuthenticationProviderAthenz sysPropProvider = new AuthenticationProviderAthenz(); + try { + sysPropProvider.initialize(emptyConf); + } catch (Exception e) { + fail("Fail to Read pulsar.athenz.domain.names from System Properties"); + } + } + @Test public void testAuthenticateSignedToken() throws Exception {