Skip to content

Commit

Permalink
Merge pull request #120 from KPMP/KPMP-1202_AddShibInfoToLogs
Browse files Browse the repository at this point in the history
Kpmp 1202 add shib info to logs
  • Loading branch information
zwright authored Jul 29, 2019
2 parents c8c7ae2 + c71381c commit 9dbfa88
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/kpmp/filters/AuthorizationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void doFilter(ServletRequest incomingRequest, ServletResponse incomingRes
// This is where we will implement the logic to talk to the user portal and do
// authorization.

logger.logInfoMessage(this.getClass(), user.getEmail(), null, this.getClass().getSimpleName() + ".doFilter",
logger.logInfoMessage(this.getClass(), user, null, this.getClass().getSimpleName() + ".doFilter",
"Passing through authentication filter");
chain.doFilter(incomingRequest, incomingResponse);
}
Expand Down
44 changes: 34 additions & 10 deletions src/main/java/org/kpmp/logging/LoggingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Service
public class LoggingService {

private static final String LOG_MESSAGE_FORMAT = "USERID: {} | PKGID: {} | URI: {} | MSG: {} ";
private static final String LOG_MESSAGE_FORMAT = "USER: {} | PKGID: {} | URI: {} | MSG: {} ";
private ShibbolethUserService shibUserService;

@Autowired
Expand All @@ -27,53 +27,77 @@ public void logInfoMessage(Class clazz, String packageId, String message, HttpSe
Logger log = LoggerFactory.getLogger(clazz);
try {
User user = shibUserService.getUser(request);
log.info(LOG_MESSAGE_FORMAT, user.getEmail(), packageId, request.getRequestURI(), message);
if (user == null) {
log.info(LOG_MESSAGE_FORMAT, "Unknown user", packageId, request.getRequestURI(), message);
} else {
log.info(LOG_MESSAGE_FORMAT, user.toString(), packageId, request.getRequestURI(), message);
}
} catch (UnsupportedEncodingException e) {
log.error(LOG_MESSAGE_FORMAT, "unknown", packageId, request.getRequestURI(),
"ERROR: Unable to get user information from request: " + e.getMessage());
}
}

@SuppressWarnings("rawtypes")
public void logInfoMessage(Class clazz, String userId, String packageId, String uri, String message) {
public void logInfoMessage(Class clazz, User user, String packageId, String uri, String message) {
Logger log = LoggerFactory.getLogger(clazz);
log.info(LOG_MESSAGE_FORMAT, userId, packageId, uri, message);
if (user == null) {
log.info(LOG_MESSAGE_FORMAT, "Unknown user", packageId, uri, message);
} else {
log.info(LOG_MESSAGE_FORMAT, user.toString(), packageId, uri, message);
}
}

@SuppressWarnings("rawtypes")
public void logErrorMessage(Class clazz, String packageId, String message, HttpServletRequest request) {
Logger log = LoggerFactory.getLogger(clazz);
try {
User user = shibUserService.getUser(request);
log.error(LOG_MESSAGE_FORMAT, user.getEmail(), packageId, request.getRequestURI(), message);
if (user == null) {
log.error(LOG_MESSAGE_FORMAT, "Unknown user", packageId, request.getRequestURI(), message);
} else {
log.error(LOG_MESSAGE_FORMAT, user.toString(), packageId, request.getRequestURI(), message);
}
} catch (UnsupportedEncodingException e) {
log.error(LOG_MESSAGE_FORMAT, "unknown", packageId, request.getRequestURI(),
"ERROR: Unable to get user information from request: " + e.getMessage());
}
}

@SuppressWarnings("rawtypes")
public void logErrorMessage(Class clazz, String userId, String packageId, String uri, String message) {
public void logErrorMessage(Class clazz, User user, String packageId, String uri, String message) {
Logger log = LoggerFactory.getLogger(clazz);
log.error(LOG_MESSAGE_FORMAT, userId, packageId, uri, message);
if (user == null) {
log.error(LOG_MESSAGE_FORMAT, "Unknown user", packageId, uri, message);
} else {
log.error(LOG_MESSAGE_FORMAT, user.toString(), packageId, uri, message);
}
}

@SuppressWarnings("rawtypes")
public void logWarnMessage(Class clazz, String packageId, String message, HttpServletRequest request) {
Logger log = LoggerFactory.getLogger(clazz);
try {
User user = shibUserService.getUser(request);
log.warn(LOG_MESSAGE_FORMAT, user.getEmail(), packageId, request.getRequestURI(), message);
if (user == null) {
log.warn(LOG_MESSAGE_FORMAT, "Unknown user", packageId, request.getRequestURI(), message);
} else {
log.warn(LOG_MESSAGE_FORMAT, user.toString(), packageId, request.getRequestURI(), message);
}
} catch (UnsupportedEncodingException e) {
log.error(LOG_MESSAGE_FORMAT, "unknown", packageId, request.getRequestURI(),
"ERROR: Unable to get user information from request: " + e.getMessage());
}
}

@SuppressWarnings("rawtypes")
public void logWarnMessage(Class clazz, String userId, String packageId, String uri, String message) {
public void logWarnMessage(Class clazz, User user, String packageId, String uri, String message) {
Logger log = LoggerFactory.getLogger(clazz);
log.warn(LOG_MESSAGE_FORMAT, userId, packageId, uri, message);
if (user == null) {
log.warn(LOG_MESSAGE_FORMAT, "Unknown user", packageId, uri, message);
} else {
log.warn(LOG_MESSAGE_FORMAT, user.toString(), packageId, uri, message);
}
}

}
21 changes: 7 additions & 14 deletions src/main/java/org/kpmp/packages/CustomPackageRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public CustomPackageRepository(PackageRepository repo, MongoTemplate mongoTempla
this.logger = logger;
}

public String saveDynamicForm(JSONObject packageMetadata) throws JSONException {
public String saveDynamicForm(JSONObject packageMetadata, User userFromHeader) throws JSONException {
Date startTime = new Date();
String packageId = universalIdGenerator.generateUniversalId();
String submitterEmail = packageMetadata.getString(PackageKeys.SUBMITTER_EMAIL.getKey());
String submitterEmail = userFromHeader.getEmail();
JSONArray files = packageMetadata.getJSONArray(PackageKeys.FILES.getKey());

logger.logInfoMessage(this.getClass(), submitterEmail, packageId,
logger.logInfoMessage(this.getClass(), userFromHeader, packageId,
this.getClass().getSimpleName() + ".saveDynamicForm",
fileUploadStartTiming.format(new Object[] { startTime, submitterEmail, packageId, files.length() }));

Expand All @@ -77,7 +77,7 @@ public String saveDynamicForm(JSONObject packageMetadata) throws JSONException {
file.put(PackageKeys.ID.getKey(), universalIdGenerator.generateUniversalId());
}

User user = findUser(packageMetadata, submitterEmail);
User user = findUser(userFromHeader);
cleanUpObject(packageMetadata);

DBRef userRef = new DBRef(USERS_COLLECTION, new ObjectId(user.getId()));
Expand All @@ -102,17 +102,10 @@ private void cleanUpObject(JSONObject packageMetadata) {
packageMetadata.remove(PackageKeys.SUBMITTER_LAST_NAME.getKey());
}

private User findUser(JSONObject packageMetadata, String submitterEmail) throws JSONException {
User user = userRepository.findByEmail(submitterEmail);
private User findUser(User userFromHeader) throws JSONException {
User user = userRepository.findByEmail(userFromHeader.getEmail());
if (user == null) {
User newUser = new User();
JSONObject submitter = packageMetadata.getJSONObject(PackageKeys.SUBMITTER.getKey());
if (submitter.has(PackageKeys.DISPLAY_NAME.getKey())) {
newUser.setDisplayName(submitter.getString(PackageKeys.DISPLAY_NAME.getKey()));
}
newUser.setEmail(submitter.getString(PackageKeys.EMAIL.getKey()));
newUser.setFirstName(submitter.getString(PackageKeys.FIRST_NAME.getKey()));
newUser.setLastName(submitter.getString(PackageKeys.LAST_NAME.getKey()));
User newUser = userFromHeader;
user = userRepository.save(newUser);
}
return user;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/kpmp/packages/PackageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.json.JSONObject;
import org.kpmp.logging.LoggingService;
import org.kpmp.shibboleth.ShibbolethUserService;
import org.kpmp.users.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
Expand Down Expand Up @@ -57,10 +58,11 @@ public PackageController(PackageService packageService, LoggingService logger,

@RequestMapping(value = "/v1/packages", method = RequestMethod.POST)
public @ResponseBody String postPackageInformation(@RequestBody String packageInfoString,
HttpServletRequest request) throws JSONException {
HttpServletRequest request) throws JSONException, UnsupportedEncodingException {
JSONObject packageInfo = new JSONObject(packageInfoString);
logger.logInfoMessage(this.getClass(), null, "Posting package info: " + packageInfo, request);
String packageId = packageService.savePackageInformation(packageInfo);
User user = shibUserService.getUser(request);
String packageId = packageService.savePackageInformation(packageInfo, user);
return packageId;
}

Expand Down Expand Up @@ -110,7 +112,7 @@ public PackageController(PackageService packageService, LoggingService logger,
if (packageService.validatePackageForZipping(packageId, shibUserService.getUser(request))) {
try {
String removeErrantEqualSign = hostname.replace("=", "");
packageService.createZipFile(packageId, removeErrantEqualSign);
packageService.createZipFile(packageId, removeErrantEqualSign, shibUserService.getUser(request));

fileUploadResponse = new FileUploadResponse(true);
} catch (Exception e) {
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/org/kpmp/packages/PackageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public Path getPackageFile(String packageId) {
return filePath;
}

public String savePackageInformation(JSONObject packageMetadata) throws JSONException {
return packageRepository.saveDynamicForm(packageMetadata);
public String savePackageInformation(JSONObject packageMetadata, User user) throws JSONException {
return packageRepository.saveDynamicForm(packageMetadata, user);
}

public Package findPackage(String packageId) {
Expand All @@ -94,42 +94,41 @@ public void saveFile(MultipartFile file, String packageId, String filename, bool
packageFileHandler.saveMultipartFile(file, packageId, filename, shouldAppend);
}

public void createZipFile(String packageId, String origin) throws Exception {
public void createZipFile(String packageId, String origin, User user) throws Exception {

Package packageInfo = packageRepository.findByPackageId(packageId);
String packageMetadata = packageRepository.getJSONByPackageId(packageId);

List<Attachment> attachments = packageInfo.getAttachments();
String displaySize = FileUtils.byteCountToDisplaySize(getTotalSizeOfAttachmentsInBytes(attachments));
Date finishUploadTime = new Date();
long duration = calculateDurationInSeconds(packageInfo.getCreatedAt(), finishUploadTime);
double uploadRate = calculateUploadRate(duration, attachments);
DecimalFormat rateFormat = new DecimalFormat("###.###");

String submitterEmail = packageInfo.getSubmitter().getEmail();
logger.logInfoMessage(this.getClass(), submitterEmail, packageId,
this.getClass().getSimpleName() + ".createZipFile",
logger.logInfoMessage(this.getClass(), user, packageId, this.getClass().getSimpleName() + ".createZipFile",
fileUploadFinishTiming
.format(new Object[] { finishUploadTime, submitterEmail, packageId, attachments.size(),
.format(new Object[] { finishUploadTime, user.toString(), packageId, attachments.size(),
displaySize, duration + " seconds", rateFormat.format(uploadRate) + " MB/sec" }));

new Thread() {
public void run() {
try {
String packageMetadata = packageRepository.getJSONByPackageId(packageId);
packageZipper.createZipFile(packageMetadata);
stateHandler.sendNotification(packageId, packageInfo.getPackageType(), packageInfo.getCreatedAt(),
packageInfo.getSubmitter().getFirstName(), packageInfo.getSubmitter().getLastName(),
packageInfo.getSubjectId(), origin);
} catch (Exception e) {
logger.logErrorMessage(PackageService.class, submitterEmail, packageId,
PackageService.class.getSimpleName(), e.getMessage());
logger.logErrorMessage(PackageService.class, user, packageId, PackageService.class.getSimpleName(),
e.getMessage());
}
logger.logInfoMessage(PackageService.class, null, packageId,
PackageService.class.getSimpleName() + ".createZipFile",
zipPackage.format(new Object[] { "Zip file created for package: ", packageId }));
long zipDuration = calculateDurationInSeconds(finishUploadTime, new Date());
logger.logInfoMessage(PackageService.class, submitterEmail, packageId,
logger.logInfoMessage(PackageService.class, user, packageId,
PackageService.class.getSimpleName() + ".createZipFile",
zipTiming.format(new Object[] { packageInfo.getCreatedAt(), submitterEmail, packageId,
zipTiming.format(new Object[] { packageInfo.getCreatedAt(), user.toString(), packageId,
packageInfo.getAttachments().size(), displaySize, zipDuration + " seconds" }));
}

Expand Down Expand Up @@ -178,7 +177,7 @@ protected boolean validateFileLengthsMatch(List<Attachment> filesInPackage, Stri
for (Attachment attachment : filesInPackage) {
String filename = attachment.getFileName();
if (new File(packagePath + filename).length() != attachment.getSize()) {
logger.logErrorMessage(this.getClass(), user.getEmail(), packageId,
logger.logErrorMessage(this.getClass(), user, packageId,
this.getClass().getSimpleName() + ".validateFileLengthsMatch", zipIssue.format(new Object[] {
"File size in metadata does not match file size on disk for file: " + filename }));
everythingMatches = false;
Expand All @@ -191,7 +190,7 @@ protected boolean checkFilesExist(List<String> filesOnDisk, List<String> filesIn
User user) {
boolean sameFiles = filesOnDisk.equals(filesInPackage);
if (!sameFiles) {
logger.logErrorMessage(this.getClass(), user.getEmail(), packageId,
logger.logErrorMessage(this.getClass(), user, packageId,
this.getClass().getSimpleName() + ".checkFilesExist",
zipIssue.format(new Object[] { "File list in metadata does not match file list on disk" }));
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/kpmp/shibboleth/ShibbolethUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public User getUser(HttpServletRequest request) throws UnsupportedEncodingExcept
String firstName = encoder.convertFromLatin1(value);
value = handleNull(request.getHeader("sn"));
String lastName = encoder.convertFromLatin1(value);
value = handleNull(request.getHeader("eppn"));
String shibId = encoder.convertFromLatin1(value);

User user = new User();
user.setDisplayName(displayName);
user.setLastName(lastName);
user.setFirstName(firstName);
user.setEmail(email);
user.setShibId(shibId);

return user;

Expand Down
21 changes: 15 additions & 6 deletions src/main/java/org/kpmp/users/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
public class User {

@Id
String id;
String firstName;
String lastName;
String displayName;
String email;
private String id;
private String firstName;
private String lastName;
private String displayName;
private String email;
private String shibId;

public String getId() {
return id;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void setEmail(String emailAddress) {
@Override
public String toString() {
return "userId: " + id + ", firstName: " + firstName + ", lastName: " + lastName + ", displayName: "
+ displayName + ", email: " + email;
+ displayName + ", email: " + email + ", shibId: " + shibId;
}

public String generateJSON() throws JsonProcessingException {
Expand All @@ -76,4 +77,12 @@ public String generateJSONForApp() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(this);
}

public String getShibId() {
return shibId;
}

public void setShibId(String shibId) {
this.shibId = shibId;
}
}
7 changes: 5 additions & 2 deletions src/main/java/org/kpmp/users/UserJsonMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

public abstract class UserJsonMixin {

@JsonIgnore
abstract String getId();
@JsonIgnore
abstract String getId();

@JsonIgnore
abstract String getShibId();

}
4 changes: 2 additions & 2 deletions src/test/java/org/kpmp/filters/AuthorizationFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void testDoFilter() throws Exception {
filter.doFilter(incomingRequest, incomingResponse, chain);

verify(chain).doFilter(incomingRequest, incomingResponse);
verify(logger, times(1)).logInfoMessage(AuthorizationFilter.class, "[email protected]", null,
"AuthorizationFilter.doFilter", "Passing through authentication filter");
verify(logger, times(1)).logInfoMessage(AuthorizationFilter.class, user, null, "AuthorizationFilter.doFilter",
"Passing through authentication filter");
}

@Test
Expand Down
Loading

0 comments on commit 9dbfa88

Please sign in to comment.