1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
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
45 }
46
47 private void setUpService() {
48 try {
49 KeyPairGenerator keyGen = KeyPairGenerator.getInstance(CLIENT_KEY_GENERATOR_ALGORITHM);
50
51 keyGen.initialize(CLIENT_KEY_PAIR_KEY_SIZE);
52
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 }