001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.security.admin;
017
018 import java.io.IOException;
019 import java.security.GeneralSecurityException;
020 import java.security.KeyPair;
021 import java.security.KeyPairGenerator;
022 import java.security.KeyStore;
023 import java.security.PrivateKey;
024 import java.security.cert.Certificate;
025
026 import org.kuali.rice.ksb.security.admin.service.impl.JavaSecurityManagementServiceImpl;
027
028 /**
029 * This is a mock class used by the KSB test harness to supplant the {@link JavaSecurityManagementServiceImpl} class
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 *
033 */
034 public class MockJavaSecurityManagementServiceImpl extends JavaSecurityManagementServiceImpl implements MockJavaSecurityManagementService {
035
036 private static final String FAKE_KEYSTORE_ALIAS = "test_keystore_alias";
037 private static final String FAKE_KEYSTORE_PASSWORD = "test_keystore_pass";
038
039 private KeyStore moduleKeyStore;
040 private PrivateKey modulePrivateKey;
041
042 @Override
043 public void afterPropertiesSet() throws Exception {
044 // method is empty in order to override operation of JavaSecurityManagementServiceImpl.afterPropertiesSet()
045 }
046
047 private void setUpService() {
048 try {
049 KeyPairGenerator keyGen = KeyPairGenerator.getInstance(CLIENT_KEY_GENERATOR_ALGORITHM);
050 // SecureRandom random = SecureRandom.getInstance(CLIENT_SECURE_RANDOM_ALGORITHM);
051 keyGen.initialize(CLIENT_KEY_PAIR_KEY_SIZE);
052 // keyGen.initialize(new RSAKeyGenParameterSpec(512,RSAKeyGenParameterSpec.F0));
053 KeyPair pair = keyGen.generateKeyPair();
054
055 this.modulePrivateKey = pair.getPrivate();
056 Certificate cert = generateCertificate(pair, getModuleKeyStoreAlias());
057 this.moduleKeyStore = generateKeyStore(cert, pair.getPrivate(), getModuleKeyStoreAlias(), getModuleKeyStorePassword());
058 } catch (GeneralSecurityException e) {
059 e.printStackTrace();
060 } catch (IOException e) {
061 e.printStackTrace();
062 }
063 }
064
065 @Override
066 public String getModuleKeyStoreLocation() {
067 throw new RuntimeException("KeyStoreLocation should not be needed in unit tests");
068 }
069
070 @Override
071 public String getModuleKeyStoreAlias() {
072 return FAKE_KEYSTORE_ALIAS;
073 }
074
075 @Override
076 public String getModuleKeyStorePassword() {
077 return FAKE_KEYSTORE_PASSWORD;
078 }
079
080 @Override
081 public KeyStore getModuleKeyStore() {
082 if (this.moduleKeyStore == null) {
083 setUpService();
084 }
085 return this.moduleKeyStore;
086 }
087
088 @Override
089 public PrivateKey getModulePrivateKey() {
090 if (this.modulePrivateKey == null) {
091 setUpService();
092 }
093 return this.modulePrivateKey;
094 }
095
096 }