This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
DefaultSecDispatcher.java
325 lines (261 loc) · 10.1 KB
/
DefaultSecDispatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/**
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.sonatype.plexus.components.sec.dispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.cipher.PlexusCipher;
import org.sonatype.plexus.components.cipher.PlexusCipherException;
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
/**
* @plexus.component role-hint="default"
* @author Oleg Gusakov</a>
*/
public class DefaultSecDispatcher
extends AbstractLogEnabled
implements SecDispatcher
{
public static final String SYSTEM_PROPERTY_SEC_LOCATION = "settings.security";
public static final String TYPE_ATTR = "type";
public static final char ATTR_START = '[';
public static final char ATTR_STOP = ']';
/**
* DefaultHandler
*
* @plexus.requirement
*/
protected PlexusCipher _cipher;
/**
* All available dispatchers
*
* @plexus.requirement role="org.sonatype.plexus.components.sec.dispatcher.PasswordDecryptor"
*/
protected Map _decryptors;
/**
*
* @plexus.configuration default-value="~/.settings-security.xml"
*/
protected String _configurationFile = "~/.settings-security.xml";
// ---------------------------------------------------------------
public String decrypt( String str )
throws SecDispatcherException
{
if( ! isEncryptedString( str ) )
return str;
String bare = null;
try
{
bare = _cipher.unDecorate( str );
}
catch ( PlexusCipherException e1 )
{
throw new SecDispatcherException( e1 );
}
try
{
Map attr = stripAttributes( bare );
String res = null;
SettingsSecurity sec = getSec();
if( attr == null || attr.get( "type" ) == null )
{
String master = getMaster( sec );
res = _cipher.decrypt( bare, master );
}
else
{
String type = (String) attr.get( TYPE_ATTR );
if( _decryptors == null )
throw new SecDispatcherException( "plexus container did not supply any required dispatchers - cannot lookup "+type );
Map conf = SecUtil.getConfig( sec, type );
PasswordDecryptor dispatcher = (PasswordDecryptor) _decryptors.get( type );
if( dispatcher == null )
throw new SecDispatcherException( "no dispatcher for hint "+type );
String pass = attr == null ? bare : strip( bare );
return dispatcher.decrypt( pass, attr, conf );
}
return res;
}
catch ( Exception e )
{
throw new SecDispatcherException(e);
}
}
private String strip( String str )
{
int pos = str.indexOf( ATTR_STOP );
if( pos == str.length() )
return null;
if( pos != -1 )
return str.substring( pos+1 );
return str;
}
private Map stripAttributes( String str )
{
int start = str.indexOf( ATTR_START );
int stop = str.indexOf( ATTR_STOP );
if ( start != -1 && stop != -1 && stop > start )
{
if( stop == start+1 )
return null;
String attrs = str.substring( start+1, stop ).trim();
if( attrs == null || attrs.length() < 1 )
return null;
Map res = null;
StringTokenizer st = new StringTokenizer( attrs, ", " );
while( st.hasMoreTokens() )
{
if( res == null )
res = new HashMap( st.countTokens() );
String pair = st.nextToken();
int pos = pair.indexOf( '=' );
if( pos == -1 )
continue;
String key = pair.substring( 0, pos ).trim();
if( pos == pair.length() )
{
res.put( key, null );
continue;
}
String val = pair.substring( pos+1 );
res.put( key, val.trim() );
}
return res;
}
return null;
}
//----------------------------------------------------------------------------
private boolean isEncryptedString( String str )
{
if( str == null )
return false;
return _cipher.isEncryptedString( str );
}
//----------------------------------------------------------------------------
private SettingsSecurity getSec()
throws SecDispatcherException
{
String location = System.getProperty( SYSTEM_PROPERTY_SEC_LOCATION
, getConfigurationFile()
);
String realLocation = location.charAt( 0 ) == '~'
? System.getProperty( "user.home" ) + location.substring( 1 )
: location
;
SettingsSecurity sec = SecUtil.read( realLocation, true );
if( sec == null )
throw new SecDispatcherException( "cannot retrieve master password. Please check that "+realLocation+" exists and has data" );
return sec;
}
//----------------------------------------------------------------------------
private String getMaster( SettingsSecurity sec )
throws SecDispatcherException
{
String master = sec.getMaster();
if( master == null )
throw new SecDispatcherException( "master password is not set" );
try
{
return _cipher.decryptDecorated( master, SYSTEM_PROPERTY_SEC_LOCATION );
}
catch ( PlexusCipherException e )
{
throw new SecDispatcherException(e);
}
}
//---------------------------------------------------------------
public String getConfigurationFile()
{
return _configurationFile;
}
public void setConfigurationFile( String file )
{
_configurationFile = file;
}
//----------------------------------------------------------------------------
// ***************************************************************
/**
* Encrytion helper
* @throws IOException
*/
//---------------------------------------------------------------
private static boolean propertyExists( String [] values, String [] av )
{
if( values != null )
{
for( int i=0; i< values.length; i++ )
{
String p = System.getProperty( values[i] );
if( p != null )
return true;
}
if( av != null )
for( int i=0; i< values.length; i++ )
for( int j=0; j< av.length; j++ )
{
if( ("--"+values[i]).equals( av[j] ) )
return true;
}
}
return false;
}
private static final void usage()
{
System.out.println("usage: java -jar ...jar [-m|-p]\n-m: encrypt master password\n-p: encrypt password");
}
//---------------------------------------------------------------
public static void main( String[] args )
throws Exception
{
if( args == null || args.length < 1 )
{
usage();
return;
}
if( "-m".equals( args[0] ) || propertyExists( SYSTEM_PROPERTY_MASTER_PASSWORD, args ) )
show( true );
else if( "-p".equals( args[0] ) || propertyExists( SYSTEM_PROPERTY_SERVER_PASSWORD, args ) )
show( false );
else
usage();
}
//---------------------------------------------------------------
private static void show( boolean showMaster )
throws Exception
{
if( showMaster )
System.out.print("\nsettings master password\n");
else
System.out.print("\nsettings server password\n");
System.out.print("enter password: ");
BufferedReader r = new BufferedReader( new InputStreamReader( System.in ) );
String pass = r.readLine();
System.out.println("\n");
DefaultPlexusCipher dc = new DefaultPlexusCipher();
DefaultSecDispatcher dd = new DefaultSecDispatcher();
dd._cipher = dc;
if( showMaster )
System.out.println( dc.encryptAndDecorate( pass, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ) );
else
{
SettingsSecurity sec = dd.getSec();
System.out.println( dc.encryptAndDecorate( pass, dd.getMaster(sec) ) );
}
}
//---------------------------------------------------------------
//---------------------------------------------------------------
}