class Ucert::EquinixTracker

Class to handle Equinix user entitlement report

Attributes

equinix_2_ad_user[R]
equinix_user_entitlement[R]
equinix_user_status_report[RW]

Class constant variables

file_equinix_user_map[RW]

Class constant variables

verbose[RW]

Class constant variables

Public Class Methods

new(params ={}) click to toggle source

Instance default variables

# File lib/ucert/equinix_tracker.rb, line 20
def initialize (params ={})
  @verbose=params.fetch(:verbose, false)
  # Equinix user entitlement report generation:
              # Contact IT/GO for the Secured Access List report
  @equinix_user_entitlement_report = File.dirname(__FILE__)+"/../../data/equinix/Secured Access List_CHINA MERCHANTS BANK.xlsx"
  # Equinix to AD user map file
  @file_equinix_user_map =  File.dirname(__FILE__)+"/../../data/equinix/equinix_user_map.txt"
              @equinix_user_entitlement = Hash.new
              # Load user map from the local cacsh file
              @equinix_2_ad_user=load_known_user_map_from_file(@file_equinix_user_map)
              # Load the user entitlement instance variable from the user report
              parse_equinix_user_entitlement_report(@equinix_user_entitlement_report)
              # Procedure to add DN foreign key to the @equinix_user_entitlement, by performing the AD search
              insert_dn
              # Save the user map to local cache file
              save!
      end

Public Instance Methods

be_search_by_dn(dn) click to toggle source

Search user entitlement record by AD DN

# File lib/ucert/equinix_tracker.rb, line 187
    def be_search_by_dn (dn)
            begin
  puts "Perform search on the user entitlement records by AD DN: #{dn}" if @verbose
  @equinix_user_entitlement.each do |key, val|
      return val if @equinix_user_entitlement[key]['DN'].eql? dn
  end
                    return nil
rescue => ee
  puts "Exception on method #{__method__}: #{ee}"
end
    end
Also aliased as: search_by_dn
dn_2_index(dn) click to toggle source

Retrieve the user index from the @equinix_user_entitlement data structure

# File lib/ucert/equinix_tracker.rb, line 100
def dn_2_index (dn)
                begin
(1..@equinix_user_entitlement.count).map do |index|
  return index if @equinix_user_entitlement[index]["DN"]==dn
end
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                end
end
print_user()
save!(file=@file_equinix_user_map)
search_by_dn(dn)
Alias for: be_search_by_dn

Private Instance Methods

insert_dn() click to toggle source

Procedures to add additonal field 'dn' into the @equinix_user_entitlement data structure, by person the AD search

# File lib/ucert/equinix_tracker.rb, line 111
def insert_dn
                begin
                        tracker = Ucert::AdTracker.new(:verbose=>false)
                         @equinix_user_entitlement.each do |index, record|
                                puts "\n\nPerform DN lookup for record: #{record}" if @verbose
                                key1 = record['Username:'] if record['Username:']
                                key2 = record['First Name:'] + " " + record['Last Name:'] if record['First Name:'] and record['Last Name:']
                                key3 = record['First Name'] if record['First Name']
                                my_key = record['Username:'].upcase + ":" + record['First Name:'].upcase
                                puts "Perform 1st order search from the local cache: #{my_key}" if @verbose
                                if @equinix_2_ad_user.key?(my_key)
                                        dn=@equinix_2_ad_user[my_key]
                                        # additional logic to update the existing DN record
                                        unless tracker.ad_person_records.key?(dn)
                                                dn = update_dn(tracker,dn)
                                        end
                                        puts "Found in the local cache file: #{dn}" if @verbose
                                else
                                        if dn.nil? and !key1.nil?
                                                puts "Perform 2nd order search only if the 1st one fail, by using: #{key1}" if @verbose
                                                dn = tracker.ad_search_by_text(key1, "person")
                                        end
                                        if dn.nil? and !key2.nil?
                                                puts "Perform 3rd order search only if the last fail, by using: #{key2}" if @verbose
                                                dn = tracker.ad_search_by_text(key2, "person")
                                        end
                                        if dn.nil? and !key3.nil?
                                                puts "Perform 4th order search only if the last fail, by using: #{key3}" if @verbose
                                                dn = tracker.ad_search_by_text(key3, "person")
                                        end
                                end
                                @equinix_user_entitlement[index]['DN'] = dn
                        end
                        tracker=nil
                rescue => ee
                        puts "Exception on method #{__method__}: #{ee}"
                end
