Skip to content

Commit

Permalink
GH24992 Admin Logger: Simple admin command logger
Browse files Browse the repository at this point in the history
  • Loading branch information
OndroMih committed Jun 22, 2024
1 parent 2127e35 commit 1482d3f
Showing 1 changed file with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> data) {
if (data != null) {
Expand All @@ -155,9 +157,9 @@ public static void adjustParameters(Map<String, String> 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<String, String> data) {
if (data != null) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<String> 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<String> 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<String> logs = (List<String>) ar.getExtraProperties().get("commandLog");
if (logs == null) {
Expand Down Expand Up @@ -362,7 +396,8 @@ public static void resolveParamValues(Map<String, String> 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());
Expand All @@ -374,8 +409,8 @@ public static void resolveParamValues(Map<String, String> 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.
Expand Down Expand Up @@ -639,13 +674,13 @@ public static Response getDeleteResponse(int status, String message, HttpHeaders

/**
* <p>
* 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:
* </p>
* <code style="margin-left: 3em">DELETE http://localhost:4848/.../foo?cascade=true</code>
* <p>
* 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
*/
Expand Down Expand Up @@ -908,7 +943,8 @@ public static Map<String, String> 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());
Expand Down

0 comments on commit 1482d3f

Please sign in to comment.