Skip to content

Commit

Permalink
#386 Fixed logging for security requests
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Oct 9, 2019
1 parent 9a471a0 commit 2b1476d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/main/java/com/marklogic/mgmt/ManageClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/marklogic/mgmt/ManageClientTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 2b1476d

Please sign in to comment.