From 2b1476dcbc487d517a58ede3e159ab7b60a458c6 Mon Sep 17 00:00:00 2001 From: Rob Rudin Date: Wed, 9 Oct 2019 09:27:54 -0400 Subject: [PATCH] #386 Fixed logging for security requests --- .../java/com/marklogic/mgmt/ManageClient.java | 15 ++++++++++++-- .../com/marklogic/mgmt/ManageClientTest.java | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/marklogic/mgmt/ManageClientTest.java diff --git a/src/main/java/com/marklogic/mgmt/ManageClient.java b/src/main/java/com/marklogic/mgmt/ManageClient.java index 3c8fe7efd..0fd445cb2 100644 --- a/src/main/java/com/marklogic/mgmt/ManageClient.java +++ b/src/main/java/com/marklogic/mgmt/ManageClient.java @@ -10,6 +10,7 @@ import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.io.IOException; @@ -367,12 +368,22 @@ protected void logRequest(String path, String contentType, String method) { protected void logSecurityUserRequest(String path, String contentType, String method) { if (logger.isInfoEnabled()) { - String username = manageConfig != null ? manageConfig.getUsername() : "(unknown)"; logger.info(String.format("Sending %s %s request as user '%s' (who must have the 'manage-admin' and 'security' roles) to path: %s", - contentType, method, username, path)); + contentType, method, determineUsernameForSecurityUserRequest(), path)); } } + protected String determineUsernameForSecurityUserRequest() { + String username = "(unknown)"; + if (manageConfig != null) { + username = manageConfig.getSecurityUsername(); + if (StringUtils.isEmpty(username)) { + username = manageConfig.getUsername(); + } + } + return username; + } + public URI buildUri(String path) { return manageConfig.buildUri(path); } diff --git a/src/test/java/com/marklogic/mgmt/ManageClientTest.java b/src/test/java/com/marklogic/mgmt/ManageClientTest.java new file mode 100644 index 000000000..b59588777 --- /dev/null +++ b/src/test/java/com/marklogic/mgmt/ManageClientTest.java @@ -0,0 +1,20 @@ +package com.marklogic.mgmt; + +import org.junit.Assert; +import org.junit.Test; + +public class ManageClientTest extends Assert { + + @Test + public void determineUsernameForSecurityUserRequest() { + ManageConfig config = new ManageConfig(); + config.setSecurityUsername("admin"); + config.setUsername("someone"); + + ManageClient client = new ManageClient(config); + assertEquals("admin", client.determineUsernameForSecurityUserRequest()); + + config.setSecurityUsername(null); + assertEquals("someone", client.determineUsernameForSecurityUserRequest()); + } +}