Class: Conjur::Graph

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/conjur/graph.rb

Overview

A Graph represents a directed graph of roles.

An instance of this class is returned by API#role_graph.

Examples:

Graphs act like arrays of edges

graph.each do |edge|
  puts "#{edge.parent} -> #{edge.child}"
end
# role1 -> role2
# role2 -> role3

Defined Under Namespace

Classes: Edge

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#edgesArray<Conjur::Graph::Edge> (readonly)

Returns an array containing the directed edges of the graph.

Returns:



41
42
43
# File 'lib/conjur/graph.rb', line 41

def edges
  @edges
end

Instance Method Details

#as_json(short = false) ⇒ Hash, Array

Convert the graph to a JSON serializable data structure. The value returned by this method can have two forms: An array of arrays when short is true, or hash like { 'graph' => [ {'parent' => 'roleid', 'child' => 'roleid'} ]} otherwise.

Examples:

Graph formats

graph = api.role_graph 'conjur:group:pubkeys-1.0/key-managers'

# Short format
graph.as_json true
#  => [
#  ["conjur:group:pubkeys-1.0/key-managers", "conjur:group:pubkeys-1.0/admin"],
#  ["conjur:group:pubkeys-1.0/admin", "conjur:user:admin"]
# ]

# Default format (you can omit the false parameter in this case)
graph.as_json false
# => {
#  "graph" => [
#    {"parent"=>"conjur:group:pubkeys-1.0/key-managers", "child"=>"conjur:group:pubkeys-1.0/admin"},
#    {"parent"=>"conjur:group:pubkeys-1.0/admin", "child"=>"conjur:user:admin"}
#  ]
#}

Parameters:

  • short (Boolean) (defaults to: false)

    whether to use short of default format

Returns:

  • (Hash, Array)

    JSON serializable representation of the graph



112
113
114
115
# File 'lib/conjur/graph.rb', line 112

def as_json short = false
  edges = self.edges.map{|e| e.as_json(short)}
  short ? edges : {'graph' => edges}
end

#each_edge {|edge| ... } ⇒ Conjur::Graph Also known as: each

Enumerates the edges of this graph.

Yield Parameters:

Returns:



62
63
64
65
66
# File 'lib/conjur/graph.rb', line 62

def each_edge
  return enum_for(__method__) unless block_given?
  edges.each{|e| yield e}
  self
end

#each_vertex {|vertex| ... } ⇒ Conjur::Graph

Enumerates the vertices (roles) of this graph

Yield Parameters:

Returns:



73
74
75
76
# File 'lib/conjur/graph.rb', line 73

def each_vertex
  return enum_for(__method__) unless block_given?
  vertices.each{|v| yield v}
end

#to_dot(name = nil) ⇒ String

Returns a string formatted for use by the graphviz dot tool.

Parameters:

  • name (String, NilClass) (defaults to: nil)

    An identifier to assign to the graph. This can be omitted unless you are writing multiple graphs to a single file. This must be in the ID format specified by http://www.graphviz.org/content/dot-language.

Returns:

  • (String)

    the dot format (used by graphviz, among others) representation of this graph.



124
125
126
127
128
129
130
131
132
133
# File 'lib/conjur/graph.rb', line 124

def to_dot name = nil
  dot = "digraph #{name || ''} {"
  vertices.each do |v|
    dot << "\n\t" << dot_node(v)
  end
  edges.each do |e|
    dot << "\n\t" << dot_edge(e)
  end
  dot << "\n}"
end

#to_json(short = false) ⇒ String

Serialize the graph as JSON

Parameters:

  • short (Boolean) (defaults to: false)

    when true, the graph is serialized as an array of arrays instead of an array of hashes.

Returns:

  • (String)

    the JSON serialized graph.

See Also:



83
84
85
# File 'lib/conjur/graph.rb', line 83

def to_json short =  false
  as_json(short).to_json
end

#verticesArray<Conjur::Role> Also known as: roles

Return the vertices (roles) of the graph as an array.

Returns:



137
138
139
# File 'lib/conjur/graph.rb', line 137

def vertices
  @vertices ||= edges.inject([]) {|a, pair| a.concat pair.to_a }.uniq
end