Skip to content

Commit

Permalink
Remove error logging from API proxy (#73)
Browse files Browse the repository at this point in the history
* 1. Remove error log from api proxy
2. Add server response to exception in case of error
3. Add all the OAuth predefined providers as constants
4. Fix Authz test to not conflict between parallel runs in CI

* Fix testUser test
  • Loading branch information
slavikm authored Oct 25, 2023
1 parent d5a49c8 commit de5d90c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/management-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.descope</groupId>
<artifactId>java-sdk</artifactId>
<version>1.0.8</version>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.descope</groupId>
<artifactId>java-sdk</artifactId>
<modelVersion>4.0.0</modelVersion>
<version>1.0.9</version>
<version>1.0.10</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>Java library used to integrate with Descope.</description>
<url>https://github.com/descope/descope-java</url>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/descope/exception/ServerCommonException.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import static com.descope.exception.ErrorCode.INVALID_ARGUMENT;
import static com.descope.exception.ErrorCode.INVALID_SIGNING_KEY;

import lombok.Getter;
import lombok.ToString;

@ToString(callSuper = true, includeFieldNames = true)
@Getter
public class ServerCommonException extends DescopeException {

private String serverResponse;

protected ServerCommonException(String message, String code) {
super(message);
setCode(code);
Expand All @@ -32,7 +36,9 @@ public static ServerCommonException invalidSigningKey(String error) {
return new ServerCommonException(error, INVALID_SIGNING_KEY);
}

public static ServerCommonException genericServerError(String message, String code) {
return new ServerCommonException(message, code);
public static ServerCommonException genericServerError(String message, String code, String serverResponse) {
var e = new ServerCommonException(message, code);
e.serverResponse = serverResponse;
return e;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/descope/literals/AppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public class AppConstants {
public static final String TENANTS_CLAIM_KEY = "tenants";
public static final String PERMISSIONS_CLAIM_KEY = "permissions";
public static final String ROLES_CLAIM_KEY = "roles";
public static final String OAUTH_PROVIDER_APPLE = "apple";
public static final String OAUTH_PROVIDER_DISCORD = "discord";
public static final String OAUTH_PROVIDER_FACEBOOK = "facebook";
public static final String OAUTH_PROVIDER_GITHUB = "github";
public static final String OAUTH_PROVIDER_GITLAB = "gitlab";
public static final String OAUTH_PROVIDER_GOOGLE = "google";
public static final String OAUTH_PROVIDER_LINKEDIN = "linkedin";
public static final String OAUTH_PROVIDER_MICROSOFT = "microsoft";
public static final String OAUTH_PROVIDER_SLACK = "slack";
}
8 changes: 4 additions & 4 deletions src/main/java/com/descope/proxy/impl/AbstractProxyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,19 @@ private static <R> Supplier<R> toSupplierOfType(
}
try {
var errorDetails = objectMapper.readValue(tee, JsonBodyHandler.ErrorDetails.class);
log.error(errorDetails.getActualMessage());
log.error(bs.toString());
log.debug(errorDetails.getActualMessage());
log.debug(bs.toString());
if (ErrorCode.RATE_LIMIT_EXCEEDED.equals(errorDetails.errorCode)) {
throw new RateLimitExceededException(
errorDetails.getActualMessage(),
errorDetails.getErrorCode(),
responseInfo.headers().firstValueAsLong(RETRY_AFTER_HEADER).orElse(DEFAULT_RETRY));
}
throw ServerCommonException.genericServerError(
errorDetails.getActualMessage(), errorDetails.getErrorCode());
errorDetails.getActualMessage(), errorDetails.getErrorCode(), bs.toString());
} catch (IOException e) {
throw ServerCommonException.genericServerError(
bs.toString(), String.valueOf(responseInfo.statusCode()));
bs.toString(), String.valueOf(responseInfo.statusCode()), bs.toString());
}
}
var res = objectMapper.readValue(tee, returnClz);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.descope.sdk.auth.impl;

import static com.descope.literals.AppConstants.OAUTH_PROVIDER_GOOGLE;
import static com.descope.sdk.TestUtils.MOCK_JWT_RESPONSE;
import static com.descope.sdk.TestUtils.MOCK_SIGNING_KEY;
import static com.descope.sdk.TestUtils.MOCK_TOKEN;
Expand Down Expand Up @@ -100,7 +101,7 @@ void testExchangeToken() {
}

void testExampleRequireBrowser() throws Exception {
System.out.println(oauthService.start("google", "https://localhost/kuku", null));
System.out.println(oauthService.start(OAUTH_PROVIDER_GOOGLE, "https://localhost/kuku", null));
String encodedCode = "";
var code = URLDecoder.decode(encodedCode, "UTF-8");
var authInfo = oauthService.exchangeToken(code);
Expand Down
32 changes: 17 additions & 15 deletions src/test/java/com/descope/sdk/mgmt/impl/AuthzServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,20 +402,23 @@ void testFunctionalFullCycle() throws Exception {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.findAndRegisterModules();
Schema s = mapper.readValue(new File("src/test/data/files.yaml"), Schema.class);
authzService.deleteSchema();
authzService.saveSchema(s, true);
authzService.createRelations(List.of(
new Relation("Dev", "parent", "org", "Descope", null, null, null, null),
new Relation("Sales", "parent", "org", "Descope", null, null, null, null),
new Relation("Dev", "member", "org", "u1", null, null, null, null),
new Relation("Dev", "member", "org", "u3", null, null, null, null),
new Relation("Sales", "member", "org", "u2", null, null, null, null),
new Relation("Presentations", "parent", "folder", "Internal", null, null, null, null),
new Relation("roadmap.ppt", "parent", "doc", "Presentations", null, null, null, null),
new Relation("roadmap.ppt", "owner", "doc", "u1", null, null, null, null),
new Relation("Internal", "viewer", "folder", null, "Descope", "member", "org", null),
new Relation("Presentations", "editor", "folder", null, "Sales", "member", "org", null)
));
Schema existingSchema = authzService.loadSchema();
if (!existingSchema.getName().equals(s.getName())) {
authzService.deleteSchema();
authzService.saveSchema(s, true);
authzService.createRelations(List.of(
new Relation("Dev", "parent", "org", "Descope", null, null, null, null),
new Relation("Sales", "parent", "org", "Descope", null, null, null, null),
new Relation("Dev", "member", "org", "u1", null, null, null, null),
new Relation("Dev", "member", "org", "u3", null, null, null, null),
new Relation("Sales", "member", "org", "u2", null, null, null, null),
new Relation("Presentations", "parent", "folder", "Internal", null, null, null, null),
new Relation("roadmap.ppt", "parent", "doc", "Presentations", null, null, null, null),
new Relation("roadmap.ppt", "owner", "doc", "u1", null, null, null, null),
new Relation("Internal", "viewer", "folder", null, "Descope", "member", "org", null),
new Relation("Presentations", "editor", "folder", null, "Sales", "member", "org", null)
));
}
var resp = authzService.hasRelations(List.of(
new RelationQuery("roadmap.ppt", "owner", "doc", "u1", false),
new RelationQuery("roadmap.ppt", "editor", "doc", "u1", false),
Expand All @@ -438,6 +441,5 @@ void testFunctionalFullCycle() throws Exception {
assertThat(respUsersRelations).size().isEqualTo(2);
var respWhat = authzService.whatCanTargetAccess("u1");
assertThat(respWhat).size().isEqualTo(7);
authzService.deleteSchema();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ void testFunctionalTestUsers() {
}
assertTrue(found);
searchResponse = userService.searchAll(
UserSearchRequest.builder().testUsersOnly(true).build());
UserSearchRequest.builder().testUsersOnly(true).emails(List.of(user.getEmail())).build());
found = false;
for (var u : searchResponse.getUsers()) {
if (u.getUserId().equals(createResponse.getUser().getUserId())) {
Expand Down

0 comments on commit de5d90c

Please sign in to comment.