1111 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212 */
1313
14- package org .sonatype .plexus .components .sec . dispatcher ;
14+ package org .codehaus .plexus .components .secdispatcher . internal ;
1515
1616import javax .inject .Inject ;
1717import javax .inject .Named ;
1818import javax .inject .Singleton ;
1919
2020import java .io .BufferedReader ;
2121import java .io .InputStreamReader ;
22+ import java .net .URI ;
23+ import java .net .URISyntaxException ;
24+ import java .util .Collections ;
2225import java .util .HashMap ;
2326import java .util .Map ;
2427import java .util .StringTokenizer ;
2528
26- import org .sonatype .plexus .components .cipher .DefaultPlexusCipher ;
27- import org .sonatype .plexus .components .cipher .PlexusCipher ;
28- import org .sonatype .plexus .components .cipher .PlexusCipherException ;
29- import org .sonatype .plexus .components .sec .dispatcher .model .SettingsSecurity ;
29+ import org .codehaus .plexus .components .cipher .PlexusCipher ;
30+ import org .codehaus .plexus .components .cipher .PlexusCipherException ;
31+ import org .codehaus .plexus .components .cipher .internal .DefaultPlexusCipher ;
32+ import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
33+ import org .codehaus .plexus .components .secdispatcher .SecDispatcherException ;
34+ import org .codehaus .plexus .components .secdispatcher .model .SettingsSecurity ;
35+
36+ import static java .util .Objects .requireNonNull ;
3037
3138/**
3239 * @author Oleg Gusakov
3340 */
3441@ Singleton
3542@ Named
3643public class DefaultSecDispatcher implements SecDispatcher {
37- private static final String DEFAULT_CONFIGURATION = "~/.settings-security.xml" ;
44+ public static final String DEFAULT_CONFIGURATION = "~/.m2/ settings-security.xml" ;
3845
3946 public static final String SYSTEM_PROPERTY_SEC_LOCATION = "settings.security" ;
4047
4148 public static final String TYPE_ATTR = "type" ;
42-
4349 public static final char ATTR_START = '[' ;
44-
4550 public static final char ATTR_STOP = ']' ;
4651
47- /**
48- * DefaultHandler
49- */
50- protected final PlexusCipher _cipher ;
51-
52- /**
53- * All available dispatchers
54- */
55- protected final Map <String , PasswordDecryptor > _decryptors ;
56-
57- /**
58- * Configuration file
59- */
60- protected String _configurationFile ;
52+ protected final PlexusCipher cipher ;
53+ protected final Map <String , MasterPasswordSource > masterPasswordSources ;
54+ protected final Map <String , PasswordDecryptor > decryptors ;
55+ protected String configurationFile ;
6156
6257 @ Inject
6358 public DefaultSecDispatcher (
64- final PlexusCipher _cipher ,
65- final Map <String , PasswordDecryptor > _decryptors ,
66- @ Named ("${_configurationFile:-" + DEFAULT_CONFIGURATION + "}" ) final String _configurationFile ) {
67- this ._cipher = _cipher ;
68- this ._decryptors = _decryptors ;
69- this ._configurationFile = _configurationFile ;
70- }
71-
72- /**
73- * Ctor to be used in tests and other simplified cases (no decryptors and config).
74- */
75- public DefaultSecDispatcher (final PlexusCipher _cipher ) {
76- this (_cipher , new HashMap <>(), DEFAULT_CONFIGURATION );
59+ PlexusCipher cipher ,
60+ Map <String , MasterPasswordSource > masterPasswordSources ,
61+ Map <String , PasswordDecryptor > decryptors ,
62+ @ Named ("${configurationFile:-" + DEFAULT_CONFIGURATION + "}" ) final String configurationFile ) {
63+ this .cipher = cipher ;
64+ this .masterPasswordSources = masterPasswordSources ;
65+ this .decryptors = decryptors ;
66+ this .configurationFile = configurationFile ;
7767 }
7868
7969 // ---------------------------------------------------------------
@@ -85,7 +75,7 @@ public String decrypt(String str) throws SecDispatcherException {
8575 String bare ;
8676
8777 try {
88- bare = _cipher .unDecorate (str );
78+ bare = cipher .unDecorate (str );
8979
9080 Map <String , String > attr = stripAttributes (bare );
9181
@@ -96,17 +86,17 @@ public String decrypt(String str) throws SecDispatcherException {
9686 if (attr == null || attr .get ("type" ) == null ) {
9787 String master = getMaster (sec );
9888
99- res = _cipher .decrypt (bare , master );
89+ res = cipher .decrypt (bare , master );
10090 } else {
10191 String type = attr .get (TYPE_ATTR );
10292
103- if (_decryptors == null )
93+ if (decryptors == null )
10494 throw new SecDispatcherException (
10595 "plexus container did not supply any required dispatchers - cannot lookup " + type );
10696
10797 Map <String , String > conf = SecUtil .getConfig (sec , type );
10898
109- PasswordDecryptor dispatcher = _decryptors .get (type );
99+ PasswordDecryptor dispatcher = decryptors .get (type );
110100
111101 if (dispatcher == null ) throw new SecDispatcherException ("no dispatcher for hint " + type );
112102
@@ -141,7 +131,7 @@ private Map<String, String> stripAttributes(String str) {
141131
142132 Map <String , String > res = null ;
143133
144- StringTokenizer st = new StringTokenizer (attrs , ", " );
134+ StringTokenizer st = new StringTokenizer (attrs , "," );
145135
146136 while (st .hasMoreTokens ()) {
147137 if (res == null ) res = new HashMap <>(st .countTokens ());
@@ -170,7 +160,7 @@ private Map<String, String> stripAttributes(String str) {
170160 private boolean isEncryptedString (String str ) {
171161 if (str == null ) return false ;
172162
173- return _cipher .isEncryptedString (str );
163+ return cipher .isEncryptedString (str );
174164 }
175165
176166 // ----------------------------------------------------------------------------
@@ -192,23 +182,25 @@ private SettingsSecurity getSec() throws SecDispatcherException {
192182 // ----------------------------------------------------------------------------
193183
194184 private String getMaster (SettingsSecurity sec ) throws SecDispatcherException {
195- String master = sec .getMaster ();
196-
197- if (master == null ) throw new SecDispatcherException ("master password is not set" );
198-
185+ String masterSource = requireNonNull (sec .getMasterSource (), "masterSource is null" );
199186 try {
200- return _cipher .decryptDecorated (master , SYSTEM_PROPERTY_SEC_LOCATION );
201- } catch (PlexusCipherException e ) {
202- throw new SecDispatcherException (e .getMessage (), e );
187+ URI masterSourceUri = new URI (masterSource );
188+ for (MasterPasswordSource masterPasswordSource : masterPasswordSources .values ()) {
189+ String master = masterPasswordSource .handle (masterSourceUri );
190+ if (master != null ) return master ;
191+ }
192+ } catch (URISyntaxException e ) {
193+ throw new SecDispatcherException ("Invalid master source URI" , e );
203194 }
195+ throw new SecDispatcherException ("master password could not be fetched" );
204196 }
205197 // ---------------------------------------------------------------
206198 public String getConfigurationFile () {
207- return _configurationFile ;
199+ return configurationFile ;
208200 }
209201
210202 public void setConfigurationFile (String file ) {
211- _configurationFile = file ;
203+ configurationFile = file ;
212204 }
213205
214206 // ---------------------------------------------------------------
@@ -241,6 +233,12 @@ private static void usage() {
241233
242234 // ---------------------------------------------------------------
243235
236+ private static final String [] SYSTEM_PROPERTY_MASTER_PASSWORD =
237+ new String [] {"settings.master.password" , "settings-master-password" };
238+
239+ private static final String [] SYSTEM_PROPERTY_SERVER_PASSWORD =
240+ new String [] {"settings.server.password" , "settings-server-password" };
241+
244242 public static void main (String [] args ) throws Exception {
245243 if (args == null || args .length < 1 ) {
246244 usage ();
@@ -267,7 +265,8 @@ private static void show(boolean showMaster) throws Exception {
267265 System .out .println ("\n " );
268266
269267 DefaultPlexusCipher dc = new DefaultPlexusCipher ();
270- DefaultSecDispatcher dd = new DefaultSecDispatcher (dc );
268+ DefaultSecDispatcher dd =
269+ new DefaultSecDispatcher (dc , Collections .emptyMap (), Collections .emptyMap (), DEFAULT_CONFIGURATION );
271270
272271 if (showMaster )
273272 System .out .println (dc .encryptAndDecorate (pass , DefaultSecDispatcher .SYSTEM_PROPERTY_SEC_LOCATION ));
0 commit comments