Skip to content

Commit c523461

Browse files
committed
Add all attributes and shorten them
1 parent 1cd393b commit c523461

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public interface SecDispatcher {
3030
*
3131
* @see #availableDispatchers()
3232
*/
33-
String DISPATCHER_NAME_ATTR = "name";
33+
String DISPATCHER_NAME_ATTR = "n";
34+
35+
/**
36+
* Attribute for version that dispatcher should use.
37+
*/
38+
String DISPATCHER_VERSION_ATTR = "v";
3439

3540
/**
3641
* Returns the set of available dispatcher metadata, never {@code null}.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ public static Map<String, String> getConfig(SettingsSecurity sec, String name) {
9191
return null;
9292
}
9393

94+
public static String specVersion() {
95+
String specVer = SecDispatcher.class.getPackage().getSpecificationVersion();
96+
if (specVer == null) {
97+
specVer = "test"; // in UT
98+
}
99+
return specVer;
100+
}
101+
94102
private static final boolean IS_WINDOWS =
95103
System.getProperty("os.name", "unknown").startsWith("Windows");
96104

@@ -102,7 +110,7 @@ public static void write(Path target, SettingsSecurity configuration, boolean do
102110
Path tempFile = parent.resolve(target.getFileName() + "."
103111
+ Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp");
104112

105-
configuration.setModelVersion(SecDispatcher.class.getPackage().getSpecificationVersion());
113+
configuration.setModelVersion(specVersion());
106114
configuration.setModelEncoding(StandardCharsets.UTF_8.name());
107115

108116
try {

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
3131
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
3232
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
33+
import org.codehaus.plexus.components.secdispatcher.internal.SecUtil;
3334

3435
/**
3536
* This dispatcher is logically equivalent (but much more secure) that Maven3 "master password" encryption.
@@ -39,8 +40,9 @@
3940
public class MasterDispatcher implements Dispatcher, DispatcherMeta {
4041
public static final String NAME = "master";
4142

42-
private static final String MASTER_CIPHER = "cipher";
43-
private static final String MASTER_SOURCE = "source";
43+
private static final String MASTER_CIPHER_ATTR = "c";
44+
private static final String CONF_MASTER_CIPHER = "cipher";
45+
private static final String CONF_MASTER_SOURCE = "source";
4446

4547
private final PlexusCipher cipher;
4648
protected final Map<String, MasterSource> masterSources;
@@ -64,7 +66,7 @@ public String displayName() {
6466
@Override
6567
public Collection<Field> fields() {
6668
return List.of(
67-
Field.builder(MASTER_SOURCE)
69+
Field.builder(CONF_MASTER_SOURCE)
6870
.optional(false)
6971
.description("Source of the master password")
7072
.options(masterSources.entrySet().stream()
@@ -87,7 +89,7 @@ public Collection<Field> fields() {
8789
})
8890
.toList())
8991
.build(),
90-
Field.builder(MASTER_CIPHER)
92+
Field.builder(CONF_MASTER_CIPHER)
9193
.optional(false)
9294
.description("Cipher to use with master password")
9395
.options(cipher.availableCiphers().stream()
@@ -104,7 +106,8 @@ public EncryptPayload encrypt(String str, Map<String, String> attributes, Map<St
104106
String encrypted = cipher.encrypt(masterCipher, str, getMasterPassword(config));
105107
HashMap<String, String> attr = new HashMap<>(attributes);
106108
attr.put(SecDispatcher.DISPATCHER_NAME_ATTR, NAME);
107-
attr.put(MASTER_CIPHER, masterCipher);
109+
attr.put(SecDispatcher.DISPATCHER_VERSION_ATTR, SecUtil.specVersion());
110+
attr.put(MASTER_CIPHER_ATTR, masterCipher);
108111
return new EncryptPayload(attr, encrypted);
109112
} catch (PlexusCipherException e) {
110113
throw new SecDispatcherException("Encrypt failed", e);
@@ -123,9 +126,9 @@ public String decrypt(String str, Map<String, String> attributes, Map<String, St
123126
}
124127

125128
private String getMasterPassword(Map<String, String> config) throws SecDispatcherException {
126-
String masterSource = config.get(MASTER_SOURCE);
129+
String masterSource = config.get(CONF_MASTER_SOURCE);
127130
if (masterSource == null) {
128-
throw new SecDispatcherException("Invalid configuration: Missing configuration " + MASTER_SOURCE);
131+
throw new SecDispatcherException("Invalid configuration: Missing configuration " + CONF_MASTER_SOURCE);
129132
}
130133
for (MasterSource masterPasswordSource : masterSources.values()) {
131134
String masterPassword = masterPasswordSource.handle(masterSource);
@@ -135,14 +138,18 @@ private String getMasterPassword(Map<String, String> config) throws SecDispatche
135138
}
136139

137140
private String getMasterCipher(Map<String, String> source, boolean config) throws SecDispatcherException {
138-
String masterCipher = source.get(MASTER_CIPHER);
139-
if (masterCipher == null) {
140-
if (config) {
141-
throw new SecDispatcherException("Invalid configuration: Missing configuration " + MASTER_CIPHER);
142-
} else {
143-
throw new SecDispatcherException("Malformed attributes: Missing attribute " + MASTER_CIPHER);
141+
if (config) {
142+
String masterCipher = source.get(CONF_MASTER_CIPHER);
143+
if (masterCipher == null) {
144+
throw new SecDispatcherException("Invalid configuration: Missing configuration " + CONF_MASTER_CIPHER);
144145
}
146+
return masterCipher;
147+
} else {
148+
String masterCipher = source.get(MASTER_CIPHER_ATTR);
149+
if (masterCipher == null) {
150+
throw new SecDispatcherException("Malformed attributes: Missing attribute " + MASTER_CIPHER_ATTR);
151+
}
152+
return masterCipher;
145153
}
146-
return masterCipher;
147154
}
148155
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ protected void roundtrip() throws Exception {
8383
String encrypted = sd.encrypt("supersecret", Map.of(SecDispatcher.DISPATCHER_NAME_ATTR, "master", "a", "b"));
8484
// example:
8585
// {[name=master,cipher=AES/GCM/NoPadding,a=b]vvq66pZ7rkvzSPStGTI9q4QDnsmuDwo+LtjraRel2b0XpcGJFdXcYAHAS75HUA6GLpcVtEkmyQ==}
86+
System.out.println(encrypted);
8687
assertTrue(encrypted.startsWith("{") && encrypted.endsWith("}"));
87-
assertTrue(encrypted.contains("name=master"));
88-
assertTrue(encrypted.contains("cipher=" + AESGCMNoPadding.CIPHER_ALG));
88+
assertTrue(encrypted.contains("n=master"));
89+
assertTrue(encrypted.contains("c=" + AESGCMNoPadding.CIPHER_ALG));
90+
assertTrue(encrypted.contains("v=test"));
8991
assertTrue(encrypted.contains("a=b"));
9092
String pass = sd.decrypt(encrypted);
9193
assertEquals("supersecret", pass);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void readWrite() throws IOException {
6868
Path path = Path.of("./target/sec.xml");
6969
SettingsSecurity config = SecUtil.read(path);
7070
assertNotNull(config);
71-
assertEquals(SettingsSecurity.class.getPackage().getSpecificationVersion(), config.getModelVersion());
71+
assertEquals(SecUtil.specVersion(), config.getModelVersion());
7272
assertEquals(StandardCharsets.UTF_8.name(), config.getModelEncoding());
7373
assertEquals("magic:mighty", config.getDefaultDispatcher());
7474
SecUtil.write(path, config, false);
@@ -79,7 +79,7 @@ void readWriteWithBackup() throws IOException {
7979
Path path = Path.of("./target/sec.xml");
8080
SettingsSecurity config = SecUtil.read(path);
8181
assertNotNull(config);
82-
assertEquals(SettingsSecurity.class.getPackage().getSpecificationVersion(), config.getModelVersion());
82+
assertEquals(SecUtil.specVersion(), config.getModelVersion());
8383
assertEquals(StandardCharsets.UTF_8.name(), config.getModelEncoding());
8484
assertEquals("magic:mighty", config.getDefaultDispatcher());
8585
SecUtil.write(path, config, true);

0 commit comments

Comments
 (0)