Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(auto): log kas key cache #130

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ jobs:
contents: read
packages: read
needs: platform-integration
uses: opentdf/tests/.github/workflows/xtest.yml@main
uses: opentdf/tests/.github/workflows/xtest.yml@feature/enable-cached-kesy
with:
java-ref: ${{ github.ref }}

Expand Down
20 changes: 20 additions & 0 deletions sdk/src/main/java/io/opentdf/platform/sdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ public static class KASInfo {
public String KID;
public Boolean Default;
public String Algorithm;
@Override
public String toString() {
var sb = new StringBuilder("KASInfo{");
if (this.URL != null) {
sb.append("URL:\"").append(this.URL).append("\",");
}
if (this.PublicKey != null) {
sb.append("PublicKey:\"").append(this.PublicKey).append("\",");
}
if (this.KID != null) {
sb.append("KID:\"").append(this.KID).append("\",");
}
if (this.Default != null) {
sb.append("Default:").append(this.Default).append(",");
}
if (this.Algorithm != null) {
sb.append("Algorithm:\"").append(this.Algorithm).append("\",");
}
return sb.append("}").toString();
}
}


Expand Down
11 changes: 10 additions & 1 deletion sdk/src/main/java/io/opentdf/platform/sdk/KASKeyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KASKeyCache {
public static Logger logger = LoggerFactory.getLogger(KASKeyCache.class);

Map<KASKeyRequest, TimeStampedKASInfo> cache;

public KASKeyCache() {
Expand All @@ -22,22 +27,26 @@ public Config.KASInfo get(String url, String algorithm) {
TimeStampedKASInfo cachedValue = cache.get(cacheKey);

if (cachedValue == null) {
logger.info("Cache miss [{}#{}]", url, algorithm);
return null;
}

LocalDateTime aMinAgo = now.minus(5, ChronoUnit.MINUTES);
if (aMinAgo.isAfter(cachedValue.timestamp)) {
logger.info("Cache out of date [{}#{}]", url, algorithm);
cache.remove(cacheKey);
return null;
}

logger.info("Cache hit [{}#{}]: [{}]", url, algorithm, cachedValue.kasInfo);
return cachedValue.kasInfo;
}

public void store(Config.KASInfo kasInfo) {
KASKeyRequest cacheKey = new KASKeyRequest(kasInfo.URL, kasInfo.Algorithm);
cache.put(cacheKey, new TimeStampedKASInfo(kasInfo, LocalDateTime.now()));
}
logger.info("Cache store [{}]", kasInfo);
}
}

class TimeStampedKASInfo {
Expand Down
5 changes: 2 additions & 3 deletions sdk/src/main/java/io/opentdf/platform/sdk/TDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ private void prepareManifest(Config.TDFConfig tdfConfig, SDK.KAS kas) {
var getKI = new Config.KASInfo();
getKI.URL = splitInfo.kas;
getKI.Algorithm = "rsa:2048";
getKI = kas.getPublicKey(getKI);
latestKASInfo.put(splitInfo.kas, getKI);
ki = getKI;
ki = kas.getPublicKey(getKI);
latestKASInfo.put(splitInfo.kas, ki);
}
if (conjunction.containsKey(splitInfo.splitID)) {
conjunction.get(splitInfo.splitID).add(ki);
Expand Down
Loading