Skip to content

Commit

Permalink
GH24992 Admin Logger: Extract logger to a class, log caller name
Browse files Browse the repository at this point in the history
  • Loading branch information
OndroMih committed Jun 22, 2024
1 parent 1482d3f commit a1aca39
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.admin.rest.commandrecorder;

import static java.util.stream.Collectors.joining;

import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Stream;
import javax.security.auth.Subject;
import org.glassfish.admin.rest.RestLogging;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.security.common.UserPrincipal;

/**
*
* @author Ondro Mihalyi
*/
public class AdminCommandRecorder {

public void logCommand(String commandName, ParameterMap parameters, Subject subject) {
if (shouldLogCommand(commandName)) {
final Logger logger = RestLogging.restLogger;
String commandLine = constructCommandLine(commandName, parameters);
Optional<UserPrincipal> userPrincipalMaybe = getUserPrincipal(subject);
logger.info(() -> {
return userPrincipalMaybe.map(user -> "User " + user.getName())
.orElse("Unknown user")
+ " executed command in the Admin Console: " + commandLine;
});
}

}

private 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 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();
}

private Optional<UserPrincipal> getUserPrincipal(Subject subject) {
return subject.getPrincipals().stream()
.filter(principal -> principal instanceof UserPrincipal)
.map(UserPrincipal.class::cast)
.findAny();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2009, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -16,7 +17,6 @@
*/
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 @@ -92,6 +92,8 @@
import static org.glassfish.admin.rest.utils.Util.getHtml;
import static org.glassfish.admin.rest.utils.Util.methodNameFromDtdName;

import org.glassfish.admin.rest.commandrecorder.AdminCommandRecorder;

/**
* Resource utilities class. Used by resource templates, <code>TemplateListOfResource</code> and
* <code>TemplateRestResource</code>
Expand Down Expand Up @@ -221,7 +223,8 @@ public static RestActionReporter runCommand(String commandName, ParameterMap par
*/
public static RestActionReporter runCommand(String commandName, ParameterMap parameters, Subject subject, boolean managedJob) {

logCommand(commandName, parameters);
AdminCommandRecorder recorder = new AdminCommandRecorder();
recorder.logCommand(commandName, parameters, subject);

CommandRunner cr = Globals.getDefaultHabitat().getService(CommandRunner.class);
RestActionReporter ar = new RestActionReporter();
Expand Down Expand Up @@ -269,35 +272,6 @@ 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

0 comments on commit a1aca39

Please sign in to comment.