Skip to content

Commit 1810922

Browse files
authored
fix: allow null setting for sensitive string (#5402)
1 parent b8324eb commit 1810922

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

core/common/participant-context-config-core/src/main/java/org/eclipse/edc/participantcontext/config/ParticipantContextConfigImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ public Boolean getBoolean(String participantContextId, String key, Boolean defau
8585

8686
@Override
8787
public String getSensitiveString(String participantContextId, String key) {
88-
var encryptedValue = privateConfig(participantContextId).getString(key);
88+
var encryptedValue = privateConfig(participantContextId).getString(key, null);
89+
if (encryptedValue == null) {
90+
return null;
91+
}
8992
return encryptionService.decrypt(encryptedValue)
9093
.orElseThrow(f -> new EdcException(format("Failed to decrypt sensitive config value for key %s and participant context %s", key, participantContextId)));
9194
}

core/common/participant-context-config-core/src/test/java/org/eclipse/edc/participantcontext/config/ParticipantContextConfigImplTest.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.edc.spi.EdcException;
2222
import org.eclipse.edc.spi.result.Result;
2323
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
24+
import org.junit.jupiter.api.Nested;
2425
import org.junit.jupiter.api.Test;
2526
import org.junit.jupiter.api.extension.ExtensionContext;
2627
import org.junit.jupiter.params.ParameterizedTest;
@@ -31,9 +32,11 @@
3132
import java.util.Map;
3233
import java.util.stream.Stream;
3334

35+
import static java.util.Collections.emptyMap;
3436
import static org.assertj.core.api.Assertions.assertThat;
3537
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3638
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.verifyNoInteractions;
3740
import static org.mockito.Mockito.when;
3841

3942
public class ParticipantContextConfigImplTest {
@@ -85,23 +88,38 @@ void notFound(SettingCall setting, String key, String value, Object expectedValu
8588

8689
}
8790

88-
@Test
89-
void shouldGetPrivateSetting() {
91+
@Nested
92+
class GetSensitiveString {
9093

91-
var cfg = ParticipantContextConfiguration.Builder.newInstance().participantContextId(PARTICIPANT_CONTEXT_ID)
92-
.entries(Map.of("key", "value"))
93-
.privateEntries(Map.of("private.key", "encryptedValue"))
94-
.build();
94+
@Test
95+
void shouldGetPrivateSetting() {
96+
var cfg = ParticipantContextConfiguration.Builder.newInstance().participantContextId(PARTICIPANT_CONTEXT_ID)
97+
.entries(Map.of("key", "value"))
98+
.privateEntries(Map.of("private.key", "encryptedValue"))
99+
.build();
95100

96-
when(encryptionService.decrypt("encryptedValue")).thenReturn(Result.success("decryptedValue"));
97-
when(store.get(PARTICIPANT_CONTEXT_ID)).thenReturn(cfg);
101+
when(encryptionService.decrypt("encryptedValue")).thenReturn(Result.success("decryptedValue"));
102+
when(store.get(PARTICIPANT_CONTEXT_ID)).thenReturn(cfg);
98103

99-
var result = contextConfig.getSensitiveString(PARTICIPANT_CONTEXT_ID, "private.key");
104+
var result = contextConfig.getSensitiveString(PARTICIPANT_CONTEXT_ID, "private.key");
100105

106+
assertThat(result).isNotNull()
107+
.isEqualTo("decryptedValue");
108+
}
101109

102-
assertThat(result).isNotNull()
103-
.isEqualTo("decryptedValue");
110+
@Test
111+
void shouldReturnNull_whenNoSettingFound() {
112+
var cfg = ParticipantContextConfiguration.Builder.newInstance().participantContextId(PARTICIPANT_CONTEXT_ID)
113+
.entries(emptyMap())
114+
.privateEntries(emptyMap())
115+
.build();
116+
when(store.get(PARTICIPANT_CONTEXT_ID)).thenReturn(cfg);
117+
118+
var result = contextConfig.getSensitiveString(PARTICIPANT_CONTEXT_ID, "any");
104119

120+
assertThat(result).isNull();
121+
verifyNoInteractions(encryptionService);
122+
}
105123
}
106124

107125
@FunctionalInterface

extensions/common/vault/vault-hashicorp/src/main/java/org/eclipse/edc/vault/hashicorp/HashicorpVault.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,24 @@ public Result<Void> deleteSecret(String vaultPartition, String key) {
8888
.deleteSecret(key);
8989
}
9090

91+
/**
92+
* creates a new HashicorpVaultClient specific configuration / auth settings for the given vault partition.
93+
* If no vault config is found for the given partition, the default is returned.
94+
*/
9195
private @NotNull HashicorpVaultClient getVaultClient(String vaultPartition) {
9296
if (vaultPartition == null) {
9397
return createDefault();
9498
}
95-
var client = createForPartition(vaultPartition);
96-
if (client == null) {
97-
if (vaultConfig.isAllowFallback()) {
98-
return createDefault();
99-
}
100-
throw new IllegalArgumentException("No vault config found for partition '%s' and falling back to the default vault is not allowed".formatted(vaultPartition));
101-
}
102-
return client;
103-
}
10499

105-
/**
106-
* creates a new HashicorpVaultClient specific configuration / auth settings for the given vault partition.
107-
* If no vault config is found for the given partition, null is returned.
108-
*/
109-
private @Nullable HashicorpVaultClient createForPartition(String vaultPartition) {
110100
var settings = forParticipant(vaultPartition, participantContextConfig);
111-
if (settings == null) {
112-
return null;
101+
if (settings != null) {
102+
return new HashicorpVaultClient(monitor, settings.config(), edcHttpClient, mapper, settings.tokenProvider(edcHttpClient));
103+
}
104+
105+
if (vaultConfig.isAllowFallback()) {
106+
return createDefault();
113107
}
114-
return new HashicorpVaultClient(monitor, settings.config(), edcHttpClient, mapper, settings.tokenProvider(edcHttpClient));
108+
throw new IllegalArgumentException("No vault config found for partition '%s' and falling back to the default vault is not allowed".formatted(vaultPartition));
115109
}
116110

117111
/**

spi/common/participant-context-config-spi/src/main/java/org/eclipse/edc/participantcontext/spi/config/ParticipantContextConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public interface ParticipantContextConfig {
106106
*
107107
* @param participantContextId the participant context identifier
108108
* @param key of the setting
109-
* @return a String representation of the setting
110-
* @throws EdcException if no setting is found
109+
* @return a String representation of the setting, null if the setting does not exist
110+
* @throws EdcException if the setting cannot be decrypted
111111
*/
112112
String getSensitiveString(String participantContextId, String key);
113113
}

0 commit comments

Comments
 (0)