diff --git a/nucleus/admin/rest/rest-service/src/main/java/org/glassfish/admin/rest/utils/ResourceUtil.java b/nucleus/admin/rest/rest-service/src/main/java/org/glassfish/admin/rest/utils/ResourceUtil.java index 43f39e92680..2aaf3bd3cdf 100644 --- a/nucleus/admin/rest/rest-service/src/main/java/org/glassfish/admin/rest/utils/ResourceUtil.java +++ b/nucleus/admin/rest/rest-service/src/main/java/org/glassfish/admin/rest/utils/ResourceUtil.java @@ -14,9 +14,10 @@ * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 */ - package org.glassfish.admin.rest.utils; +import static java.util.stream.Collectors.joining; + import com.sun.enterprise.config.serverbeans.Config; import com.sun.enterprise.config.serverbeans.Domain; @@ -98,6 +99,7 @@ * @author Rajeshwar Patil */ public class ResourceUtil { + private static final String DAS_LOOK_FOR_CERT_PROPERTY_NAME = "org.glassfish.admin.DASCheckAdminCert"; private static final String MESSAGE_PARAMETERS = "messageParameters"; private static RestConfig restConfig = null; @@ -139,9 +141,9 @@ private ResourceUtil() { } /** - * Adjust the input parameters. In case of POST and DELETE methods, user can provide name, id or DEFAULT parameter for - * primary parameter(i.e the object to create or delete). This method is used to rename primary parameter name to - * DEFAULT irrespective of what user provides. + * Adjust the input parameters. In case of POST and DELETE methods, user can provide name, id or DEFAULT parameter + * for primary parameter(i.e the object to create or delete). This method is used to rename primary parameter name + * to DEFAULT irrespective of what user provides. */ public static void adjustParameters(Map data) { if (data != null) { @@ -155,9 +157,9 @@ public static void adjustParameters(Map data) { } /** - * Adjust the input parameters. In case of POST and DELETE methods, user can provide id or DEFAULT parameter for primary - * parameter(i.e the object to create or delete). This method is used to rename primary parameter name to DEFAULT - * irrespective of what user provides. + * Adjust the input parameters. In case of POST and DELETE methods, user can provide id or DEFAULT parameter for + * primary parameter(i.e the object to create or delete). This method is used to rename primary parameter name to + * DEFAULT irrespective of what user provides. */ public static void defineDefaultParameters(Map data) { if (data != null) { @@ -218,6 +220,9 @@ public static RestActionReporter runCommand(String commandName, ParameterMap par * @return */ public static RestActionReporter runCommand(String commandName, ParameterMap parameters, Subject subject, boolean managedJob) { + + logCommand(commandName, parameters); + CommandRunner cr = Globals.getDefaultHabitat().getService(CommandRunner.class); RestActionReporter ar = new RestActionReporter(); final CommandInvocation commandInvocation = cr.getCommandInvocation(commandName, ar, subject); @@ -264,6 +269,35 @@ public ActionReport process(ActionReport report, EventOutput ec) { }); } + private static void logCommand(String commandName, ParameterMap parameters) { + if (shouldLogCommand(commandName)) { + final Logger logger = RestLogging.restLogger; + String commandLine = constructCommandLine(commandName, parameters); + logger.info(() -> "Executed admin command in Admin Console: " + commandLine); + } + + } + + private static String constructCommandLine(String commandName, ParameterMap parameters) { + final String DEFAULT_PARAM_KEY = "DEFAULT"; + final StringBuilder commandLineBuilder = new StringBuilder(); + final Stream namedParamsStream = parameters.entrySet().stream() + .filter(param -> !"userpassword".equals(param.getKey())) + .filter(param -> !DEFAULT_PARAM_KEY.equals(param.getKey())) + .map(param -> "--" + param.getKey() + "=" + param.getValue().get(0)); + final List unnamedParams = parameters.get(DEFAULT_PARAM_KEY); + return Stream.concat(Stream.concat(Stream.of(commandName), namedParamsStream), unnamedParams != null ? unnamedParams.stream() : Stream.empty()) + .collect(joining(" ")); + } + + private static boolean shouldLogCommand(String commandName) { + return Stream.of("version", "_(.*)", "list(.*)", "get(.*)", "(.*)-list-services", "uptime", + "enable-asadmin-recorder", "disable-asadmin-recorder", "set-asadmin-recorder-configuration", + "asadmin-recorder-enabled") + .filter(commandName::matches) + .findAny().isEmpty(); + } + public static void addCommandLog(RestActionReporter ar, String commandName, ParameterMap parameters) { List logs = (List) ar.getExtraProperties().get("commandLog"); if (logs == null) { @@ -362,7 +396,8 @@ public static void resolveParamValues(Map commandParams, UriInfo if (value.equals(Constants.VAR_PARENT)) { processParams.put(entry.getKey(), pathSegments.get(pathSegments.size() - 2).getPath()); } else if (value.startsWith(Constants.VAR_GRANDPARENT)) { - int number = (value.equals(Constants.VAR_GRANDPARENT)) ? 1 : // no number given + int number = (value.equals(Constants.VAR_GRANDPARENT)) ? 1 + : // no number given Integer.parseInt(value.substring(Constants.VAR_GRANDPARENT.length())); processParams.put(entry.getKey(), pathSegments.get(pathSegments.size() - (number + 2)).getPath()); @@ -374,8 +409,8 @@ public static void resolveParamValues(Map commandParams, UriInfo } /** - * Constructs and returns the resource method meta-data. This method is called to get meta-data in case of update method - * (POST). + * Constructs and returns the resource method meta-data. This method is called to get meta-data in case of update + * method (POST). * * @param configBeanModel the config bean associated with the resource. * @return MethodMetaData the meta-data store for the resource method. @@ -639,13 +674,13 @@ public static Response getDeleteResponse(int status, String message, HttpHeaders /** *

- * This method takes any query string parameters and adds them to the specified map. This is used, for example, with the - * delete operation when cascading deletes are required: + * This method takes any query string parameters and adds them to the specified map. This is used, for example, with + * the delete operation when cascading deletes are required: *

* DELETE http://localhost:4848/.../foo?cascade=true *

- * The reason we need to use query parameters versus "body" variables is the limitation that HttpURLConnection has in - * this regard. + * The reason we need to use query parameters versus "body" variables is the limitation that HttpURLConnection has + * in this regard. * * @param data */ @@ -908,7 +943,8 @@ public static Map getResourceLinks(Dom dom, UriInfo uriInfo, boo /** * @param qualifiedTypeName - * @return unqualified type name for given qualified type name. This is a substring of qualifiedTypeName after last "." + * @return unqualified type name for given qualified type name. This is a substring of qualifiedTypeName after last + * "." */ public static String getUnqualifiedTypeName(String qualifiedTypeName) { return qualifiedTypeName.substring(qualifiedTypeName.lastIndexOf(".") + 1, qualifiedTypeName.length());