View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.security.admin;
17  
18  import java.io.IOException;
19  import java.security.GeneralSecurityException;
20  import java.security.KeyPair;
21  import java.security.KeyPairGenerator;
22  import java.security.KeyStore;
23  import java.security.PrivateKey;
24  import java.security.cert.Certificate;
25  
26  import org.kuali.rice.ksb.security.admin.service.impl.JavaSecurityManagementServiceImpl;
27  
28  /**
29   * This is a mock class used by the KSB test harness to supplant the {@link JavaSecurityManagementServiceImpl} class
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   *
33   */
34  public class MockJavaSecurityManagementServiceImpl extends JavaSecurityManagementServiceImpl implements MockJavaSecurityManagementService {
35      
36      private static final String FAKE_KEYSTORE_ALIAS = "test_keystore_alias";
37      private static final String FAKE_KEYSTORE_PASSWORD = "test_keystore_pass";
38  
39      private KeyStore moduleKeyStore;
40      private PrivateKey modulePrivateKey;
41  
42      @Override
43      public void afterPropertiesSet() throws Exception {
44          // method is empty in order to override operation of JavaSecurityManagementServiceImpl.afterPropertiesSet()
45      }
46      
47      private void setUpService() {
48          try {
49              KeyPairGenerator keyGen = KeyPairGenerator.getInstance(CLIENT_KEY_GENERATOR_ALGORITHM);
50  //            SecureRandom random = SecureRandom.getInstance(CLIENT_SECURE_RANDOM_ALGORITHM);
51              keyGen.initialize(CLIENT_KEY_PAIR_KEY_SIZE);
52  //            keyGen.initialize(new RSAKeyGenParameterSpec(512,RSAKeyGenParameterSpec.F0));
53              KeyPair pair = keyGen.generateKeyPair();
54  
55              this.modulePrivateKey = pair.getPrivate();
56              Certificate cert = generateCertificate(pair, getModuleKeyStoreAlias());
57              this.moduleKeyStore = generateKeyStore(cert, pair.getPrivate(), getModuleKeyStoreAlias(), getModuleKeyStorePassword());
58          } catch (GeneralSecurityException e) {
59              e.printStackTrace();
60          } catch (IOException e) {
61              e.printStackTrace();
62          }
63      }
64  
65      @Override
66      public String getModuleKeyStoreLocation() {
67          throw new RuntimeException("KeyStoreLocation should not be needed in unit tests");
68      }
69  
70      @Override
71      public String getModuleKeyStoreAlias() {
72          return FAKE_KEYSTORE_ALIAS;
73      }
74  
75      @Override
76      public String getModuleKeyStorePassword() {
77          return FAKE_KEYSTORE_PASSWORD;
78      }
79  
80      @Override
81      public KeyStore getModuleKeyStore() {
82          if (this.moduleKeyStore == null) {
83              setUpService();
84          }
85          return this.moduleKeyStore;
86      }
87      
88      @Override
89      public PrivateKey getModulePrivateKey() {
90          if (this.modulePrivateKey == null) {
91              setUpService();
92          }
93          return this.modulePrivateKey;
94      }
95  
96  }