~ubuntu-branches/ubuntu/maverick/eucalyptus/maverick

« back to all changes in this revision

Viewing changes to clc/modules/authentication/src/main/java/com/eucalyptus/auth/DatabaseAuthProvider.java

  • Committer: Bazaar Package Importer
  • Author(s): Dave Walker (Daviey)
  • Date: 2010-07-21 17:27:10 UTC
  • mfrom: (1.1.38 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721172710-7xv07dmdqgivc3t9
Tags: 2.0~bzr1211-0ubuntu1
* New major upstream version merge, 2.0 (r1211).
* debian/patches/:
  - 01-wsdl-stubs.patch, debian/wsdl.md5sums: wsdl stubs updated.
  - 02-Makefile.patch: Updated to reflect new code layout.
  - 07-local_support_euca_conf-in.patch: Updated to reflect new code layout.
  - 08-ubuntu-default-networking.patch: Refreshed.
  - 09-small-128-192MB.patch: Updated to point to new location.
  - 10-disable-iscsi.patch: Refreshed.
  - 11-state-cleanup-memleakfix.patch: Removed, fixed upstream.
  - 15-fix-default-ramdisk.patch: Updated to point to new location.
  - 16-kvm_libvirt_xml_default_use_kvm.patch: Updated to reflect changes.
  - 17-fix_walrus_OOM_errors.patch: Removed, fixed upstream.
  - 18-priv_security.patch: Updated to reflect upstream changes.
  - 20-brute-force-webui.patch: Updated to reflect upstream changes. 
  - 21-eucalyptus-1.7-with-gwt-1.6.4.patch: New patch, allows 
    eucalyptus-1.7 to be built against gwt 1.6.4. Based on patch courtesy 
    of Dmitrii Zagorodnov, upstream. (LP: #597330)
* debian/eucalyptus-java-common.links: 
  - Changed symlink for groovy, point to groovy.all.jar, making compatiable 
    with groovy versions >1.7. (LP: #595421)
  - Added ant.jar & jetty-rewrite-handler.jar as they are now required.
* debian/control
  - & debian/build-jars: Added libjavassist-java and libjetty-extra-java as 
    build dependencies.
  - Added libjetty-extra-java as a dependency of eucalyptus-java-common
* The binary resulting jar's have been renamed from eucalyptus-*-1.6.2.jar
  to eucalyptus-*-main.jar:    
  - debian/eucalyptus-cc.upstart
  - debian/eucalyptus-cloud.install
  - debian/eucalyptus-common.eucalyptus.upstart
  - debian/eucalyptus-java-common.install
  - debian/eucalyptus-network.upstart
  - debian/eucalyptus-sc.install
  - debian/eucalyptus-walrus.install
* debian/eucalyptus-java-common.install: New upstream jars that have been
  installed:
  - eucalyptus-db-hsqldb-ext-main.jar
  - eucalyptus-component-main.jar
* debian/control:
  - Updated Standards Version to 3.8.4 (no change)
  - Updated the upstream Homepage to: http://open.eucalyptus.com/
  - Changed Vcs-Bzr to reflect new location of Ubuntu hosted development branch.
  - Made the Build Dependency of groovy and the binary eucalyptus-java-common
    package depend on version >=1.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 *Copyright (c) 2009  Eucalyptus Systems, Inc.
 
3
 * 
 
4
 *  This program is free software: you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation, only version 3 of the License.
 
7
 * 
 
8
 * 
 
9
 *  This file is distributed in the hope that it will be useful, but WITHOUT
 
10
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
12
 *  for more details.
 
13
 * 
 
14
 *  You should have received a copy of the GNU General Public License along
 
15
 *  with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 * 
 
17
 *  Please contact Eucalyptus Systems, Inc., 130 Castilian
 
18
 *  Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
 
19
 *  if you need additional information or have any questions.
 
20
 * 
 
21
 *  This file may incorporate work covered under the following copyright and
 
22
 *  permission notice:
 
23
 * 
 
24
 *    Software License Agreement (BSD License)
 
25
 * 
 
26
 *    Copyright (c) 2008, Regents of the University of California
 
27
 *    All rights reserved.
 
28
 * 
 
29
 *    Redistribution and use of this software in source and binary forms, with
 
30
 *    or without modification, are permitted provided that the following
 
31
 *    conditions are met:
 
32
 * 
 
33
 *      Redistributions of source code must retain the above copyright notice,
 
34
 *      this list of conditions and the following disclaimer.
 
35
 * 
 
36
 *      Redistributions in binary form must reproduce the above copyright
 
37
 *      notice, this list of conditions and the following disclaimer in the
 
38
 *      documentation and/or other materials provided with the distribution.
 
39
 * 
 
40
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 
41
 *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 
42
 *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 
43
 *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 
44
 *    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
45
 *    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
46
 *    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
47
 *    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
48
 *    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
49
 *    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
50
 *    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
 
51
 *    THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
 
52
 *    LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
 
53
 *    SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
 
54
 *    IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
 
55
 *    BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
 
56
 *    THE REGENTS’ DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
 
57
 *    OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
 
58
 *    WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
 
59
 *    ANY SUCH LICENSES OR RIGHTS.
 
60
 *******************************************************************************
 
61
 * Author: chris grzegorczyk <grze@eucalyptus.com>
 
62
 */
 
63
package com.eucalyptus.auth;
 
64
 
 
65
import java.security.GeneralSecurityException;
 
66
import java.security.cert.X509Certificate;
 
67
import java.util.List;
 
68
import org.apache.log4j.Logger;
 
69
import org.hibernate.Session;
 
70
import org.hibernate.criterion.Example;
 
71
import org.hibernate.criterion.MatchMode;
 
72
import com.eucalyptus.auth.api.GroupProvider;
 
73
import com.eucalyptus.auth.api.NoSuchCertificateException;
 
74
import com.eucalyptus.auth.api.UserInfoProvider;
 
75
import com.eucalyptus.auth.api.UserProvider;
 
76
import com.eucalyptus.auth.crypto.Crypto;
 
77
import com.eucalyptus.auth.crypto.Hmacs;
 
78
import com.eucalyptus.auth.principal.Group;
 
79
import com.eucalyptus.auth.principal.User;
 
80
import com.eucalyptus.auth.util.B64;
 
81
import com.eucalyptus.auth.util.PEMFiles;
 
82
import com.eucalyptus.entities.EntityWrapper;
 
83
import com.eucalyptus.util.EucalyptusCloudException;
 
84
import com.eucalyptus.util.Tx;
 
85
import com.google.common.collect.Lists;
 
86
 
 
87
public class DatabaseAuthProvider implements UserProvider, GroupProvider, UserInfoProvider {
 
88
  private static Logger LOG = Logger.getLogger( DatabaseAuthProvider.class );
 
89
  
 
90
  public DatabaseAuthProvider( ) {}
 
91
  
 
92
  @Override
 
93
  public User addUser( String userName, Boolean isAdmin, Boolean isEnabled ) throws UserExistsException {
 
94
    UserEntity newUser = new UserEntity( userName );
 
95
    newUser.setQueryId( Hmacs.generateQueryId( userName ) );
 
96
    newUser.setSecretKey( Hmacs.generateSecretKey( userName ) );
 
97
    newUser.setAdministrator( isAdmin );
 
98
    newUser.setEnabled( isEnabled );
 
99
    newUser.setPassword( Crypto.generateHashedPassword( userName ) );
 
100
    newUser.setToken( Crypto.generateSessionToken( userName ) );
 
101
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
102
    try {
 
103
      db.add( newUser );
 
104
      db.commit( );
 
105
    } catch ( Throwable t ) {
 
106
      db.rollback( );
 
107
      throw new UserExistsException( t );
 
108
    }
 
109
    EntityWrapper<UserInfo> dbU = EntityWrapper.get( UserInfo.class );
 
110
    try {
 
111
      String confirmCode = Crypto.generateSessionToken( userName );
 
112
      UserInfo newUserInfo = new UserInfo( userName, confirmCode );
 
113
      dbU.add( newUserInfo );
 
114
      dbU.commit( );
 
115
    } catch ( Exception e ) {
 
116
      LOG.debug( e, e );
 
117
      dbU.rollback( );
 
118
    }    
 
119
    User proxy = new DatabaseWrappedUser( newUser );
 
120
    Groups.DEFAULT.addMember( proxy );
 
121
    return proxy;
 
122
  }
 
123
  
 
124
  @Override
 
125
  public void deleteUser( String userName ) throws NoSuchUserException {
 
126
    UserEntity user = new UserEntity( userName );
 
127
    EntityWrapper<UserInfo> dbU = EntityWrapper.get( UserInfo.class );
 
128
    try {
 
129
      UserInfo newUserInfo = dbU.getUnique( new UserInfo( userName ) );
 
130
      dbU.delete( newUserInfo );
 
131
      dbU.commit( );
 
132
    } catch ( Exception e ) {
 
133
      LOG.debug( e, e );
 
134
      dbU.rollback( );
 
135
    }    
 
136
    EntityWrapper<User> db = Authentication.getEntityWrapper( );
 
137
    try {
 
138
      User foundUser = db.getUnique( user );
 
139
      for( Group g : Groups.lookupUserGroups( foundUser ) ) {
 
140
        g.removeMember( foundUser );
 
141
      }
 
142
      db.delete( foundUser );
 
143
      db.commit( );
 
144
    } catch ( Exception e ) {
 
145
      db.rollback( );
 
146
      throw new NoSuchUserException( e );
 
147
    }
 
148
  }
 
149
  
 
150
  @Override
 
151
  public List<Group> lookupUserGroups( User user ) {
 
152
    List<Group> userGroups = Lists.newArrayList( );
 
153
    EntityWrapper<GroupEntity> db = Authentication.getEntityWrapper( );
 
154
    try {
 
155
      UserEntity userInfo = db.recast( UserEntity.class ).getUnique( new UserEntity( user.getName( ) ) );
 
156
      for ( GroupEntity g : db.query( new GroupEntity( ) ) ) {
 
157
        if ( g.isMember( userInfo ) ) {
 
158
          userGroups.add( DatabaseWrappedGroup.newInstance( g ) );
 
159
        }
 
160
      }
 
161
      db.commit( );
 
162
    } catch ( EucalyptusCloudException e ) {
 
163
      LOG.debug( e, e );
 
164
      db.rollback( );
 
165
    }
 
166
    return userGroups;
 
167
  }
 
168
  
 
169
  @Override
 
170
  public Group lookupGroup( String groupName ) throws NoSuchGroupException {
 
171
    EntityWrapper<GroupEntity> db = Authentication.getEntityWrapper( );
 
172
    try {
 
173
      GroupEntity group = db.getUnique( new GroupEntity( groupName ) );
 
174
      db.commit( );
 
175
      return DatabaseWrappedGroup.newInstance( group );
 
176
    } catch ( EucalyptusCloudException e ) {
 
177
      db.rollback( );
 
178
      throw new NoSuchGroupException( e );
 
179
    }
 
180
  }
 
181
  
 
182
  @Override
 
183
  public List<User> listAllUsers( ) {
 
184
    List<User> users = Lists.newArrayList( );
 
185
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
186
    UserEntity searchUser = new UserEntity( );
 
187
    UserEntity user = null;
 
188
    try {
 
189
      for( UserEntity u : db.query( searchUser ) ) {
 
190
        users.add( new DatabaseWrappedUser( u ) );
 
191
      }
 
192
      db.commit( );
 
193
    } catch ( Throwable e ) {
 
194
      db.rollback( );
 
195
    }
 
196
    return users;
 
197
  }
 
198
  
 
199
  @Override
 
200
  public List<User> listEnabledUsers( ) {
 
201
    List<User> users = Lists.newArrayList( );
 
202
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
203
    UserEntity searchUser = new UserEntity( );
 
204
    searchUser.setEnabled( true );
 
205
    UserEntity user = null;
 
206
    try {
 
207
      for( UserEntity u : db.query( searchUser ) ) {
 
208
        users.add( new DatabaseWrappedUser( u ) );
 
209
      }
 
210
      db.commit( );
 
211
    } catch ( Throwable e ) {
 
212
      db.rollback( );
 
213
    }
 
214
    return users;
 
215
  }
 
216
  
 
217
  @Override
 
218
  public User lookupCertificate( X509Certificate cert ) throws NoSuchUserException {
 
219
    String certPem = B64.url.encString( PEMFiles.getBytes( cert ) );
 
220
    UserEntity searchUser = new UserEntity( );
 
221
    searchUser.setEnabled( true );
 
222
    X509Cert searchCert = new X509Cert( );
 
223
    searchCert.setPemCertificate( certPem );
 
224
    searchCert.setRevoked( null );
 
225
    EntityWrapper<UserEntity> db = EntityWrapper.get( searchUser );
 
226
    Session session = db.getSession( );
 
227
    try {
 
228
      Example qbeUser = Example.create( searchUser ).enableLike( MatchMode.EXACT );
 
229
      Example qbeCert = Example.create( searchCert ).enableLike( MatchMode.EXACT );
 
230
      List<UserEntity> users = ( List<UserEntity> ) session.createCriteria( UserEntity.class ).setCacheable( true ).add( qbeUser ).createCriteria( "certificates" )
 
231
                                               .setCacheable( true ).add( qbeCert ).list( );
 
232
      UserEntity ret = users.size( ) == 1 ? users.get( 0 ) : null;
 
233
      int size = users.size( );
 
234
      if ( ret != null ) {
 
235
        return new DatabaseWrappedUser( ret );
 
236
      } else {
 
237
        throw new GeneralSecurityException( ( size == 0 ) ? "No user with the specified certificate." : "Multiple users with the same certificate." );
 
238
      }
 
239
    } catch ( Throwable t ) {
 
240
      throw new NoSuchUserException( t );
 
241
    } finally {
 
242
      db.rollback( );
 
243
    }
 
244
  }
 
245
  
 
246
  @Override
 
247
  public boolean checkRevokedCertificate( X509Certificate cert ) throws NoSuchCertificateException {
 
248
    String certPem = B64.url.encString( PEMFiles.getBytes( cert ) );
 
249
    UserEntity searchUser = new UserEntity( );
 
250
    searchUser.setEnabled( true );
 
251
    X509Cert searchCert = new X509Cert( );
 
252
    searchCert.setPemCertificate( certPem );
 
253
    searchCert.setRevoked( true );
 
254
    EntityWrapper<UserEntity> db = EntityWrapper.get( searchUser );
 
255
    Session session = db.getSession( );
 
256
    try {
 
257
      Example qbeUser = Example.create( searchUser ).enableLike( MatchMode.EXACT );
 
258
      Example qbeCert = Example.create( searchCert ).enableLike( MatchMode.EXACT );
 
259
      List<User> users = ( List<User> ) session.createCriteria( User.class ).setCacheable( true ).add( qbeUser ).createCriteria( "certificates" )
 
260
                                               .setCacheable( true ).add( qbeCert ).list( );
 
261
      if( users.isEmpty( ) || users.size( ) > 1 ) {
 
262
        throw new NoSuchCertificateException( "Failed to identify user (found " + users.size() + ") from certificate information: " + cert.getSubjectX500Principal( ).toString( ) );
 
263
      } else {
 
264
        return true;
 
265
      }
 
266
    } finally {
 
267
      db.rollback( );
 
268
    }
 
269
  }
 
270
  
 
271
  
 
272
  @Override
 
273
  public User lookupQueryId( String queryId ) throws NoSuchUserException {
 
274
    String userName = null;
 
275
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
276
    UserEntity searchUser = new UserEntity( );
 
277
    searchUser.setQueryId( queryId );
 
278
    UserEntity user = null;
 
279
    try {
 
280
      user = db.getUnique( searchUser );
 
281
      db.commit( );
 
282
    } catch ( Throwable e ) {
 
283
      db.rollback( );
 
284
      throw new NoSuchUserException( e );
 
285
    }
 
286
    return new DatabaseWrappedUser( user );
 
287
  }
 
288
  
 
289
  @Override
 
290
  public User lookupUser( String userName ) throws NoSuchUserException {
 
291
    EntityWrapper<UserEntity> db = Authentication.getEntityWrapper( );
 
292
    UserEntity searchUser = new UserEntity( userName );
 
293
    UserEntity user = null;
 
294
    try {
 
295
      user = db.getUnique( searchUser );
 
296
      db.commit( );
 
297
    } catch ( Throwable e ) {
 
298
      db.rollback( );
 
299
      throw new NoSuchUserException( e );
 
300
    }
 
301
    return new DatabaseWrappedUser( user );
 
302
  }
 
303
  
 
304
  @Override
 
305
  public Group addGroup( String groupName ) throws GroupExistsException {
 
306
    EntityWrapper<GroupEntity> db = Authentication.getEntityWrapper( );
 
307
    GroupEntity newGroup = new GroupEntity( groupName );
 
308
    try {
 
309
      db.add( newGroup );
 
310
      db.commit( );
 
311
    } catch ( Throwable t ) {
 
312
      db.rollback( );
 
313
      throw new GroupExistsException( t );
 
314
    }
 
315
    return DatabaseWrappedGroup.newInstance( newGroup );
 
316
  }
 
317
  
 
318
  @Override
 
319
  public List<Group> listAllGroups( ) {
 
320
    List<Group> ret = Lists.newArrayList( );
 
321
    GroupEntity search = new GroupEntity( );
 
322
    EntityWrapper<GroupEntity> db = EntityWrapper.get( search );
 
323
    List<GroupEntity> groupList = db.query( search );
 
324
    for ( GroupEntity g : groupList ) {
 
325
      ret.add( DatabaseWrappedGroup.newInstance( g ) );
 
326
    }
 
327
    return ret;
 
328
  }
 
329
  @Override
 
330
  public void deleteGroup( String groupName ) throws NoSuchGroupException {
 
331
    Groups.checkNotRestricted( groupName );
 
332
    EntityWrapper<GroupEntity> db = Authentication.getEntityWrapper( );
 
333
    GroupEntity delGroup = new GroupEntity( groupName );
 
334
    try {
 
335
      GroupEntity g = db.getUnique( delGroup );
 
336
      db.delete( g );
 
337
      db.commit( );
 
338
    } catch ( Throwable t ) {
 
339
      db.rollback( );
 
340
      throw new NoSuchGroupException( t );
 
341
    }    
 
342
  }
 
343
 
 
344
  @Override
 
345
  public void updateUser( String name, Tx<User> userTx ) throws NoSuchUserException {
 
346
    UserEntity search = new UserEntity(name);
 
347
    EntityWrapper<UserEntity> db = EntityWrapper.get( search );
 
348
    try {
 
349
      UserEntity entity = db.getUnique( search );
 
350
      userTx.fire( entity );
 
351
      db.commit( );
 
352
    } catch ( EucalyptusCloudException e ) {
 
353
      db.rollback( );
 
354
      throw new NoSuchUserException( e.getMessage( ), e );
 
355
    } catch ( Throwable e ) {
 
356
      db.rollback( );
 
357
      LOG.error( e, e );
 
358
      throw new NoSuchUserException( e.getMessage( ), e );
 
359
    }    
 
360
  }
 
361
 
 
362
  @Override
 
363
  public void addUserInfo( UserInfo user ) throws UserExistsException {
 
364
    EntityWrapper<UserInfo> dbWrapper = EntityWrapper.get( UserInfo.class );
 
365
    try {
 
366
      dbWrapper.add( user );
 
367
      dbWrapper.commit();
 
368
    } catch ( Exception e1 ) {
 
369
      dbWrapper.rollback();
 
370
      LOG.error( e1, e1 );
 
371
      throw new UserExistsException( "User info exists", e1 );
 
372
    }    
 
373
  }
 
374
 
 
375
  @Override
 
376
  public void deleteUserInfo( String userName ) throws NoSuchUserException {
 
377
    EntityWrapper<UserInfo> dbWrapper = new EntityWrapper<UserInfo>( );
 
378
    try {
 
379
      UserInfo userInfo = dbWrapper.getUnique( new UserInfo(userName) );
 
380
      dbWrapper.delete( userInfo );
 
381
      dbWrapper.commit( );
 
382
    } catch ( EucalyptusCloudException e1 ) {
 
383
      dbWrapper.rollback( );
 
384
      LOG.error( e1, e1 );
 
385
      throw new NoSuchUserException( "User info does not exist", e1 );  
 
386
    }    
 
387
  }
 
388
 
 
389
  @Override
 
390
  public UserInfo getUserInfo( UserInfo search ) throws NoSuchUserException {
 
391
    EntityWrapper<UserInfo> dbWrapper = new EntityWrapper<UserInfo>( );
 
392
    try {
 
393
      UserInfo userInfo = dbWrapper.getUnique( search );
 
394
      dbWrapper.commit( );
 
395
      return userInfo;
 
396
    } catch ( EucalyptusCloudException e ) {
 
397
      dbWrapper.rollback( );
 
398
      throw new NoSuchUserException( "User info does not exist", e );
 
399
    }
 
400
  }
 
401
 
 
402
  @Override
 
403
  public void updateUserInfo( String name, Tx<UserInfo> infoTx ) throws NoSuchUserException {
 
404
    EntityWrapper<UserInfo> dbWrapper = new EntityWrapper<UserInfo>( );
 
405
    try {
 
406
      UserInfo userInfo = dbWrapper.getUnique( new UserInfo(name) );
 
407
      infoTx.fire( userInfo );
 
408
      dbWrapper.commit( );
 
409
    } catch ( EucalyptusCloudException e ) {
 
410
      dbWrapper.rollback( );
 
411
      LOG.error( e, e );
 
412
      throw new NoSuchUserException( "User info does not exist", e );
 
413
    } catch ( Throwable t ) {
 
414
      dbWrapper.rollback( );
 
415
      LOG.error( t, t );
 
416
      throw new NoSuchUserException( "Error in updating user info", t );
 
417
    }
 
418
  }
 
419
}