Class: Zanzibar::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zanzibar/client.rb

Overview

Class for performing low-level WSDL actions against Secret Server

Instance Method Summary collapse

Constructor Details

#initialize(username, password, domain, wsdl, globals = {}) ⇒ Client

Initializes the Savon client class variable with the wdsl document location and optional global variables

Parameters:

  • globals{},

    optional



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zanzibar/client.rb', line 14

def initialize(username, password, domain, wsdl, globals = {})
  @username = username
  @password = password
  @domain = domain

  globals = {} if globals.nil?

  wsdl_loc = wsdl
  @client = Savon.client(globals) do
    wsdl wsdl_loc
  end
end

Instance Method Details

#download_file_attachment_by_item_id(scrt_id, secret_item_id, item_type, token = nil) ⇒ Hash

Get an “Attachment”-type file from a secret

Parameters:

  • the (Integer)

    id of the secret

  • the (Integer)

    id of the attachment on the secret

  • the (String)

    type of the item being downloaded

Returns:

  • (Hash)

    contents and metadata of the downloaded file



76
77
78
79
80
81
# File 'lib/zanzibar/client.rb', line 76

def download_file_attachment_by_item_id(scrt_id, secret_item_id, item_type, token = nil)
  token = generate_token unless token
  @client.call(:download_file_attachment_by_item_id, message:
              { token: token, secretId: scrt_id, secretItemId: secret_item_id || get_scrt_item_id(scrt_id, item_type, token) })
         .hash[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result]
end

#generate_tokenObject

Get an authentication token for interacting with Secret Server. These are only good for about 10 minutes so just get a new one each time. Will raise an error if there is an issue with the authentication.

Returns:

  • the authentication token for the current user.



31
32
33
34
35
36
37
38
# File 'lib/zanzibar/client.rb', line 31

def generate_token
  response = @client.call(:authenticate, message: { username: @username, password: @password, organization: '', domain: @domain })
                    .hash[:envelope][:body][:authenticate_response][:authenticate_result]
  raise "Error generating the authentication token for user #{@username}: #{response[:errors][:string]}" if response[:errors]
  response[:token]
rescue Savon::Error => err
  raise "There was an error generating the authentiaton token for user #{@username}: #{err}"
end

#get_scrt_item_id(scrt_id, type, token) ⇒ Integer

Get the secret item id that relates to a key file or attachment. Will raise on error

Parameters:

  • the (Integer)

    secret id

  • the (String)

    type of secret item to get, one of privatekey, publickey, attachment

Returns:

  • (Integer)

    the secret item id



59
60
61
62
63
64
65
66
67
68
# File 'lib/zanzibar/client.rb', line 59

def get_scrt_item_id(scrt_id, type, token)
  secret = get_secret(scrt_id, token)
  secret_items = secret[:secret][:items][:secret_item]
  begin
    return get_secret_item_by_field_name(secret_items, type)[:id]
  rescue => e

    raise "Unknown type, #{type}. #{e}"
  end
end

#get_secret(scrt_id, token = nil) ⇒ Hash

Get a secret returned as a hash Will raise an error if there was an issue getting the secret

Parameters:

  • the (Integer)

    secret id

Returns:

  • (Hash)

    the secret hash retrieved from the wsdl



45
46
47
48
49
50
51
# File 'lib/zanzibar/client.rb', line 45

def get_secret(scrt_id, token = nil)
  secret = @client.call(:get_secret, message: { token: token || generate_token, secretId: scrt_id }).hash[:envelope][:body][:get_secret_response][:get_secret_result]
  raise "There was an error getting secret #{scrt_id}: #{secret[:errors][:string]}" if secret[:errors]
  return secret
rescue Savon::Error => err
  raise "There was an error getting the secret with id #{scrt_id}: #{err}"
end

#get_secret_item_by_field_name(secret_items, field_name) ⇒ Object

Extract an item from a secret based on field name



85
86
87
88
89
# File 'lib/zanzibar/client.rb', line 85

def get_secret_item_by_field_name(secret_items, field_name)
  secret_items.each do |item|
    return item if item[:field_name] == field_name
  end
end