galovics commented on code in PR #4825:
URL: https://github.com/apache/fineract/pull/4825#discussion_r2240800301


##########
integration-tests/src/test/java/org/apache/fineract/integrationtests/IpTrackingIntegrationTest.java:
##########
@@ -0,0 +1,74 @@
+/**
+ * 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.integrationtests;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import io.restassured.builder.RequestSpecBuilder;
+import io.restassured.builder.ResponseSpecBuilder;
+import io.restassured.http.ContentType;
+import io.restassured.specification.RequestSpecification;
+import io.restassured.specification.ResponseSpecification;
+import java.util.HashMap;
+import java.util.List;
+import org.apache.fineract.integrationtests.common.AuditHelper;
+import org.apache.fineract.integrationtests.common.ClientHelper;
+import org.apache.fineract.integrationtests.common.Utils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class IpTrackingIntegrationTest {
+
+    private AuditHelper auditHelper;
+    private static final String EXPECTED_LOCAL_IP = "127.0.0.1";
+    private RequestSpecification requestSpec;
+    private ResponseSpecification responseSpec;
+    private ResponseSpecification responseSpecForSearch;
+
+    @BeforeEach
+    public void setup() {
+        Utils.initializeRESTAssured();
+        this.requestSpec = new 
RequestSpecBuilder().setContentType(ContentType.JSON).build();
+        this.requestSpec.header("Fineract-Platform-TenantId", "default");
+        this.requestSpec.auth().basic("mifos", "password");
+        this.responseSpec = new 
ResponseSpecBuilder().expectStatusCode(200).build();
+        this.responseSpecForSearch = new 
ResponseSpecBuilder().expectStatusCode(200).build();
+        this.auditHelper = new AuditHelper(this.requestSpec, 
this.responseSpec);
+    }
+
+    @Test
+    public void capturesIpAddressWhenCreatingClient() throws Exception {
+        List<HashMap<String, Object>> auditsRecieved;
+
+        // given
+        final Integer clientId = ClientHelper.createClient(this.requestSpec, 
this.responseSpec);
+        ClientHelper.verifyClientCreatedOnServer(this.requestSpec, 
this.responseSpec, clientId);
+        auditsRecieved = auditHelper.getAuditDetails(clientId, "CREATE", 
"CLIENT");
+
+        // when
+        String ip = auditsRecieved.get(0).get("ip").toString();
+
+        // then
+        if (!ip.isEmpty()) {
+            assertEquals(EXPECTED_LOCAL_IP, ip);
+        } else {
+            assertEquals("", ip);
+        }

Review Comment:
   How can there be an IF in the assertion? This seems to be wrong.



##########
fineract-provider/src/main/resources/application.properties:
##########
@@ -62,6 +62,7 @@ 
fineract.correlation.enabled=${FINERACT_LOGGING_HTTP_CORRELATION_ID_ENABLED:fals
 
fineract.correlation.header-name=${FINERACT_LOGGING_HTTP_CORRELATION_ID_HEADER_NAME:X-Correlation-ID}
 
 fineract.job.stuck-retry-threshold=${FINERACT_JOB_STUCK_RETRY_THRESHOLD:5}
+fineract.client-ip-tracking.enabled"=${FINERACT_CLIENT-IP-TRACKING_ENABLED:false}

Review Comment:
   Use underscores in the env var name.



##########
fineract-core/src/main/java/org/apache/fineract/infrastructure/core/api/JsonCommand.java:
##########
@@ -597,4 +599,15 @@ public void checkForUnsupportedParameters(final Type 
typeOfMap, final String jso
         this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, 
requestDataParameters);
     }
 
+    public String getClientIp() {
+        ServletRequestAttributes attrs = (ServletRequestAttributes) 
RequestContextHolder.getRequestAttributes();
+        String clientIp = "";
+        if (attrs != null) {
+            Object ipAttr = attrs.getRequest().getAttribute("IP");
+            if (ipAttr != null) {
+                clientIp = ipAttr.toString();
+            }
+        }
+        return clientIp;

Review Comment:
   I meant the utility method here.



##########
fineract-core/src/main/java/org/apache/fineract/infrastructure/core/filters/CallerIpTrackingFilter.java:
##########
@@ -0,0 +1,100 @@
+/**
+ * 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.infrastructure.core.filters;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+@Component
+@ConditionalOnProperty("fineract.client-ip-tracking.enabled")
+@RequiredArgsConstructor
+@Slf4j
+public class CallerIpTrackingFilter extends OncePerRequestFilter {

Review Comment:
   I don't get the whole filter approach.
   
   If this is truly just extracting the IP info from the servlet request, why 
don't you simply create a utility method for it in a utility class and use it 
when needed? Why do you change the attributes of the request?



##########
fineract-core/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java:
##########
@@ -50,6 +50,8 @@ public class FineractProperties {
 
     private FineractCorrelationProperties correlation;
 
+    private FineractGeolocationProperties geolocation;

Review Comment:
   You changed the property name, it's not geolocation anymore.



-- 
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