Skip to content

Commit

Permalink
Expose Authz GetModified
Browse files Browse the repository at this point in the history
  • Loading branch information
slavikm committed Jan 22, 2024
1 parent cdc8f0d commit 5d6877c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/descope/literals/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@ public static class ManagementEndPoints {
public static final String MANAGEMENT_AUTHZ_RE_RESOURCE = "/v1/mgmt/authz/re/resource";
public static final String MANAGEMENT_AUTHZ_RE_TARGETS = "/v1/mgmt/authz/re/targets";
public static final String MANAGEMENT_AUTHZ_RE_TARGET_ALL = "/v1/mgmt/authz/re/targetall";
public static final String MANAGEMENT_AUTHZ_GET_MODIFIED = "/v1/mgmt/authz/getmodified";
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/descope/sdk/mgmt/AuthzService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.descope.sdk.mgmt;

import com.descope.exception.DescopeException;
import com.descope.model.authz.Modified;
import com.descope.model.authz.Namespace;
import com.descope.model.authz.Relation;
import com.descope.model.authz.RelationDefinition;
import com.descope.model.authz.RelationQuery;
import com.descope.model.authz.Schema;
import java.time.Instant;
import java.util.List;

/** Provides ReBAC authorization service APIs. */
Expand Down Expand Up @@ -149,4 +151,14 @@ void saveRelationDefinition(RelationDefinition relationDefinition, String namesp
* @throws DescopeException If there occurs any exception, a subtype of this exception will be thrown.
*/
List<Relation> whatCanTargetAccess(String target) throws DescopeException;

/**
* Return list of targets and resources changed since the given date.
* Should be used to invalidate local caches.
*
* @param since return the changes since this instant
* @return {@link Modified} including resources and targets changed
* @throws DescopeException If there occurs any exception, a subtype of this exception will be thrown.
*/
Modified getModified(Instant since) throws DescopeException;
}
18 changes: 18 additions & 0 deletions src/main/java/com/descope/sdk/mgmt/impl/AuthzServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.descope.sdk.mgmt.impl;

import static com.descope.literals.Routes.ManagementEndPoints.MANAGEMENT_AUTHZ_GET_MODIFIED;
import static com.descope.literals.Routes.ManagementEndPoints.MANAGEMENT_AUTHZ_NS_DELETE;
import static com.descope.literals.Routes.ManagementEndPoints.MANAGEMENT_AUTHZ_NS_SAVE;
import static com.descope.literals.Routes.ManagementEndPoints.MANAGEMENT_AUTHZ_RD_DELETE;
Expand All @@ -21,6 +22,7 @@
import com.descope.exception.ServerCommonException;
import com.descope.model.authz.HasRelationsResponse;
import com.descope.model.authz.LoadSchemaResponse;
import com.descope.model.authz.Modified;
import com.descope.model.authz.Namespace;
import com.descope.model.authz.Relation;
import com.descope.model.authz.RelationDefinition;
Expand All @@ -31,6 +33,8 @@
import com.descope.model.client.Client;
import com.descope.proxy.ApiProxy;
import com.descope.sdk.mgmt.AuthzService;
import java.time.Instant;
import java.time.Period;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -231,4 +235,18 @@ public List<Relation> whatCanTargetAccess(String target) throws DescopeException
RelationsResponse resp = apiProxy.post(getUri(MANAGEMENT_AUTHZ_RE_TARGET_ALL), request, RelationsResponse.class);
return resp.getRelations();
}

@Override
public Modified getModified(Instant since) throws DescopeException {
Instant now = Instant.now();
if (since != null && (since.isBefore(now.minus(Period.ofDays(1))) || since.isAfter(now))) {
throw ServerCommonException.invalidArgument("since");
}
Map<String, Object> request = new HashMap<>();
if (since != null) {
request.put("since", since.toEpochMilli());
}
ApiProxy apiProxy = getApiProxy();
return apiProxy.post(getUri(MANAGEMENT_AUTHZ_GET_MODIFIED), request, Modified.class);
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/descope/sdk/mgmt/impl/AuthzServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.descope.exception.ServerCommonException;
import com.descope.model.authz.HasRelationsResponse;
import com.descope.model.authz.LoadSchemaResponse;
import com.descope.model.authz.Modified;
import com.descope.model.authz.Namespace;
import com.descope.model.authz.Relation;
import com.descope.model.authz.RelationDefinition;
Expand All @@ -31,6 +32,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.time.Instant;
import java.time.Period;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -399,6 +402,28 @@ void testWhatCanUserAccessForSuccess() {
}
}

@Test
void testGetModifiedForWrongSince() {
ServerCommonException thrown =
assertThrows(
ServerCommonException.class,
() -> authzService.getModified(Instant.now().minus(Period.ofDays(2))));
assertNotNull(thrown);
assertEquals("The since argument is invalid", thrown.getMessage());
}

@Test
void testGetModifiedForSuccess() {
ApiProxy apiProxy = mock(ApiProxy.class);
doReturn(new Modified(null, null, true)).when(apiProxy).post(any(), any(), any());
try (MockedStatic<ApiProxyBuilder> mockedApiProxyBuilder = mockStatic(ApiProxyBuilder.class)) {
mockedApiProxyBuilder.when(
() -> ApiProxyBuilder.buildProxy(any(), any())).thenReturn(apiProxy);
Modified modified = authzService.getModified(null);
assertTrue(modified.isSchemaChanged());
}
}

@RetryingTest(value = 3, suspendForMs = 30000, onExceptions = RateLimitExceededException.class)
void testFunctionalFullCycle() throws Exception {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Expand Down

0 comments on commit 5d6877c

Please sign in to comment.