Skip to content

Commit 59a5900

Browse files
danyi1212ankmish14
andauthored
dan/per-11891-add-proxy-facts-to-java-sdk (#31)
* Add support for proxy facts via PDP * Add support for proxy facts via PDP * Added proxy facts urls to all facts APIs --------- Co-authored-by: Mishra <anmishra@navan.com>
1 parent 941b584 commit 59a5900

7 files changed

Lines changed: 145 additions & 45 deletions

File tree

src/main/java/io/permit/sdk/PermitConfig.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class PermitConfig {
1212
private final String opa;
1313
private final String apiUrl;
1414
private final Boolean debugMode;
15+
private final Boolean proxyFactsViaPdp;
16+
private final int factsSyncTimeout;
1517

1618
// logger config
1719
private final String logLevel;
@@ -45,6 +47,8 @@ private PermitConfig(Builder builder) {
4547
this.defaultTenant = builder.defaultTenant;
4648
this.useDefaultTenantIfEmpty = builder.useDefaultTenantIfEmpty;
4749
this.context = builder.context;
50+
this.proxyFactsViaPdp = builder.proxyFactsViaPdp;
51+
this.factsSyncTimeout = builder.factsSyncTimeout;
4852
String runtimeVersion = Permit.class.getPackage().getImplementationVersion();
4953
this.version = (runtimeVersion == null) ? defaultVersion : runtimeVersion;
5054
}
@@ -95,6 +99,24 @@ public Boolean isDebugMode() {
9599
return debugMode;
96100
}
97101

102+
/**
103+
* Returns whether the facts via the PDP API instead of using the default Permit REST API
104+
*
105+
* @return {@code true} if proxying facts via the PDP is enabled, {@code false} otherwise.
106+
*/
107+
public Boolean isProxyFactsViaPdp() {
108+
return proxyFactsViaPdp;
109+
}
110+
111+
/**
112+
* Returns the timeout for facts synchronization.
113+
*
114+
* @return The facts synchronization timeout.
115+
*/
116+
public int getFactsSyncTimeout() {
117+
return factsSyncTimeout;
118+
}
119+
98120
/**
99121
* Returns the log level configured for the Permit SDK Logger.
100122
*
@@ -208,6 +230,8 @@ public static class Builder {
208230
private String opa = "http://localhost:8181";
209231
private String apiUrl = "https://api.permit.io";
210232
private Boolean debugMode = false;
233+
private Boolean proxyFactsViaPdp = false;
234+
private int factsSyncTimeout = 0;
211235

212236
// logger config
213237
private String logLevel = "info";
@@ -279,6 +303,28 @@ public Builder withDebugMode(Boolean debugMode) {
279303
return this;
280304
}
281305

306+
/**
307+
* Configures whether the SDK should proxy facts via the PDP API instead of using the default Permit REST API.
308+
*
309+
* @param proxyFactsViaPdp The proxy facts via PDP mode to be set.
310+
* @return The updated {@code Builder} object.
311+
*/
312+
public Builder withProxyFactsViaPdp(Boolean proxyFactsViaPdp) {
313+
this.proxyFactsViaPdp = proxyFactsViaPdp;
314+
return this;
315+
}
316+
317+
/**
318+
* Configures the timeout for facts synchronization.
319+
*
320+
* @param factsSyncTimeout The facts synchronization timeout to be set.
321+
* @return The updated {@code Builder} object.
322+
*/
323+
public Builder withFactsSyncTimeout(int factsSyncTimeout) {
324+
this.factsSyncTimeout = factsSyncTimeout;
325+
return this;
326+
}
327+
282328
/**
283329
* Sets the log level configured for the Permit SDK Logger.
284330
*

src/main/java/io/permit/sdk/api/BaseApi.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ protected BaseApi(OkHttpClient client, PermitConfig config, Logger logger) {
2626
this.client = client;
2727
this.config = config;
2828
this.logger = logger;
29-
this.headers = new Headers.Builder()
29+
Headers.Builder headersBuilder = new Headers.Builder()
3030
.add("Content-Type", "application/json")
3131
.add("Authorization", String.format("Bearer %s", this.config.getToken()))
32-
.add("X-Permit-SDK-Version", String.format("java:%s", this.config.version))
33-
.build();
32+
.add("X-Permit-SDK-Version", String.format("java:%s", this.config.version));
33+
34+
if (config.isProxyFactsViaPdp() && config.getFactsSyncTimeout() > 0) {
35+
headersBuilder.add("X-Wait-Timeout", String.valueOf(config.getFactsSyncTimeout()));
36+
}
37+
this.headers = headersBuilder.build();
3438
}
3539

3640
protected <T> T callApiAndParseJson(Request request, Class<T> modelClass) throws IOException, PermitApiError {
@@ -86,6 +90,10 @@ protected String buildUrl(String relativeUrl) {
8690
return String.format("%s%s", config.getApiUrl(), relativeUrl);
8791
}
8892

93+
protected String buildPdpUrl(String relativeUrl) {
94+
return String.format("%s%s", config.getPdpAddress(), relativeUrl);
95+
}
96+
8997
protected Request buildRequest(Request.Builder builder) {
9098
return builder.headers(this.headers).build();
9199
}

src/main/java/io/permit/sdk/api/RelationshipTuplesApi.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import io.permit.sdk.ApiContextLevel;
55
import io.permit.sdk.ApiKeyLevel;
66
import io.permit.sdk.PermitConfig;
7-
import io.permit.sdk.openapi.models.*;
7+
import io.permit.sdk.openapi.models.RelationshipTupleCreate;
8+
import io.permit.sdk.openapi.models.RelationshipTupleDelete;
9+
import io.permit.sdk.openapi.models.RelationshipTupleRead;
810
import okhttp3.*;
911
import org.slf4j.LoggerFactory;
1012
import java.io.IOException;
11-
import java.util.List;
1213
import java.util.Objects;
1314

1415
interface IRelationshipTuplesApi {
@@ -41,14 +42,23 @@ public RelationshipTuplesApi(OkHttpClient client, PermitConfig config) {
4142
* @return The complete URL for relationship tuples.
4243
*/
4344
private String getRelationshipTuplesUrl(String url) {
44-
return buildUrl(
45-
String.format(
46-
"/v2/facts/%s/%s/relationship_tuples%s",
47-
config.getContext().getProject(),
48-
config.getContext().getEnvironment(),
49-
url
50-
)
51-
);
45+
if (Boolean.TRUE.equals(config.isProxyFactsViaPdp())) {
46+
return buildPdpUrl(
47+
String.format(
48+
"/facts/relationship_tuples%s",
49+
url
50+
)
51+
);
52+
} else {
53+
return buildUrl(
54+
String.format(
55+
"/v2/facts/%s/%s/relationship_tuples%s",
56+
config.getContext().getProject(),
57+
config.getContext().getEnvironment(),
58+
url
59+
)
60+
);
61+
}
5262
}
5363

5464
/**

src/main/java/io/permit/sdk/api/ResourceInstancesApi.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,23 @@ public ResourceInstancesApi(OkHttpClient client, PermitConfig config) {
4646
* @return The formatted URL for the ResourceInstances API.
4747
*/
4848
private String getResourceInstancesUrl(String url) {
49-
return buildUrl(
50-
String.format(
51-
"/v2/facts/%s/%s/resource_instances%s",
52-
config.getContext().getProject(),
53-
config.getContext().getEnvironment(),
54-
url
55-
)
56-
);
49+
if (Boolean.TRUE.equals(config.isProxyFactsViaPdp())) {
50+
return buildPdpUrl(
51+
String.format(
52+
"/facts/resource_instances%s",
53+
url
54+
)
55+
);
56+
} else {
57+
return buildUrl(
58+
String.format(
59+
"/v2/facts/%s/%s/resource_instances%s",
60+
config.getContext().getProject(),
61+
config.getContext().getEnvironment(),
62+
url
63+
)
64+
);
65+
}
5766
}
5867

5968
/**

src/main/java/io/permit/sdk/api/RoleAssignmentsApi.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,23 @@ public RoleAssignmentsApi(OkHttpClient client, PermitConfig config) {
4444
* @return The complete URL for role assignments.
4545
*/
4646
private String getRoleAssignmentsUrl(String url) {
47-
return buildUrl(
48-
String.format(
49-
"/v2/facts/%s/%s/role_assignments%s",
50-
config.getContext().getProject(),
51-
config.getContext().getEnvironment(),
52-
url
53-
)
54-
);
47+
if (Boolean.TRUE.equals(config.isProxyFactsViaPdp())) {
48+
return buildPdpUrl(
49+
String.format(
50+
"/facts/role_assignments%s",
51+
url
52+
)
53+
);
54+
} else {
55+
return buildUrl(
56+
String.format(
57+
"/v2/facts/%s/%s/role_assignments%s",
58+
config.getContext().getProject(),
59+
config.getContext().getEnvironment(),
60+
url
61+
)
62+
);
63+
}
5564
}
5665

5766
/**

src/main/java/io/permit/sdk/api/TenantsApi.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,23 @@ public TenantsApi(OkHttpClient client, PermitConfig config) {
4949
* @return The formatted URL for the Tenants API.
5050
*/
5151
private String getTenantsUrl(String url) {
52-
return buildUrl(
53-
String.format(
54-
"/v2/facts/%s/%s/tenants%s",
55-
config.getContext().getProject(),
56-
config.getContext().getEnvironment(),
57-
url
58-
)
59-
);
52+
if (Boolean.TRUE.equals(config.isProxyFactsViaPdp())) {
53+
return buildPdpUrl(
54+
String.format(
55+
"/facts/tenants%s",
56+
url
57+
)
58+
);
59+
} else {
60+
return buildUrl(
61+
String.format(
62+
"/v2/facts/%s/%s/tenants%s",
63+
config.getContext().getProject(),
64+
config.getContext().getEnvironment(),
65+
url
66+
)
67+
);
68+
}
6069
}
6170

6271
/**

src/main/java/io/permit/sdk/api/UsersApi.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,23 @@ public UsersApi(OkHttpClient client, PermitConfig config) {
5656
* @return The formatted URL for the endpoint.
5757
*/
5858
private String getUsersUrl(String url) {
59-
return buildUrl(
60-
String.format(
61-
"/v2/facts/%s/%s/users%s",
62-
config.getContext().getProject(),
63-
config.getContext().getEnvironment(),
64-
url
65-
)
66-
);
59+
if (Boolean.TRUE.equals(config.isProxyFactsViaPdp())) {
60+
return buildPdpUrl(
61+
String.format(
62+
"/facts/users%s",
63+
url
64+
)
65+
);
66+
} else {
67+
return buildUrl(
68+
String.format(
69+
"/v2/facts/%s/%s/users%s",
70+
config.getContext().getProject(),
71+
config.getContext().getEnvironment(),
72+
url
73+
)
74+
);
75+
}
6776
}
6877

6978
/**

0 commit comments

Comments
 (0)