1 | package com.nailabs.abac.test; |
---|
2 | |
---|
3 | import java.io.*; |
---|
4 | import java.util.*; |
---|
5 | import com.nailabs.abac.process.*; |
---|
6 | |
---|
7 | import com.nailabs.abac.credential.RtmlEntity; |
---|
8 | import com.nailabs.abac.credential.RtmlExpression; |
---|
9 | import com.nailabs.abac.trust.TrustTarget; |
---|
10 | |
---|
11 | import edu.stanford.rt.credential.CredentialStore; |
---|
12 | import edu.stanford.rt.credential.RTContext; |
---|
13 | import edu.stanford.rt.parser.RTParser; |
---|
14 | import edu.stanford.peer.rbtm.credential.Entity; |
---|
15 | import edu.stanford.peer.rbtm.credential.EntityExpression; |
---|
16 | import edu.stanford.peer.rbtm.credential.Role; |
---|
17 | import edu.stanford.peer.rbtm.credential.RoleName; |
---|
18 | import edu.stanford.peer.rbtm.credential.SimpleRoleName; |
---|
19 | import edu.stanford.peer.rbtm.credential.StaticCredential; |
---|
20 | |
---|
21 | import org.xml.sax.SAXParseException; |
---|
22 | |
---|
23 | public class RtmlTest extends TestEngine { |
---|
24 | |
---|
25 | public static void parsePrepInfo(HashMap conf) { |
---|
26 | String section = "PrepInfo"; |
---|
27 | HashSet propSet = (HashSet)conf.get(section); |
---|
28 | Properties props = new Properties(); |
---|
29 | try { |
---|
30 | Iterator i = propSet.iterator(); |
---|
31 | while(i.hasNext()) { |
---|
32 | StringTokenizer st = |
---|
33 | new StringTokenizer((String)i.next(), "="); |
---|
34 | Entity key = |
---|
35 | (Entity)RtmlExpression.getEntityExpression(st.nextToken()); |
---|
36 | props.setProperty(key.toString(), st.nextToken()); |
---|
37 | } |
---|
38 | props.list(System.out); |
---|
39 | conf.put(section, props); |
---|
40 | } catch (Exception ex) { |
---|
41 | ex.printStackTrace(); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | public static void parseEntityId(HashMap map) { |
---|
46 | String section = "EntityID"; |
---|
47 | RtmlEntity eid = null; |
---|
48 | HashSet propSet = (HashSet)map.get(section); |
---|
49 | Properties props = new Properties(); |
---|
50 | try { |
---|
51 | Iterator i = propSet.iterator(); |
---|
52 | while(i.hasNext()) { |
---|
53 | props.load(new StringBufferInputStream((String)i.next())); |
---|
54 | } |
---|
55 | eid = new RtmlEntity(props.getProperty("ShortName"), |
---|
56 | props.getProperty("HashCode")); |
---|
57 | //props.list(System.out); |
---|
58 | } catch (Exception ex) { |
---|
59 | ex.printStackTrace(); |
---|
60 | } |
---|
61 | map.put(section, eid); |
---|
62 | } |
---|
63 | |
---|
64 | public static void parseResourcePolicy(HashMap conf) { |
---|
65 | ResourcePolicy policy = new ResourcePolicy(); |
---|
66 | String section = "ResourcePolicy"; |
---|
67 | HashSet policySet = (HashSet)conf.get(section); |
---|
68 | if(policySet == null)policySet = new HashSet(); |
---|
69 | Iterator i = policySet.iterator(); |
---|
70 | |
---|
71 | while(i.hasNext()) { |
---|
72 | try { |
---|
73 | StringTokenizer st = |
---|
74 | new StringTokenizer((String)i.next(), "="); |
---|
75 | String key = st.nextToken(); |
---|
76 | Role value = //(Role)StaticCredential.getRole(st.nextToken()); |
---|
77 | (Role)RtmlExpression.getEntityExpression(st.nextToken()); |
---|
78 | policy.addResourceFact(key, value); |
---|
79 | } |
---|
80 | catch(Exception ex) { |
---|
81 | ex.printStackTrace(); |
---|
82 | } |
---|
83 | } |
---|
84 | conf.put(section, policy); |
---|
85 | System.out.println("ResourcePolicy = " + policy); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | public static void parseAckPolicy(HashMap conf) { |
---|
90 | AckPolicy policy = new AckPolicy(); |
---|
91 | String section = "AckPolicy"; |
---|
92 | HashSet policySet = (HashSet)conf.get(section); |
---|
93 | if(policySet == null)policySet = new HashSet(); |
---|
94 | Iterator i = policySet.iterator(); |
---|
95 | |
---|
96 | while(i.hasNext()) { |
---|
97 | try { |
---|
98 | StringTokenizer st = |
---|
99 | new StringTokenizer((String)i.next(), "="); |
---|
100 | EntityExpression key = |
---|
101 | RtmlExpression.getEntityExpression(st.nextToken()); |
---|
102 | EntityExpression value = |
---|
103 | RtmlExpression.getEntityExpression(st.nextToken()); |
---|
104 | policy.addAckFact(key, value); |
---|
105 | } |
---|
106 | catch(Exception ex) { |
---|
107 | ex.printStackTrace(); |
---|
108 | } |
---|
109 | } |
---|
110 | conf.put(section, policy); |
---|
111 | System.out.println("AckPolicy = " + policy); |
---|
112 | } |
---|
113 | |
---|
114 | public static void parseRtmlProperties(HashMap map) { |
---|
115 | String section = "RTML"; |
---|
116 | HashSet propSet = (HashSet)map.get(section); |
---|
117 | Properties props = new Properties(); |
---|
118 | try { |
---|
119 | Iterator i = propSet.iterator(); |
---|
120 | while(i.hasNext()) { |
---|
121 | props.load(new StringBufferInputStream((String)i.next())); |
---|
122 | } |
---|
123 | //props.list(System.out); |
---|
124 | // CUSTOMIZED FOR TOMCAT |
---|
125 | if(props.containsKey("SystemDomain")) { |
---|
126 | System.setProperty("edu.stanford.rt.SystemDomain", |
---|
127 | props.getProperty("SystemDomain")); |
---|
128 | } |
---|
129 | map.put(section, props); |
---|
130 | } catch (Exception ex) { |
---|
131 | ex.printStackTrace(); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | public static void parseCredentialStore(HashMap map) { |
---|
136 | String section = "CredentialStore"; |
---|
137 | String fileName = ((Properties)map.get("RTML")).getProperty(section); |
---|
138 | RTParser parser = null; |
---|
139 | RTContext context = null; |
---|
140 | CredentialStore store = null; |
---|
141 | try { |
---|
142 | parser = new RTParser(); |
---|
143 | context = new RTContext(parser); |
---|
144 | store = new CredentialStore(parser); |
---|
145 | FileInputStream in = new FileInputStream(new File(fileName)); |
---|
146 | parser.parseCredentialStore(in, context, store); |
---|
147 | } |
---|
148 | catch (SAXParseException spex) { |
---|
149 | System.out.print("SAX Parser error in element " + spex.getPublicId()); |
---|
150 | System.out.print(": line = " + spex.getLineNumber()); |
---|
151 | System.out.println(", col = " + spex.getColumnNumber()); |
---|
152 | } |
---|
153 | catch (Exception ex) { |
---|
154 | System.out.println(ex.getMessage()); |
---|
155 | ex.printStackTrace(); |
---|
156 | } |
---|
157 | map.put("Parser", parser); |
---|
158 | map.put("RTContext", context); |
---|
159 | map.put(section, store); |
---|
160 | } |
---|
161 | |
---|
162 | public static void parseDiscoveryStore(HashMap map) { |
---|
163 | String section = "DDEStore"; |
---|
164 | String fileName = ((Properties)map.get("RTML")).getProperty(section); |
---|
165 | RTParser parser = null; |
---|
166 | RTContext context = null; |
---|
167 | CredentialStore store = null; |
---|
168 | try { |
---|
169 | parser = new RTParser(); |
---|
170 | context = new RTContext(parser); |
---|
171 | store = new CredentialStore(parser); |
---|
172 | FileInputStream in = new FileInputStream(new File(fileName)); |
---|
173 | parser.parseCredentialStore(in, context, store); |
---|
174 | } |
---|
175 | catch (SAXParseException spex) { |
---|
176 | System.out.print("SAX Parser error in element " + spex.getPublicId()); |
---|
177 | System.out.print(": line = " + spex.getLineNumber()); |
---|
178 | System.out.println(", col = " + spex.getColumnNumber()); |
---|
179 | } |
---|
180 | catch (Exception ex) { |
---|
181 | System.out.println(ex.getMessage()); |
---|
182 | ex.printStackTrace(); |
---|
183 | } |
---|
184 | map.put("DDEParser", parser); |
---|
185 | map.put("DDEContext", context); |
---|
186 | map.put(section, store); |
---|
187 | } |
---|
188 | |
---|
189 | public static HashMap loadConfiguration(String name) { |
---|
190 | HashMap map = |
---|
191 | edu.stanford.peer.rbtm.test.TestEngine.loadConfiguration(name); |
---|
192 | System.out.println("Loaded configuration from " + name); |
---|
193 | System.out.println("map = " + map ); |
---|
194 | parseEntityId(map); |
---|
195 | parseAckPolicy(map); |
---|
196 | parseACPolicy(map); |
---|
197 | parseResourcePolicy(map); |
---|
198 | // CUSTOMIZED FOR RTML |
---|
199 | parseRtmlProperties(map); |
---|
200 | parseCredentialStore(map); |
---|
201 | parsePolicyReachable(map); |
---|
202 | parseSelfReachable(map); |
---|
203 | parseStrategyHints(map); |
---|
204 | // CUSTOMIZED FOR DISCOVERY |
---|
205 | parsePrepInfo(map); |
---|
206 | parseDiscoveryStore(map); |
---|
207 | return map; |
---|
208 | } |
---|
209 | |
---|
210 | public static void main(String arg[]) { |
---|
211 | HashMap oConf = loadConfiguration((arg.length>0)?arg[0]:"alice.txt"); |
---|
212 | HashMap vConf = loadConfiguration((arg.length>1)?arg[1]:"medsup.txt"); |
---|
213 | String resourceID = (arg.length > 2)? arg[2]: "default"; |
---|
214 | ResourcePolicy resourcePolicy = |
---|
215 | (ResourcePolicy)vConf.get("ResourcePolicy"); |
---|
216 | TrustTarget primaryTT = null; |
---|
217 | |
---|
218 | // Create frontiers |
---|
219 | FrontierManager.addFrontier(vConf); |
---|
220 | FrontierManager.addFrontier(oConf); |
---|
221 | // Create entities for the negotiators |
---|
222 | Entity o = (Entity)oConf.get("EntityID"); |
---|
223 | Entity v = (Entity)vConf.get("EntityID"); |
---|
224 | |
---|
225 | System.out.println("vConf = " + vConf.toString()); |
---|
226 | System.out.println("oConf = " + oConf.toString()); |
---|
227 | |
---|
228 | // Initialize the contexts for each side |
---|
229 | NegotiationContext oContext = new NegotiationContext(oConf); |
---|
230 | NegotiationContext vContext = new NegotiationContext(vConf); |
---|
231 | |
---|
232 | PropertiesObserver oObs = |
---|
233 | new PropertiesObserver(oContext.getSelf()); |
---|
234 | PropertiesObserver vObs = |
---|
235 | new PropertiesObserver(vContext.getSelf()); |
---|
236 | RMINegotiator oAgent = null, vAgent = null; |
---|
237 | |
---|
238 | try { |
---|
239 | oAgent = new RMINegotiator(oContext); |
---|
240 | vAgent = new RMINegotiator(vContext); |
---|
241 | //v = new RtmlEntity("SAdmir", "FakeHashKeySAdmir"); |
---|
242 | //o = new RtmlEntity("USSH", "FakeHashKeyUSSH"); |
---|
243 | System.out.println("opponent = " + o + "," + o.hashCode()); |
---|
244 | System.out.println("verifier = " + v + "," + v.hashCode()); |
---|
245 | |
---|
246 | Role targetRole = resourcePolicy.requires(resourceID); |
---|
247 | System.out.println("target role = " + targetRole + |
---|
248 | "," + targetRole.hashCode()); |
---|
249 | primaryTT = new TrustTarget(v, targetRole, o); |
---|
250 | |
---|
251 | oContext.getGraph().addObserver(oObs); |
---|
252 | oContext.getStrategy().addObserver(oObs); |
---|
253 | vContext.getGraph().addObserver(vObs); |
---|
254 | vContext.getStrategy().addObserver(vObs); |
---|
255 | } |
---|
256 | catch(Exception ex) { |
---|
257 | ex.printStackTrace(System.err); |
---|
258 | } |
---|
259 | vAgent.setPeer(oAgent); |
---|
260 | System.out.println("Primary trust target = " + primaryTT); |
---|
261 | vAgent.setRoot(primaryTT); |
---|
262 | boolean success = vAgent.negotiate(); |
---|
263 | System.out.println("The negotiation has " + ((success)? "succeeded": |
---|
264 | "failed")); |
---|
265 | oObs.close(); |
---|
266 | vObs.close(); |
---|
267 | System.exit(0); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|