Module: WGU::ThycoticComms

Defined in:
lib/pps_commons/thycotic_comms.rb

Class Method Summary collapse

Class Method Details

.folder_lookup(*args) ⇒ Object

find a good way to remove this hardcoding of 545 parent folder id



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pps_commons/thycotic_comms.rb', line 41

def self.folder_lookup(*args)
  base_folder_id = ENV['SECRET_STORE_BASE_FOLDER']
  return base_folder_id if args.empty?
  query = args[0] || base_folder_id
  parent_folder_query = args[1]

  params = if query.to_s[/^\d+$/]
    ["api/v1/folders/#{query}"]
  else
    params_hash = { 'filter.searchText' => query }

    unless parent_folder_query.nil?
      params_hash['filter.parentFolder'] = parent_folder_query
    end

    ["api/v1/folders/lookup", params_hash]
  end

  self.get(*params)['records']
end

.get(url, params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pps_commons/thycotic_comms.rb', line 8

def self.get(url, params = {})
  res = nil
  retry_count = 0
  refresh_token = self.get_api_key
  query_url = "#{url}?" + params.map { |k,v| "#{k}=#{v}&" }.join.gsub(/\&$/, '')
  query_url = self.ss_url(query_url)
  begin
    RestClient.get(query_url,{ Authorization: "Bearer #{refresh_token}" })
    res = RestClient.get(
      query_url,
      { Authorization: "Bearer #{refresh_token}" }
    )
  rescue RestClient::Unauthorized => e
    retry_count += 1
    refresh_token = self.update_tmp_pass!
    retry unless retry_count > 1
    raise e
  end

  JSON.parse(res.body)
end

.get_secret(query, folder_name) ⇒ Object



30
31
32
33
# File 'lib/pps_commons/thycotic_comms.rb', line 30

def self.get_secret(query, folder_name)
  secret_id_res = self.secret_lookup(query, folder_name)
  secret_id_res.empty? ? { 'items' => [] } : self.secret_lookup(secret_id_res.first['id'])
end

.getter(term, body) ⇒ Object



35
36
37
38
# File 'lib/pps_commons/thycotic_comms.rb', line 35

def self.getter(term, body)
  tupple = body['items'].find { |field| field['fieldName'][/^#{term}$/i] }
  tupple['itemValue'] unless tupple.nil?
end

.secret_lookup(secret_query, folder_query = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pps_commons/thycotic_comms.rb', line 62

def self.secret_lookup(secret_query, folder_query = nil)
  folder_id = if folder_query.to_s[/^\d+$/]
    folder_query
  else
    res = self.folder_lookup(folder_query)
    res.nil? ? self.folder_lookup : res.first['id']
  end

  secret_params = if secret_query.to_s[/^\d+$/]
    ["api/v1/secrets/#{secret_query}"]
  else
    params_hash = if secret_query.kind_of?(String)
      {
        'filter.folderId' => folder_id,
        'filter.searchText' => secret_query
      }
    else
      { 'filter.parentFolderId' => folder_id }.merge(secret_query)
    end

    ['api/v1/secrets/lookup', params_hash]
  end

  result = self.get(*secret_params)
  result.include?('records') ? result['records'] : result
end