1 | package com.nailabs.abac.credential; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.util.Iterator; |
---|
6 | import java.util.HashSet; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.Hashtable; |
---|
9 | import java.util.Collection; |
---|
10 | import java.security.PublicKey; |
---|
11 | import edu.stanford.peer.rbtm.util.Predicate; |
---|
12 | import edu.stanford.peer.rbtm.credential.Entity; |
---|
13 | import edu.stanford.peer.rbtm.credential.EntityExpression; |
---|
14 | import edu.stanford.peer.rbtm.credential.Role; |
---|
15 | import edu.stanford.peer.rbtm.credential.RoleName; |
---|
16 | import edu.stanford.peer.rbtm.credential.SimpleEntity; |
---|
17 | import edu.stanford.peer.rbtm.credential.LinkedRole; |
---|
18 | import edu.stanford.peer.rbtm.credential.StaticCredential; |
---|
19 | |
---|
20 | import edu.stanford.peer.rbtm.engine.*; |
---|
21 | |
---|
22 | import edu.stanford.rt.credential.*; |
---|
23 | import edu.stanford.rt.parser.RTParser; |
---|
24 | |
---|
25 | /** |
---|
26 | * An RTML-aware GraphEngine, formerly known as CredentialManager. |
---|
27 | */ |
---|
28 | public class RtmlEngine extends GraphEngine { |
---|
29 | /** internal store of parsed RTML */ |
---|
30 | protected CredentialStore store; |
---|
31 | |
---|
32 | /** static credential to credential domain mapping */ |
---|
33 | protected HashMap indexByDomains = new HashMap(); |
---|
34 | |
---|
35 | /** internal cache of entity hash id to short name mapping */ |
---|
36 | private static Hashtable entityCache = new Hashtable(10); |
---|
37 | |
---|
38 | public RtmlEngine() { |
---|
39 | super(); |
---|
40 | } |
---|
41 | |
---|
42 | /** default constructor used for the opponents transmitted credentials*/ |
---|
43 | public RtmlEngine(CredentialStore store) { |
---|
44 | super(); |
---|
45 | this.store = store; |
---|
46 | importDomains(store); |
---|
47 | } |
---|
48 | |
---|
49 | /** predicated constructor used by oppo, sens, and simp frontiers */ |
---|
50 | public RtmlEngine(CredentialStore store, Predicate p) { |
---|
51 | super(p); |
---|
52 | this.store = store; |
---|
53 | importDomains(store); |
---|
54 | } |
---|
55 | |
---|
56 | /** convenience method for looking up a shortname from a credential hash */ |
---|
57 | public static String getShortName(String hash) { |
---|
58 | return (String)entityCache.get(hash); |
---|
59 | } |
---|
60 | |
---|
61 | /** adds a new entity hash to short name (reverse) mapping */ |
---|
62 | public static void addEntity(String hash, String shortName) { |
---|
63 | entityCache.put(hash, shortName); |
---|
64 | } |
---|
65 | |
---|
66 | /** add the static credentials which make up a credential domain */ |
---|
67 | public void addDomain(CredentialDomain domain) { |
---|
68 | Iterator creds = convert(domain).iterator(); |
---|
69 | try{ |
---|
70 | while(creds.hasNext()) { |
---|
71 | StaticCredential cred = (StaticCredential)creds.next(); |
---|
72 | HashSet siblings = (HashSet)indexByDomains.get(domain); |
---|
73 | //System.out.println("Adding " + cred); |
---|
74 | addCredential(cred); |
---|
75 | if(siblings == null) { |
---|
76 | siblings = new HashSet(1); |
---|
77 | indexByDomains.put(domain, siblings); |
---|
78 | } |
---|
79 | siblings.add(cred); |
---|
80 | } |
---|
81 | store.addCredentialDomain(domain.getHashID(), domain); |
---|
82 | } catch(Exception ex) { |
---|
83 | //ex.printStackTrace(); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * removes a the domain for this credential and any othe credentials in |
---|
89 | * the same domain. |
---|
90 | */ |
---|
91 | public void removeCredential(RtmlCredential cred) { |
---|
92 | CredentialDomain domain = cred.getCredentialDomain(); |
---|
93 | HashSet credSet = (HashSet)indexByDomains.get(domain); |
---|
94 | if(credSet != null) { |
---|
95 | super.removeCredentials((Collection)credSet); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** protected accessor method used for importing a set of cred domains */ |
---|
100 | protected CredentialStore getCredentialStore() { |
---|
101 | return store; |
---|
102 | } |
---|
103 | |
---|
104 | /** importing domains is used publicly for credential discovery */ |
---|
105 | public void importDomains(RtmlEngine engine) { |
---|
106 | importDomains(engine.getCredentialStore()); |
---|
107 | } |
---|
108 | |
---|
109 | /** internal method for loading the engine from a credential store */ |
---|
110 | public void importDomains(CredentialStore store) { |
---|
111 | HashMap domains = store.getCredentialDomains(); |
---|
112 | Iterator i = domains.values().iterator(); |
---|
113 | while(i.hasNext()) { |
---|
114 | //System.out.println("domain = " + i.next().getClass()); |
---|
115 | addDomain((CredentialDomain)i.next()); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | /** convert a credential domain into a set of static credentials */ |
---|
121 | public static Collection convert(CredentialDomain domain) { |
---|
122 | HashSet set = new HashSet(); |
---|
123 | Iterator roles = domain.roleDefinitionIterator(); |
---|
124 | int count = 0; |
---|
125 | while(roles.hasNext()) { |
---|
126 | //System.out.println("Role Definition # " + ++count); |
---|
127 | RoleDefinition def = (RoleDefinition)roles.next(); |
---|
128 | Role head = (Role) |
---|
129 | RtmlExpression.convert(def.getHead(), domain); |
---|
130 | EntityExpression body = |
---|
131 | RtmlExpression.convert(def.getBody(), domain); |
---|
132 | StaticCredential cred = new RtmlCredential(head, body, domain); |
---|
133 | //System.out.println("\tcred = " + cred); |
---|
134 | set.add(cred); |
---|
135 | } |
---|
136 | return set; |
---|
137 | } |
---|
138 | |
---|
139 | /** main method for a test application */ |
---|
140 | public static void main(String argv[]) { |
---|
141 | if(argv.length < 1) { |
---|
142 | System.out.println("java com.nailabs.abac.credential.RtmlCredential cred_store.xml"); |
---|
143 | System.exit(1); |
---|
144 | } |
---|
145 | try { |
---|
146 | RTParser parser = new RTParser(); |
---|
147 | //System domain is created here. |
---|
148 | RTContext context = new RTContext(parser); |
---|
149 | CredentialStore store = new CredentialStore(parser); |
---|
150 | |
---|
151 | parser.parseCredentialStore(new FileInputStream(new File(argv[0])), |
---|
152 | context, store); |
---|
153 | CredentialDomain domain = |
---|
154 | store.getCredentialDomain(System.getProperty( |
---|
155 | "com.nailabs.abac.credential.Hash", "FakeHashCredential01")); |
---|
156 | convert(domain); |
---|
157 | } catch(Exception ex) { |
---|
158 | ex.printStackTrace(); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|