-
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 (#257)
- Loading branch information
1 parent
6ec5f3f
commit e1a0d85
Showing
19 changed files
with
463 additions
and
15 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
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
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
22 changes: 22 additions & 0 deletions
22
marketplace-service/src/main/java/com/axonivy/market/constants/LoggingConstants.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,22 @@ | ||
package com.axonivy.market.constants; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class LoggingConstants { | ||
|
||
public static final String ENTRY_FORMAT = " <%s>%s</%s>%n"; | ||
public static final String ENTRY_START = " <LogEntry>\n"; | ||
public static final String ENTRY_END = " </LogEntry>\n"; | ||
public static final String DATE_FORMAT = "yyyy-MM-dd"; | ||
public static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss"; | ||
public static final String LOG_START = "<Logs>\n"; | ||
public static final String LOG_END = "</Logs>"; | ||
public static final String METHOD = "method"; | ||
public static final String ARGUMENTS = "arguments"; | ||
public static final String TIMESTAMP = "timestamp"; | ||
public static final String NO_ARGUMENTS = "No arguments"; | ||
public static final String MARKET_WEBSITE = "marketplace-website"; | ||
|
||
} |
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 { | ||
} |
92 changes: 92 additions & 0 deletions
92
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,92 @@ | ||
package com.axonivy.market.logging; | ||
|
||
import com.axonivy.market.constants.CommonConstants; | ||
import com.axonivy.market.constants.LoggingConstants; | ||
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 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}") | ||
public String logFilePath; | ||
|
||
@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 (!LoggingConstants.MARKET_WEBSITE.equals(headersMap.get(CommonConstants.REQUESTED_BY))) { | ||
throw new MissingHeaderException(); | ||
} | ||
} | ||
} | ||
|
||
private Map<String, String> extractHeaders(HttpServletRequest request, MethodSignature signature, | ||
JoinPoint joinPoint) { | ||
return Map.of( | ||
LoggingConstants.METHOD, escapeXml(String.valueOf(signature.getMethod())), | ||
LoggingConstants.TIMESTAMP, escapeXml(getCurrentTimestamp()), | ||
CommonConstants.USER_AGENT, escapeXml(request.getHeader(CommonConstants.USER_AGENT)), | ||
LoggingConstants.ARGUMENTS, escapeXml(getArgumentsString(signature.getParameterNames(), joinPoint.getArgs())), | ||
CommonConstants.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(LoggingConstants.LOG_START); | ||
} | ||
int lastLogIndex = content.lastIndexOf(LoggingConstants.LOG_END); | ||
if (lastLogIndex != -1) { | ||
content.delete(lastLogIndex, content.length()); | ||
} | ||
content.append(buildLogEntry(headersMap)); | ||
content.append(LoggingConstants.LOG_END); | ||
|
||
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(); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
package com.axonivy.market.util; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
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); | ||
} | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
package com.axonivy.market.util; | ||
|
||
import com.axonivy.market.constants.LoggingConstants; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class LoggingUtils { | ||
|
||
public static String getCurrentDate() { | ||
return new SimpleDateFormat(LoggingConstants.DATE_FORMAT).format(System.currentTimeMillis()); | ||
} | ||
|
||
public static String getCurrentTimestamp() { | ||
return new SimpleDateFormat(LoggingConstants.TIMESTAMP_FORMAT).format(System.currentTimeMillis()); | ||
} | ||
|
||
public static String escapeXml(String value) { | ||
if (StringUtils.isEmpty(value)) { | ||
return StringUtils.EMPTY; | ||
} | ||
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 LoggingConstants.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(); | ||
Map<String, String> map = new TreeMap<>(headersMap); | ||
logEntry.append(LoggingConstants.ENTRY_START); | ||
map.forEach((key, value) -> logEntry.append(String.format(LoggingConstants.ENTRY_FORMAT, key, value, key))); | ||
logEntry.append(LoggingConstants.ENTRY_END); | ||
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
Oops, something went wrong.