Skip to content

Commit 8dc1597

Browse files
committed
Cleanup
1 parent e0d3d20 commit 8dc1597

File tree

5 files changed

+185
-161
lines changed

5 files changed

+185
-161
lines changed

src/main/java/org/codehaus/plexus/components/secdispatcher/SecDispatcher.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.codehaus.plexus.components.secdispatcher;
1515

1616
import java.util.Map;
17+
import java.util.Set;
1718

1819
/**
1920
* This component decrypts a string, passed to it
@@ -23,7 +24,12 @@
2324
public interface SecDispatcher {
2425
String DEFAULT_CONFIGURATION = "~/.m2/settings-security.xml";
2526
String SYSTEM_PROPERTY_CONFIGURATION_LOCATION = "settings.security";
26-
String DISPATCHER_NAME_ATTR = "dispatcher.name";
27+
String DISPATCHER_NAME_ATTR = "name";
28+
29+
/**
30+
* Returns the set of available dispatcher names, never {@code null}.
31+
*/
32+
Set<String> availableDispatchers();
2733

2834
/**
2935
* encrypt given plaintext string

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/DefaultSecDispatcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.HashMap;
2121
import java.util.Map;
22+
import java.util.Set;
2223
import java.util.StringTokenizer;
2324
import java.util.stream.Collectors;
2425

@@ -56,7 +57,10 @@ public DefaultSecDispatcher(
5657
this.configurationFile = requireNonNull(configurationFile);
5758
}
5859

59-
// ---------------------------------------------------------------
60+
@Override
61+
public Set<String> availableDispatchers() {
62+
return Set.copyOf(dispatchers.keySet());
63+
}
6064

6165
@Override
6266
public String encrypt(String str, Map<String, String> attr) throws SecDispatcherException {
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
14+
package org.codehaus.plexus.components.secdispatcher.internal;
15+
16+
import java.io.FileWriter;
17+
import java.nio.charset.StandardCharsets;
18+
import java.util.Base64;
19+
import java.util.Map;
20+
21+
import org.codehaus.plexus.components.cipher.internal.DefaultPlexusCipher;
22+
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
23+
import org.codehaus.plexus.components.secdispatcher.internal.dispatcher.StaticDispatcher;
24+
import org.codehaus.plexus.components.secdispatcher.internal.sources.EnvMasterPasswordSource;
25+
import org.codehaus.plexus.components.secdispatcher.internal.sources.GpgAgentMasterPasswordSource;
26+
import org.codehaus.plexus.components.secdispatcher.internal.sources.StaticMasterPasswordSource;
27+
import org.codehaus.plexus.components.secdispatcher.internal.sources.SystemPropertyMasterPasswordSource;
28+
import org.codehaus.plexus.components.secdispatcher.model.SettingsSecurity;
29+
import org.codehaus.plexus.components.secdispatcher.model.io.stax.SecurityConfigurationStaxWriter;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Disabled;
32+
import org.junit.jupiter.api.Test;
33+
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
37+
38+
public class DefaultSecDispatcherTest {
39+
String masterPassword = "masterPw";
40+
String password = "somePassword";
41+
String passwordEncrypted = "{TT2NQZ4iAdoHqsSfYUab3s6X2IHl5qaf4vx/F8DvtSI=}";
42+
43+
private void saveSec(String masterSource) throws Exception {
44+
SettingsSecurity sec = new SettingsSecurity();
45+
sec.setMasterSource(masterSource);
46+
47+
try (FileWriter fw = new FileWriter("./target/sec.xml")) {
48+
new SecurityConfigurationStaxWriter().write(fw, sec);
49+
fw.flush();
50+
}
51+
System.setProperty(DefaultSecDispatcher.SYSTEM_PROPERTY_CONFIGURATION_LOCATION, "./target/sec.xml");
52+
}
53+
54+
@BeforeEach
55+
public void prepare() throws Exception {
56+
saveSec("magic:might");
57+
}
58+
59+
@Test
60+
void testEncrypt() throws Exception {
61+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
62+
new DefaultPlexusCipher(),
63+
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
64+
Map.of(),
65+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
66+
String enc = sd.encrypt(password, null);
67+
assertNotNull(enc);
68+
String password1 = sd.decrypt(enc);
69+
assertEquals(password, password1);
70+
}
71+
72+
@Test
73+
void testDecrypt() throws Exception {
74+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
75+
new DefaultPlexusCipher(),
76+
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
77+
Map.of(),
78+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
79+
String pass = sd.decrypt(passwordEncrypted);
80+
assertNotNull(pass);
81+
assertEquals(password, pass);
82+
}
83+
84+
@Test
85+
void testDecryptSystemProperty() throws Exception {
86+
System.setProperty("foobar", masterPassword);
87+
saveSec("prop:foobar");
88+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
89+
new DefaultPlexusCipher(),
90+
Map.of(
91+
"prop",
92+
new SystemPropertyMasterPasswordSource(),
93+
"env",
94+
new EnvMasterPasswordSource(),
95+
"gpg",
96+
new GpgAgentMasterPasswordSource()),
97+
Map.of(),
98+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
99+
String pass = sd.decrypt(passwordEncrypted);
100+
assertNotNull(pass);
101+
assertEquals(password, pass);
102+
}
103+
104+
@Test
105+
void testDecryptEnv() throws Exception {
106+
saveSec("env:MASTER_PASSWORD");
107+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
108+
new DefaultPlexusCipher(),
109+
Map.of(
110+
"prop",
111+
new SystemPropertyMasterPasswordSource(),
112+
"env",
113+
new EnvMasterPasswordSource(),
114+
"gpg",
115+
new GpgAgentMasterPasswordSource()),
116+
Map.of(),
117+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
118+
String pass = sd.decrypt(passwordEncrypted);
119+
assertNotNull(pass);
120+
assertEquals(password, pass);
121+
}
122+
123+
@Disabled("triggers GPG agent: remove this and type in 'masterPw'")
124+
@Test
125+
void testDecryptGpg() throws Exception {
126+
saveSec("gpg-agent:/run/user/1000/gnupg/S.gpg-agent");
127+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
128+
new DefaultPlexusCipher(),
129+
Map.of(
130+
"prop",
131+
new SystemPropertyMasterPasswordSource(),
132+
"env",
133+
new EnvMasterPasswordSource(),
134+
"gpg",
135+
new GpgAgentMasterPasswordSource()),
136+
Map.of(),
137+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
138+
String pass = sd.decrypt(passwordEncrypted);
139+
assertNotNull(pass);
140+
assertEquals(password, pass);
141+
}
142+
143+
@Test
144+
void testEncryptWithDispatcher() throws Exception {
145+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
146+
new DefaultPlexusCipher(),
147+
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
148+
Map.of("magic", new StaticDispatcher("decrypted", "encrypted")),
149+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
150+
151+
String enc = sd.encrypt("whatever", Map.of(SecDispatcher.DISPATCHER_NAME_ATTR, "magic", "a", "b"));
152+
assertNotNull(enc);
153+
assertTrue(enc.contains("encrypted"));
154+
assertTrue(enc.contains(SecDispatcher.DISPATCHER_NAME_ATTR + "=magic"));
155+
String password1 = sd.decrypt(enc);
156+
assertEquals("decrypted", password1);
157+
}
158+
159+
@Test
160+
void testDecryptWithDispatcher() throws Exception {
161+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
162+
new DefaultPlexusCipher(),
163+
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
164+
Map.of("magic", new StaticDispatcher("decrypted", "encrypted")),
165+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
166+
167+
String pass = sd.decrypt("{" + Base64.getEncoder().encodeToString("whatever".getBytes(StandardCharsets.UTF_8))
168+
+ "[a=b," + SecDispatcher.DISPATCHER_NAME_ATTR + "=magic]}");
169+
assertNotNull(pass);
170+
assertEquals("decrypted", pass);
171+
}
172+
}

0 commit comments

Comments
 (0)