Skip to content

Commit 7c7f03a

Browse files
update logging for post requests
1 parent 85b32be commit 7c7f03a

File tree

1 file changed

+86
-3
lines changed

1 file changed

+86
-3
lines changed

server/src/main/java/com/cloud/api/ApiServlet.java

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.apache.cloudstack.context.CallContext;
5050
import org.apache.cloudstack.managed.context.ManagedContext;
5151
import org.apache.cloudstack.utils.consoleproxy.ConsoleAccessUtils;
52+
import org.apache.commons.collections.MapUtils;
5253

5354
import org.apache.logging.log4j.Logger;
5455
import org.apache.logging.log4j.LogManager;
@@ -82,8 +83,38 @@ public class ApiServlet extends HttpServlet {
8283
private static final String REPLACEMENT = "_";
8384
private static final String LOGGER_REPLACEMENTS = "[\n\r\t]";
8485
private static final Pattern GET_REQUEST_COMMANDS = Pattern.compile("^(get|list|query|find)(\\w+)+$");
85-
private static final HashSet<String> GET_REQUEST_COMMANDS_LIST = new HashSet<String>(Set.of("isaccountallowedtocreateofferingswithtags",
86+
private static final HashSet<String> GET_REQUEST_COMMANDS_LIST = new HashSet<>(Set.of("isaccountallowedtocreateofferingswithtags",
8687
"readyforshutdown", "cloudianisenabled", "quotabalance", "quotasummary", "quotatarifflist", "quotaisenabled", "quotastatement", "verifyoauthcodeandgetuser"));
88+
private static final HashSet<String> POST_REQUESTS_TO_DISABLE_LOGGING = new HashSet<>(Set.of(
89+
"login",
90+
"oauthlogin",
91+
"createaccount",
92+
"createuser",
93+
"updateuser",
94+
"forgotpassword",
95+
"resetpassword",
96+
"importrole",
97+
"updaterolepermission",
98+
"updateprojectrolepermission",
99+
"createstoragepool",
100+
"addhost",
101+
"updatehostpassword",
102+
"addcluster",
103+
"addvmwaredc",
104+
"configureoutofbandmanagement",
105+
"uploadcustomcertificate",
106+
"addciscovnmcresource",
107+
"addnetscalerloadbalancer",
108+
"createtungstenfabricprovider",
109+
"addnsxcontroller",
110+
"configtungstenfabricservice",
111+
"createnetworkacl",
112+
"updatenetworkaclitem",
113+
"quotavalidateactivationrule",
114+
"quotatariffupdate",
115+
"listandswitchsamlaccount",
116+
"uploadresourceicon"
117+
));
87118

88119
@Inject
89120
ApiServerService apiServer;
@@ -199,11 +230,24 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
199230

200231
utf8Fixup(req, params);
201232

233+
final Object[] commandObj = params.get(ApiConstants.COMMAND);
234+
final String command = commandObj == null ? null : (String) commandObj[0];
235+
202236
// logging the request start and end in management log for easy debugging
203237
String reqStr = "";
204238
String cleanQueryString = StringUtils.cleanString(req.getQueryString());
205239
if (LOGGER.isDebugEnabled()) {
206240
reqStr = auditTrailSb.toString() + " " + cleanQueryString;
241+
if (req.getMethod().equalsIgnoreCase("POST") && org.apache.commons.lang3.StringUtils.isNotBlank(command)) {
242+
if (!POST_REQUESTS_TO_DISABLE_LOGGING.contains(command.toLowerCase()) && !reqParams.containsKey(ApiConstants.USER_DATA)) {
243+
String cleanParamsString = getCleanParamsString(reqParams);
244+
if (org.apache.commons.lang3.StringUtils.isNotBlank(cleanParamsString)) {
245+
reqStr += "\n" + cleanParamsString;
246+
}
247+
} else {
248+
reqStr += " " + command;
249+
}
250+
}
207251
LOGGER.debug("===START=== " + reqStr);
208252
}
209253

@@ -219,8 +263,6 @@ void processRequestInContext(final HttpServletRequest req, final HttpServletResp
219263
responseType = (String)responseTypeParam[0];
220264
}
221265

222-
final Object[] commandObj = params.get(ApiConstants.COMMAND);
223-
final String command = commandObj == null ? null : (String) commandObj[0];
224266
final Object[] userObj = params.get(ApiConstants.USERNAME);
225267
String username = userObj == null ? null : (String)userObj[0];
226268
if (LOGGER.isTraceEnabled()) {
@@ -672,4 +714,45 @@ private static String getCorrectIPAddress(String ip) {
672714
}
673715
return null;
674716
}
717+
718+
private String getCleanParamsString(Map<String, String[]> reqParams) {
719+
if (MapUtils.isEmpty(reqParams)) {
720+
return "";
721+
}
722+
723+
StringBuilder cleanParamsString = new StringBuilder();
724+
for (Map.Entry<String, String[]> reqParam : reqParams.entrySet()) {
725+
if (org.apache.commons.lang3.StringUtils.isBlank(reqParam.getKey())) {
726+
continue;
727+
}
728+
729+
cleanParamsString.append(reqParam.getKey());
730+
cleanParamsString.append("=");
731+
732+
if (reqParam.getKey().toLowerCase().contains("password")
733+
|| reqParam.getKey().toLowerCase().contains("privatekey")
734+
|| reqParam.getKey().toLowerCase().contains("accesskey")
735+
|| reqParam.getKey().toLowerCase().contains("secretkey")) {
736+
cleanParamsString.append("\n");
737+
continue;
738+
}
739+
740+
if (reqParam.getValue() == null || reqParam.getValue().length == 0) {
741+
cleanParamsString.append("\n");
742+
continue;
743+
}
744+
745+
for (String param : reqParam.getValue()) {
746+
if (org.apache.commons.lang3.StringUtils.isBlank(param)) {
747+
continue;
748+
}
749+
String cleanParamString = StringUtils.cleanString(param.trim());
750+
cleanParamsString.append(cleanParamString);
751+
cleanParamsString.append(" ");
752+
}
753+
cleanParamsString.append("\n");
754+
}
755+
756+
return cleanParamsString.toString();
757+
}
675758
}

0 commit comments

Comments
 (0)