Redact user id in logs

This commit is contained in:
kuroppoi 2024-01-10 01:16:04 +01:00
parent 9d288bde09
commit 774794f751
5 changed files with 22 additions and 14 deletions

View File

@ -8,7 +8,8 @@ public record Configuration(
boolean clearPlayerDreamInfoOnWake,
boolean allowOverwritingPlayerDreamInfo,
boolean allowPlayerGameVersionMismatch,
boolean allowWfcRegistrationThroughLogin) {
boolean allowWfcRegistrationThroughLogin,
boolean logSensitiveInfo) {
public static final Configuration DEFAULT = new Configuration("local", true, false, false, true);
public static final Configuration DEFAULT = new Configuration("local", true, false, false, true, false);
}

View File

@ -21,7 +21,11 @@ public class User {
}
public String getFormattedId() {
return "%s000".formatted(id).replaceAll("(.{4})(?!$)", "$1-");
return getFormattedId(false);
}
public String getFormattedId(boolean redact) {
return redact ? "%s-XXXX-XXXX-XXXX".formatted(id.substring(0, 4)) : "%s000".formatted(id).replaceAll("(.{4})(?!$)", "$1-");
}
public String getId() {

View File

@ -8,6 +8,7 @@ import java.util.function.Supplier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import entralinked.Configuration;
import entralinked.Entralinked;
import entralinked.model.user.GameProfile;
import entralinked.model.user.ServiceSession;
@ -37,6 +38,7 @@ public class GameSpyHandler extends SimpleChannelInboundHandler<GameSpyRequest>
private static final Logger logger = LogManager.getLogger();
private final SecureRandom secureRandom = new SecureRandom();
private final Configuration configuration;
private final UserManager userManager;
private Channel channel;
private String serverChallenge;
@ -45,6 +47,7 @@ public class GameSpyHandler extends SimpleChannelInboundHandler<GameSpyRequest>
private GameProfile profile;
public GameSpyHandler(Entralinked entralinked) {
this.configuration = entralinked.getConfiguration();
this.userManager = entralinked.getUserManager();
}
@ -62,7 +65,7 @@ public class GameSpyHandler extends SimpleChannelInboundHandler<GameSpyRequest>
@Override
public void channelInactive(ChannelHandlerContext ctx) {
logger.debug("User {} disconnected from GameSpy server", user == null ? null : user.getFormattedId());
logger.debug("User {} disconnected from GameSpy server", user == null ? null : user.getFormattedId(!configuration.logSensitiveInfo()));
// Clear data
serverChallenge = null;
@ -86,7 +89,7 @@ public class GameSpyHandler extends SimpleChannelInboundHandler<GameSpyRequest>
// Handle timeout
if(cause instanceof ReadTimeoutException) {
logger.debug("User {} timed out", user == null ? null : user.getFormattedId());
logger.debug("User {} timed out", user == null ? null : user.getFormattedId(!configuration.logSensitiveInfo()));
return;
}
@ -142,7 +145,7 @@ public class GameSpyHandler extends SimpleChannelInboundHandler<GameSpyRequest>
userManager.saveUser(user); // It's not too big of a deal if this fails for some reason
}
logger.info("User {} logged in with profile {}", user.getFormattedId(), profile.getId());
logger.info("User {} logged in with profile {}", user.getFormattedId(!configuration.logSensitiveInfo()), profile.getId());
// Prepare and send response
sessionKey = secureRandom.nextInt(Integer.MAX_VALUE);
@ -197,7 +200,7 @@ public class GameSpyHandler extends SimpleChannelInboundHandler<GameSpyRequest>
}
public void handleLogout() {
logger.info("User {} logged out of profile {}", user.getFormattedId(), profile.getId());
logger.info("User {} logged out of profile {}", user.getFormattedId(!configuration.logSensitiveInfo()), profile.getId());
sessionKey = -1; // Is there a point?
}

View File

@ -91,12 +91,12 @@ public class NasHandler implements HttpHandler {
// Should *never* return null in this location
user = userManager.authenticateUser(userId, request.password());
logger.info("Created account for user {}", user.getFormattedId());
logger.info("Created account for user {}", user.getFormattedId(!configuration.logSensitiveInfo()));
}
// Prepare GameSpy server credentials
ServiceCredentials credentials = userManager.createServiceSession(user, "gamespy", request.branchCode());
logger.info("Created GameSpy session for user {}", user.getFormattedId());
logger.info("Created GameSpy session for user {}", user.getFormattedId(!configuration.logSensitiveInfo()));
result(ctx, new NasLoginResponse("gamespy.com", credentials.authToken(), credentials.challenge()));
}
@ -120,7 +120,7 @@ public class NasHandler implements HttpHandler {
return;
}
logger.info("Created account for user {}", user.getFormattedId());
logger.info("Created account for user {}", user.getFormattedId(!configuration.logSensitiveInfo()));
result(ctx, NasReturnCode.REGISTRATION_SUCCESS);
}
@ -148,7 +148,7 @@ public class NasHandler implements HttpHandler {
// Prepare user credentials
ServiceCredentials credentials = userManager.createServiceSession(user, service, null);
logger.info("Created {} session for user {}",
type.equals("0000") ? "PGL" : type.equals("9000") ? "DLS1" : "this should never be logged", user.getFormattedId());
type.equals("0000") ? "PGL" : type.equals("9000") ? "DLS1" : "this should never be logged", user.getFormattedId(!configuration.logSensitiveInfo()));
result(ctx, new NasServiceLocationResponse(true, service, credentials.authToken()));
}

View File

@ -201,7 +201,7 @@ public class PglHandler implements HttpHandler {
return;
}
logger.info("Player {} is downloading save data as user {}", player.getGameSyncId(), user.getFormattedId());
logger.info("Player {} is downloading save data as user {}", player.getGameSyncId(), user.getFormattedId(!configuration.logSensitiveInfo()));
// Write status code
writeStatusCode(outputStream, 0);
@ -369,7 +369,7 @@ public class PglHandler implements HttpHandler {
return;
}
logger.info("User {} is Memory Linking with player {}", user.getFormattedId(), player.getGameSyncId());
logger.info("User {} is Memory Linking with player {}", user.getFormattedId(!configuration.logSensitiveInfo()), player.getGameSyncId());
// Send the save data!
try(FileInputStream inputStream = new FileInputStream(file)) {
@ -450,7 +450,7 @@ public class PglHandler implements HttpHandler {
return;
}
logger.info("Player {} is uploading save data as user {}", player.getGameSyncId(), user.getFormattedId());
logger.info("Player {} is uploading save data as user {}", player.getGameSyncId(), user.getFormattedId(!configuration.logSensitiveInfo()));
// Try to store save data
if(!playerManager.storePlayerGameSaveFile(player, ctx.bodyInputStream())) {