Copilot commented on code in PR #149:
URL: 
https://github.com/apache/fineract-consumer-facing/pull/149#discussion_r4076504008


##########
consumer/src/main/java/org/apache/fineract/consumer/audit/command/service/AuditCommandServiceImpl.java:
##########
@@ -62,11 +64,22 @@ public AuditCommandServiceImpl(AuditEventCommandRepository 
auditEventCommandRepo
             AuditPiiScreen auditPiiScreen, ObjectMapper objectMapper,
             @Value(MAX_BATCH_SIZE_PROPERTY) int maxBatchSize,
             @Value(MAX_DETAILS_BYTES_PROPERTY) int maxDetailsBytes) {
+        this(auditEventCommandRepository, accessPolicyEvaluator, 
userClientResolver, auditPiiScreen,
+                objectMapper, null, maxBatchSize, maxDetailsBytes);
+    }
+
+    public AuditCommandServiceImpl(AuditEventCommandRepository 
auditEventCommandRepository,
+            AccessPolicyEvaluator accessPolicyEvaluator, UserClientResolver 
userClientResolver,
+            AuditPiiScreen auditPiiScreen, ObjectMapper objectMapper,
+            CorrelationIdHolder correlationIdHolder,
+            @Value(MAX_BATCH_SIZE_PROPERTY) int maxBatchSize,
+            @Value(MAX_DETAILS_BYTES_PROPERTY) int maxDetailsBytes) {

Review Comment:
   Adding the holder overload leaves this `@Service` with two unannotated 
constructors. Spring does not select between multiple constructors implicitly, 
so application startup can fail instead of injecting `CorrelationIdHolder`, 
which would prevent the client audit correlation path from being wired. Mark 
this full constructor as `@Autowired` or remove the legacy overload.



##########
consumer/src/main/java/org/apache/fineract/consumer/audit/command/service/ServerAuditEventListener.java:
##########
@@ -41,6 +42,11 @@ public class ServerAuditEventListener {
 
     private final AuditEventCommandRepository auditEventCommandRepository;
     private final ObjectMapper objectMapper;
+    private final CorrelationIdHolder correlationIdHolder;
+
+    public ServerAuditEventListener(AuditEventCommandRepository 
auditEventCommandRepository, ObjectMapper objectMapper) {
+        this(auditEventCommandRepository, objectMapper, null);
+    }

Review Comment:
   This compatibility constructor leaves `@RequiredArgsConstructor` generating 
a second, three-argument constructor. Since neither constructor is annotated 
for injection, Spring cannot reliably choose the constructor that includes 
`CorrelationIdHolder` (and may fail application startup or instantiate the 
legacy overload), so correlation support may be disabled. Keep one constructor 
or explicitly mark the full constructor as the autowired one.



##########
consumer/src/main/java/org/apache/fineract/consumer/infrastructure/correlation/filter/CorrelationIdFilter.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fineract.consumer.infrastructure.correlation.filter;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.UUID;
+import lombok.RequiredArgsConstructor;
+import 
org.apache.fineract.consumer.infrastructure.correlation.service.CorrelationIdHolder;
+import org.apache.fineract.consumer.infrastructure.web.data.ConsumerHeaders;
+import org.slf4j.MDC;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+@RequiredArgsConstructor
+public class CorrelationIdFilter extends OncePerRequestFilter {
+
+    private static final String MDC_CORRELATION_ID_KEY = "correlationId";
+
+    private final CorrelationIdHolder correlationIdHolder;
+
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, 
HttpServletResponse response,
+            FilterChain filterChain) throws ServletException, IOException {
+        String correlationId = 
request.getHeader(ConsumerHeaders.CORRELATION_ID);
+        if (correlationId == null || correlationId.isBlank()) {
+            correlationId = UUID.randomUUID().toString();

Review Comment:
   `correlationId` is copied directly from an untrusted header, but 
`AuditEvent.correlationId` is persisted in a column limited to 64 characters. A 
caller can send a longer value; client/server audit inserts then fail (the code 
swallows those failures), so the new correlation field is silently lost. 
Validate the accepted ID length (or replace overlong values with a generated 
ID) before setting the holder, response, MDC, or forwarding it.



##########
consumer/src/main/java/org/apache/fineract/consumer/audit/command/service/ServerAuditEventListener.java:
##########
@@ -57,8 +63,9 @@ public void 
onNonTransactionalEvent(NonTransactionalAuditEvent event) {
     private void persist(UUID eventUuid, AuditEventType eventType, Long userId,
             boolean unknownPrincipal, String deviceFingerprint, Map<String, 
Object> details) {
         try {
+            String correlationId = correlationIdHolder != null ? 
correlationIdHolder.get() : null;
             AuditEvent entity = AuditEvent.forServerEvent(eventUuid, 
eventType, eventType.getSeverity(), userId,
-                    unknownPrincipal, deviceFingerprint, serialize(details));
+                    unknownPrincipal, deviceFingerprint, serialize(details), 
correlationId);

Review Comment:
   `CorrelationIdHolder` is backed by a `ThreadLocal`, but both listener 
annotations dispatch off the request thread (`@ApplicationModuleListener` after 
commit and `@Async` for non-transactional events). By the time `persist` runs, 
`CorrelationIdFilter` has already cleared the holder, so server audit rows will 
keep a null correlation ID in real requests; the unit test calls the listener 
synchronously and does not exercise this path. Capture the ID in the published 
event (or explicitly propagate the context) and persist that value here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to