-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MARP-1548 Suspicious installation counts
- Loading branch information
1 parent
8145ab3
commit 9fb102f
Showing
8 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
marketplace-service/src/main/java/com/axonivy/market/logging/Loggable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.axonivy.market.logging; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface Loggable { | ||
} |
94 changes: 94 additions & 0 deletions
94
marketplace-service/src/main/java/com/axonivy/market/logging/LoggableAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.axonivy.market.logging; | ||
|
||
import com.axonivy.market.constants.CommonConstants; | ||
import com.axonivy.market.exceptions.model.MissingHeaderException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static com.axonivy.market.util.FileUtils.createFile; | ||
import static com.axonivy.market.util.FileUtils.writeToFile; | ||
import static com.axonivy.market.util.LoggingUtils.*; | ||
|
||
@Log4j2 | ||
@Aspect | ||
@Component | ||
public class LoggableAspect { | ||
|
||
@Value("${loggable.log-path:marketplace-service/logs}") | ||
private String logFilePath; | ||
|
||
private static final String REQUESTED_BY = "marketplace-website"; | ||
|
||
@Before("@annotation(com.axonivy.market.logging.Loggable)") | ||
public void logMethodCall(JoinPoint joinPoint) throws MissingHeaderException { | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
ServletRequestAttributes attributes = | ||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | ||
if (attributes != null) { | ||
HttpServletRequest request = attributes.getRequest(); | ||
Map<String, String> headersMap = extractHeaders(request, signature, joinPoint); | ||
saveLogToDailyFile(headersMap); | ||
|
||
// block execution if request isn't from Market or Ivy Designer | ||
if (!Objects.equals(headersMap.get(CommonConstants.REQUESTED_BY), REQUESTED_BY)) { | ||
throw new MissingHeaderException(); | ||
} | ||
} | ||
} | ||
|
||
private Map<String, String> extractHeaders(HttpServletRequest request, MethodSignature signature, | ||
JoinPoint joinPoint) { | ||
return Map.of( | ||
"method", escapeXml(String.valueOf(signature.getMethod())), | ||
"timestamp", escapeXml(getCurrentTimestamp()), | ||
"user-agent", escapeXml(request.getHeader(CommonConstants.USER_AGENT)), | ||
"arguments", escapeXml(getArgumentsString(signature.getParameterNames(), joinPoint.getArgs())), | ||
"x-requested-by", escapeXml(request.getHeader(CommonConstants.REQUESTED_BY)) | ||
); | ||
} | ||
|
||
// Use synchronized to prevent race condition | ||
private synchronized void saveLogToDailyFile(Map<String, String> headersMap) { | ||
try { | ||
File logFile = createFile(generateFileName()); | ||
|
||
StringBuilder content = new StringBuilder(); | ||
if (logFile.exists()) { | ||
content.append(new String(Files.readAllBytes(logFile.toPath()))); | ||
} | ||
if (content.isEmpty()) { | ||
content.append("<Logs>\n"); | ||
} | ||
int lastLogIndex = content.lastIndexOf("</Logs>"); | ||
if (lastLogIndex != -1) { | ||
content.delete(lastLogIndex, content.length()); | ||
} | ||
content.append(buildLogEntry(headersMap)); | ||
content.append("</Logs>\n"); | ||
|
||
writeToFile(logFile, content.toString()); | ||
} catch (IOException e) { | ||
log.error("Error writing log to file: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
private String generateFileName() { | ||
return Path.of(logFilePath, "log-" + getCurrentDate() + ".xml").toString(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
marketplace-service/src/main/java/com/axonivy/market/util/FileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.axonivy.market.util; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class FileUtils { | ||
|
||
public static File createFile(String fileName) throws IOException { | ||
File file = new File(fileName); | ||
File parentDir = file.getParentFile(); | ||
if (parentDir != null && !parentDir.exists() && !parentDir.mkdirs()) { | ||
throw new IOException("Failed to create directory: " + parentDir.getAbsolutePath()); | ||
} | ||
if (!file.exists() && !file.createNewFile()) { | ||
throw new IOException("Failed to create file: " + file.getAbsolutePath()); | ||
} | ||
return file; | ||
} | ||
|
||
public static void writeToFile(File file, String content) throws IOException { | ||
try (FileWriter writer = new FileWriter(file, false)) { | ||
writer.write(content); | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
marketplace-service/src/main/java/com/axonivy/market/util/LoggingUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.axonivy.market.util; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class LoggingUtils { | ||
|
||
public static String getCurrentDate() { | ||
return new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()); | ||
} | ||
|
||
public static String getCurrentTimestamp() { | ||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()); | ||
} | ||
|
||
public static String escapeXml(String value) { | ||
if (value == null) { | ||
return ""; | ||
} | ||
return value.replace("&", "&") | ||
.replace("<", "<") | ||
.replace(">", ">") | ||
.replace("\"", """) | ||
.replace("'", "'"); | ||
} | ||
|
||
public static String getArgumentsString(String[] paramNames, Object[] args) { | ||
if (paramNames == null || paramNames.length == 0 || args == null || args.length == 0) { | ||
return "No arguments"; | ||
} | ||
return IntStream.range(0, paramNames.length) | ||
.mapToObj(i -> paramNames[i] + ": " + args[i]) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
|
||
public static String buildLogEntry(Map<String, String> headersMap) { | ||
StringBuilder logEntry = new StringBuilder(); | ||
logEntry.append(" <LogEntry>\n"); | ||
headersMap.forEach((key, value) -> logEntry.append(String.format(" <%s>%s</%s>\n", key, value, key))); | ||
logEntry.append(" </LogEntry>\n"); | ||
return logEntry.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters