Skip to content

Commit 3198bdc

Browse files
committed
More stuff
1 parent 981f223 commit 3198bdc

File tree

5 files changed

+122
-88
lines changed

5 files changed

+122
-88
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

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

16+
import java.util.Map;
17+
1618
/**
1719
* This component decrypts a string, passed to it
1820
*
@@ -21,13 +23,24 @@
2123
public interface SecDispatcher {
2224
String DEFAULT_CONFIGURATION = "~/.m2/settings-security.xml";
2325
String SYSTEM_PROPERTY_CONFIGURATION_LOCATION = "settings.security";
26+
String TYPE_ATTR = "type";
27+
28+
/**
29+
* encrypt given plaintext string
30+
*
31+
* @param str the plaintext to encrypt
32+
* @param attr the attributes, may be {@code null}
33+
* @return encrypted string
34+
* @throws SecDispatcherException in case of problem
35+
*/
36+
String encrypt(String str, Map<String, String> attr) throws SecDispatcherException;
2437

2538
/**
2639
* decrypt given encrypted string
2740
*
28-
* @param str
29-
* @return decrypted string
30-
* @throws SecDispatcherException
41+
* @param str the encrypted string
42+
* @return plaintext string
43+
* @throws SecDispatcherException in case of problem
3144
*/
3245
String decrypt(String str) throws SecDispatcherException;
3346
}

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

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424
import java.util.StringTokenizer;
25+
import java.util.stream.Collectors;
2526

