source: java/net/deterlab/abac/CredentialGraph.java @ d4bcf94

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since d4bcf94 was d4bcf94, checked in by Mike Ryan <mikeryan@…>, 13 years ago

add a few missing things required by crudge

  • Property mode set to 100644
File size: 6.2 KB
Line 
1package net.deterlab.abac;
2
3import org.apache.commons.collections15.*;
4import org.apache.commons.collections15.functors.*;
5
6import edu.uci.ics.jung.graph.*;
7import edu.uci.ics.jung.graph.event.*;
8import edu.uci.ics.jung.graph.util.*;
9
10import java.awt.geom.Point2D;
11import java.io.*;
12import java.util.*;
13
14/**
15 * Represents a global graph of credentials in the form of principals and
16 * attributes.
17 */
18public class CredentialGraph {
19    protected Graph<Role,Credential> g = null;
20    protected java.util.Set<Credential> derived_edges = new HashSet<Credential>();
21    protected Query pq;
22    protected boolean m_dirty;
23
24    public CredentialGraph() {
25        /* create the graph */
26        g = Graphs.<Role,Credential>synchronizedDirectedGraph(
27                new DirectedSparseGraph<Role,Credential>());
28        pq = new Query(g);
29    }
30
31    /**
32     * Returns a Query object which can be used to query the graph. This object
33     * becomes invalid if the graph is modified.
34     */
35    public Query querier() {
36        derive_implied_edges();
37        return new Query(g);
38    }
39
40    /**
41     * Add a role to the graph./
42     */
43    public void add_vertex(Role role) {
44        g.addVertex(role);
45    }
46
47    /**
48     * Remove a role from the grpah if it exists.
49     */
50    public void remove_vertex(Role role) {
51        g.removeVertex(role);
52    }
53
54    /**
55     * Get a list of all roles known to the graph.
56     */
57    public Collection<Role> roles() {
58        return g.getVertices();
59    }
60
61    /**
62     * Add a credential to the graph.
63     */
64    public void add_credential(Credential cred) {
65        Role tail = cred.tail();
66        Role head = cred.head();
67
68        /* explicitly add the vertices, otherwise get a null pointer exception */
69        g.addVertex(tail);
70        g.addVertex(head);
71
72        g.addEdge(cred, tail, head);
73
74        // add the prereqs of an intersection to the graph
75        if (tail.is_intersection())
76            for (Role prereq : tail.prereqs())
77                g.addVertex(prereq);
78
79        m_dirty = true;
80    }
81
82    /**
83     * Remove a credential from the graph.
84     */
85    public void remove_credential(Credential cred) {
86        if (g.containsEdge(cred))
87            g.removeEdge(cred);
88        m_dirty = true;
89    }
90
91    /**
92     * Returns a collection of the credentials in the graph.
93     */
94    public Collection<Credential> credentials() {
95        Collection<Credential> creds = new HashSet<Credential>();
96
97        // only return creds with a cert: all others are derived edges
98        for (Credential cred : g.getEdges())
99            if (cred.cert() != null)
100                creds.add(cred);
101
102        return creds;
103    }
104
105    /**
106     * Derive the implied edges in the graph, according to RT0 derivation rules.
107     * They are added to this graph. See "Distributed Credential Chain Discovery
108     * in Trust Management" by Ninghui Li et al. for details. Note that a
109     * derived linking edge can imply a new intersection edge and vice versa.
110     * Therefore we iteratively derive edges, giving up when an iteration
111     * produces 0 new edges.
112     */
113    protected synchronized void derive_implied_edges() {
114        // nothing to do on a clean graph
115        if (!m_dirty)
116            return;
117
118        clear_old_edges();
119
120        // iteratively derive links. continue as long as new links are added
121        while (derive_links_iter() > 0)
122            ;
123        m_dirty = false;
124    }
125
126    /**
127     * Single iteration of deriving implied edges. Returns the number of new
128     * links added.
129     */
130    protected int derive_links_iter() {
131        int count = 0;
132
133        /* for every node in the graph.. */
134        for (Role vertex : g.getVertices()) {
135            if (vertex.is_intersection()) {
136                // for each prereq edge:
137                //     find set of principals that have the prereq
138                // find the intersection of all sets (i.e., principals that satisfy all prereqs)
139                // for each principal in intersection:
140                //     add derived edge
141
142                Set<Role> principals = null;
143
144                for (Role prereq : vertex.prereqs()) {
145                    Set<Role> cur_principals = pq.find_principals(prereq);
146
147                    if (principals == null)
148                        principals = cur_principals;
149                    else
150                        // no, they couldn't just call it "intersection"
151                        principals.retainAll(cur_principals);
152
153                    if (principals.size() == 0)
154                        break;
155                }
156
157                // add em
158                for (Role principal : principals)
159                    if (add_derived_edge(vertex, principal))
160                        ++count;
161            }
162
163            else if (vertex.is_linking()) {
164                // make the rest of the code a bit clearer
165                Role A_r1_r2 = vertex;
166
167                Role A_r1 = new Role(A_r1_r2.A_r1());
168                String r2 = A_r1_r2.r2();
169
170                /* locate the node A.r1 */
171                if (!g.containsVertex(A_r1)) continue; /* boring: nothing of the form A.r1 */
172
173                /* for each B that satisfies A_r1 */
174                for (Role principal : pq.find_principals(A_r1)) {
175                    Role B_r2 = new Role(principal + "." + r2);
176                    if (!g.containsVertex(B_r2)) continue;
177
178                    if (add_derived_edge(A_r1_r2, B_r2))
179                        ++count;
180                }
181            }
182        }
183
184        return count;
185    }
186
187    /**
188     * Add a derived edge in the graph. Returns true only if the edge does not
189     * exist.
190     */
191    protected boolean add_derived_edge(Role head, Role tail) {
192        // edge exists: return false
193        if (g.findEdge(tail, head) != null)
194            return false;
195
196        // add the new edge
197        Credential derived_edge = new Credential(head, tail);
198        derived_edges.add(derived_edge);
199        g.addEdge(derived_edge, tail, head);
200
201        return true;
202    }
203
204    /**
205     * Clear the derived edges that currently exist in the graph. This is done
206     * before the edges are rederived. The derived edges in filtered graphs are
207     * also cleared.
208     */
209    protected void clear_old_edges() { 
210        for (Credential i: derived_edges) 
211            g.removeEdge(i);
212        derived_edges = new HashSet<Credential>();
213    }
214}
Note: See TracBrowser for help on using the repository browser.