Skip to content

Commit a4fe5ec

Browse files
authored
Merge pull request #292 from PerimeterX/release/v6.6.0
Release/v6.6.0 -> master
2 parents b25a83b + 3b2b446 commit a4fe5ec

16 files changed

+254
-57
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Change Log
2+
## [v6.6.0](https://github.com/PerimeterX/perimeterx-java-sdk/compare/v6.6.0...HEAD) (2023-27-04)
3+
- Updating readme with `customIsSensitve`, `customParametersExtraction`
4+
- Added an option to configure logger without slf4j using `PXConfiguration.setPxLoggerSeverity(<loggerSeverity>)`
5+
- Added an option to close PerimeterX [SDKNEW-2781](https://perimeterx.atlassian.net/browse/SDKNEW-2781)
26

37
## [v6.5.0](https://github.com/PerimeterX/perimeterx-java-sdk/compare/v6.5.0...HEAD) (2023-03-04)
48
- Adding custom is sensitive configuration option

README.md

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# [PerimeterX](http://www.perimeterx.com) Java SDK
66

7-
> Latest stable version: [v6.5.0](https://search.maven.org/#artifactdetails%7Ccom.perimeterx%7Cperimeterx-sdk%7C6.4.5%7Cjar)
7+
> Latest stable version: [v6.6.0](https://search.maven.org/#artifactdetails%7Ccom.perimeterx%7Cperimeterx-sdk%7C6.6.0%7Cjar)
88
99
## Table of Contents
1010

@@ -16,6 +16,7 @@
1616
- [Advanced Usage Examples](#advanced-usage)
1717
- [Data Enrichment](#data-enrichment)
1818
- [Custom Parameters](#custom-parameters)
19+
- [Custom Sensitive Request](#custom-sensitive-request)
1920
- [Multiple Application Support](#multi-app-support)
2021
- [Configuration](CONFIGURATIONS.md)
2122
- [Logging and Troubleshooting](#loggin-troubleshoot)
@@ -190,31 +191,51 @@ enforcer.setVerificationHandler(new MyVerificationHandler(config));
190191
...
191192
```
192193
193-
#### <a name="custom-parameters"></a> Custom Parameters
194+
#### <a name="custom-sensitive-request"></a> Custom Sensitive Request
195+
With the `customIsSensitive` predicate you can force the request to be sensitive.
196+
The input of the function is the same request that sent to the method `pxVerify`.
197+
If the function throws exception, it is equivalent to returning `false`.
198+
Implementing this configuration does NOT override other `sensitive` configurations, like `sensitive_routes`.
194199
195-
With the `customParametersProvider` function you can add up to 10 custom parameters to be sent back to PerimeterX servers. When set, the function is called before setting the payload on every request to PerimetrX servers.
200+
> **Note**
201+
> The request body can only be read once by default. If your function requires reading the body
202+
> consider using RequestWrapper which caches the body. Send the wrapped request to
203+
> `pxVerify` instead of the native one.
196204
197-
MyCustomParametersProvider.java:
205+
In your filter:
198206
```java
199207
...
200-
public class MyCustomParametersProvider implements CustomParametersProvider {
201-
public CustomParameters buildCustomParameters(PXConfiguration pxConfiguration, PXContext pxContext) {
202-
CustomParameters customParameters = new CustomParameters();
203-
customParameters.setCustomParam1("my_custom_param_1");
204-
customParameters.setCustomParam2("my_custom_param_2");
208+
PXConfiguration pxConfiguration = new PXConfiguration.Builder()
205209
...
206-
customParameters.setCustomParam10("my_custom_param_10");
207-
return customParameters;
208-
}
209-
}
210+
.customIsSensitiveRequest((req) -> req.getHeader("example-header") == "example-value")
211+
.build();
212+
210213
```
211214
212-
Then, in your filter:
215+
#### <a name="custom-parameters"></a> Custom Parameters
216+
217+
With the `customParametersExtraction` function you can add up to 10 custom parameters to be sent back to PerimeterX servers.
218+
When set, the function is called before setting the payload on every request to PerimetrX servers.
219+
The input of the function is the same request that sent to the method `pxVerify`.
220+
If the function throws exception, it is equivalent to returning empty custom params.
221+
Implementing this configuration overrides the deprecated configuration `customParameterProvider`.
222+
223+
> **Note**
224+
> The request body can only be read once by default. If your function requires reading the body
225+
> consider using RequestWrapper which caches the body. Send the wrapped request to
226+
> `pxVerify` instead of the native one.
227+
228+
In your filter:
213229
```java
214230
...
215231
PXConfiguration pxConfiguration = new PXConfiguration.Builder()
216232
...
217-
.customParametersProvider(new MyCustomParametersProvider())
233+
.customParametersExtraction((req) -> {
234+
CustomParameters customParameters = new CustomParameters();
235+
customParameters.setCustomParam1("example-value");
236+
customParameters.setCustomParam2(req.getHeader("example-header"));
237+
return customParameters;
238+
})
218239
.build();
219240
...
220241
```
@@ -251,6 +272,20 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
251272
252273
For further information please visit [SLF4J](https://www.slf4j.org/manual.html) and [Logback](https://logback.qos.ch).
253274
275+
If you wish to use a basic logger which uses `System.out` and `System.err` to print debug and error accordingly,
276+
Change the value of the static variable to your desired level.
277+
```java
278+
import com.perimeterx.models.configuration.PXConfiguration;
279+
import com.perimeterx.utils.LoggerSeverity;
280+
281+
PXConfiguration.setPxLoggerSeverity(LoggerSeverity.DEBUG);
282+
```
283+
> **Note**
284+
> This method can be executed once, no need to execute it every request.
285+
286+
287+
---
288+
254289
The following steps are welcome when contributing to our project.
255290
256291
#### Fork/Clone

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<name>PerimeterX JAVA SDK</name>
88
<groupId>com.perimeterx</groupId>
99
<artifactId>perimeterx-sdk</artifactId>
10-
<version>6.5.0</version>
10+
<version>6.6.0</version>
1111

1212
<packaging>jar</packaging>
1313
<description>PerimeterX Java SDK</description>

px_metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "6.5.0",
2+
"version": "6.6.0",
33
"supported_features": [
44
"additional_activity_handler",
55
"advanced_blocking_response",

src/main/java/com/perimeterx/api/PerimeterX.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,12 @@
6161

6262
import javax.servlet.http.HttpServletRequest;
6363
import javax.servlet.http.HttpServletResponseWrapper;
64+
import java.io.Closeable;
6465
import java.io.IOException;
6566
import java.net.URISyntaxException;
6667
import java.security.InvalidKeyException;
6768
import java.security.NoSuchAlgorithmException;
68-
import java.util.Arrays;
6969
import java.util.Base64;
70-
import java.util.Optional;
7170

7271
import static com.perimeterx.utils.Constants.*;
7372
import static java.util.Objects.isNull;
@@ -78,7 +77,7 @@
7877
* Created by shikloshi on 03/07/2016.
7978
*/
8079

81-
public class PerimeterX {
80+
public class PerimeterX implements Closeable {
8281

8382
private static final PXLogger logger = PXLogger.getLogger(PerimeterX.class);
8483

@@ -90,14 +89,15 @@ public class PerimeterX {
9089
private HostnameProvider hostnameProvider;
9190
private VerificationHandler verificationHandler;
9291
private ReverseProxy reverseProxy;
92+
private PXHttpClient pxClient = null;
9393

9494
private void init(PXConfiguration configuration) throws PXException {
9595
logger.debug(PXLogger.LogReason.DEBUG_INITIALIZING_MODULE);
9696
configuration.mergeConfigurations();
9797
this.configuration = configuration;
9898
hostnameProvider = new DefaultHostnameProvider();
9999
ipProvider = new CombinedIPProvider(configuration);
100-
PXHttpClient pxClient = new PXHttpClient(configuration);
100+
this.pxClient = new PXHttpClient(configuration);
101101
this.activityHandler = new BufferedActivityHandler(pxClient, this.configuration);
102102

103103
if (configuration.isRemoteConfigurationEnabled()) {
@@ -337,4 +337,10 @@ public void setVerificationHandler(VerificationHandler verificationHandler) {
337337
this.verificationHandler = verificationHandler;
338338
}
339339

340+
@Override
341+
public void close() throws IOException {
342+
if(this.pxClient != null) {
343+
this.pxClient.close();
344+
}
345+
}
340346
}

src/main/java/com/perimeterx/http/PXHttpClient.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.http.nio.reactor.IOReactorExceptionHandler;
4040
import org.apache.http.util.EntityUtils;
4141

42+
import java.io.Closeable;
4243
import java.io.IOException;
4344
import java.net.SocketTimeoutException;
4445
import java.nio.charset.Charset;
@@ -50,7 +51,7 @@
5051
* <p>
5152
* Created by shikloshi on 04/07/2016.
5253
*/
53-
public class PXHttpClient implements PXClient {
54+
public class PXHttpClient implements PXClient, Closeable {
5455
private static final int INACTIVITY_PERIOD_TIME_MS = 1000;
5556
private static final long MAX_IDLE_TIME_SEC = 30L;
5657

@@ -61,7 +62,7 @@ public class PXHttpClient implements PXClient {
6162
private CloseableHttpClient httpClient;
6263
private CloseableHttpAsyncClient asyncHttpClient;
6364
private PoolingNHttpClientConnectionManager nHttpConnectionManager;
64-
65+
private final TimerValidateRequestsQueue timerConfigUpdater;
6566
private PXConfiguration pxConfiguration;
6667

6768
public PXHttpClient(PXConfiguration pxConfiguration) throws PXException {
@@ -73,7 +74,7 @@ public PXHttpClient(PXConfiguration pxConfiguration) throws PXException {
7374
throw new PXException(e);
7475
}
7576

76-
TimerValidateRequestsQueue timerConfigUpdater = new TimerValidateRequestsQueue(nHttpConnectionManager, pxConfiguration);
77+
this.timerConfigUpdater = new TimerValidateRequestsQueue(nHttpConnectionManager, pxConfiguration);
7778
timerConfigUpdater.schedule();
7879
}
7980

@@ -316,4 +317,17 @@ public void sendEnforcerTelemetry(EnforcerTelemetry enforcerTelemetry) throws IO
316317
}
317318
}
318319
}
320+
321+
@Override
322+
public void close() throws IOException {
323+
this.timerConfigUpdater.close();
324+
325+
if (this.asyncHttpClient != null) {
326+
this.asyncHttpClient.close();
327+
}
328+
329+
if (this.httpClient != null) {
330+
this.httpClient.close();
331+
}
332+
}
319333
}

src/main/java/com/perimeterx/http/TimerValidateRequestsQueue.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import com.perimeterx.utils.PXLogger;
55
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
66

7+
import java.io.Closeable;
78
import java.util.Timer;
89
import java.util.TimerTask;
910

10-
public class TimerValidateRequestsQueue extends TimerTask {
11+
public class TimerValidateRequestsQueue extends TimerTask implements Closeable {
1112

1213
private static final PXLogger logger = PXLogger.getLogger(TimerValidateRequestsQueue.class);
1314

15+
private Timer timer = null;
1416
private PoolingNHttpClientConnectionManager nHttpConnectionManager;
1517
private PXConfiguration pxConfiguration;
1618

@@ -29,7 +31,16 @@ public void run() {
2931
* Sets a new timer object and runs its execution method
3032
*/
3133
public void schedule() {
32-
Timer timer = new Timer();
34+
this.close();
35+
timer = new Timer();
3336
timer.schedule(this, 0, pxConfiguration.getValidateRequestQueueInterval());
3437
}
38+
39+
@Override
40+
public void close() {
41+
if(timer != null) {
42+
timer.cancel();
43+
timer = null;
44+
}
45+
}
3546
}

src/main/java/com/perimeterx/models/configuration/PXConfiguration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.perimeterx.models.risk.CustomParameters;
1616
import com.perimeterx.utils.Constants;
1717
import com.perimeterx.utils.FilesUtils;
18+
import com.perimeterx.utils.LoggerSeverity;
1819
import com.perimeterx.utils.PXLogger;
1920
import lombok.AllArgsConstructor;
2021
import lombok.Builder;
@@ -46,6 +47,15 @@
4647
@Getter
4748
public class PXConfiguration {
4849
private static final PXLogger logger = PXLogger.getLogger(PXConfiguration.class);
50+
private static LoggerSeverity loggerSeverity = null;
51+
52+
public static LoggerSeverity getPxLoggerSeverity() {
53+
return loggerSeverity;
54+
}
55+
56+
public static void setPxLoggerSeverity(LoggerSeverity severity) {
57+
loggerSeverity = severity;
58+
}
4959

5060
@JsonProperty("px_app_id")
5161
private String appId;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.perimeterx.utils;
2+
3+
import java.io.PrintStream;
4+
public class ConsoleLogger implements PXLogger {
5+
private final LoggerSeverity severity;
6+
7+
public ConsoleLogger(LoggerSeverity severity) {
8+
this.severity = severity;
9+
}
10+
11+
private void log(PrintStream out, String prefix, Object msg, Object... additional) {
12+
StringBuilder builder = new StringBuilder();
13+
builder.append(prefix);
14+
builder.append(msg);
15+
for (Object arg : additional) {
16+
builder.append(" ").append(arg.toString());
17+
}
18+
out.println(builder);
19+
}
20+
@Override
21+
public void debug(LogReason reason, Object... args) {
22+
if(severity.level >= LoggerSeverity.DEBUG.level) {
23+
log(System.out, PXLogger.DEBUG_PREFIX, reason, args);
24+
}
25+
}
26+
27+
@Override
28+
public void debug(String msg, Object... args) {
29+
if(severity.level >= LoggerSeverity.DEBUG.level) {
30+
log(System.out, PXLogger.DEBUG_PREFIX, msg, args);
31+
}
32+
}
33+
34+
@Override
35+
public void error(LogReason reason, Object... args) {
36+
if(severity.level >= LoggerSeverity.ERROR.level) {
37+
log(System.err, PXLogger.ERROR_PREFIX, reason, args);
38+
}
39+
}
40+
41+
@Override
42+
public void error(String msg, Object... args) {
43+
if(severity.level >= LoggerSeverity.ERROR.level) {
44+
log(System.err, msg, args);
45+
}
46+
}
47+
}

src/main/java/com/perimeterx/utils/HMACUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static boolean isHMACValid(String encodedString, String hmac,
2626
byte[] bCookieHmac = StringUtils.hexStringToByteArray(hmac);
2727
isValid = Arrays.equals(bHMAC, bCookieHmac);
2828
} catch (Exception e) {
29-
logger.error(PXLogger.LogReason.DEBUG_COOKIE_HMAC_VALIDATION_FAILED, e.getMessage());
29+
logger.debug(PXLogger.LogReason.DEBUG_COOKIE_HMAC_VALIDATION_FAILED, e.getMessage());
3030
isValid = false;
3131
}
3232

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.perimeterx.utils;
2+
3+
import com.fasterxml.jackson.annotation.JsonValue;
4+
5+
public enum LoggerSeverity {
6+
NONE(0),
7+
ERROR(10),
8+
DEBUG(100);
9+
10+
11+
public final int level;
12+
13+
LoggerSeverity(int level) {
14+
this.level = level;
15+
}
16+
17+
@JsonValue
18+
public String jsonName() {
19+
return this.name().toLowerCase();
20+
}
21+
}

0 commit comments

Comments
 (0)