Skip to content

Commit 9d20647

Browse files
authored
feat: add logging (#49)
Adds `slf4j` as well as a `log4j2` backend for testing. Also * add some log statements * catch only the exception emitted by GRPC when making GRPC calls
1 parent 3eeb626 commit 9d20647

File tree

7 files changed

+62
-19
lines changed

7 files changed

+62
-19
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# java-sdk
22

33
OpenTDF Java SDK
4+
5+
### Logging
6+
We use [slf4j](https://www.slf4j.org/), without providing a backend. We use log4j2 in our tests.

pom.xml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,13 @@
3737
<version>5.10.1</version>
3838
<type>pom</type>
3939
<scope>import</scope>
40-
</dependency>
41-
<dependency>
42-
<groupId>org.apache.logging.log4j</groupId>
43-
<artifactId>log4j-api</artifactId>
44-
<version>${log4j.version}</version>
4540
</dependency>
4641
<dependency>
4742
<groupId>org.apache.logging.log4j</groupId>
48-
<artifactId>log4j-core</artifactId>
49-
<version>${log4j.version}</version>
50-
</dependency>
51-
<dependency>
52-
<groupId>org.apache.logging.log4j</groupId>
53-
<artifactId>log4j-slf4j2-impl</artifactId>
54-
<version>${log4j.version}</version>
43+
<artifactId>log4j-bom</artifactId>
44+
<version>2.23.1</version>
45+
<type>pom</type>
46+
<scope>import</scope>
5547
</dependency>
5648
<dependency>
5749
<groupId>org.projectlombok</groupId>

protocol/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757
<exec executable="buf" dir="../">
5858
<arg value="generate" />
5959
<arg value="https://github.com/opentdf/platform.git#branch=main,subdir=service" />
60-
<arg value="--exclude-path"/>
61-
<arg value="authorization/idp_plugin.proto"/>
6260
</exec>
6361
<exec executable="buf" dir="../">
6462
<arg value="generate" />

sdk/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
<artifactId>protocol</artifactId>
1818
<version>0.1.0-SNAPSHOT</version><!-- {x-version-update:java-sdk:current} -->
1919
</dependency>
20+
<dependency>
21+
<groupId>org.slf4j</groupId>
22+
<artifactId>slf4j-api</artifactId>
23+
<version>2.0.13</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.apache.logging.log4j</groupId>
27+
<artifactId>log4j-slf4j2-impl</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.apache.logging.log4j</groupId>
32+
<artifactId>log4j-core</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.logging.log4j</groupId>
37+
<artifactId>log4j-api</artifactId>
38+
<scope>test</scope>
39+
</dependency>
2040
<dependency>
2141
<groupId>org.junit.jupiter</groupId>
2242
<artifactId>junit-jupiter</artifactId>

sdk/src/main/java/io/opentdf/platform/sdk/GRPCAuthInterceptor.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
import com.nimbusds.oauth2.sdk.http.HTTPRequest;
1616
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
1717
import com.nimbusds.oauth2.sdk.token.AccessToken;
18-
import com.nimbusds.oauth2.sdk.tokenexchange.TokenExchangeGrant;
1918
import io.grpc.CallOptions;
2019
import io.grpc.Channel;
2120
import io.grpc.ClientCall;
2221
import io.grpc.ClientInterceptor;
2322
import io.grpc.ForwardingClientCall;
2423
import io.grpc.Metadata;
2524
import io.grpc.MethodDescriptor;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2627

2728
import java.net.URI;
2829
import java.net.URISyntaxException;
@@ -40,6 +41,9 @@ class GRPCAuthInterceptor implements ClientInterceptor {
4041
private final RSAKey rsaKey;
4142
private final URI tokenEndpointURI;
4243

44+
private static final Logger logger = LoggerFactory.getLogger(GRPCAuthInterceptor.class);
45+
46+
4347
/**
4448
* Constructs a new GRPCAuthInterceptor with the specified client authentication and RSA key.
4549
*
@@ -101,6 +105,8 @@ private synchronized AccessToken getToken() {
101105
// If the token is expired or initially null, get a new token
102106
if (token == null || isTokenExpired()) {
103107

108+
logger.trace("The current access token is expired or empty, getting a new one");
109+
104110
// Construct the client credentials grant
105111
AuthorizationGrant clientGrant = new ClientCredentialsGrant();
106112

@@ -124,9 +130,17 @@ private synchronized AccessToken getToken() {
124130
throw new RuntimeException("Token request failed: " + error);
125131
}
126132

127-
this.token = tokenResponse.toSuccessResponse().getTokens().getAccessToken();
128-
// DPoPAccessToken dPoPAccessToken = tokens.getDPoPAccessToken();
129133

134+
var tokens = tokenResponse.toSuccessResponse().getTokens();
135+
if (tokens.getDPoPAccessToken() != null) {
136+
logger.trace("retrieved a new DPoP access token");
137+
} else if (tokens.getAccessToken() != null) {
138+
logger.trace("retrieved a new access token");
139+
} else {
140+
logger.trace("got an access token of unknown type");
141+
}
142+
143+
this.token = tokens.getAccessToken();
130144

131145
if (token.getLifetime() != 0) {
132146
// Need some type of leeway but not sure whats best

sdk/src/main/java/io/opentdf/platform/sdk/SDKBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
import io.grpc.ManagedChannel;
1515
import io.grpc.ManagedChannelBuilder;
1616
import io.grpc.Status;
17+
import io.grpc.StatusRuntimeException;
1718
import io.opentdf.platform.wellknownconfiguration.GetWellKnownConfigurationRequest;
1819
import io.opentdf.platform.wellknownconfiguration.GetWellKnownConfigurationResponse;
1920
import io.opentdf.platform.wellknownconfiguration.WellKnownServiceGrpc;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
2023

2124
import java.io.IOException;
2225
import java.util.UUID;
@@ -65,7 +68,7 @@ ManagedChannel buildChannel() {
6568
var stub = WellKnownServiceGrpc.newBlockingStub(bootstrapChannel);
6669
try {
6770
config = stub.getWellKnownConfiguration(GetWellKnownConfigurationRequest.getDefaultInstance());
68-
} catch (Exception e) {
71+
} catch (StatusRuntimeException e) {
6972
Status status = Status.fromThrowable(e);
7073
throw new SDKException(String.format("Got grpc status [%s] when getting configuration", status), e);
7174
}
@@ -82,7 +85,7 @@ ManagedChannel buildChannel() {
8285
.getFieldsOrThrow(PLATFORM_ISSUER)
8386
.getStringValue();
8487

85-
} catch (Exception e) {
88+
} catch (StatusRuntimeException e) {
8689
throw new SDKException("Error getting the issuer from the platform", e);
8790
}
8891

sdk/src/test/resources/log4j2.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration status="WARN">
3+
<appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
6+
</Console>
7+
</appenders>
8+
<loggers>
9+
<root level="trace">
10+
<appender-ref ref="Console"/>
11+
</root>
12+
</loggers>
13+
</configuration>

0 commit comments

Comments
 (0)