end
parse_equinix_user_entitlement_report(file) click to toggle source

Parsing the Equinix user entitlement report in text format

# File lib/ucert/equinix_tracker.rb, line 39
def parse_equinix_user_entitlement_report (file)
        begin
                puts "Start parsing Excel workbook file: #{file}" if @verbose
                equinix_user_entitlement=Hash.new
                workbook = RubyXL::Parser.parse(file)
                workbook.worksheets.each do |worksheet|
                        parse_equinix_worksheet(worksheet)
                end
                workbook=nil
        rescue => ee
                puts "Exception on method #{__method__}: #{ee}"
        end
end
parse_equinix_worksheet(worksheet) click to toggle source

Parsing the Equinix user report individual worksheet

# File lib/ucert/equinix_tracker.rb, line 54
  def parse_equinix_worksheet (worksheet)
begin
                          puts "Parsing worksheet: #{worksheet.sheet_name}" if @verbose
                          row_cnt=0
                          user_index=0
                          header=Array.new
                          worksheet.count.times  do |row|
                                  row_cnt+=1
                                  puts "Parsing workbook row: #{row_cnt}" if @verbose
                                  entry=Array.new
                                  # Processing Header Row
                                  if row_cnt==1
                                          0.upto(worksheet[row].size) do |col|
                                                  if worksheet[row][col].nil?
                                                          header.push(nil)
                                                  else
                                                          header.push(worksheet[row][col].value.to_s)
                                                  end
                                          end
                                          next
                                  else
                                          0.upto(worksheet[row].size) do |col|
                                                  if worksheet[row][col].nil?
                                                          entry.push(nil)
                                                  else
                                                          entry.push(worksheet[row][col].value.to_s)
                                                  end
                                          end
                                  end
                                  puts "Entry: #{entry}" if @verbose
                                  record = header.zip(entry) #.to_h.reject {|k,v| k.nil? or k.empty?}
                                  record_h = record.shift(4).to_h
                                  puts "User record header: #{record_h}" if @verbose
                                  entitlement = record.to_h.reject {|k,v| k.nil? or k.empty?}
                                  puts "User entitlement: #{entitlement}" if @verbose
                                  user_index = user_index + 1
                                  @equinix_user_entitlement[user_index] = record_h unless @equinix_user_entitlement.key?(user_index)
                                  @equinix_user_entitlement[user_index]["entitlements"]=Array.new unless @equinix_user_entitlement[user_index]["entitlements"]
                                  @equinix_user_entitlement[user_index]["entitlements"].push(entitlement)
                          end
                  rescue => ee
                          puts "Exception on method #{__method__}: #{ee}"
                  end
  end
print_user_entitlement() click to toggle source

Print out the user entitlement table in plain text, to be imported into database

Also aliased as: print_user
save_equinix_user_map!(file=@file_equinix_user_map) click to toggle source

Save the Equinix to AD user mapping relation into the cache file

# File lib/ucert/equinix_tracker.rb, line 167
def save_equinix_user_map!(file=@file_equinix_user_map)
        puts "Saving the known Equinix to AD user mapping relationship to file: #{file} ..." if @verbose
        begin
                timestamp=Time.now
                f=File.open(file, 'w')
                f.write "# Equinix to AD user map file created by the #{self.class} class #{__method__} method at: #{timestamp}"
                @equinix_user_entitlement.values.map do |record|
                        key = record['Username:'].upcase + ":" + record['First Name:'].upcase
                        value = record['DN']
                        f.write "\n#{key}|#{value}"
                end
                f.close
                puts "Equinix to AD user map file is successfully saved to: #{file}" if @verbose
        rescue => ee
                puts "Exception on method #{__method__}: #{ee}" if @verbose
        end
end
Also aliased as: save!