Module: AdGear::Infrastructure::GroupManager::Utils

Includes:
Config, Logging
Included in:
App, LDAP
Defined in:
lib/utils.rb

Overview

The global utils container. Used for storing common stuff.

Since:

  • 0.1.0

Constant Summary

Constants included from Logging

Logging::Log

Constants included from Config

Config::GLOBAL_CONFIG

Class Method Summary collapse

Methods included from Logging

fatal

Methods included from Config

list_all_groups, list_func_groups, list_locations, list_org_groups, list_perm_groups, list_users

Class Method Details

.compare_attributes(local, remote) ⇒ Object

A helper method that compares two objects, accounting for LDAP idiosyncracies.

Since:

  • 0.1.0



136
137
138
# File 'lib/utils.rb', line 136

def compare_attributes(local, remote)
  (local == [] ? nil : local) != remote
end

.create_ops_list(local_groups, remote_groups) ⇒ Object

Creates the list of operations to perform to synchronize local with remote.

Since:

  • 0.1.0



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/utils.rb', line 94

def create_ops_list(local_groups, remote_groups)
  operations = {
    create: [],
    modify: [],
    delete: []
  }

  local_groups.each do |gr, vals|
    # if remote group exists, diff each attribute
    if remote_groups.key?(gr)
      vals.keys.each do |key|
        message = {
          msg: "diffing local and remote instances of #{gr}",
          cn: gr,
          attrib: key,
          local: local_groups[gr][key],
          remote: remote_groups.dig(gr, key),
          require_change: compare_attributes(local_groups[gr][key], remote_groups.dig(gr, key))
        }
        Log.debug(message) if message[:require_change]
        if compare_attributes(local_groups[gr][key], remote_groups.dig(gr, key))
          operations[:modify] << { cn: gr, attrib: key, value: local_groups[gr][key] }
        end
      end
    else
      # if remote group doesn't exist create key, then modify
      operations[:create] << gr
      vals.keys.each do |key|
        operations[:modify] << { cn: gr, attrib: key, value: local_groups[gr][key] }
      end
    end
  end

  # delete remote groups that aren't locally described
  (remote_groups.keys - local_groups.keys).each { |gr| operations[:delete] << gr }

  # return stuff to do
  operations
end

.diff_op_exist?(ops, type, target) ⇒ Boolean

I don’t remember what this does

Returns:

  • (Boolean)

Since:

  • 0.1.0



38
39
40
# File 'lib/utils.rb', line 38

def diff_op_exist?(ops, type, target)
  ops.select { |op| op[0] == type && op[1] == target }.any?
end

.duplicate?(ops, item) ⇒ Boolean

Checks if the reverse operation exists

Returns:

  • (Boolean)

Since:

  • 0.1.0



44
45
46
47
48
49
50
51
52
# File 'lib/utils.rb', line 44

def duplicate?(ops, item)
  opposite = case item[0]
             when '-'
               '+'
             when '+'
               '-'
             end
  ops.select { |op| op[0] == opposite && op[1] == item[1] && op[2] == item[2] }.any?
end

.find_ou(cn) ⇒ Object

Finds in which OU a given CN can be found.

Since:

  • 0.1.0



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/utils.rb', line 56

def find_ou(cn)
  if GLOBAL_CONFIG[:data][:organizational].key?(cn)
    'OU=Organizational, OU=Keycloak Groups'
  elsif GLOBAL_CONFIG[:data][:functional].key?(cn)
    'OU=Functional, OU=Keycloak Groups'
  elsif GLOBAL_CONFIG[:data][:permissions].key?(cn)
    'OU=Permission, OU=Keycloak Groups'
  elsif GLOBAL_CONFIG[:data][:locations].key?(cn)
    'OU=Location, OU=Keycloak Groups'
  else
    'OU=Keycloak Users'
  end
end

.sort_member(all_groups) ⇒ Object

Sorts the member array bundle of groups.

Since:

  • 0.1.0



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/utils.rb', line 72

def sort_member(all_groups)
  ordered_groups = {}
  all_groups.keys.sort.each do |group|
    ordered_groups[group] = {}
    begin
      all_groups[group].sort.map { |k, v| ordered_groups[group][k] = v }
    rescue => e
      Log.debug(group)
      Log.debug(all_groups[group])
      Log.error(e)
      exit(1)
    end
    unless !all_groups[group].key?(:member) || all_groups[group][:member].nil?
      ordered_groups[group].delete(:member)
      ordered_groups[group][:member] = all_groups[group][:member].sort
    end
  end
  ordered_groups
end

.stringify_all_keys(hash) ⇒ Object

A helper method that converts all symbol keys to string keys.

Since:

  • 0.1.0



16
17
18
19
20
21
22
# File 'lib/utils.rb', line 16

def stringify_all_keys(hash)
  stringified_hash = {}
  hash.each do |k, v|
    stringified_hash[k.to_s] = v.is_a?(Hash) ? stringify_all_keys(v) : v
  end
  stringified_hash
end

.symbolify_all_keys(hash) ⇒ Object

A helper method that converts all string keys to symbol keys.

Since:

  • 0.1.0



26
27
28
29
30
31
32
33
34
# File 'lib/utils.rb', line 26

def symbolify_all_keys(hash)
  Log.debug(msg: 'symbolifiying', data: hash)
  symbolified = {}
  hash.keys.each do |k|
    newkey = %w[member description].include?(k) ? k.to_sym : k
    symbolified[newkey] = hash[k].is_a?(Hash) ? symbolify_all_keys(hash[k]) : hash[k]
  end
  symbolified
end