Skip to content

Commit 5f0a83b

Browse files
committed
Add test, fixes
1 parent 293039d commit 5f0a83b

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/dispatchers/ForwardingDispatcher.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,23 @@
1313

1414
package org.codehaus.plexus.components.secdispatcher.internal.dispatchers;
1515

16-
import org.codehaus.plexus.components.secdispatcher.Dispatcher;
17-
import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
18-
import org.codehaus.plexus.components.secdispatcher.MasterSource;
19-
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
20-
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
21-
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
22-
2316
import javax.inject.Inject;
2417
import javax.inject.Named;
2518
import javax.inject.Singleton;
19+
2620
import java.util.ArrayList;
2721
import java.util.Collection;
2822
import java.util.HashMap;
2923
import java.util.List;
3024
import java.util.Map;
3125

26+
import org.codehaus.plexus.components.secdispatcher.Dispatcher;
27+
import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
28+
import org.codehaus.plexus.components.secdispatcher.MasterSource;
29+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
30+
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
31+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
32+
3233
/**
3334
* This dispatcher forwards requests fully to defined sources.
3435
*/
@@ -58,29 +59,26 @@ public String displayName() {
5859

5960
@Override
6061
public Collection<Field> fields() {
61-
return List.of(
62-
Field.builder(CONF_SOURCE)
63-
.optional(false)
64-
.description("Source of the password")
65-
.options(sources.entrySet().stream()
66-
.map(e -> {
67-
MasterSource ms = e.getValue();
68-
if (ms instanceof MasterSourceMeta m) {
69-
Field.Builder b =
70-
Field.builder(e.getKey()).description(m.description());
71-
if (m.configTemplate().isPresent()) {
72-
b.defaultValue(m.configTemplate().get());
73-
}
74-
return b.build();
75-
} else {
76-
return Field.builder(e.getKey())
77-
.description(e.getKey()
78-
+ "(Field not described, needs manual configuration)")
79-
.build();
80-
}
81-
})
82-
.toList())
83-
.build());
62+
return List.of(Field.builder(CONF_SOURCE)
63+
.optional(false)
64+
.description("Source of the password")
65+
.options(sources.entrySet().stream()
66+
.map(e -> {
67+
MasterSource ms = e.getValue();
68+
if (ms instanceof MasterSourceMeta m) {
69+
Field.Builder b = Field.builder(e.getKey()).description(m.description());
70+
if (m.configTemplate().isPresent()) {
71+
b.defaultValue(m.configTemplate().get());
72+
}
73+
return b.build();
74+
} else {
75+
return Field.builder(e.getKey())
76+
.description(e.getKey() + "(Field not described, needs manual configuration)")
77+
.build();
78+
}
79+
})
80+
.toList())
81+
.build());
8482
}
8583

8684
@Override

src/test/java/org/codehaus/plexus/components/secdispatcher/internal/DefaultSecDispatcherTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
2424
import org.codehaus.plexus.components.secdispatcher.internal.cipher.AESGCMNoPadding;
25+
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.ForwardingDispatcher;
2526
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.LegacyDispatcher;
2627
import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.MasterDispatcher;
2728
import org.codehaus.plexus.components.secdispatcher.internal.sources.EnvMasterSource;
@@ -80,6 +81,18 @@ void masterWithSystemPropertyRoundTrip() throws Exception {
8081
roundtrip();
8182
}
8283

84+
@Test
85+
void forwardingWithEnvDecrypt() throws Exception {
86+
saveSec("forwarding", Map.of("source", "env"));
87+
decryptForwarding("{[name=forwarding,version=something]env:MASTER_PASSWORD}", "masterPw");
88+
}
89+
90+
@Test
91+
void forwardingWithSystemPropertyDecrypt() throws Exception {
92+
saveSec("forwarding", Map.of("source", "system-property"));
93+
decryptForwarding("{[name=forwarding,version=something]system-property:masterPassword}", "masterPw");
94+
}
95+
8396
@Test
8497
void validate() throws Exception {
8598
saveSec("master", Map.of("source", "system-property:masterPassword", "cipher", AESGCMNoPadding.CIPHER_ALG));
@@ -157,7 +170,7 @@ void detection() {
157170
protected void roundtrip() throws Exception {
158171
DefaultSecDispatcher sd = construct();
159172

160-
assertEquals(2, sd.availableDispatchers().size());
173+
assertEquals(3, sd.availableDispatchers().size());
161174
String encrypted = sd.encrypt("supersecret", Map.of(SecDispatcher.DISPATCHER_NAME_ATTR, "master", "a", "b"));
162175
// example:
163176
// {[name=master,cipher=AES/GCM/NoPadding,a=b]vvq66pZ7rkvzSPStGTI9q4QDnsmuDwo+LtjraRel2b0XpcGJFdXcYAHAS75HUA6GLpcVtEkmyQ==}
@@ -170,6 +183,14 @@ protected void roundtrip() throws Exception {
170183
assertEquals("supersecret", pass);
171184
}
172185

186+
protected void decryptForwarding(String encrypted, String decrypted) throws Exception {
187+
DefaultSecDispatcher sd = construct();
188+
189+
assertEquals(3, sd.availableDispatchers().size());
190+
String pass = sd.decrypt(encrypted);
191+
assertEquals(decrypted, pass);
192+
}
193+
173194
protected DefaultSecDispatcher construct() {
174195
return new DefaultSecDispatcher(
175196
Map.of(
@@ -184,7 +205,15 @@ protected DefaultSecDispatcher construct() {
184205
GpgAgentMasterSource.NAME,
185206
new GpgAgentMasterSource())),
186207
"legacy",
187-
new LegacyDispatcher()),
208+
new LegacyDispatcher(),
209+
"forwarding",
210+
new ForwardingDispatcher(Map.of(
211+
EnvMasterSource.NAME,
212+
new EnvMasterSource(),
213+
SystemPropertyMasterSource.NAME,
214+
new SystemPropertyMasterSource(),
215+
GpgAgentMasterSource.NAME,
216+
new GpgAgentMasterSource()))),
188217
CONFIG_PATH);
189218
}
190219

0 commit comments

Comments
 (0)