28
28
29
29
import org .codehaus .plexus .components .cipher .PlexusCipher ;
30
30
import org .codehaus .plexus .components .cipher .PlexusCipherException ;
31
- import org .codehaus .plexus .components .secdispatcher .MasterMeta ;
32
31
import org .codehaus .plexus .components .secdispatcher .Meta ;
33
32
import org .codehaus .plexus .components .secdispatcher .SecDispatcher ;
34
33
import org .codehaus .plexus .components .secdispatcher .SecDispatcherException ;
@@ -46,18 +45,15 @@ public class DefaultSecDispatcher implements SecDispatcher {
46
45
public static final String ATTR_STOP = "]" ;
47
46
48
47
protected final PlexusCipher cipher ;
49
- protected final Map <String , MasterSource > masterSources ;
50
48
protected final Map <String , Dispatcher > dispatchers ;
51
49
protected final String configurationFile ;
52
50
53
51
@ Inject
54
52
public DefaultSecDispatcher (
55
53
PlexusCipher cipher ,
56
- Map <String , MasterSource > masterSources ,
57
54
Map <String , Dispatcher > dispatchers ,
58
55
@ Named ("${configurationFile:-" + DEFAULT_CONFIGURATION + "}" ) final String configurationFile ) {
59
56
this .cipher = requireNonNull (cipher );
60
- this .masterSources = requireNonNull (masterSources );
61
57
this .dispatchers = requireNonNull (dispatchers );
62
58
this .configurationFile = requireNonNull (configurationFile );
63
59
}
@@ -67,38 +63,28 @@ public Set<Meta> availableDispatchers() {
67
63
return Set .copyOf (dispatchers .values ().stream ().map (Dispatcher ::meta ).collect (Collectors .toSet ()));
68
64
}
69
65
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
-
81
66
@ Override
82
67
public String encrypt (String str , Map <String , String > attr ) throws SecDispatcherException {
83
68
if (isEncryptedString (str )) return str ;
84
69
85
70
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 <>();
91
73
} 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 );
101
75
}
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 ));
102
88
return cipher .decorate (res );
103
89
} catch (PlexusCipherException e ) {
104
90
throw new SecDispatcherException (e .getMessage (), e );
@@ -111,16 +97,13 @@ public String decrypt(String str) throws SecDispatcherException {
111
97
try {
112
98
String bare = cipher .unDecorate (str );
113
99
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 ());
123
102
}
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 ));
124
107
} catch (PlexusCipherException e ) {
125
108
throw new SecDispatcherException (e .getMessage (), e );
126
109
}
@@ -143,12 +126,7 @@ public void writeConfiguration(SettingsSecurity configuration) throws IOExceptio
143
126
144
127
private Map <String , String > prepareDispatcherConfig (String type ) {
145
128
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 );
152
130
if (conf != null ) {
153
131
dispatcherConf .putAll (conf );
154
132
}
@@ -165,27 +143,25 @@ private String strip(String str) {
165
143
}
166
144
167
145
private Map <String , String > stripAttributes (String str ) {
146
+ HashMap <String , String > result = new HashMap <>();
168
147
int start = str .indexOf (ATTR_START );
169
148
int stop = str .indexOf (ATTR_STOP );
170
149
if (start != -1 && stop != -1 && stop > start ) {
171
150
if (start != 0 ) throw new SecDispatcherException ("Attributes can be prefix only" );
172
151
if (stop == start + 1 ) return null ;
173
152
String attrs = str .substring (start + 1 , stop ).trim ();
174
153
if (attrs .isEmpty ()) return null ;
175
- Map <String , String > res = null ;
176
154
StringTokenizer st = new StringTokenizer (attrs , "," );
177
155
while (st .hasMoreTokens ()) {
178
- if (res == null ) res = new HashMap <>(st .countTokens ());
179
156
String pair = st .nextToken ();
180
157
int pos = pair .indexOf ('=' );
181
158
if (pos == -1 ) throw new SecDispatcherException ("Attribute malformed: " + pair );
182
159
String key = pair .substring (0 , pos ).trim ();
183
160
String val = pair .substring (pos + 1 ).trim ();
184
- res .put (key , val );
161
+ result .put (key , val );
185
162
}
186
- return res ;
187
163
}
188
- return null ;
164
+ return result ;
189
165
}
190
166
191
167
private boolean isEncryptedString (String str ) {
@@ -199,40 +175,18 @@ private Path getConfigurationPath() {
199
175
return Paths .get (location );
200
176
}
201
177
202
- private SettingsSecurity getConfiguration (boolean mandatory ) throws SecDispatcherException {
178
+ private SettingsSecurity getConfiguration () throws SecDispatcherException {
203
179
Path path = getConfigurationPath ();
204
180
try {
205
181
SettingsSecurity sec = SecUtil .read (path );
206
- if (mandatory && sec == null )
182
+ if (sec == null )
207
183
throw new SecDispatcherException ("Please check that configuration file on path " + path + " exists" );
208
184
return sec ;
209
185
} catch (IOException e ) {
210
186
throw new SecDispatcherException (e .getMessage (), e );
211
187
}
212
188
}
213
189
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
-
236
190
public String getConfigurationFile () {
237
191
return configurationFile ;
238
192
}
0 commit comments