Skip to content

Commit

Permalink
[PAGOPA-2341] fix: resolved bug on binary data extracted from Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-deri committed Nov 14, 2024
1 parent 5452132 commit 361e9c5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/main/java/it/gov/pagopa/wispconverter/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@ public RedisTemplate<String, Object> registerRedisSimpleTemplate(final LettuceCo
template.setConnectionFactory(connectionFactory);
return template;
}

@Bean(name = "redisBinaryTemplate")
public RedisTemplate<String, byte[]> registerRedisBinaryTemplate(final LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, byte[]> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new org.springframework.data.redis.serializer.RedisSerializer<byte[]>() {
@Override
public byte[] serialize(byte[] bytes) {
return bytes;
}

@Override
public byte[] deserialize(byte[] bytes) {
return bytes;
}
});
template.setConnectionFactory(connectionFactory);
return template;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class CacheRepository {
@Qualifier("redisSimpleTemplate")
private RedisTemplate<String, Object> redisSimpleTemplate;

@Autowired
@Qualifier("redisBinaryTemplate")
private RedisTemplate<String, byte[]> redisBinaryTemplate;

public void insert(String key, String value, long ttlInMinutes) {
this.redisSimpleTemplate.opsForValue().set(key, value, Duration.ofMinutes(ttlInMinutes));
}
Expand All @@ -36,6 +40,16 @@ public <T> T read(String key, Class<T> clazz) {
return result;
}

public byte[] readByte(String key) {
byte[] result = {};
try {
result = this.redisBinaryTemplate.opsForValue().get(key);
} catch (Exception e) {
log.error(String.format("Cannot correctly extract binary object retrieved with key [%s]", key));

Check failure

Code scanning / CodeQL

Insertion of sensitive information into log files High

This
potentially sensitive information
is written to a log file.

Check failure

Code scanning / CodeQL

Log Injection High

This log entry depends on a
user-provided value
.
}
return result;
}

public boolean delete(String key) {
Boolean isDeleted = this.redisSimpleTemplate.delete(key);
return isDeleted != null && isDeleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.azure.messaging.servicebus.ServiceBusMessage;
import com.azure.messaging.servicebus.ServiceBusSenderClient;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import it.gov.pagopa.wispconverter.controller.model.ReceiptTimerRequest;
import it.gov.pagopa.wispconverter.repository.CacheRepository;
import it.gov.pagopa.wispconverter.service.model.ReceiptDto;
Expand All @@ -19,11 +18,10 @@
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Slf4j
Expand All @@ -34,7 +32,6 @@ public class ReceiptTimerService {
public static final String PAYMENT_TOKEN_CACHING_KEY_TEMPLATE = "2_wisp_%s";
private final CacheRepository cacheRepository;
private final ReService reService;
private final Pattern paymentTokenCachingKeyTemplatePattern = Pattern.compile("\\s*(\\{.*\\})\\s*");
@Value("${azure.sb.wisp-payment-timeout-queue.connectionString}")
private String connectionString;
@Value("${azure.sb.queue.receiptTimer.name}")
Expand Down Expand Up @@ -141,17 +138,14 @@ private void populateMDC(String paymentToken) {
String domainId = null;
String noticeNumber = null;
try {
String cacheInfo = cacheRepository.read(String.format(PAYMENT_TOKEN_CACHING_KEY_TEMPLATE, paymentToken), String.class);
if (cacheInfo != null) {
Matcher matcher = paymentTokenCachingKeyTemplatePattern.matcher(cacheInfo);
if (matcher.matches()) {
ReceiptTimerRequest receiptTimerRequest = new Gson().fromJson(matcher.group(1), ReceiptTimerRequest.class);
MDC.put(Constants.MDC_SESSION_ID, receiptTimerRequest.getSessionId());
domainId = receiptTimerRequest.getFiscalCode();
noticeNumber = receiptTimerRequest.getNoticeNumber();
}
}
} catch (JsonSyntaxException e) {
byte[] primitiveByteArray = cacheRepository.readByte(String.format(PAYMENT_TOKEN_CACHING_KEY_TEMPLATE, paymentToken));
String byteArrayAsString = new String(primitiveByteArray, StandardCharsets.UTF_8);
String objectAsString = byteArrayAsString.substring(byteArrayAsString.indexOf('{'), byteArrayAsString.lastIndexOf('}') + 1);
ReceiptTimerRequest receiptTimerRequest = new Gson().fromJson(objectAsString, ReceiptTimerRequest.class);
MDC.put(Constants.MDC_SESSION_ID, receiptTimerRequest.getSessionId());
domainId = receiptTimerRequest.getFiscalCode();
noticeNumber = receiptTimerRequest.getNoticeNumber();
} catch (Exception e) {
log.debug("Impossible to generate data for MDC from cached payment token.", e);
}
MDCUtil.setReceiptTimerInfoInMDC(domainId, noticeNumber, paymentToken);
Expand Down

0 comments on commit 361e9c5

Please sign in to comment.