Skip to content

Commit 0aaf015

Browse files
committed
Simplify it more
Do not provide "bad" and "never used" defaults as before as it was just the source of confusion, as old component had to be "redefined" in Plexus XML or alike anyway, to be usable in Maven.
1 parent 4509688 commit 0aaf015

File tree

4 files changed

+34
-63
lines changed

4 files changed

+34
-63
lines changed

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@
2525
* @author Oleg Gusakov
2626
*/
2727
public interface SecDispatcher {
28-
/**
29-
* The default path of configuration.
30-
* <p>
31-
* The character {@code ~} (tilde) may be present as first character ONLY and is
32-
* interpreted as "user.home" system property, and it MUST be followed by path separator.
33-
*/
34-
String DEFAULT_CONFIGURATION = "~/.m2/settings-security.xml";
35-
36-
/**
37-
* Java System Property that may be set, to override configuration path.
38-
*/
39-
String SYSTEM_PROPERTY_CONFIGURATION_LOCATION = "settings.security";
40-
4128
/**
4229
* Attribute that selects a dispatcher.
4330
*
@@ -58,7 +45,7 @@ public interface SecDispatcher {
5845
* @return encrypted string
5946
* @throws SecDispatcherException in case of problem
6047
*/
61-
String encrypt(String str, Map<String, String> attr) throws SecDispatcherException;
48+
String encrypt(String str, Map<String, String> attr) throws SecDispatcherException, IOException;
6249

6350
/**
6451
* Decrypt given encrypted string.
@@ -67,7 +54,7 @@ public interface SecDispatcher {
6754
* @return decrypted string
6855
* @throws SecDispatcherException in case of problem
6956
*/
70-
String decrypt(String str) throws SecDispatcherException;
57+
String decrypt(String str) throws SecDispatcherException, IOException;
7158

7259
/**
7360
* Reads the effective configuration, eventually creating new instance if not present.

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

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313

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

16-
import javax.inject.Inject;
17-
import javax.inject.Named;
18-
import javax.inject.Singleton;
19-
2016
import java.io.IOException;
17+
import java.nio.file.Files;
2118
import java.nio.file.Path;
22-
import java.nio.file.Paths;
2319
import java.util.Collection;
2420
import java.util.HashMap;
2521
import java.util.List;
@@ -40,26 +36,33 @@
4036
import static java.util.Objects.requireNonNull;
4137

4238
/**
39+
* Note: this implementation is NOT a JSR330 component. Integrating apps anyway want to customize it (at least
40+
* the name and location of configuration file), so instead as before (providing "bad" configuration file just
41+
* to have one), it is the duty of integrator to wrap and "finish" the implementation in a way it suits the
42+
* integrator. Also, using "globals" like Java System Properties are bad thing, and it is integrator who knows
43+
* what is needed anyway.
44+
* <p>
45+
* Recommended way for integration is to create JSR330 {@link javax.inject.Provider}.
46+
*
4347
* @author Oleg Gusakov
4448
*/
45-
@Singleton
46-
@Named
4749
public class DefaultSecDispatcher implements SecDispatcher {
4850
public static final String ATTR_START = "[";
4951
public static final String ATTR_STOP = "]";
5052

5153
protected final PlexusCipher cipher;
5254
protected final Map<String, Dispatcher> dispatchers;
53-
protected final String configurationFile;
55+
protected final Path configurationFile;
5456

55-
@Inject
56-
public DefaultSecDispatcher(
57-
PlexusCipher cipher,
58-
Map<String, Dispatcher> dispatchers,
59-
@Named("${configurationFile:-" + DEFAULT_CONFIGURATION + "}") final String configurationFile) {
57+
public DefaultSecDispatcher(PlexusCipher cipher, Map<String, Dispatcher> dispatchers, Path configurationFile) {
6058
this.cipher = requireNonNull(cipher);
6159
this.dispatchers = requireNonNull(dispatchers);
6260
this.configurationFile = requireNonNull(configurationFile);
61+
62+
// file may or may not exist, but one thing is certain: it cannot be an exiting directory
63+
if (Files.isDirectory(configurationFile)) {
64+
throw new IllegalArgumentException("configurationFile cannot be a directory");
65+
}
6366
}
6467

6568
@Override
@@ -94,7 +97,7 @@ public Collection<Field> fields() {
9497
}
9598

9699
@Override
97-
public String encrypt(String str, Map<String, String> attr) throws SecDispatcherException {
100+
public String encrypt(String str, Map<String, String> attr) throws SecDispatcherException, IOException {
98101
if (isEncryptedString(str)) return str;
99102

100103
try {
@@ -130,7 +133,7 @@ public String encrypt(String str, Map<String, String> attr) throws SecDispatcher
130133
}
131134

132135
@Override
133-
public String decrypt(String str) throws SecDispatcherException {
136+
public String decrypt(String str) throws SecDispatcherException, IOException {
134137
if (!isEncryptedString(str)) return str;
135138
try {
136139
String bare = cipher.unDecorate(str);
@@ -149,7 +152,7 @@ public String decrypt(String str) throws SecDispatcherException {
149152

150153
@Override
151154
public SettingsSecurity readConfiguration(boolean createIfMissing) throws IOException {
152-
SettingsSecurity configuration = SecUtil.read(getConfigurationPath());
155+
SettingsSecurity configuration = SecUtil.read(configurationFile);
153156
if (configuration == null && createIfMissing) {
154157
configuration = new SettingsSecurity();
155158
}
@@ -159,10 +162,10 @@ public SettingsSecurity readConfiguration(boolean createIfMissing) throws IOExce
159162
@Override
160163
public void writeConfiguration(SettingsSecurity configuration) throws IOException {
161164
requireNonNull(configuration, "configuration is null");
162-
SecUtil.write(getConfigurationPath(), configuration, true);
165+
SecUtil.write(configurationFile, configuration, true);
163166
}
164167

165-
private Map<String, String> prepareDispatcherConfig(String type) {
168+
protected Map<String, String> prepareDispatcherConfig(String type) throws IOException {
166169
HashMap<String, String> dispatcherConf = new HashMap<>();
167170
Map<String, String> conf = SecUtil.getConfig(getConfiguration(), type);
168171
if (conf != null) {
@@ -171,7 +174,7 @@ private Map<String, String> prepareDispatcherConfig(String type) {
171174
return dispatcherConf;
172175
}
173176

174-
private String strip(String str) {
177+
protected String strip(String str) {
175178
int start = str.indexOf(ATTR_START);
176179
int stop = str.indexOf(ATTR_STOP);
177180
if (start != -1 && stop != -1 && stop > start) {
@@ -180,7 +183,7 @@ private String strip(String str) {
180183
return str;
181184
}
182185

183-
private Map<String, String> stripAttributes(String str) {
186+
protected Map<String, String> stripAttributes(String str) {
184187
HashMap<String, String> result = new HashMap<>();
185188
int start = str.indexOf(ATTR_START);
186189
int stop = str.indexOf(ATTR_STOP);
@@ -202,30 +205,12 @@ private Map<String, String> stripAttributes(String str) {
202205
return result;
203206
}
204207

205-
private boolean isEncryptedString(String str) {
208+
protected boolean isEncryptedString(String str) {
206209
if (str == null) return false;
207210
return cipher.isEncryptedString(str);
208211
}
209212

210-
private Path getConfigurationPath() {
211-
String location = System.getProperty(SYSTEM_PROPERTY_CONFIGURATION_LOCATION, getConfigurationFile());
212-
location = location.charAt(0) == '~' ? System.getProperty("user.home") + location.substring(1) : location;
213-
return Paths.get(location);
214-
}
215-
216-
private SettingsSecurity getConfiguration() throws SecDispatcherException {
217-
Path path = getConfigurationPath();
218-
try {
219-
SettingsSecurity sec = SecUtil.read(path);
220-
if (sec == null)
221-
throw new SecDispatcherException("Please check that configuration file on path " + path + " exists");
222-
return sec;
223-
} catch (IOException e) {
224-
throw new SecDispatcherException(e.getMessage(), e);
225-
}
226-
}
227-
228-
public String getConfigurationFile() {
229-
return configurationFile;
213+
protected SettingsSecurity getConfiguration() throws SecDispatcherException, IOException {
214+
return SecUtil.read(configurationFile);
230215
}
231216
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.OutputStream;
2121
import java.nio.charset.StandardCharsets;
2222
import java.nio.file.Files;
23-
import java.nio.file.NoSuchFileException;
2423
import java.nio.file.Path;
2524
import java.nio.file.StandardCopyOption;
2625
import java.util.HashMap;
@@ -58,8 +57,6 @@ public static SettingsSecurity read(Path configurationFile) throws IOException {
5857
sec = new SecurityConfigurationStaxReader().read(in);
5958
}
6059
return sec;
61-
} catch (NoSuchFileException e) {
62-
return null;
6360
} catch (XMLStreamException e) {
6461
throw new IOException("Parsing error", e);
6562
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.OutputStream;
1717
import java.nio.charset.StandardCharsets;
1818
import java.nio.file.Files;
19+
import java.nio.file.Path;
1920
import java.nio.file.Paths;
2021
import java.util.Map;
2122

@@ -36,6 +37,8 @@
3637
import static org.junit.jupiter.api.Assertions.assertTrue;
3738

3839
public class DefaultSecDispatcherTest {
40+
private final Path CONFIG_PATH = Paths.get("./target/sec.xml");
41+
3942
private void saveSec(String dispatcher, Map<String, String> config) throws Exception {
4043
SettingsSecurity sec = new SettingsSecurity();
4144
sec.setModelEncoding(StandardCharsets.UTF_8.name());
@@ -56,10 +59,9 @@ private void saveSec(SettingsSecurity sec) throws Exception {
5659
sec.setModelEncoding(StandardCharsets.UTF_8.name());
5760
sec.setModelVersion(SecDispatcher.class.getPackage().getSpecificationVersion());
5861

59-
try (OutputStream fos = Files.newOutputStream(Paths.get("./target/sec.xml"))) {
62+
try (OutputStream fos = Files.newOutputStream(CONFIG_PATH)) {
6063
new SecurityConfigurationStaxWriter().write(fos, sec);
6164
}
62-
System.setProperty(DefaultSecDispatcher.SYSTEM_PROPERTY_CONFIGURATION_LOCATION, "./target/sec.xml");
6365
}
6466

6567
@Test
@@ -74,7 +76,7 @@ void masterWithSystemPropertyRoundTrip() throws Exception {
7476
roundtrip();
7577
}
7678

77-
protected void roundtrip() {
79+
protected void roundtrip() throws Exception {
7880
DefaultSecDispatcher sd = construct();
7981

8082
assertEquals(1, sd.availableDispatchers().size());
@@ -104,6 +106,6 @@ protected DefaultSecDispatcher construct() {
104106
new SystemPropertyMasterSource(),
105107
GpgAgentMasterSource.NAME,
106108
new GpgAgentMasterSource()))),
107-
DefaultSecDispatcher.DEFAULT_CONFIGURATION);
109+
CONFIG_PATH);
108110
}
109111
}

0 commit comments

Comments
 (0)