1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.utils.security;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.gwe.drivers.ResourceHandle;
26 import org.gwe.drivers.fileSystems.FileHandle;
27 import org.gwe.drivers.netAccess.HostHandle;
28
29
30
31
32
33 public class KeyStore implements Serializable {
34
35 private static Log log = LogFactory.getLog(KeyStore.class);
36
37 private static final List<AccessControl> DEFAULT_ACCESS_CONTROLS;
38
39 static {
40 DEFAULT_ACCESS_CONTROLS = new ArrayList<AccessControl>();
41 DEFAULT_ACCESS_CONTROLS.add(AccessControl.createDefaultAC(ProtocolScheme.LOCAL, "localhost"));
42 DEFAULT_ACCESS_CONTROLS.add(AccessControl.createDefaultAC(ProtocolScheme.FILE, ""));
43 }
44
45 public static KeyStore createKeyStore(AccountInfo account, String host) {
46
47 Realm realm = new Realm(ProtocolScheme.SSH + ";" + ProtocolScheme.SFTP, host, host);
48
49
50 List<AccessControl> acs = new ArrayList<AccessControl>();
51 acs.add(new AccessControl(account, realm));
52
53
54 KeyStore result = new KeyStore();
55 result.setAccessControls(acs);
56 return result;
57 }
58
59
60 private List<AccessControl> accessControls = new ArrayList<AccessControl>();
61
62 public List<AccessControl> getAccessControls() {
63 if (accessControls == null)
64 accessControls = new ArrayList<AccessControl>();
65 return accessControls;
66 }
67
68 public void setAccessControls(List<AccessControl> accessControls) {
69 this.accessControls = accessControls;
70 }
71
72 public void init() {
73 for (AccessControl currAC : getAccessControls()) {
74 AccountInfo acct = currAC.getAccount();
75 acct.init();
76 for (Realm realm : currAC.getRealms()) realm.setAccount(acct);
77 }
78 }
79
80 public List<Realm> getRealms() {
81 List<Realm> realms = new ArrayList<Realm>();
82
83 for (AccessControl ac : getAccessControls()) {
84 for (Realm realm : ac.getRealms()) realms.add(realm);
85 }
86 return realms;
87 }
88
89 public Realm resolveRealm(ProtocolScheme scheme, String host) {
90 Realm matchingRealm = resolveRealm(scheme, host, getAccessControls());
91 return (matchingRealm != null) ? matchingRealm : resolveRealm(scheme, host, DEFAULT_ACCESS_CONTROLS);
92 }
93
94 private Realm resolveRealm(ProtocolScheme scheme, String host, List<AccessControl> acList) {
95 Realm matchingRealm;
96 for (AccessControl currAC : acList) {
97 matchingRealm = currAC.findMatchingRealm(scheme, host);
98 if (matchingRealm != null) return matchingRealm;
99 }
100 return null;
101 }
102
103 public ResourceLink<FileHandle> createFileLink(String uriStr) {
104 return createResourceLink(ThinURI.asNormalizedFileURI(null, uriStr));
105 }
106
107 public ResourceLink<HostHandle> createHostLink(String uriStr) {
108 if (uriStr == null || uriStr.equals(""))
109 uriStr = ProtocolScheme.LOCAL.toURIStr("localhost");
110 return createResourceLink(uriStr);
111 }
112
113 private <HANDLE_TYPE extends ResourceHandle> ResourceLink<HANDLE_TYPE> createResourceLink(String uriStr) {
114 ThinURI uri = ThinURI.createBlind(uriStr);
115 ProtocolScheme scheme = ProtocolScheme.valueOf(uri.getScheme().toUpperCase());
116 Realm realm = resolveRealm(scheme, uri.getHost());
117 return createResourceLink(uri, realm);
118 }
119
120 private <HANDLE_TYPE extends ResourceHandle> ResourceLink<HANDLE_TYPE> createResourceLink(ThinURI uri, Realm realm) {
121 if (realm == null)
122 log.warn("No realm found for uri" + uri);
123
124 return (ResourceLink<HANDLE_TYPE>)
125 ((realm == null) ?
126 new ResourceLink<HANDLE_TYPE>(uri, null) :
127 realm.createResourceLink(uri));
128 }
129
130 public void test() {
131 for (AccessControl ac : getAccessControls())
132 for (Realm realm : ac.getRealms()) realm.test();
133 }
134 }
135