3030import org .codehaus .plexus .components .secdispatcher .MasterSourceMeta ;
3131import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
3232import 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.
3940public 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}
0 commit comments