2627
import org.codehaus.plexus.components.cipher.PlexusCipher;
2728
import org.codehaus.plexus.components.cipher.PlexusCipherException;
@@ -37,77 +38,82 @@
3738
@Singleton
3839
@Named
3940
public class DefaultSecDispatcher implements SecDispatcher {
40-
public static final String TYPE_ATTR = "type";
41-
public static final char ATTR_START = '[';
42-
public static final char ATTR_STOP = ']';
41+
public static final String ATTR_START = "[";
42+
public static final String ATTR_STOP = "]";
4343

4444
protected final PlexusCipher cipher;
4545
protected final Map<String, MasterPasswordSource> masterPasswordSources;
46-
protected final Map<String, PasswordDecryptor> decryptors;
47-
protected String configurationFile;
46+
protected final Map<String, Dispatcher> dispatchers;
47+
protected final String configurationFile;
4848

4949
@Inject
5050
public DefaultSecDispatcher(
5151
PlexusCipher cipher,
5252
Map<String, MasterPasswordSource> masterPasswordSources,
53-
Map<String, PasswordDecryptor> decryptors,
53+
Map<String, Dispatcher> dispatchers,
5454
@Named("${configurationFile:-" + DEFAULT_CONFIGURATION + "}") final String configurationFile) {
55-
this.cipher = cipher;
56-
this.masterPasswordSources = masterPasswordSources;
57-
this.decryptors = decryptors;
58-
this.configurationFile = configurationFile;
55+
this.cipher = requireNonNull(cipher);
56+
this.masterPasswordSources = requireNonNull(masterPasswordSources);
57+
this.dispatchers = requireNonNull(dispatchers);
58+
this.configurationFile = requireNonNull(configurationFile);
5959
}
6060

6161
// ---------------------------------------------------------------
6262

6363
@Override
64-
public String decrypt(String str) throws SecDispatcherException {
65-
if (!isEncryptedString(str)) return str;
66-
67-
String bare;
64+
public String encrypt(String str, Map<String, String> attr) throws SecDispatcherException {
65+
if (isEncryptedString(str)) return str;
6866

6967
try {
70-
bare = cipher.unDecorate(str);
71-
72-
Map<String, String> attr = stripAttributes(bare);
73-
7468
String res;
75-
7669
SettingsSecurity sec = getSec();
77-
78-
if (attr == null || attr.get("type") == null) {
70+
if (attr == null || attr.get(TYPE_ATTR) == null) {
7971
String master = getMaster(sec);
80-
81-
res = cipher.decrypt(bare, master);
72+
res = cipher.encrypt(str, master);
8273
} else {
8374
String type = attr.get(TYPE_ATTR);
84-
85-
if (decryptors == null)
86-
throw new SecDispatcherException(
87-
"plexus container did not supply any required dispatchers - cannot lookup " + type);
88-
8975
Map<String, String> conf = SecUtil.getConfig(sec, type);
76+
Dispatcher dispatcher = dispatchers.get(type);
77+
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for type " + type);
78+
res = dispatcher.encrypt(str, attr, conf);
79+
res += ATTR_START
80+
+ attr.entrySet().stream()
81+
.map(e -> e.getKey() + "=" + e.getValue())
82+
.collect(Collectors.joining(","))
83+
+ ATTR_STOP;
84+
}
85+
return cipher.decorate(res);
86+
} catch (PlexusCipherException e) {
87+
throw new SecDispatcherException(e.getMessage(), e);
88+
}
89+
}
9090

91-
PasswordDecryptor dispatcher = decryptors.get(type);
92-
93-
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for hint " + type);
94-
91+
@Override
92+
public String decrypt(String str) throws SecDispatcherException {
93+
if (!isEncryptedString(str)) return str;
94+
try {
95+
String bare = cipher.unDecorate(str);
96+
Map<String, String> attr = stripAttributes(bare);
97+
SettingsSecurity sec = getSec();
98+
if (attr == null || attr.get(TYPE_ATTR) == null) {
99+
String master = getMaster(sec);
100+
return cipher.decrypt(bare, master);
101+
} else {
102+
String type = attr.get(TYPE_ATTR);
103+
Map<String, String> conf = SecUtil.getConfig(sec, type);
104+
Dispatcher dispatcher = dispatchers.get(type);
105+
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for type " + type);
95106
String pass = strip(bare);
96-
97107
return dispatcher.decrypt(pass, attr, conf);
98108
}
99-
100-
return res;
101109
} catch (PlexusCipherException e) {
102110
throw new SecDispatcherException(e.getMessage(), e);
103111
}
104112
}
105113

106114
private String strip(String str) {
107115
int pos = str.indexOf(ATTR_STOP);
108-
109116
if (pos != -1) return str.substring(pos + 1);
110-
111117
return str;
112118
}
113119

@@ -151,7 +157,6 @@ private Map<String, String> stripAttributes(String str) {
151157

152158
private boolean isEncryptedString(String str) {
153159
if (str == null) return false;
154-
155160
return cipher.isEncryptedString(str);
156161
}
157162

@@ -171,8 +176,6 @@ private SettingsSecurity getSec() throws SecDispatcherException {
171176
return sec;
172177
}
173178

174-
// ----------------------------------------------------------------------------
175-
176179
private String getMaster(SettingsSecurity sec) throws SecDispatcherException {
177180
String masterSource = requireNonNull(sec.getMasterSource(), "masterSource is null");
178181
try {
@@ -186,36 +189,8 @@ private String getMaster(SettingsSecurity sec) throws SecDispatcherException {
186189
}
187190
throw new SecDispatcherException("master password could not be fetched");
188191
}
189-
// ---------------------------------------------------------------
192+
190193
public String getConfigurationFile() {
191194
return configurationFile;
192195
}
193-
194-
public void setConfigurationFile(String file) {
195-
configurationFile = file;
196-
}
197-
198-
// ---------------------------------------------------------------
199-
200-
private static boolean propertyExists(String[] values, String[] av) {
201-
if (values != null) {
202-
for (String item : values) {
203-
String p = System.getProperty(item);
204-
205-
if (p != null) {
206-
return true;
207-
}
208-
}
209-
210-
if (av != null)
211-
for (String value : values)
212-
for (String s : av) {
213-
if (("--" + value).equals(s)) {
214-
return true;
215-
}
216-
}
217-
}
218-
219-
return false;
220-
}
221196
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,31 @@
1818
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
1919

2020
/**
21-
*
21+
* Dispatcher.
2222
*
2323
* @author Oleg Gusakov
2424
* @version $Id$
2525
*
2626
*/
27-
public interface PasswordDecryptor {
27+
public interface Dispatcher {
28+
/**
29+
* encrypt given plaintext string
30+
*
31+
* @param str string to encrypt
32+
* @param attributes attributes, never {@code null}
33+
* @param config configuration from settings-security.xml, may be {@code null}
34+
* @return encrypted string
35+
*/
36+
String encrypt(String str, Map<String, String> attributes, Map<String, String> config)
37+
throws SecDispatcherException;
38+
2839
/**
2940
* decrypt given encrypted string
3041
*
31-
* @param str - string to decrypt
32-
* @param attributes - string attributes
33-
* @param config - configuration from settings-security.xml, if any
42+
* @param str string to decrypt
43+
* @param attributes attributes, never {@code null}
44+
* @param config configuration from settings-security.xml, may be {@code null}
3445
* @return decrypted string
35-
*
36-
* @throws SecDispatcherException
3746
*/
3847
String decrypt(String str, Map<String, String> attributes, Map<String, String> config)
3948
throws SecDispatcherException;
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
1313

14-
package org.codehaus.plexus.components.secdispatcher.internal.decryptor;
14+
package org.codehaus.plexus.components.secdispatcher.internal.dispatcher;
1515

1616
import java.util.Map;
1717

1818
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
19-
import org.codehaus.plexus.components.secdispatcher.internal.PasswordDecryptor;
19+
import org.codehaus.plexus.components.secdispatcher.internal.Dispatcher;
2020

2121
import static java.util.Objects.requireNonNull;
2222

23-
public class StaticPasswordDecryptor implements PasswordDecryptor {
23+
public class StaticDispatcher implements Dispatcher {
2424
private final String decrypted;
25+
private final String encrypted;
2526

26-
public StaticPasswordDecryptor(String decrypted) {
27+
public StaticDispatcher(String decrypted, String encrypted) {
2728
this.decrypted = requireNonNull(decrypted);
29+
this.encrypted = requireNonNull(encrypted);
30+
}
31+
32+
@Override
33+
public String encrypt(String str, Map<String, String> attributes, Map<String, String> config)
34+
throws SecDispatcherException {
35+
return encrypted;
2836
}
2937

3038
@Override

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.Map;
2020

2121
import org.codehaus.plexus.components.cipher.internal.DefaultPlexusCipher;
22-
import org.codehaus.plexus.components.secdispatcher.internal.decryptor.StaticPasswordDecryptor;
22+
import org.codehaus.plexus.components.secdispatcher.internal.dispatcher.StaticDispatcher;
2323
import org.codehaus.plexus.components.secdispatcher.internal.sources.EnvMasterPasswordSource;
2424
import org.codehaus.plexus.components.secdispatcher.internal.sources.GpgAgentMasterPasswordSource;
2525
import org.codehaus.plexus.components.secdispatcher.internal.sources.StaticMasterPasswordSource;
@@ -45,7 +45,7 @@
4545
public class SecUtilTest {
4646
String masterPassword = "masterPw";
4747
String password = "somePassword";
48-
String passwordEncrypted = "{a/8OtPCGPvQLUVF+n6+UDTD3SeRCdqb0tPJLF71cs29M9Ms81MAb3Y1XG/TS4C4f}";
48+
String passwordEncrypted = "{TT2NQZ4iAdoHqsSfYUab3s6X2IHl5qaf4vx/F8DvtSI=}";
4949

5050
String _confName = "cname";
5151

@@ -101,6 +101,20 @@ void testRead() throws Exception {
101101
assertEquals(_propVal, conf.get(_propName));
102102
}
103103

104+
@Test
105+
void testEncrypt() throws Exception {
106+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
107+
new DefaultPlexusCipher(),
108+
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
109+
Map.of(),
110+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
111+
112+
String enc = sd.encrypt(password, null);
113+
assertNotNull(enc);
114+
String password1 = sd.decrypt(enc);
115+
assertEquals(password, password1);
116+
}
117+
104118
@Test
105119
void testDecrypt() throws Exception {
106120
DefaultSecDispatcher sd = new DefaultSecDispatcher(
@@ -187,18 +201,33 @@ void testDecryptGpg() throws Exception {
187201
}
188202

189203
@Test
190-
void testDecryptWithDecryptor() throws Exception {
204+
void testEncryptWithDispatcher() throws Exception {
205+
DefaultSecDispatcher sd = new DefaultSecDispatcher(
206+
new DefaultPlexusCipher(),
207+
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
208+
Map.of("magic", new StaticDispatcher("decrypted", "encrypted")),
209+
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
210+
211+
String enc = sd.encrypt("whatever", Map.of("type", "magic", "a", "b"));
212+
assertNotNull(enc);
213+
System.out.println(enc);
214+
String password1 = sd.decrypt(enc);
215+
assertEquals("decrypted", password1);
216+
}
217+
218+
@Test
219+
void testDecryptWithDispatcher() throws Exception {
191220
DefaultSecDispatcher sd = new DefaultSecDispatcher(
192221
new DefaultPlexusCipher(),
193222
Map.of("static", new StaticMasterPasswordSource(masterPassword)),
194-
Map.of("magic", new StaticPasswordDecryptor("magic")),
223+
Map.of("magic", new StaticDispatcher("decrypted", "encrypted")),
195224
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
196225

197226
String pass = sd.decrypt("{" + Base64.getEncoder().encodeToString("whatever".getBytes(StandardCharsets.UTF_8))
198227
+ "[a=b,type=magic]}");
199228

200229
assertNotNull(pass);
201230

202-
assertEquals("magic", pass);
231+
assertEquals("decrypted", pass);
203232
}
204233
}

0 commit comments

Comments
 (0)