source: java/net/deterlab/abac/CredentialGraph.java @ 90f939f

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

load Credential from attribute cert

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