[8780cbec] | 1 | package com.nailabs.abac.credential; |
---|
| 2 | |
---|
| 3 | import com.nailabs.abac.test.RtmlTest; |
---|
| 4 | |
---|
| 5 | import edu.stanford.rt.credential.CredentialDomain; |
---|
| 6 | import edu.stanford.rt.credential.CredentialStore; |
---|
| 7 | import edu.stanford.rt.credential.HashID; |
---|
| 8 | import edu.stanford.rt.credential.RTContext; |
---|
| 9 | |
---|
| 10 | import edu.stanford.rt.parser.*; |
---|
| 11 | import edu.stanford.peer.rbtm.credential.*; |
---|
| 12 | import edu.stanford.peer.rbtm.util.ResultEvidenceMap; |
---|
| 13 | |
---|
| 14 | import java.io.*; |
---|
| 15 | import java.net.*; |
---|
| 16 | import java.util.*; |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * A distributed discovery engine based on the RTML-aware GraphEngine. |
---|
| 20 | */ |
---|
| 21 | public class DDEngine extends RtmlEngine { |
---|
| 22 | |
---|
| 23 | protected RTParser parser; |
---|
| 24 | |
---|
| 25 | protected RTContext context; |
---|
| 26 | |
---|
| 27 | protected Properties lookupMap = new Properties(); |
---|
| 28 | |
---|
| 29 | static String PORT_PROPERTY = "com.nailabs.abac.discovery.port"; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * The default constructor grabs parameters from the configuration |
---|
| 33 | * HashMap, including [Parser] and [PrepInfo] sections. |
---|
| 34 | */ |
---|
| 35 | public DDEngine(HashMap config) { |
---|
| 36 | super(); |
---|
| 37 | parser = (RTParser)config.get("DDEParser"); |
---|
| 38 | context = (RTContext)config.get("DDEContext"); |
---|
| 39 | lookupMap = (Properties)config.get("PrepInfo"); |
---|
| 40 | store = (CredentialStore)config.get("DDEStore"); |
---|
| 41 | try { |
---|
| 42 | if(context == null) { |
---|
| 43 | context = new RTContext(parser); |
---|
| 44 | } |
---|
| 45 | if(store == null) { |
---|
| 46 | store = (CredentialStore)config.get("CredentialStore"); |
---|
| 47 | } |
---|
| 48 | if(store == null) { |
---|
| 49 | store = new CredentialStore(parser); |
---|
| 50 | } |
---|
| 51 | } catch (Exception ex) { |
---|
| 52 | ex.printStackTrace(); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | protected URL createURL(String host, String operation, String expression) { |
---|
| 57 | StringBuffer buff = new StringBuffer("http://"); |
---|
| 58 | buff.append(host).append("?"); |
---|
| 59 | buff.append(operation).append("=").append(expression); |
---|
| 60 | try { |
---|
| 61 | System.out.println("Constructing url = " + buff); |
---|
| 62 | return new URL(buff.toString()); |
---|
| 63 | } |
---|
| 64 | catch(Exception ex) { |
---|
| 65 | ex.printStackTrace(); |
---|
| 66 | } |
---|
| 67 | return null; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | protected Vector parseOutput(URL url) { |
---|
| 72 | Vector documents = new Vector(); |
---|
| 73 | StringWriter out = null; |
---|
| 74 | |
---|
| 75 | try { |
---|
| 76 | URLConnection connection = url.openConnection(); |
---|
| 77 | InputStream in = connection.getInputStream(); |
---|
| 78 | BufferedReader buffedReader |
---|
| 79 | = new BufferedReader(new InputStreamReader(in)); |
---|
| 80 | out = new StringWriter(); |
---|
| 81 | // grab the first line so we don't have an empty vector item |
---|
| 82 | out.write(buffedReader.readLine()); |
---|
| 83 | while(true) { |
---|
| 84 | String line = buffedReader.readLine(); |
---|
| 85 | if(line == null)break; |
---|
| 86 | if(line.startsWith("<?xml")) { |
---|
| 87 | documents.add(out.getBuffer().toString()); |
---|
| 88 | out = new StringWriter(); |
---|
| 89 | } |
---|
| 90 | out.write(line); |
---|
| 91 | out.write("\n"); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | } catch(Exception ex) { |
---|
| 95 | //ex.printStackTrace(); |
---|
| 96 | } |
---|
| 97 | if(out != null) { |
---|
| 98 | documents.add(out.getBuffer().toString()); |
---|
| 99 | } |
---|
| 100 | for(int cnt = 0; cnt < documents.size(); cnt++) { |
---|
| 101 | try { |
---|
| 102 | String xml = (String)documents.get(cnt); |
---|
| 103 | if(xml.equals("null")) { |
---|
| 104 | documents.remove(cnt--); |
---|
| 105 | continue; |
---|
| 106 | } |
---|
| 107 | System.out.println("---------begin xml-------"); |
---|
| 108 | System.out.println(xml); |
---|
| 109 | System.out.println("----------end xml--------"); |
---|
| 110 | InputStream in = new ByteArrayInputStream(xml.getBytes()); |
---|
| 111 | documents.set |
---|
| 112 | (cnt, (Object) parser.parseCredentialDomain(in,context)); |
---|
| 113 | } catch(Exception ex) { |
---|
| 114 | ex.printStackTrace(); |
---|
| 115 | documents.remove(cnt--); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | return documents; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | protected String getLocation(EntityExpression expr) { |
---|
| 122 | String base; |
---|
| 123 | |
---|
| 124 | if(expr instanceof Entity) { |
---|
| 125 | base = expr.toString(); |
---|
| 126 | } else if(expr instanceof edu.stanford.peer.rbtm.credential.Role) { |
---|
| 127 | base = |
---|
| 128 | ((edu.stanford.peer.rbtm.credential.Role)expr).getBase().toString(); |
---|
| 129 | } else if(expr instanceof edu.stanford.peer.rbtm.credential.LinkedRole) |
---|
| 130 | { |
---|
| 131 | base = ((edu.stanford.peer.rbtm.credential.LinkedRole)expr).getFirstRole().getBase().toString(); |
---|
| 132 | } else if(expr instanceof Intersection) { |
---|
| 133 | base = getLocation((EntityExpression) |
---|
| 134 | ((Intersection)expr).getParts().next()); |
---|
| 135 | } else { |
---|
| 136 | base = new String(); |
---|
| 137 | } |
---|
| 138 | System.out.println("getting url for " + base); |
---|
| 139 | return lookupMap.getProperty(base); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | protected Vector discover(String operation, EntityExpression expr) { |
---|
| 143 | URL url = createURL(getLocation(expr), operation, |
---|
| 144 | RtmlExpression.toString(expr)); |
---|
| 145 | Vector v = parseOutput(url); |
---|
| 146 | Vector results = new Vector(v.size()); |
---|
| 147 | Iterator domains = v.iterator(); |
---|
| 148 | while(domains.hasNext()) { |
---|
| 149 | CredentialDomain domain = (CredentialDomain)domains.next(); |
---|
| 150 | HashID hash = domain.getHashID(); |
---|
| 151 | try { |
---|
| 152 | addDomain(domain); |
---|
| 153 | Iterator creds = RtmlEngine.convert(domain).iterator(); |
---|
| 154 | while(creds.hasNext()) { |
---|
| 155 | RtmlCredential cred = (RtmlCredential)creds.next(); |
---|
| 156 | results.add(cred); |
---|
| 157 | System.out.println("-----+Adding " + cred); |
---|
| 158 | } |
---|
| 159 | } catch(Exception ex) { |
---|
| 160 | System.err.println("Error with credential domain " + |
---|
| 161 | domain.getHashID()); |
---|
| 162 | ex.printStackTrace(); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | return results; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | synchronized public Iterator |
---|
| 169 | findCredentialsDefiningRole(edu.stanford.peer.rbtm.credential.Role r) { |
---|
| 170 | return discover("Defines", r).iterator(); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | synchronized public Collection getCredentialsIssuedBy(Entity e) { |
---|
| 174 | return discover("Issuer", e); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | synchronized public Iterator findCredentialsBySubject(EntityExpression s) { |
---|
| 178 | return discover("Subject", s).iterator(); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | public static void main(String[] args) { |
---|
| 182 | if(System.getProperty("edu.stanford.rt.SystemDomain") == null) { |
---|
| 183 | System.setProperty("edu.stanford.rt.SystemDomain", |
---|
| 184 | "../rtml/schemas/SystemSpec.xml"); |
---|
| 185 | } |
---|
| 186 | HashMap conf = new HashMap(6); |
---|
| 187 | Properties prepInfo = new Properties(); |
---|
| 188 | prepInfo.setProperty("SE", |
---|
| 189 | "wwinsbor1.rv.nailabs.com:8180/demo/se-discovery"); |
---|
| 190 | prepInfo.setProperty("SAdmir!FakeHashKeySAdmir", |
---|
| 191 | "wwinsbor1.rv.nailabs.com:8180/demo/se-discovery"); |
---|
| 192 | prepInfo.setProperty("US", |
---|
| 193 | "wwinsbor1.rv.nailabs.com:8180/demo/us-discovery"); |
---|
| 194 | prepInfo.setProperty("USEUCOM", |
---|
| 195 | "wwinsbor1.rv.nailabs.com:8180/demo/us-discovery"); |
---|
| 196 | prepInfo.setProperty("USSH!FakeHashKeyUSSH", |
---|
| 197 | "wwinsbor1.rv.nailabs.com:8180/demo/us-discovery"); |
---|
| 198 | prepInfo.setProperty("USN", |
---|
| 199 | "wwinsbor1.rv.nailabs.com:8180/demo/us-discovery"); |
---|
| 200 | prepInfo.setProperty("RADMSmith", |
---|
| 201 | "wwinsbor1.rv.nailabs.com:8180/demo/us-discovery"); |
---|
| 202 | conf.put("PrepInfo", prepInfo); |
---|
| 203 | Properties rtml = new Properties(System.getProperties()); |
---|
| 204 | if(rtml.getProperty("DDEStore") == null) { |
---|
| 205 | rtml.setProperty("DDEStore", "test/rtml/dde-client.xml"); |
---|
| 206 | } |
---|
| 207 | conf.put("RTML", rtml); |
---|
| 208 | RtmlTest.parseDiscoveryStore(conf); |
---|
| 209 | try { |
---|
| 210 | DDEngine engine = new DDEngine(conf); |
---|
| 211 | ResultEvidenceMap map = (ResultEvidenceMap) |
---|
| 212 | engine.backwardSearch(new Role("SAdmir!FakeHashKeySAdmir", |
---|
| 213 | "getsSLocs")); |
---|
| 214 | Collection v = map.resultSet(); |
---|
| 215 | for(Iterator i = v.iterator(); i.hasNext(); ) { |
---|
| 216 | System.out.println("--------------------"); |
---|
| 217 | Object result = i.next(); |
---|
| 218 | System.out.println(result.toString()); |
---|
| 219 | System.out.println(map.getResultEvidence(result).toString()); |
---|
| 220 | } |
---|
| 221 | System.out.println("--------------------"); |
---|
| 222 | map = (ResultEvidenceMap) |
---|
| 223 | engine.forwardSearch(new SimpleEntity("USSH!FakeHashKeyUSSH")); |
---|
| 224 | //engine.forwardSearch(new SimpleEntity("SAdmir!FakeHashKeySAdmir")); |
---|
| 225 | //engine.forwardSearch(new Role("US!FakeHashKeyUS","usr")); |
---|
| 226 | |
---|
| 227 | v = map.resultSet(); |
---|
| 228 | for(Iterator i = v.iterator(); i.hasNext(); ) { |
---|
| 229 | System.out.println("--------------------"); |
---|
| 230 | Object result = i.next(); |
---|
| 231 | System.out.println(result.toString()); |
---|
| 232 | System.out.println(map.getResultEvidence(result).toString()); |
---|
| 233 | } |
---|
| 234 | } catch(Exception ex) { |
---|
| 235 | ex.printStackTrace(); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|