1 | package com.nailabs.abac.test; |
---|
2 | |
---|
3 | import com.nailabs.abac.process.*; |
---|
4 | import com.nailabs.abac.trust.*; |
---|
5 | import edu.stanford.peer.rbtm.credential.*; |
---|
6 | import edu.stanford.peer.rbtm.engine.*; |
---|
7 | import java.util.*; |
---|
8 | import java.io.*; |
---|
9 | |
---|
10 | public class TestEngine extends edu.stanford.peer.rbtm.test.TestEngine { |
---|
11 | |
---|
12 | public static void parseStrategyHints(HashMap conf) { |
---|
13 | String section = "Strategy"; |
---|
14 | HashSet propSet = (HashSet)conf.get(section); |
---|
15 | Properties props = new Properties(); |
---|
16 | try { |
---|
17 | Iterator i = propSet.iterator(); |
---|
18 | while(i.hasNext()) { |
---|
19 | props.load(new StringBufferInputStream((String)i.next())); |
---|
20 | } |
---|
21 | props.list(System.out); |
---|
22 | conf.put(section, props); |
---|
23 | } catch (Exception ex) { |
---|
24 | //ex.printStackTrace(); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | public static void parseEntityId(HashMap conf) { |
---|
29 | String section = "EntityID"; |
---|
30 | HashSet nameSet = (HashSet)conf.get(section); |
---|
31 | String name = null; |
---|
32 | try { |
---|
33 | name = (String)nameSet.iterator().next(); |
---|
34 | conf.put(section, new SimpleEntity(name)); |
---|
35 | } |
---|
36 | catch(Exception ex) { |
---|
37 | //ex.printStackTrace(); |
---|
38 | } |
---|
39 | } |
---|
40 | public static void parseAckPolicy(HashMap conf) { |
---|
41 | AckPolicy policy = new AckPolicy(); |
---|
42 | String section = "AckPolicy"; |
---|
43 | HashSet policySet = (HashSet)conf.get(section); |
---|
44 | if(policySet == null)policySet = new HashSet(); |
---|
45 | Iterator i = policySet.iterator(); |
---|
46 | |
---|
47 | while(i.hasNext()) { |
---|
48 | try { |
---|
49 | StringTokenizer st = |
---|
50 | new StringTokenizer((String)i.next(), "="); |
---|
51 | EntityExpression key = |
---|
52 | StaticCredential.getEntityExpression(st.nextToken()); |
---|
53 | EntityExpression value = |
---|
54 | StaticCredential.getEntityExpression(st.nextToken()); |
---|
55 | policy.addAckFact(key, value); |
---|
56 | } |
---|
57 | catch(Exception ex) { |
---|
58 | ex.printStackTrace(); |
---|
59 | } |
---|
60 | } |
---|
61 | conf.put(section, policy); |
---|
62 | System.out.println("AckPolicy = " + policy); |
---|
63 | } |
---|
64 | |
---|
65 | public static void parseACPolicy(HashMap conf) { |
---|
66 | ACPolicy policy = new ACPolicy(); |
---|
67 | String section = "AccessControl"; |
---|
68 | HashSet policySet = (HashSet)conf.get(section); |
---|
69 | if(policySet == null)policySet = new HashSet(); |
---|
70 | Iterator i = policySet.iterator(); |
---|
71 | |
---|
72 | while(i.hasNext()) { |
---|
73 | try { |
---|
74 | StringTokenizer st = |
---|
75 | new StringTokenizer((String)i.next(), "="); |
---|
76 | StaticCredential key = new StaticCredential(st.nextToken()); |
---|
77 | String valueString = st.nextToken().trim(); |
---|
78 | Role value = (valueString.equalsIgnoreCase("true"))? |
---|
79 | ACPolicy.TRUE: |
---|
80 | (Role)StaticCredential.getRole(valueString); |
---|
81 | policy.addACFact(key, value); |
---|
82 | } |
---|
83 | catch(Exception ex) { |
---|
84 | ex.printStackTrace(); |
---|
85 | } |
---|
86 | } |
---|
87 | conf.put(section, policy); |
---|
88 | System.out.println("ACPolicy = " + policy); |
---|
89 | } |
---|
90 | |
---|
91 | protected static HashSet parseCredentials(String section, HashMap conf) { |
---|
92 | HashSet creds = new HashSet(); |
---|
93 | |
---|
94 | try { |
---|
95 | Iterator i = ((HashSet)conf.get(section)).iterator(); |
---|
96 | while(i.hasNext()) { |
---|
97 | creds.add(new StaticCredential((String)i.next())); |
---|
98 | } |
---|
99 | } |
---|
100 | catch(Exception ex) { |
---|
101 | ex.printStackTrace(); |
---|
102 | } |
---|
103 | System.out.println(section + ": " + creds); |
---|
104 | return creds; |
---|
105 | } |
---|
106 | |
---|
107 | protected static HashSet parseRoleNames(String section, HashMap conf) { |
---|
108 | HashSet names = new HashSet(); |
---|
109 | |
---|
110 | try { |
---|
111 | Iterator i = ((HashSet)conf.get(section)).iterator(); |
---|
112 | while(i.hasNext()) { |
---|
113 | names.add(new SimpleRoleName((String)i.next())); |
---|
114 | } |
---|
115 | } |
---|
116 | catch(NullPointerException npe) { |
---|
117 | System.out.println("warning: " + section + " is null"); |
---|
118 | } |
---|
119 | catch(Exception ex) { |
---|
120 | ex.printStackTrace(); |
---|
121 | } |
---|
122 | System.out.println(section + ": " + names); |
---|
123 | return names; |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | public static void parseIssuerTracesAll(HashMap conf) { |
---|
128 | String section = "IssuerTracesAll"; |
---|
129 | HashSet traces = parseRoleNames(section, conf); |
---|
130 | conf.put(section, traces); |
---|
131 | } |
---|
132 | |
---|
133 | public static void parseIssuerTracesDef(HashMap conf) { |
---|
134 | String section = "IssuerTracesDef"; |
---|
135 | HashSet traces = parseRoleNames(section, conf); |
---|
136 | conf.put(section, traces); |
---|
137 | } |
---|
138 | |
---|
139 | public static void parseIssuerTraceable(HashMap conf) { |
---|
140 | String section = "IssuerTraceable"; |
---|
141 | HashSet traces = parseRoleNames(section, conf); |
---|
142 | conf.put(section, traces); |
---|
143 | } |
---|
144 | |
---|
145 | public static void parseSubjectTraceable(HashMap conf) { |
---|
146 | String section = "SubjectTraceable"; |
---|
147 | HashSet traces = parseRoleNames(section, conf); |
---|
148 | conf.put(section, traces); |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | public static void parsePolicyReachable(HashMap conf) { |
---|
153 | String section = "PolicyReachable"; |
---|
154 | HashSet reached = parseCredentials(section, conf); |
---|
155 | conf.put(section, reached); |
---|
156 | } |
---|
157 | |
---|
158 | public static void parseSelfReachable(HashMap conf) { |
---|
159 | String section = "SelfReachable"; |
---|
160 | HashSet reached = parseCredentials(section, conf); |
---|
161 | conf.put(section, reached); |
---|
162 | } |
---|
163 | |
---|
164 | /** */ |
---|
165 | public static HashMap loadConfiguration(String name) { |
---|
166 | HashMap map = |
---|
167 | edu.stanford.peer.rbtm.test.TestEngine.loadConfiguration(name); |
---|
168 | |
---|
169 | System.out.println("Loaded configuration from " + name); |
---|
170 | System.out.println("map = " + map ); |
---|
171 | parseEntityId(map); |
---|
172 | parseAckPolicy(map); |
---|
173 | parseACPolicy(map); |
---|
174 | //parseIssuerTraceable(map); |
---|
175 | parseIssuerTracesAll(map); |
---|
176 | parseIssuerTracesDef(map); |
---|
177 | parseSubjectTraceable(map); |
---|
178 | parsePolicyReachable(map); |
---|
179 | parseSelfReachable(map); |
---|
180 | parseStrategyHints(map); |
---|
181 | return map; |
---|
182 | } |
---|
183 | |
---|
184 | public static void main(String arg[]) { |
---|
185 | HashMap oConf = loadConfiguration((arg.length>0)?arg[0]:"alice.txt"); |
---|
186 | HashMap vConf = loadConfiguration((arg.length>1)?arg[1]:"medsup.txt"); |
---|
187 | TrustTarget primaryTT = null; |
---|
188 | |
---|
189 | //System.out.println("vConf = " + vConf.toString()); |
---|
190 | //System.out.println("oConf = " + oConf.toString()); |
---|
191 | |
---|
192 | // Create frontiers |
---|
193 | FrontierManager.addFrontier(oConf); |
---|
194 | FrontierManager.addFrontier(vConf); |
---|
195 | // Create entities for the negotiators |
---|
196 | Entity o = (Entity)oConf.get("EntityID"); |
---|
197 | Entity v = (Entity)vConf.get("EntityID"); |
---|
198 | System.out.println("opponent = " + o); |
---|
199 | System.out.println("verifier = " + v); |
---|
200 | // Initialize the contexts for each side |
---|
201 | NegotiationContext oContext = new NegotiationContext(oConf); |
---|
202 | NegotiationContext vContext = new NegotiationContext(vConf); |
---|
203 | |
---|
204 | PropertiesObserver oObs = |
---|
205 | new PropertiesObserver(oContext.getSelf()); |
---|
206 | PropertiesObserver vObs = |
---|
207 | new PropertiesObserver(vContext.getSelf()); |
---|
208 | RMINegotiator oAgent = null, vAgent = null; |
---|
209 | |
---|
210 | try { |
---|
211 | oAgent = new RMINegotiator(oContext); |
---|
212 | vAgent = new RMINegotiator(vContext); |
---|
213 | primaryTT = new TrustTarget((arg.length > 2)? arg[2]: |
---|
214 | "MedSup: MedSup.discount <<-?- Alice"); |
---|
215 | oContext.getGraph().addObserver(oObs); |
---|
216 | oContext.getStrategy().addObserver(oObs); |
---|
217 | vContext.getGraph().addObserver(vObs); |
---|
218 | vContext.getStrategy().addObserver(vObs); |
---|
219 | } |
---|
220 | catch(Exception ex) { |
---|
221 | ex.printStackTrace(System.err); |
---|
222 | } |
---|
223 | vAgent.setPeer(oAgent); |
---|
224 | System.out.println("Primary trust target = " + primaryTT); |
---|
225 | vAgent.setRoot(primaryTT); |
---|
226 | boolean success = vAgent.negotiate(); |
---|
227 | System.out.println("The negotiation has " + ((success)? "succeeded": |
---|
228 | "failed")); |
---|
229 | oObs.close(); |
---|
230 | vObs.close(); |
---|
231 | } |
---|
232 | |
---|
233 | } |
---|