From 95953e8b9849f6d6c5a37164d33744da8e46533e Mon Sep 17 00:00:00 2001 From: thanicz Date: Wed, 27 Nov 2024 13:30:35 +0100 Subject: [PATCH 1/3] KNOX-3051: Ability to extend classpath with configurable paths --- .../apache/knox/gateway/launcher/Command.java | 8 +- .../knox/gateway/launcher/Extender.java | 77 +++++++ .../knox/gateway/launcher/ExtenderTest.java | 195 ++++++++++++++++++ 3 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java create mode 100644 gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java diff --git a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java index f0a3b49451..52f92077ff 100644 --- a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java +++ b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.FilenameFilter; +import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; @@ -53,14 +54,17 @@ class Command { Boolean fork = Boolean.FALSE; Boolean redirect = Boolean.FALSE; // Controls redirecting stderr to stdout if forking. Boolean restream = Boolean.TRUE; // Controls creation of threads to read/write stdin, stdout, stderr of child if forking. + Extender extender; - Command( File base, Properties config, String[] args ) throws MalformedURLException { + Command( File base, Properties config, String[] args ) throws IOException { this.base = base; this.mainArgs = args ; + this.extender = new Extender( base, config ); consumeConfig( config ); } - void consumeConfig( Properties config ) throws MalformedURLException { + void consumeConfig( Properties config ) throws IOException { + extender.extendClassPathProperty(); mainClass = config.getProperty( MAIN_CLASS ); config.remove( MAIN_CLASS ); mainMethod = config.getProperty( MAIN_METHOD, mainMethod ); diff --git a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java new file mode 100644 index 0000000000..9a87e332f9 --- /dev/null +++ b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java @@ -0,0 +1,77 @@ +/* + * 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.knox.gateway.launcher; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Extender { + + private static final String CLASSPATH_EXTENSION_PROPERTY = "gateway.server.classpath.extension"; + private static final String CLASSPATH_PROPERTY_PATTERN = "\\s*" + CLASSPATH_EXTENSION_PROPERTY + "\\s*(.*?)\\s*"; + private static final String CONFIG_FILE = "gateway-site.xml"; + private static final String CONFIG_PATH = "../conf/" + CONFIG_FILE; + private static final String CLASS_PATH_PROPERTY = "class.path"; + private static final String MAIN_CLASS_PROPERTY = "main.class"; + private static final String GATEWAY_SERVER_MAIN_CLASS = "org.apache.knox.gateway.GatewayServer"; + private static final String[] CLASS_PATH_DELIMITERS = new String[]{",", ";"}; + + private final File base; + private final Properties properties; + + public Extender(File base, Properties properties) { + this.base = base; + this.properties = properties; + } + + public void extendClassPathProperty() throws IOException { + Path configFilePath = Paths.get(base.getPath(), CONFIG_PATH); + if (GATEWAY_SERVER_MAIN_CLASS.equals(properties.getProperty(MAIN_CLASS_PROPERTY)) && Files.isReadable(configFilePath)) { + String configContent = new String(Files.readAllBytes(configFilePath), StandardCharsets.UTF_8); + extractExtensionPathIntoProperty(configContent); + } + } + + protected void extractExtensionPathIntoProperty(String configContent) { + Pattern pattern = Pattern.compile(CLASSPATH_PROPERTY_PATTERN, Pattern.DOTALL); + Matcher matcher = pattern.matcher(configContent); + + if (matcher.find()) { + StringBuilder newClassPath = new StringBuilder(matcher.group(1).trim()); + if (newClassPath.length() > 0) { + if (!endsWithDelimiter(newClassPath.toString())) { + newClassPath.append(CLASS_PATH_DELIMITERS[1]); + } + newClassPath.append(properties.getProperty(CLASS_PATH_PROPERTY)); + properties.setProperty(CLASS_PATH_PROPERTY, newClassPath.toString()); + } + } + } + + private boolean endsWithDelimiter(String path) { + return Arrays.stream(CLASS_PATH_DELIMITERS).anyMatch(path::endsWith); + } +} diff --git a/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java b/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java new file mode 100644 index 0000000000..f5adc73786 --- /dev/null +++ b/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java @@ -0,0 +1,195 @@ +/* + * 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.knox.gateway.launcher; + + +import org.junit.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class ExtenderTest { + + private Path tempDir; + private Path confDir; + private Path configFilePath; + + @Test + public void extendClassPathPropertyTest() throws IOException { + this.setupDirs(); + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + properties.setProperty("main.class", "org.apache.knox.gateway.GatewayServer"); + Extender extender = new Extender(confDir.toFile(), properties); + + String configContent = this.getConfigContent("/new/classp/*"); + Files.write(configFilePath, configContent.getBytes(StandardCharsets.UTF_8)); + extender.extendClassPathProperty(); + + assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); + this.cleanUpDirs(); + } + + @Test + public void extendClassPathPropertyDifferentMainClassTest() throws IOException { + this.setupDirs(); + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + properties.setProperty("main.class", "org.apache.knox.gateway.KnoxCLI"); + Extender extender = new Extender(confDir.toFile(), properties); + + String configContent = this.getConfigContent("/new/classp/*"); + Files.write(configFilePath, configContent.getBytes(StandardCharsets.UTF_8)); + extender.extendClassPathProperty(); + + assertEquals("classpath", properties.getProperty("class.path")); + this.cleanUpDirs(); + } + + @Test + public void extractExtensionPathIntoPropertyNoDelimTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = this.getConfigContent("/new/classp/*"); + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); + } + + @Test + public void extractExtensionPathIntoPropertyXMLFormatTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = this.getConfigContent("/new/classp/*;"); + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); + } + + @Test + public void extractExtensionPathIntoPropertyWhitespaceTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = this.getConfigContent(" /new/classp/*; "); + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); + } + + @Test + public void extractExtensionPathIntoPropertyMultipleTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = this.getConfigContent("/new/classp/*,../classp"); + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("/new/classp/*,../classp;classpath", properties.getProperty("class.path")); + } + + @Test + public void extractExtensionPathIntoPropertyEmptyTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = this.getConfigContent(""); + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("classpath", properties.getProperty("class.path")); + } + + @Test + public void extractExtensionPathIntoPropertyEmptyWhitespaceTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = this.getConfigContent(" "); + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("classpath", properties.getProperty("class.path")); + } + + @Test + public void extractExtensionPathIntoPropertyNoConfigTest() { + Properties properties = new Properties(); + properties.setProperty("class.path", "classpath"); + Extender extender = new Extender(null, properties); + + String configContent = + "\n" + + " \n" + + " gateway.webshell.read.buffer.size\n" + + " 1024\n" + + " Web Shell buffer size for reading\n" + + " \n" + + "\n" + + " \n" + + " \n" + + " gateway.websocket.JWT.validation.feature.enabled\n" + + " true\n" + + " Enable/Disable websocket JWT validation at websocket layer.\n" + + " \n" + + "\n" + + " \n" + + " \n" + + " knox.homepage.logout.enabled\n" + + " true\n" + + " Enable/disable logout from the Knox Homepage.\n" + + " \n" + + ""; + + extender.extractExtensionPathIntoProperty(configContent); + + assertEquals("classpath", properties.getProperty("class.path")); + } + + private String getConfigContent(String extensionValue) { + return "\n" + + " \n" + + " gateway.server.classpath.extension\n" + + " " + extensionValue + "\n" + + " \n" + + ""; + } + + private void setupDirs() throws IOException { + tempDir = Files.createTempDirectory("cp_extender_test"); + confDir = Files.createDirectory(tempDir.resolve("conf")); + configFilePath = confDir.resolve("gateway-site.xml"); + } + + private void cleanUpDirs() throws IOException { + Files.deleteIfExists(configFilePath); + Files.deleteIfExists(confDir); + Files.deleteIfExists(tempDir); + } +} From b83c75d8f90bcd42419230e680d069ee6c0e8bd5 Mon Sep 17 00:00:00 2001 From: thanicz Date: Mon, 2 Dec 2024 13:37:02 +0100 Subject: [PATCH 2/3] KNOX-3051: Code cleanup --- .../knox/gateway/launcher/Extender.java | 4 +- .../knox/gateway/launcher/ExtenderTest.java | 61 ++--- .../src/test/resources/gateway-site-test.xml | 210 ++++++++++++++++++ 3 files changed, 233 insertions(+), 42 deletions(-) create mode 100644 gateway-util-launcher/src/test/resources/gateway-site-test.xml diff --git a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java index 9a87e332f9..0cf60da82a 100644 --- a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java +++ b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java @@ -41,6 +41,7 @@ public class Extender { private final File base; private final Properties properties; + private final Pattern pattern = Pattern.compile(CLASSPATH_PROPERTY_PATTERN, Pattern.DOTALL); public Extender(File base, Properties properties) { this.base = base; @@ -56,8 +57,7 @@ public void extendClassPathProperty() throws IOException { } protected void extractExtensionPathIntoProperty(String configContent) { - Pattern pattern = Pattern.compile(CLASSPATH_PROPERTY_PATTERN, Pattern.DOTALL); - Matcher matcher = pattern.matcher(configContent); + final Matcher matcher = pattern.matcher(configContent); if (matcher.find()) { StringBuilder newClassPath = new StringBuilder(matcher.group(1).trim()); diff --git a/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java b/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java index f5adc73786..e288a5012d 100644 --- a/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java +++ b/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java @@ -18,8 +18,11 @@ package org.apache.knox.gateway.launcher; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -34,9 +37,22 @@ public class ExtenderTest { private Path confDir; private Path configFilePath; + @Before + public void setupDirs() throws IOException { + tempDir = Files.createTempDirectory("cp_extender_test"); + confDir = Files.createDirectory(tempDir.resolve("conf")); + configFilePath = confDir.resolve("gateway-site.xml"); + } + + @After + public void cleanUpDirs() throws IOException { + Files.deleteIfExists(configFilePath); + Files.deleteIfExists(confDir); + Files.deleteIfExists(tempDir); + } + @Test public void extendClassPathPropertyTest() throws IOException { - this.setupDirs(); Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); properties.setProperty("main.class", "org.apache.knox.gateway.GatewayServer"); @@ -47,12 +63,10 @@ public void extendClassPathPropertyTest() throws IOException { extender.extendClassPathProperty(); assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); - this.cleanUpDirs(); } @Test public void extendClassPathPropertyDifferentMainClassTest() throws IOException { - this.setupDirs(); Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); properties.setProperty("main.class", "org.apache.knox.gateway.KnoxCLI"); @@ -63,7 +77,6 @@ public void extendClassPathPropertyDifferentMainClassTest() throws IOException { extender.extendClassPathProperty(); assertEquals("classpath", properties.getProperty("class.path")); - this.cleanUpDirs(); } @Test @@ -139,35 +152,15 @@ public void extractExtensionPathIntoPropertyEmptyWhitespaceTest() { } @Test - public void extractExtensionPathIntoPropertyNoConfigTest() { + public void extractExtensionPathIntoPropertyNoConfigTest() throws IOException { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); Extender extender = new Extender(null, properties); - String configContent = - "\n" + - " \n" + - " gateway.webshell.read.buffer.size\n" + - " 1024\n" + - " Web Shell buffer size for reading\n" + - " \n" + - "\n" + - " \n" + - " \n" + - " gateway.websocket.JWT.validation.feature.enabled\n" + - " true\n" + - " Enable/Disable websocket JWT validation at websocket layer.\n" + - " \n" + - "\n" + - " \n" + - " \n" + - " knox.homepage.logout.enabled\n" + - " true\n" + - " Enable/disable logout from the Knox Homepage.\n" + - " \n" + - ""; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("gateway-site-test.xml").getFile()); - extender.extractExtensionPathIntoProperty(configContent); + extender.extractExtensionPathIntoProperty(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)); assertEquals("classpath", properties.getProperty("class.path")); } @@ -180,16 +173,4 @@ private String getConfigContent(String extensionValue) { " \n" + ""; } - - private void setupDirs() throws IOException { - tempDir = Files.createTempDirectory("cp_extender_test"); - confDir = Files.createDirectory(tempDir.resolve("conf")); - configFilePath = confDir.resolve("gateway-site.xml"); - } - - private void cleanUpDirs() throws IOException { - Files.deleteIfExists(configFilePath); - Files.deleteIfExists(confDir); - Files.deleteIfExists(tempDir); - } } diff --git a/gateway-util-launcher/src/test/resources/gateway-site-test.xml b/gateway-util-launcher/src/test/resources/gateway-site-test.xml new file mode 100644 index 0000000000..b442e19985 --- /dev/null +++ b/gateway-util-launcher/src/test/resources/gateway-site-test.xml @@ -0,0 +1,210 @@ + + + + + + gateway.service.alias.impl + org.apache.knox.gateway.services.security.impl.RemoteAliasService + + + gateway.port + 8443 + The HTTP port for the Gateway. + + + + gateway.path + gateway + The default context path for the gateway. + + + + gateway.gateway.conf.dir + deployments + The directory within GATEWAY_HOME that contains gateway topology files and deployments. + + + + gateway.hadoop.kerberos.secured + false + Boolean flag indicating whether the Hadoop cluster protected by Gateway is secured with Kerberos + + + + java.security.krb5.conf + /etc/knox/conf/krb5.conf + Absolute path to krb5.conf file + + + + java.security.auth.login.config + /etc/knox/conf/krb5JAASLogin.conf + Absolute path to JAAS login config file + + + + sun.security.krb5.debug + false + Boolean flag indicating whether to enable debug messages for krb5 authentication + + + + + gateway.websocket.feature.enabled + false + Enable/Disable websocket feature. + + + + gateway.scope.cookies.feature.enabled + false + Enable/Disable cookie scoping feature. + + + + gateway.cluster.config.monitor.ambari.enabled + false + Enable/disable Ambari cluster configuration monitoring. + + + + gateway.cluster.config.monitor.ambari.interval + 60 + The interval (in seconds) for polling Ambari for cluster configuration changes. + + + + + gateway.webshell.feature.enabled + false + Enable/Disable webshell feature. + + + gateway.webshell.max.concurrent.sessions + 20 + Maximum number of total concurrent webshell sessions + + + gateway.webshell.audit.logging.enabled + false + [Experimental Feature] Enable/Disable webshell command audit logging. + NOTE: Turning this on might log secrets that might be part of + command line arguments, please consider this before turning this on. + + + gateway.webshell.read.buffer.size + 1024 + Web Shell buffer size for reading + + + + + gateway.websocket.JWT.validation.feature.enabled + true + Enable/Disable websocket JWT validation at websocket layer. + + + + + knox.homepage.logout.enabled + true + Enable/disable logout from the Knox Homepage. + + + + + gateway.knox.token.management.users.can.see.all.tokens + admin + A comma separated list of user names who can see all tokens on the Token Management page + + + + + gateway.knox.token.eviction.grace.period + 0 + A duration (in seconds) beyond a token’s expiration to wait before evicting its state. This configuration only applies when server-managed token state is enabled either in gateway-site or at the topology level. + + + + + gateway.application.path.alias.token-generation + tokengen + + + + + + gateway.knox.admin.groups + admin + + + + + gateway.group.config.hadoop.security.group.mapping + org.apache.hadoop.security.LdapGroupsMapping + + + gateway.group.config.hadoop.security.group.mapping.ldap.bind.user + uid=guest,ou=people,dc=hadoop,dc=apache,dc=org + + + gateway.group.config.hadoop.security.group.mapping.ldap.bind.password + guest-password + + + gateway.group.config.hadoop.security.group.mapping.ldap.url + ldap://localhost:33389 + + + gateway.group.config.hadoop.security.group.mapping.ldap.base + + + + gateway.group.config.hadoop.security.group.mapping.ldap.search.filter.user + (&(|(objectclass=person)(objectclass=applicationProcess))(cn={0})) + + + gateway.group.config.hadoop.security.group.mapping.ldap.search.filter.group + (objectclass=groupOfNames) + + + gateway.group.config.hadoop.security.group.mapping.ldap.search.attr.member + member + + + gateway.group.config.hadoop.security.group.mapping.ldap.search.attr.group.name + cn + + + gateway.dispatch.whitelist.services + DATANODE,HBASEUI,HDFSUI,JOBHISTORYUI,NODEUI,YARNUI,knoxauth + The comma-delimited list of service roles for which the gateway.dispatch.whitelist should be applied. + + + gateway.dispatch.whitelist + ^https?:\/\/(localhost|127\.0\.0\.1|0:0:0:0:0:0:0:1|::1):[0-9].*$ + The whitelist to be applied for dispatches associated with the service roles specified by gateway.dispatch.whitelist.services. + If the value is DEFAULT, a domain-based whitelist will be derived from the Knox host. + + + gateway.xforwarded.header.context.append.servicename + LIVYSERVER + Add service name to x-forward-context header for the list of services defined above. + + From 46638642e8ca626e3a95f36652fe742d43cc9ecb Mon Sep 17 00:00:00 2001 From: thanicz Date: Wed, 4 Dec 2024 14:09:28 +0100 Subject: [PATCH 3/3] KNOX-3051: Rename Extender class, modified Extender to not retain reference to the properties --- .../apache/knox/gateway/launcher/Command.java | 6 +-- ...va => GatewayServerClasspathExtender.java} | 12 +++--- ...> GatewayServerClasspathExtenderTest.java} | 38 +++++++++---------- 3 files changed, 27 insertions(+), 29 deletions(-) rename gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/{Extender.java => GatewayServerClasspathExtender.java} (91%) rename gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/{ExtenderTest.java => GatewayServerClasspathExtenderTest.java} (73%) diff --git a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java index 52f92077ff..9cbee68843 100644 --- a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java +++ b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Command.java @@ -54,17 +54,17 @@ class Command { Boolean fork = Boolean.FALSE; Boolean redirect = Boolean.FALSE; // Controls redirecting stderr to stdout if forking. Boolean restream = Boolean.TRUE; // Controls creation of threads to read/write stdin, stdout, stderr of child if forking. - Extender extender; + GatewayServerClasspathExtender gatewayServerClasspathExtender; Command( File base, Properties config, String[] args ) throws IOException { this.base = base; this.mainArgs = args ; - this.extender = new Extender( base, config ); + this.gatewayServerClasspathExtender = new GatewayServerClasspathExtender( base ); consumeConfig( config ); } void consumeConfig( Properties config ) throws IOException { - extender.extendClassPathProperty(); + gatewayServerClasspathExtender.extendClassPathProperty(config); mainClass = config.getProperty( MAIN_CLASS ); config.remove( MAIN_CLASS ); mainMethod = config.getProperty( MAIN_METHOD, mainMethod ); diff --git a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/GatewayServerClasspathExtender.java similarity index 91% rename from gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java rename to gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/GatewayServerClasspathExtender.java index 0cf60da82a..caae4f530b 100644 --- a/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/Extender.java +++ b/gateway-util-launcher/src/main/java/org/apache/knox/gateway/launcher/GatewayServerClasspathExtender.java @@ -28,7 +28,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public class Extender { +public class GatewayServerClasspathExtender { private static final String CLASSPATH_EXTENSION_PROPERTY = "gateway.server.classpath.extension"; private static final String CLASSPATH_PROPERTY_PATTERN = "\\s*" + CLASSPATH_EXTENSION_PROPERTY + "\\s*(.*?)\\s*"; @@ -40,23 +40,21 @@ public class Extender { private static final String[] CLASS_PATH_DELIMITERS = new String[]{",", ";"}; private final File base; - private final Properties properties; private final Pattern pattern = Pattern.compile(CLASSPATH_PROPERTY_PATTERN, Pattern.DOTALL); - public Extender(File base, Properties properties) { + public GatewayServerClasspathExtender(File base) { this.base = base; - this.properties = properties; } - public void extendClassPathProperty() throws IOException { + public void extendClassPathProperty(Properties properties) throws IOException { Path configFilePath = Paths.get(base.getPath(), CONFIG_PATH); if (GATEWAY_SERVER_MAIN_CLASS.equals(properties.getProperty(MAIN_CLASS_PROPERTY)) && Files.isReadable(configFilePath)) { String configContent = new String(Files.readAllBytes(configFilePath), StandardCharsets.UTF_8); - extractExtensionPathIntoProperty(configContent); + extractExtensionPathIntoProperty(configContent, properties); } } - protected void extractExtensionPathIntoProperty(String configContent) { + protected void extractExtensionPathIntoProperty(String configContent, Properties properties) { final Matcher matcher = pattern.matcher(configContent); if (matcher.find()) { diff --git a/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java b/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/GatewayServerClasspathExtenderTest.java similarity index 73% rename from gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java rename to gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/GatewayServerClasspathExtenderTest.java index e288a5012d..81dfbff823 100644 --- a/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/ExtenderTest.java +++ b/gateway-util-launcher/src/test/java/org/apache/knox/gateway/launcher/GatewayServerClasspathExtenderTest.java @@ -31,7 +31,7 @@ import static org.junit.Assert.assertEquals; -public class ExtenderTest { +public class GatewayServerClasspathExtenderTest { private Path tempDir; private Path confDir; @@ -56,11 +56,11 @@ public void extendClassPathPropertyTest() throws IOException { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); properties.setProperty("main.class", "org.apache.knox.gateway.GatewayServer"); - Extender extender = new Extender(confDir.toFile(), properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(confDir.toFile()); String configContent = this.getConfigContent("/new/classp/*"); Files.write(configFilePath, configContent.getBytes(StandardCharsets.UTF_8)); - extender.extendClassPathProperty(); + gatewayServerClasspathExtender.extendClassPathProperty(properties); assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); } @@ -70,11 +70,11 @@ public void extendClassPathPropertyDifferentMainClassTest() throws IOException { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); properties.setProperty("main.class", "org.apache.knox.gateway.KnoxCLI"); - Extender extender = new Extender(confDir.toFile(), properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(confDir.toFile()); String configContent = this.getConfigContent("/new/classp/*"); Files.write(configFilePath, configContent.getBytes(StandardCharsets.UTF_8)); - extender.extendClassPathProperty(); + gatewayServerClasspathExtender.extendClassPathProperty(properties); assertEquals("classpath", properties.getProperty("class.path")); } @@ -83,10 +83,10 @@ public void extendClassPathPropertyDifferentMainClassTest() throws IOException { public void extractExtensionPathIntoPropertyNoDelimTest() { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); String configContent = this.getConfigContent("/new/classp/*"); - extender.extractExtensionPathIntoProperty(configContent); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties); assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); } @@ -95,10 +95,10 @@ public void extractExtensionPathIntoPropertyNoDelimTest() { public void extractExtensionPathIntoPropertyXMLFormatTest() { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); String configContent = this.getConfigContent("/new/classp/*;"); - extender.extractExtensionPathIntoProperty(configContent); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties); assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); } @@ -107,10 +107,10 @@ public void extractExtensionPathIntoPropertyXMLFormatTest() { public void extractExtensionPathIntoPropertyWhitespaceTest() { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); String configContent = this.getConfigContent(" /new/classp/*; "); - extender.extractExtensionPathIntoProperty(configContent); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties); assertEquals("/new/classp/*;classpath", properties.getProperty("class.path")); } @@ -119,10 +119,10 @@ public void extractExtensionPathIntoPropertyWhitespaceTest() { public void extractExtensionPathIntoPropertyMultipleTest() { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); String configContent = this.getConfigContent("/new/classp/*,../classp"); - extender.extractExtensionPathIntoProperty(configContent); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties); assertEquals("/new/classp/*,../classp;classpath", properties.getProperty("class.path")); } @@ -131,10 +131,10 @@ public void extractExtensionPathIntoPropertyMultipleTest() { public void extractExtensionPathIntoPropertyEmptyTest() { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); String configContent = this.getConfigContent(""); - extender.extractExtensionPathIntoProperty(configContent); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties); assertEquals("classpath", properties.getProperty("class.path")); } @@ -143,10 +143,10 @@ public void extractExtensionPathIntoPropertyEmptyTest() { public void extractExtensionPathIntoPropertyEmptyWhitespaceTest() { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); String configContent = this.getConfigContent(" "); - extender.extractExtensionPathIntoProperty(configContent); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(configContent, properties); assertEquals("classpath", properties.getProperty("class.path")); } @@ -155,12 +155,12 @@ public void extractExtensionPathIntoPropertyEmptyWhitespaceTest() { public void extractExtensionPathIntoPropertyNoConfigTest() throws IOException { Properties properties = new Properties(); properties.setProperty("class.path", "classpath"); - Extender extender = new Extender(null, properties); + GatewayServerClasspathExtender gatewayServerClasspathExtender = new GatewayServerClasspathExtender(null); ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("gateway-site-test.xml").getFile()); - extender.extractExtensionPathIntoProperty(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)); + gatewayServerClasspathExtender.extractExtensionPathIntoProperty(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8), properties); assertEquals("classpath", properties.getProperty("class.path")); }