From 5d9fa79705d271315f199de9c0ebf4be7903af7e Mon Sep 17 00:00:00 2001 From: Kalin Chan Date: Thu, 10 Oct 2024 14:45:55 +0100 Subject: [PATCH 1/2] FISH-9198 Add Microprofile Config TOML Support --- .../admingui/microprofile/Strings.properties | 15 +- .../microprofileConfigAPITabs.inc | 10 +- .../specs/configAPI/ordinalConfiguration.inc | 6 + .../specs/configAPI/tomlConfiguration.jsf | 126 ++++++++++++ appserver/packager/appserver-core/pom.xml | 6 +- .../microprofile/config-extensions/pom.xml | 9 +- .../extensions/toml/TOMLConfigSource.java | 190 ++++++++++++++++++ .../toml/TOMLConfigSourceConfiguration.java | 18 ++ .../admin/GetTOMLConfigurationCommand.java | 86 ++++++++ .../admin/SetTOMLConfigurationCommand.java | 120 +++++++++++ .../config/admin/ConfigSourceConstants.java | 3 +- .../config/admin/GetConfigOrdinal.java | 8 +- .../config/admin/SetConfigOrdinal.java | 9 +- .../extension/proxy/ConfigSourceProxy.java | 11 +- .../spi/MicroprofileConfigConfiguration.java | 8 +- 15 files changed, 610 insertions(+), 15 deletions(-) create mode 100644 appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/specs/configAPI/tomlConfiguration.jsf create mode 100644 appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java create mode 100644 appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSourceConfiguration.java create mode 100644 appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/GetTOMLConfigurationCommand.java create mode 100644 appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/SetTOMLConfigurationCommand.java diff --git a/appserver/admingui/microprofile-console-plugin/src/main/resources/fish/payara/admingui/microprofile/Strings.properties b/appserver/admingui/microprofile-console-plugin/src/main/resources/fish/payara/admingui/microprofile/Strings.properties index 9b862a59d5c..f21e43859ba 100644 --- a/appserver/admingui/microprofile-console-plugin/src/main/resources/fish/payara/admingui/microprofile/Strings.properties +++ b/appserver/admingui/microprofile-console-plugin/src/main/resources/fish/payara/admingui/microprofile/Strings.properties @@ -1,6 +1,6 @@ # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. # -# Copyright (c) [2018-2021] Payara Foundation and/or its affiliates. All rights reserved. +# Copyright (c) [2018-2024] Payara Foundation and/or its affiliates. All rights reserved. # # The contents of this file are subject to the terms of either the GNU # General Public License Version 2 only ("GPL") or the Common Development @@ -48,6 +48,7 @@ microprofile.configuration.openTracingTab=OpenTracing microprofile.configuration.configOrdinalTab=Ordinal microprofile.configuration.configPropertyTab=Property microprofile.configuration.configSecretsDirectoryTab=Directory +microprofile.configuration.tomlTab=TOML microprofile.configuration.awsSecretsConfigSourceTab=AWS Secrets microprofile.configuration.gcpSecretsConfigSourceTab=GCP Secrets microprofile.configuration.azureSecretsConfigSourceTab=Azure Secrets @@ -115,6 +116,8 @@ microprofile.specs.configuration.config.property.pageTitle=Property microprofile.specs.configuration.config.property.pageTitleHelpText=Configuration options to set the given property name and value in one of the built-in config sources. microprofile.specs.configuration.config.secretsDirectory.pageTitle=Secrets Directory microprofile.specs.configuration.config.secretsDirectory.pageTitleHelpText=Sets the directory to be used for the directory config source. +microprofile.specs.configuration.config.toml.pageTitle=TOML +microprofile.specs.configuration.config.toml.pageTitleHelpText=Sets the TOML Config Source configuration. microprofile.specs.configuration.config.awsSecretsConfigSource.pageTitle=AWS Secrets Config Source microprofile.specs.configuration.config.awsSecretsConfigSource.pageTitleHelpText=Configuration options for the config source which fetches data from AWS Secrets Manager. Make sure that the password aliases AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY exist. microprofile.specs.configuration.config.gcpSecretsConfigSource.pageTitle=GCP Secrets Config Source @@ -138,6 +141,7 @@ config.ordinal.configuration.jndi=JNDI config.ordinal.configuration.module=Module config.ordinal.configuration.password=Password config.ordinal.configuration.secretDirectory=Secret Directory +config.ordinal.configuration.toml=TOML config.ordinal.configuration.server=Server config.ordinal.configuration.cloud=Cloud config.ordinal.configuration.jdbc=JDBC @@ -152,7 +156,14 @@ config.property.configuration.moduleNameHelp=The name of the module when the Con config.secretsDirectory.configuration.directory=Directory config.secretsDirectory.configuration.directoryHelp=Full path to the directory containing configuration files. - +config.toml.configuration.enabledLabel=Enabled +config.toml.configuration.enabledHelpText=Enable the config source, which fetches data from the specified TOML file +config.toml.configuration.dynamicLabel=Dynamic +config.toml.configuration.dynamicHelpText=Configure the config source dynamically, which will not require a restart +config.toml.configuration.path=Path +config.toml.configuration.pathHelp=Full path to the TOML configuration file. +config.toml.configuration.depth=Depth +config.toml.configuration.depthHelp=The maximum recursion depth. config.aws.configuration.enabledLabel=Enabled config.aws.configuration.enabledHelpText=Enable the config source, which fetches data from AWS Secrets Manager config.aws.configuration.dynamicLabel=Dynamic diff --git a/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/microprofileConfigAPITabs.inc b/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/microprofileConfigAPITabs.inc index 8f0f54ced53..f085384910a 100644 --- a/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/microprofileConfigAPITabs.inc +++ b/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/microprofileConfigAPITabs.inc @@ -2,7 +2,7 @@ DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - Copyright (c) [2018-2020] Payara Foundation and/or its affiliates. All rights reserved. + Copyright (c) [2018-2024] Payara Foundation and/or its affiliates. All rights reserved. The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or the Common Development @@ -64,6 +64,14 @@ gf.redirect(page="#{request.contextPath}/microprofile/microprofile/specs/configAPI/secretsDirectoryConfiguration.jsf?configName=#{configName}"); /> + + + diff --git a/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/specs/configAPI/ordinalConfiguration.inc b/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/specs/configAPI/ordinalConfiguration.inc index 61757aeb704..6698a952dbd 100644 --- a/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/specs/configAPI/ordinalConfiguration.inc +++ b/appserver/admingui/microprofile-console-plugin/src/main/resources/microprofile/specs/configAPI/ordinalConfiguration.inc @@ -118,6 +118,12 @@ text="#{pageSession.microprofileConfigMap['secretDirOrdinality']}"/> + + + + + + + + + + + + + + + + +#include "/common/shared/alertMsg_1.inc" +#include "/microprofile/microprofile/microprofileConfigTabs.inc" +#include "/microprofile/microprofile/microprofileConfigAPITabs.inc" + + + + + + + + + + +#include "/common/shared/configNameSection.inc" + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/appserver/packager/appserver-core/pom.xml b/appserver/packager/appserver-core/pom.xml index 2f5d76c1983..916e7c75e43 100644 --- a/appserver/packager/appserver-core/pom.xml +++ b/appserver/packager/appserver-core/pom.xml @@ -2,7 +2,7 @@ diff --git a/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java new file mode 100644 index 00000000000..047b24d7b13 --- /dev/null +++ b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java @@ -0,0 +1,190 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/main/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.microprofile.config.extensions.toml; + +import com.fasterxml.jackson.dataformat.toml.TomlMapper; +import fish.payara.nucleus.microprofile.config.source.extension.ConfiguredExtensionConfigSource; +import fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration; +import jakarta.inject.Inject; +import org.jvnet.hk2.annotations.Service; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.logging.Logger; + +@Service(name = "toml-config-source") +public class TOMLConfigSource extends ConfiguredExtensionConfigSource { + + private static final Logger LOGGER = Logger.getLogger(TOMLConfigSource.class.getName()); + private final Map properties = new ConcurrentHashMap<>(); + private long lastModified; + + @Inject + MicroprofileConfigConfiguration mpConfig; + + @Override + public String getValue(String s) { + return getProperties().get(s); + } + + @Override + public String getName() { + return "toml"; + } + + @Override + public int getOrdinal() { + return Integer.parseInt(mpConfig.getTomlOrdinality()); + } + + @Override + public Map getProperties() { + Path tomlFilePath = getFilePath(); + if (tomlFilePath == null) { + return properties; + } + long tomlFileLastModified = tomlFilePath.toFile().lastModified(); + if (lastModified == tomlFileLastModified) { + return properties; + } + + try { + properties.clear(); + lastModified = tomlFileLastModified; + TomlMapper tomlMapper = new TomlMapper(); + Map config = tomlMapper.readValue(tomlFilePath.toFile(), Map.class); + + flattenToml(config, "", properties, 0, Integer.parseInt(configuration.getDepth())); + + } catch (Exception e) { + LOGGER.warning(e.getMessage()); + } + return properties; + } + + @Override + public Set getPropertyNames() { + return getProperties().keySet(); + } + + @Override + public boolean setValue(String name, String value) { + return false; + } + + @Override + public boolean deleteValue(String name) { + return false; + } + + @Override + public String getSource() { + return "toml"; + } + + private Path getFilePath() { + String path = configuration.getPath(); + if (path == null || path.isEmpty()) { + return null; + } + + Path tomlPath = Paths.get(path); + + if (!tomlPath.isAbsolute()) { + tomlPath = Paths.get(System.getProperty("com.sun.aas.instanceRoot"), tomlPath.toString()).normalize(); + } + + File file = tomlPath.toFile(); + return file.exists() ? tomlPath : null; + } + + private void flattenToml(Map tomlTable, String prefix, Map resultMap, int depth, int maxDepth) { + if (depth > maxDepth) { + throw new IllegalArgumentException("Exceeded maximum depth of " + maxDepth); + } + for (Object key : tomlTable.keySet()) { + Object value; + try { + value = tomlTable.get(key); + } catch (IllegalArgumentException ex) { + continue; + } + String fullKey = prefix.isEmpty() ? key.toString() : prefix + "." + key.toString(); + + if (value instanceof Map) { + // If the value is a TomlTable (nested table), recurse into it + flattenToml((Map) value, fullKey, resultMap, depth + 1, maxDepth); + } else if (value instanceof ArrayList) { + // If the value is a TomlArray, iterate through the array + flattenTomlArray((ArrayList) value, fullKey, resultMap, depth + 1, maxDepth); + } else { + // Otherwise, add the key-value pair to the result map + if (value == null) { + continue; + } + resultMap.put(fullKey, value.toString()); + } + } + } + + private void flattenTomlArray(ArrayList array, String prefix, Map resultMap, int depth, int maxDepth) { + if (depth > maxDepth) { + throw new IllegalArgumentException("Exceeded maximum depth of " + maxDepth); + } + for (int i = 0; i < array.size(); i++) { + Object arrayValue = array.get(i); + String arrayKey = prefix + "[" + i + "]"; + if (arrayValue instanceof Map) { + flattenToml((Map) arrayValue, arrayKey, resultMap, depth + 1, maxDepth); + } else if (arrayValue instanceof ArrayList) { + // Recursively flatten the nested array + flattenTomlArray((ArrayList) arrayValue, arrayKey, resultMap, depth + 1, maxDepth); + } else { + resultMap.put(arrayKey, arrayValue.toString()); + } + } + } +} diff --git a/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSourceConfiguration.java b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSourceConfiguration.java new file mode 100644 index 00000000000..864007326bd --- /dev/null +++ b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSourceConfiguration.java @@ -0,0 +1,18 @@ +package fish.payara.microprofile.config.extensions.toml; + +import fish.payara.nucleus.microprofile.config.spi.ConfigSourceConfiguration; +import org.jvnet.hk2.config.Attribute; +import org.jvnet.hk2.config.Configured; + +@Configured(name = "toml-config-source-configuration") + +public interface TOMLConfigSourceConfiguration extends ConfigSourceConfiguration { + + @Attribute(required = true) + String getPath(); + void setPath(String tomlPath); + + @Attribute(required = true, dataType = Integer.class) + String getDepth(); + void setDepth(String depth); +} diff --git a/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/GetTOMLConfigurationCommand.java b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/GetTOMLConfigurationCommand.java new file mode 100644 index 00000000000..64580cc2151 --- /dev/null +++ b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/GetTOMLConfigurationCommand.java @@ -0,0 +1,86 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/main/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.microprofile.config.extensions.toml.admin; + +import fish.payara.microprofile.config.extensions.toml.TOMLConfigSourceConfiguration; +import fish.payara.nucleus.microprofile.config.source.extension.BaseGetConfigSourceConfigurationCommand; +import fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration; +import org.glassfish.api.admin.CommandLock; +import org.glassfish.api.admin.ExecuteOn; +import org.glassfish.api.admin.RestEndpoint; +import org.glassfish.api.admin.RestEndpoints; +import org.glassfish.api.admin.RuntimeType; +import org.glassfish.config.support.CommandTarget; +import org.glassfish.config.support.TargetType; +import org.glassfish.hk2.api.PerLookup; +import org.jvnet.hk2.annotations.Service; + +import java.util.Map; + +/** + * asAdmin command to the get the configuration for the TOML Config Source + * + * @since 6.2024.11 + * @author Kalin Chan (Payara Foundation) + */ +@Service(name = "get-toml-config-source-configuration") +@PerLookup +@CommandLock(CommandLock.LockType.NONE) +@ExecuteOn(value = {RuntimeType.DAS, RuntimeType.INSTANCE}) +@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG, CommandTarget.DEPLOYMENT_GROUP}) +@RestEndpoints({ + @RestEndpoint(configBean = MicroprofileConfigConfiguration.class, + opType = RestEndpoint.OpType.GET, + path = "get-toml-config-source-configuration", + description = "List TOML Config Source Configuration") +}) +public class GetTOMLConfigurationCommand extends BaseGetConfigSourceConfigurationCommand { + + @Override + protected Map getConfigSourceConfiguration(TOMLConfigSourceConfiguration configuration) { + Map config = super.getConfigSourceConfiguration(configuration); + + if (configuration != null) { + config.put("Path", configuration.getPath()); + config.put("Depth", configuration.getDepth()); + } + return config; + } +} diff --git a/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/SetTOMLConfigurationCommand.java b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/SetTOMLConfigurationCommand.java new file mode 100644 index 00000000000..4143cf4527e --- /dev/null +++ b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/admin/SetTOMLConfigurationCommand.java @@ -0,0 +1,120 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can + * obtain a copy of the License at + * https://github.com/payara/Payara/blob/main/LICENSE.txt + * See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at glassfish/legal/LICENSE.txt. + * + * GPL Classpath Exception: + * The Payara Foundation designates this particular file as subject to the "Classpath" + * exception as provided by the Payara Foundation in the GPL Version 2 section of the License + * file that accompanied this code. + * + * Modifications: + * If applicable, add the following below the License Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyright [year] [name of copyright owner]" + * + * Contributor(s): + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package fish.payara.microprofile.config.extensions.toml.admin; + +import fish.payara.microprofile.config.extensions.toml.TOMLConfigSourceConfiguration; +import fish.payara.nucleus.microprofile.config.source.extension.BaseSetConfigSourceConfigurationCommand; +import fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration; +import org.glassfish.api.ActionReport; +import org.glassfish.api.Param; +import org.glassfish.api.admin.CommandLock; +import org.glassfish.api.admin.ExecuteOn; +import org.glassfish.api.admin.RestEndpoint; +import org.glassfish.api.admin.RestEndpoints; +import org.glassfish.api.admin.RuntimeType; +import org.glassfish.config.support.CommandTarget; +import org.glassfish.config.support.TargetType; +import org.glassfish.hk2.api.PerLookup; +import org.jvnet.hk2.annotations.Service; + +import java.beans.PropertyVetoException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * asAdmin command to the set the configuration for the TOML Config Source + * + * @since 6.2024.11 + * @author Kalin Chan (Payara Foundation) + */ +@Service(name = "set-toml-config-source-configuration") +@PerLookup +@CommandLock(CommandLock.LockType.NONE) +@ExecuteOn({RuntimeType.DAS, RuntimeType.INSTANCE}) +@TargetType(value = {CommandTarget.DAS, CommandTarget.STANDALONE_INSTANCE, CommandTarget.CLUSTER, CommandTarget.CLUSTERED_INSTANCE, CommandTarget.CONFIG, CommandTarget.DEPLOYMENT_GROUP}) +@RestEndpoints({ + @RestEndpoint(configBean = MicroprofileConfigConfiguration.class, + opType = RestEndpoint.OpType.POST, + path = "set-toml-config-source-configuration", + description = "Configures TOML Config Source") +}) +public class SetTOMLConfigurationCommand extends BaseSetConfigSourceConfigurationCommand { + + @Param + private String path; + + @Param + private int depth; + + + @Override + protected void applyValues(ActionReport report, TOMLConfigSourceConfiguration configuration) throws PropertyVetoException { + // do validation + Path tomlPath = Paths.get(path); + if (!Files.exists(tomlPath) || !Files.isReadable(tomlPath)) { + // try relative to the server root + report.appendMessage("Could not find readable file at " + tomlPath + "\n"); + if (tomlPath.isAbsolute()) { + report.setActionExitCode(ActionReport.ExitCode.FAILURE); + return; + } + Path instanceRoot = server.getInstanceRoot().toPath(); + Path relative = Paths.get(instanceRoot.toString(), path); + if (!Files.exists(relative) || !Files.isReadable(relative)) { + report.appendMessage("Could not find readable file at " + relative + "\n"); + report.setActionExitCode(ActionReport.ExitCode.FAILURE); + return; + } + tomlPath = relative; + } + report.appendMessage("Using readable file at " + tomlPath + "\n"); + + super.applyValues(report, configuration); + if (depth > 0) { + configuration.setDepth(String.valueOf(depth)); + } + + if (path != null) { + configuration.setPath(String.valueOf(tomlPath)); + } + } + +} diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/ConfigSourceConstants.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/ConfigSourceConstants.java index 8efb8a2268b..d555a098d01 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/ConfigSourceConstants.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/ConfigSourceConstants.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) [2020-2024] Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -53,6 +53,7 @@ public interface ConfigSourceConstants { String CLUSTER = "cluster"; String JNDI = "jndi"; String SECRETS = "secrets"; + String TOML = "toml"; String PASSWORD = "password"; String JDBC = "jdbc"; String CLOUD = "cloud"; diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/GetConfigOrdinal.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/GetConfigOrdinal.java index 6cc8c7986a0..5f598fb9211 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/GetConfigOrdinal.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/GetConfigOrdinal.java @@ -52,6 +52,8 @@ import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.PASSWORD; import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.SECRETS; import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.SERVER; +import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.TOML; + import fish.payara.nucleus.microprofile.config.spi.MicroprofileConfigConfiguration; import java.util.HashMap; import java.util.Map; @@ -90,7 +92,7 @@ }) public class GetConfigOrdinal implements AdminCommand { - @Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster,jndi,secrets,password,jdbc,cloud,ldap", defaultValue = DOMAIN) + @Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster,jndi,secrets,toml,password,jdbc,cloud,ldap", defaultValue = DOMAIN) String source; @Param(optional = true, defaultValue = "server") // if no target is specified it will be the DAS @@ -138,6 +140,10 @@ public void execute(AdminCommandContext context) { result = Integer.parseInt(serviceConfig.getSecretDirOrdinality()); break; } + case TOML: { + result = Integer.parseInt(serviceConfig.getTomlOrdinality()); + break; + } case PASSWORD: { result = Integer.parseInt(serviceConfig.getPasswordOrdinality()); break; diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/SetConfigOrdinal.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/SetConfigOrdinal.java index 5dcf285a6fd..435a77c164f 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/SetConfigOrdinal.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/admin/SetConfigOrdinal.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) [2017-2021] Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) [2017-2024] Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -55,6 +55,7 @@ import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.PASSWORD; import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.SECRETS; import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.SERVER; +import static fish.payara.nucleus.microprofile.config.admin.ConfigSourceConstants.TOML; import org.glassfish.api.Param; import org.glassfish.api.admin.AdminCommand; @@ -94,7 +95,7 @@ public class SetConfigOrdinal implements AdminCommand { @Param() int ordinal; - @Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster,jndi,secrets,password,jdbc,cloud,ldap", defaultValue = DOMAIN) + @Param(optional = true, acceptableValues = "domain,config,server,application,module,cluster,jndi,secrets,toml,password,jdbc,cloud,ldap", defaultValue = DOMAIN) String source; @Param(optional = true, defaultValue = "server") // if no target is specified it will be the DAS @@ -148,6 +149,10 @@ public Object run(MicroprofileConfigConfiguration config) { config.setSecretDirOrdinality(Integer.toString(ordinal)); break; } + case TOML: { + config.setTomlOrdinality(Integer.toString(ordinal)); + break; + } case PASSWORD : { config.setPasswordOrdinality(Integer.toString(ordinal)); break; diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/extension/proxy/ConfigSourceProxy.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/extension/proxy/ConfigSourceProxy.java index d6e4f654d08..2895328f4be 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/extension/proxy/ConfigSourceProxy.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/source/extension/proxy/ConfigSourceProxy.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) [2020] Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) [2020-2024] Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -111,5 +111,12 @@ public boolean deleteValue(String name) { } return false; } - + + @Override + public int getOrdinal() { + if (delegate != null) { + return delegate.getOrdinal(); + } + return ExtensionConfigSource.super.getOrdinal(); + } } diff --git a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/spi/MicroprofileConfigConfiguration.java b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/spi/MicroprofileConfigConfiguration.java index d2d40c1cbf7..081af3762cf 100644 --- a/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/spi/MicroprofileConfigConfiguration.java +++ b/nucleus/payara-modules/nucleus-microprofile/config-service/src/main/java/fish/payara/nucleus/microprofile/config/spi/MicroprofileConfigConfiguration.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) [2017-2021] Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) [2017-2024] Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -63,7 +63,7 @@ */ @Configured(name="microprofile-config") public interface MicroprofileConfigConfiguration extends ConfigExtension { - + @Attribute(defaultValue = DirConfigSource.DEFAULT_DIR, dataType = String.class) String getSecretDir(); void setSecretDir(String directory); @@ -72,6 +72,10 @@ public interface MicroprofileConfigConfiguration extends ConfigExtension { String getSecretDirOrdinality(); void setSecretDirOrdinality(String message); + @Attribute(defaultValue = "95", dataType = Integer.class) + String getTomlOrdinality(); + void setTomlOrdinality(String value); + @Attribute(defaultValue = "105", dataType = Integer.class) String getPasswordOrdinality(); void setPasswordOrdinality(String message); From 8a910a494d016e405c3c54fbcbe698c95f3f8c9d Mon Sep 17 00:00:00 2001 From: Kalin Chan Date: Mon, 14 Oct 2024 13:20:15 +0100 Subject: [PATCH 2/2] FISH-9198 Improve exception message --- .../microprofile/config/extensions/toml/TOMLConfigSource.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java index 047b24d7b13..41b7609e8f2 100644 --- a/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java +++ b/appserver/payara-appserver-modules/microprofile/config-extensions/src/main/java/fish/payara/microprofile/config/extensions/toml/TOMLConfigSource.java @@ -172,7 +172,7 @@ private void flattenToml(Map tomlTable, String prefix, Map private void flattenTomlArray(ArrayList array, String prefix, Map resultMap, int depth, int maxDepth) { if (depth > maxDepth) { - throw new IllegalArgumentException("Exceeded maximum depth of " + maxDepth); + throw new IllegalArgumentException(String.format("Exceeded maximum depth of %s with depth of %s", maxDepth, depth)); } for (int i = 0; i < array.size(); i++) { Object arrayValue = array.get(i);