Skip to content

Commit 35e76ab

Browse files
committed
Dispatcher prevails
1 parent 7c4f27b commit 35e76ab

File tree

17 files changed

+240
-505
lines changed

17 files changed

+240
-505
lines changed

pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>plexus-sec-dispatcher</artifactId>
13-
<version>3.0.1-SNAPSHOT</version>
13+
<version>4.0.0-SNAPSHOT</version>
1414

1515
<name>Plexus Security Dispatcher Component</name>
1616

@@ -75,7 +75,7 @@
7575
<artifactId>modello-maven-plugin</artifactId>
7676
<version>2.4.0</version>
7777
<configuration>
78-
<version>3.0.0</version>
78+
<version>4.0.0</version>
7979
<models>
8080
<model>src/main/mdo/settings-security.mdo</model>
8181
</models>
@@ -96,6 +96,9 @@
9696
<groupId>org.apache.maven.plugins</groupId>
9797
<artifactId>maven-surefire-plugin</artifactId>
9898
<configuration>
99+
<systemProperties>
100+
<masterPassword>masterPw</masterPassword>
101+
</systemProperties>
99102
<environmentVariables>
100103
<MASTER_PASSWORD>masterPw</MASTER_PASSWORD>
101104
</environmentVariables>

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public Field build() {
7676
/**
7777
* The key of the item.
7878
*/
79-
String id();
79+
String name();
8080

8181
/**
8282
* Returns the display (human) name of the item.

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.codehaus.plexus.components.secdispatcher.model.SettingsSecurity;
2121

2222
/**
23-
* This component decrypts a string, passed to it
23+
* This component decrypts a string, passed to it using various dispatchers.
2424
*
2525
* @author Oleg Gusakov
2626
*/
@@ -50,16 +50,6 @@ public interface SecDispatcher {
5050
*/
5151
Set<Meta> availableDispatchers();
5252

53-
/**
54-
* Returns the set of available ciphers, never {@code null}.
55-
*/
56-
Set<String> availableCiphers();
57-
58-
/**
59-
* Returns the set of available master password sources metadata, never {@code null}.
60-
*/
61-
Set<MasterMeta> availableMasterSourcesMetadata();
62-
6353
/**
6454
* Encrypt given plaintext string.
6555
*

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

Lines changed: 27 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import org.codehaus.plexus.components.cipher.PlexusCipher;
3030
import org.codehaus.plexus.components.cipher.PlexusCipherException;
31-
import org.codehaus.plexus.components.secdispatcher.MasterMeta;
3231
import org.codehaus.plexus.components.secdispatcher.Meta;
3332
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
3433
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
@@ -46,18 +45,15 @@ public class DefaultSecDispatcher implements SecDispatcher {
4645
public static final String ATTR_STOP = "]";
4746

4847
protected final PlexusCipher cipher;
49-
protected final Map<String, MasterSource> masterSources;
5048
protected final Map<String, Dispatcher> dispatchers;
5149
protected final String configurationFile;
5250

5351
@Inject
5452
public DefaultSecDispatcher(
5553
PlexusCipher cipher,
56-
Map<String, MasterSource> masterSources,
5754
Map<String, Dispatcher> dispatchers,
5855
@Named("${configurationFile:-" + DEFAULT_CONFIGURATION + "}") final String configurationFile) {
5956
this.cipher = requireNonNull(cipher);
60-
this.masterSources = requireNonNull(masterSources);
6157
this.dispatchers = requireNonNull(dispatchers);
6258
this.configurationFile = requireNonNull(configurationFile);
6359
}
@@ -67,38 +63,28 @@ public Set<Meta> availableDispatchers() {
6763
return Set.copyOf(dispatchers.values().stream().map(Dispatcher::meta).collect(Collectors.toSet()));
6864
}
6965

70-
@Override
71-
public Set<String> availableCiphers() {
72-
return cipher.availableCiphers();
73-
}
74-
75-
@Override
76-
public Set<MasterMeta> availableMasterSourcesMetadata() {
77-
return Set.copyOf(
78-
masterSources.values().stream().map(MasterSource::meta).collect(Collectors.toSet()));
79-
}
80-
8166
@Override
8267
public String encrypt(String str, Map<String, String> attr) throws SecDispatcherException {
8368
if (isEncryptedString(str)) return str;
8469

8570
try {
86-
String res;
87-
if (attr == null || attr.get(DISPATCHER_NAME_ATTR) == null) {
88-
SettingsSecurity sec = getConfiguration(true);
89-
String master = getMasterPassword(sec, true);
90-
res = cipher.encrypt(getMasterCipher(sec), str, master);
71+
if (attr == null) {
72+
attr = new HashMap<>();
9173
} else {
92-
String type = attr.get(DISPATCHER_NAME_ATTR);
93-
Dispatcher dispatcher = dispatchers.get(type);
94-
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for name " + type);
95-
res = ATTR_START
96-
+ attr.entrySet().stream()
97-
.map(e -> e.getKey() + "=" + e.getValue())
98-
.collect(Collectors.joining(","))
99-
+ ATTR_STOP;
100-
res += dispatcher.encrypt(str, attr, prepareDispatcherConfig(type));
74+
attr = new HashMap<>(attr);
10175
}
76+
if (attr.get(DISPATCHER_NAME_ATTR) == null) {
77+
attr.put(DISPATCHER_NAME_ATTR, getConfiguration().getDefaultDispatcher());
78+
}
79+
String name = attr.get(DISPATCHER_NAME_ATTR);
80+
Dispatcher dispatcher = dispatchers.get(name);
81+
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for name " + name);
82+
String res = ATTR_START
83+
+ attr.entrySet().stream()
84+
.map(e -> e.getKey() + "=" + e.getValue())
85+
.collect(Collectors.joining(","))
86+
+ ATTR_STOP;
87+
res += dispatcher.encrypt(str, attr, prepareDispatcherConfig(name));
10288
return cipher.decorate(res);
10389
} catch (PlexusCipherException e) {
10490
throw new SecDispatcherException(e.getMessage(), e);
@@ -111,16 +97,13 @@ public String decrypt(String str) throws SecDispatcherException {
11197
try {
11298
String bare = cipher.unDecorate(str);
11399
Map<String, String> attr = stripAttributes(bare);
114-
if (attr == null || attr.get(DISPATCHER_NAME_ATTR) == null) {
115-
SettingsSecurity sec = getConfiguration(true);
116-
String master = getMasterPassword(sec, true);
117-
return cipher.decrypt(getMasterCipher(sec), bare, master);
118-
} else {
119-
String type = attr.get(DISPATCHER_NAME_ATTR);
120-
Dispatcher dispatcher = dispatchers.get(type);
121-
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for name " + type);
122-
return dispatcher.decrypt(strip(bare), attr, prepareDispatcherConfig(type));
100+
if (attr.get(DISPATCHER_NAME_ATTR) == null) {
101+
attr.put(DISPATCHER_NAME_ATTR, getConfiguration().getDefaultDispatcher());
123102
}
103+
String name = attr.get(DISPATCHER_NAME_ATTR);
104+
Dispatcher dispatcher = dispatchers.get(name);
105+
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for name " + name);
106+
return dispatcher.decrypt(strip(bare), attr, prepareDispatcherConfig(name));
124107
} catch (PlexusCipherException e) {
125108
throw new SecDispatcherException(e.getMessage(), e);
126109
}
@@ -143,12 +126,7 @@ public void writeConfiguration(SettingsSecurity configuration) throws IOExceptio
143126

144127
private Map<String, String> prepareDispatcherConfig(String type) {
145128
HashMap<String, String> dispatcherConf = new HashMap<>();
146-
SettingsSecurity sec = getConfiguration(false);
147-
String master = getMasterPassword(sec, false);
148-
if (master != null) {
149-
dispatcherConf.put(Dispatcher.CONF_MASTER_PASSWORD, master);
150-
}
151-
Map<String, String> conf = SecUtil.getConfig(sec, type);
129+
Map<String, String> conf = SecUtil.getConfig(getConfiguration(), type);
152130
if (conf != null) {
153131
dispatcherConf.putAll(conf);
154132
}
@@ -165,27 +143,25 @@ private String strip(String str) {
165143
}
166144

167145
private Map<String, String> stripAttributes(String str) {
146+
HashMap<String, String> result = new HashMap<>();
168147
int start = str.indexOf(ATTR_START);
169148
int stop = str.indexOf(ATTR_STOP);
170149
if (start != -1 && stop != -1 && stop > start) {
171150
if (start != 0) throw new SecDispatcherException("Attributes can be prefix only");
172151
if (stop == start + 1) return null;
173152
String attrs = str.substring(start + 1, stop).trim();
174153
if (attrs.isEmpty()) return null;
175-
Map<String, String> res = null;
176154
StringTokenizer st = new StringTokenizer(attrs, ",");
177155
while (st.hasMoreTokens()) {
178-
if (res == null) res = new HashMap<>(st.countTokens());
179156
String pair = st.nextToken();
180157
int pos = pair.indexOf('=');
181158
if (pos == -1) throw new SecDispatcherException("Attribute malformed: " + pair);
182159
String key = pair.substring(0, pos).trim();
183160
String val = pair.substring(pos + 1).trim();
184-
res.put(key, val);
161+
result.put(key, val);
185162
}
186-
return res;
187163
}
188-
return null;
164+
return result;
189165
}
190166

191167
private boolean isEncryptedString(String str) {
@@ -199,40 +175,18 @@ private Path getConfigurationPath() {
199175
return Paths.get(location);
200176
}
201177

202-
private SettingsSecurity getConfiguration(boolean mandatory) throws SecDispatcherException {
178+
private SettingsSecurity getConfiguration() throws SecDispatcherException {
203179
Path path = getConfigurationPath();
204180
try {
205181
SettingsSecurity sec = SecUtil.read(path);
206-
if (mandatory && sec == null)
182+
if (sec == null)
207183
throw new SecDispatcherException("Please check that configuration file on path " + path + " exists");
208184
return sec;
209185
} catch (IOException e) {
210186
throw new SecDispatcherException(e.getMessage(), e);
211187
}
212188
}
213189

214-
private String getMasterPassword(SettingsSecurity sec, boolean mandatory) throws SecDispatcherException {
215-
if ((sec == null || sec.getMasterSource() == null) && !mandatory) {
216-
return null;
217-
}
218-
requireNonNull(sec, "configuration is null");
219-
String masterSource = requireNonNull(sec.getMasterSource(), "masterSource is null");
220-
for (MasterSource masterPasswordSource : masterSources.values()) {
221-
String masterPassword = masterPasswordSource.handle(masterSource);
222-
if (masterPassword != null) return masterPassword;
223-
}
224-
if (mandatory) {
225-
throw new SecDispatcherException("master password could not be fetched");
226-
} else {
227-
return null;
228-
}
229-
}
230-
231-
private String getMasterCipher(SettingsSecurity sec) throws SecDispatcherException {
232-
requireNonNull(sec, "configuration is null");
233-
return requireNonNull(sec.getMasterCipher(), "masterCipher is null");
234-
}
235-
236190
public String getConfigurationFile() {
237191
return configurationFile;
238192
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@
2626
*
2727
*/
2828
public interface Dispatcher {
29-
/**
30-
* Configuration key for masterPassword. It may be present, if SecDispatcher could
31-
* obtain it, but presence is optional. Still, dispatcher may throw and fail the operation
32-
* if it requires it.
33-
*/
34-
String CONF_MASTER_PASSWORD = "masterPassword";
35-
3629
/**
3730
* The metadata of this dispatcher.
3831
*/

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313

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

16-
import org.codehaus.plexus.components.secdispatcher.MasterMeta;
1716
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
1817

1918
/**
2019
* Source of master password.
2120
*/
2221
public interface MasterSource {
23-
/**
24-
* Returns the "meta" of this master source, never {@code null}.
25-
*/
26-
MasterMeta meta();
27-
2822
/**
2923
* Handles the URI to get master password. Implementation may do one of the following things:
3024
* <ul>

0 commit comments

Comments
 (0)