Skip to content

Commit 76085d3

Browse files
committed
sonarcloud
1 parent 7f5a2d4 commit 76085d3

5 files changed

Lines changed: 47 additions & 58 deletions

File tree

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.Collections;
27-
import java.util.Comparator;
2827
import java.util.HashMap;
2928
import java.util.HashSet;
3029
import java.util.List;
@@ -61,6 +60,10 @@ class Autoconfigure {
6160

6261
private static Logger logger = LoggerFactory.getLogger(Autoconfigure.class);
6362

63+
private Autoconfigure() {
64+
// Prevent instantiation, this class is a utility class that is only used statically
65+
}
66+
6467
static class KeySplitStep {
6568
final String kas;
6669
final String splitID;
@@ -288,7 +291,7 @@ List<AttributeValueFQN> getPolicy() {
288291
return policy;
289292
}
290293

291-
boolean addAllGrants(AttributeValueFQN fqn, List<KeyAccessServer> granted, List<SimpleKasKey> mapped, Attribute attr, KASKeyCache keyCache) {
294+
boolean addAllGrants(AttributeValueFQN fqn, List<KeyAccessServer> granted, List<SimpleKasKey> mapped, Attribute attr) {
292295
boolean foundMappedKey = false;
293296
for (var mappedKey: mapped) {
294297
foundMappedKey = true;
@@ -312,10 +315,11 @@ boolean addAllGrants(AttributeValueFQN fqn, List<KeyAccessServer> granted, List<
312315
continue;
313316
}
314317
var cachedGrantKeys = grantedKey.getPublicKey().getCached().getKeysList();
315-
if (cachedGrantKeys.isEmpty()) {
316-
logger.debug("no keys cached in policy service");
317-
continue;
318+
319+
if (logger.isDebugEnabled()) {
320+
logger.debug("found {} keys cached in policy service", cachedGrantKeys.size());
318321
}
322+
319323
for (var cachedGrantKey: cachedGrantKeys) {
320324
var mappedKey = new Config.KASInfo();
321325
mappedKey.URL = grantedKey.getUri();
@@ -444,7 +448,6 @@ BooleanKeyExpression insertKeysForAttribute(AttributeBooleanExpression e) throws
444448

445449
List<String> kases = grant.kases;
446450
if (kases.isEmpty()) {
447-
// TODO: replace this with a reference to the base key
448451
kases = List.of(RuleType.EMPTY_TERM);
449452
}
450453

@@ -469,13 +472,13 @@ BooleanKeyExpression assignKeysTo(AttributeBooleanExpression e) {
469472
for (var clause : e.must) {
470473
ArrayList<PublicKeyInfo> keys = new ArrayList<>();
471474
if (clause.values.isEmpty()) {
472-
logger.warn("No values found for attribute: " + clause.def.getFqn());
475+
logger.warn("No values found for attribute {}", clause.def.getFqn());
473476
continue;
474477
}
475478
for (var value : clause.values) {
476479
var mapped = mappedKeys.get(value.key);
477480
if (mapped == null) {
478-
logger.warn("No keys found for attribute value {} ", value);
481+
logger.warn("No keys found for attribute value {}", value);
479482
continue;
480483
}
481484
for (var kasInfo : mapped) {
@@ -874,16 +877,16 @@ private static Granter getGranter(KASKeyCache keyCache, List<GetAttributeValuesB
874877
var attribute = attributeAndValue.getAttribute();
875878
var namespace = attribute.getNamespace();
876879

877-
if (grants.addAllGrants(fqn, value.getGrantsList(), value.getKasKeysList(), attribute, keyCache)) {
880+
if (grants.addAllGrants(fqn, value.getGrantsList(), value.getKasKeysList(), attribute)) {
878881
storeKeysToCache(value.getGrantsList(), value.getKasKeysList(), keyCache);
879882
continue;
880883
}
881-
if (grants.addAllGrants(fqn, attribute.getGrantsList(), attribute.getKasKeysList(), attribute, keyCache)) {
884+
if (grants.addAllGrants(fqn, attribute.getGrantsList(), attribute.getKasKeysList(), attribute)) {
882885
storeKeysToCache(attribute.getGrantsList(), attribute.getKasKeysList(), keyCache);
883886
continue;
884887
}
885888
storeKeysToCache(namespace.getGrantsList(), namespace.getKasKeysList(), keyCache);
886-
grants.addAllGrants(fqn, namespace.getGrantsList(), namespace.getKasKeysList(), attribute, keyCache);
889+
grants.addAllGrants(fqn, namespace.getGrantsList(), namespace.getKasKeysList(), attribute);
887890
}
888891

889892
return grants;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ static Optional<SimpleKasKey> fetchBaseKey(WellKnownServiceClientInterface wellk
8888
try {
8989
response = RequestHelper.getOrThrow(responseMessage);
9090
} catch (ConnectException e) {
91-
logger.error("unable to retrieve configuration from well known endpoint", e);
9291
throw new SDKException("unable to retrieve base key from well known endpoint", e);
9392
}
9493

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ private void prepareManifest(Config.TDFConfig tdfConfig, Map<String, List<KASInf
150150

151151

152152
List<byte[]> symKeys = new ArrayList<>(splits.size());
153-
for (String splitID : splits.keySet()) {
153+
for (var split : splits.entrySet()) {
154+
String splitID = split.getKey();
155+
154156
// Symmetric key
155157
byte[] symKey = new byte[GCM_KEY_SIZE];
156158
sRandom.nextBytes(symKey);
@@ -177,7 +179,8 @@ private void prepareManifest(Config.TDFConfig tdfConfig, Map<String, List<KASInf
177179
encryptedMetadata = encoder.encodeToString(metadata.getBytes(StandardCharsets.UTF_8));
178180
}
179181

180-
for (Config.KASInfo kasInfo : splits.get(splitID)) {
182+
List<KASInfo> kasInfos = split.getValue();
183+
for (Config.KASInfo kasInfo : kasInfos) {
181184
if (kasInfo.PublicKey == null || kasInfo.PublicKey.isEmpty()) {
182185
throw new SDK.KasPublicKeyMissing("Kas public key is missing in kas information list");
183186
}

sdk/src/test/java/io/opentdf/platform/sdk/AutoconfigureTest.java

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package io.opentdf.platform.sdk;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
6-
import static org.junit.jupiter.api.Assertions.assertEquals;
7-
import static org.junit.jupiter.api.Assertions.assertNotNull;
8-
import static org.mockito.ArgumentMatchers.any;
9-
import static org.mockito.Mockito.mock;
10-
import static org.mockito.Mockito.never;
11-
import static org.mockito.Mockito.verify;
12-
import static org.mockito.Mockito.when;
13-
143
import com.connectrpc.ResponseMessage;
154
import com.connectrpc.UnaryBlockingCall;
165
import io.opentdf.platform.policy.Algorithm;
@@ -29,11 +18,10 @@
2918
import io.opentdf.platform.policy.attributes.GetAttributeValuesByFqnsRequest;
3019
import io.opentdf.platform.policy.attributes.GetAttributeValuesByFqnsResponse;
3120
import io.opentdf.platform.sdk.Autoconfigure.AttributeValueFQN;
21+
import io.opentdf.platform.sdk.Autoconfigure.Granter;
3222
import io.opentdf.platform.sdk.Autoconfigure.Granter.AttributeBooleanExpression;
3323
import io.opentdf.platform.sdk.Autoconfigure.Granter.BooleanKeyExpression;
3424
import io.opentdf.platform.sdk.Autoconfigure.KeySplitStep;
35-
import io.opentdf.platform.sdk.Autoconfigure.Granter;
36-
3725
import org.junit.jupiter.api.BeforeAll;
3826
import org.junit.jupiter.api.Test;
3927
import org.mockito.Mockito;
@@ -48,9 +36,20 @@
4836
import java.util.concurrent.atomic.AtomicInteger;
4937
import java.util.function.BiFunction;
5038
import java.util.function.Supplier;
51-
import java.util.stream.Collectors;
5239
import java.util.regex.Matcher;
5340
import java.util.regex.Pattern;
41+
import java.util.stream.Collectors;
42+
43+
import static org.assertj.core.api.Assertions.assertThat;
44+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
46+
import static org.junit.jupiter.api.Assertions.assertEquals;
47+
import static org.junit.jupiter.api.Assertions.assertNotNull;
48+
import static org.mockito.ArgumentMatchers.any;
49+
import static org.mockito.Mockito.mock;
50+
import static org.mockito.Mockito.never;
51+
import static org.mockito.Mockito.verify;
52+
import static org.mockito.Mockito.when;
5453

5554
public class AutoconfigureTest {
5655

@@ -514,7 +513,7 @@ public void testReasonerConstructAttributeBoolean() {
514513
var wrapper = new Object() {
515514
int i = 0;
516515
};
517-
List<KeySplitStep> plan = reasoner.getSplits(tc.getDefaults(), () -> String.valueOf(wrapper.i++ + 1), () -> Optional.empty());
516+
List<KeySplitStep> plan = reasoner.getSplits(tc.getDefaults(), () -> String.valueOf(wrapper.i++ + 1), Optional::empty);
518517
assertThat(plan)
519518
.as(tc.name)
520519
.isEqualTo(tc.getPlan());
@@ -525,7 +524,7 @@ public void testReasonerConstructAttributeBoolean() {
525524
void testUsingAttributeMappedAtNamespace() {
526525
Granter granter = Autoconfigure.newGranterFromAttributes(new KASKeyCache(), mockValueFor(mp2uns2uns));
527526
var counter = new AtomicInteger(0);
528-
var splitPlan = granter.getSplits(Collections.emptyList(), () -> Integer.toString(counter.getAndIncrement()), () -> Optional.empty());
527+
var splitPlan = granter.getSplits(Collections.emptyList(), () -> Integer.toString(counter.getAndIncrement()), Optional::empty);
529528
assertThat(splitPlan).isEqualTo(List.of(new KeySplitStep("https://mapped.example.com", "", NAMESPACE_KAS_KEY.getPublicKey().getKid())));
530529
}
531530

@@ -534,7 +533,7 @@ void testUsingAttributeMappedAtMultiplePlaces() {
534533
var attributes = new Value[]{mockValueFor(mp2uns2uns), mockValueFor(mp2uns2mp)};
535534
Granter granter = Autoconfigure.newGranterFromAttributes(new KASKeyCache(), attributes);
536535
var counter = new AtomicInteger(0);
537-
var splitPlan = granter.getSplits(Collections.emptyList(), () -> Integer.toString(counter.getAndIncrement()), () -> Optional.empty());
536+
var splitPlan = granter.getSplits(Collections.emptyList(), () -> Integer.toString(counter.getAndIncrement()), Optional::empty);
538537
assertThat(splitPlan).isEqualTo(List.of(
539538
new KeySplitStep(NAMESPACE_KAS_KEY.getKasUri(), "0", NAMESPACE_KAS_KEY.getPublicKey().getKid()),
540539
new KeySplitStep(VALUE_KEY.getKasUri(), "0", VALUE_KEY.getPublicKey().getKid())
@@ -1075,27 +1074,18 @@ void createsGranterFromService() {
10751074

10761075
// Mock the attribute service to return a response with the expected values
10771076
when(attributesServiceClient.getAttributeValuesByFqnsBlocking(any(), any())).thenAnswer(invocation -> {
1078-
return new UnaryBlockingCall<GetAttributeValuesByFqnsResponse>() {
1079-
@Override
1080-
public ResponseMessage<GetAttributeValuesByFqnsResponse> execute() {
1081-
GetAttributeValuesByFqnsResponse.Builder builder = GetAttributeValuesByFqnsResponse.newBuilder();
1082-
for (AttributeValueFQN fqn : policy) {
1083-
Value value = Value.newBuilder()
1084-
.setId(fqn.toString())
1085-
.setFqn(fqn.toString())
1086-
.build();
1087-
builder.putFqnAttributeValues(fqn.toString(),
1088-
GetAttributeValuesByFqnsResponse.AttributeAndValue.newBuilder()
1089-
.setValue(value)
1090-
.build());
1091-
}
1092-
return new ResponseMessage.Success<>(builder.build(), Collections.emptyMap(), Collections.emptyMap());
1093-
}
1094-
1095-
@Override
1096-
public void cancel() {
1097-
}
1098-
};
1077+
GetAttributeValuesByFqnsResponse.Builder builder = GetAttributeValuesByFqnsResponse.newBuilder();
1078+
for (AttributeValueFQN fqn : policy) {
1079+
Value value = Value.newBuilder()
1080+
.setId(fqn.toString())
1081+
.setFqn(fqn.toString())
1082+
.build();
1083+
builder.putFqnAttributeValues(fqn.toString(),
1084+
GetAttributeValuesByFqnsResponse.AttributeAndValue.newBuilder()
1085+
.setValue(value)
1086+
.build());
1087+
}
1088+
return TestUtil.successfulUnaryCall(builder.build());
10991089
});
11001090

11011091
// Act

sdk/src/test/java/io/opentdf/platform/sdk/FakeServices.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package io.opentdf.platform.sdk;
22

3-
import io.opentdf.platform.authorization.AuthorizationServiceClient;
43
import io.opentdf.platform.authorization.AuthorizationServiceClientInterface;
5-
import io.opentdf.platform.policy.attributes.AttributesServiceClient;
64
import io.opentdf.platform.policy.attributes.AttributesServiceClientInterface;
7-
import io.opentdf.platform.policy.kasregistry.KeyAccessServerRegistryServiceClient;
85
import io.opentdf.platform.policy.kasregistry.KeyAccessServerRegistryServiceClientInterface;
9-
import io.opentdf.platform.policy.namespaces.NamespaceServiceClient;
106
import io.opentdf.platform.policy.namespaces.NamespaceServiceClientInterface;
11-
import io.opentdf.platform.policy.resourcemapping.ResourceMappingServiceClient;
127
import io.opentdf.platform.policy.resourcemapping.ResourceMappingServiceClientInterface;
13-
import io.opentdf.platform.policy.subjectmapping.SubjectMappingServiceClient;
148
import io.opentdf.platform.policy.subjectmapping.SubjectMappingServiceClientInterface;
159
import io.opentdf.platform.wellknownconfiguration.WellKnownServiceClientInterface;
1610

0 commit comments

Comments
 (0)