2828
2929import org .codehaus .plexus .components .cipher .PlexusCipher ;
3030import org .codehaus .plexus .components .cipher .PlexusCipherException ;
31- import org .codehaus .plexus .components .secdispatcher .MasterMeta ;
3231import org .codehaus .plexus .components .secdispatcher .Meta ;
3332import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
3433import 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 }
0 commit comments