Refactoring

This commit is contained in:
Jason Davis-Cooke
2015-01-15 13:03:22 -05:00
parent d69123471b
commit c542b56a5a
2 changed files with 201 additions and 211 deletions

View File

@@ -1,210 +1,200 @@
require "zanzibar/version" require "zanzibar/version"
require 'savon' require 'savon'
require 'io/console' require 'io/console'
require 'fileutils' require 'fileutils'
module Zanzibar module Zanzibar
## ##
# Class for interacting with Secret Server # Class for interacting with Secret Server
class Zanzibar class Zanzibar
## ##
# @param args{:domain, :wsdl, :pwd, :username, :globals{}} # @param args{:domain, :wsdl, :pwd, :username, :globals{}}
def initialize(args = {}) def initialize(args = {})
if args[:username] if args[:username]
@@username = args[:username] @@username = args[:username]
else else
@@username = ENV['USER'] @@username = ENV['USER']
end end
if args[:wsdl] if args[:wsdl]
@@wsdl = args[:wsdl] @@wsdl = args[:wsdl]
else else
@@wsdl = get_wsdl_location @@wsdl = get_wsdl_location
end end
if args[:pwd] if args[:pwd]
@@password = args[:pwd] @@password = args[:pwd]
else else
@@password = prompt_for_password @@password = prompt_for_password
end end
if args[:domain] if args[:domain]
@@domain = args[:domain] @@domain = args[:domain]
else else
@@domain = prompt_for_domain @@domain = prompt_for_domain
end end
args[:globals] = {} unless args[:globals] args[:globals] = {} unless args[:globals]
init_client(args[:globals]) init_client(args[:globals])
end end
## Initializes the Savon client class variable with the wdsl document location and optional global variables ## Initializes the Savon client class variable with the wdsl document location and optional global variables
# @param globals{}, optional # @param globals{}, optional
def init_client(globals = {}) def init_client(globals = {})
globals = {} if globals == nil globals = {} if globals == nil
@@client = Savon.client(globals) do @@client = Savon.client(globals) do
wsdl @@wsdl wsdl @@wsdl
end end
end end
## Gets the user's password if none is provided in the constructor. ## Gets the user's password if none is provided in the constructor.
# @return [String] the password for the current user # @return [String] the password for the current user
def prompt_for_password def prompt_for_password
puts "Please enter password for #{@@username}:" puts "Please enter password for #{@@username}:"
return STDIN.noecho(&:gets).chomp return STDIN.noecho(&:gets).chomp
end end
## Gets the wsdl document location if none is provided in the constructor ## Gets the wsdl document location if none is provided in the constructor
# @return [String] the location of the WDSL document # @return [String] the location of the WDSL document
def get_wsdl_location def prompt_for_wsdl_location
puts "Enter the URL of the Secret Server WSDL:" puts "Enter the URL of the Secret Server WSDL:"
return STDIN.gets.chomp return STDIN.gets.chomp
end end
## Gets the domain of the Secret Server installation if none is provided in the constructor ## Gets the domain of the Secret Server installation if none is provided in the constructor
# @return [String] the domain of the secret server installation # @return [String] the domain of the secret server installation
def prompt_for_domain def prompt_for_domain
puts "Enter the domain of your Secret Server:" puts "Enter the domain of your Secret Server:"
return STDIN.gets.chomp return STDIN.gets.chomp
end end
## 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. ## 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. # Will raise an error if there is an issue with the authentication.
# @return the authentication token for the current user. # @return the authentication token for the current user.
def get_token def get_token
begin begin
response = @@client.call(:authenticate, message: { username: @@username, password: @@password, organization: "", domain: @@domain }).hash response = @@client.call(:authenticate, message: { username: @@username, password: @@password, organization: "", domain: @@domain }).hash[:envelope][:body][:authenticate_response][:authenticate_result]
if response[:envelope][:body][:authenticate_response][:authenticate_result][:errors] raise "Error generating the authentication token for user #{@@username}: #{response[:errors][:string]}" if response[:errors]
raise "Error generating the authentication token for user #{@@username}: #{response[:envelope][:body][:authenticate_response][:authenticate_result][:errors][:string]}" response[:token]
end rescue Savon::Error => err
response[:envelope][:body][:authenticate_response][:authenticate_result][:token] raise "There was an error generating the authentiaton token for user #{@@username}: #{err}"
rescue Savon::Error => err end
raise "There was an error generating the authentiaton token for user #{@@username}: #{err}" end
end
end ## Get a secret returned as a hash
# Will raise an error if there was an issue getting the secret
## Get a secret returned as a hash # @param [Integer] the secret id
# Will raise an error if there was an issue getting the secret # @return [Hash] the secret hash retrieved from the wsdl
# @param [Integer] the secret id
# @return [Hash] the secret hash retrieved from the wsdl def get_secret(scrt_id, token = nil)
begin
def get_secret(scrt_id, token = nil) secret = @@client.call(:get_secret, message: { token: token || get_token, secretId: scrt_id}).hash[:envelope][:body][:get_secret_response][:get_secret_result]
begin raise "There was an error getting secret #{scrt_id}: #{secret[:errors][:string]}" if secret[:errors]
secret = @@client.call(:get_secret, message: { token: token || get_token, secretId: scrt_id}).hash return secret
if secret[:envelope][:body][:get_secret_response][:get_secret_result][:errors] rescue Savon::Error => err
raise "There was an error getting secret #{scrt_id}: #{secret[:envelope][:body][:get_secret_response][:get_secret_result][:errors][:string]}" raise "There was an error getting the secret with id #{scrt_id}: #{err}"
end end
return secret end
rescue Savon::Error => err
raise "There was an error getting the secret with id #{scrt_id}: #{err}" ## Retrieve a simple password from a secret
end # Will raise an error if there are any issues
end # @param [Integer] the secret id
# @return [String] the password for the given secret
## Retrieve a simple password from a secret
# Will raise an error if there are any issues def get_password(scrt_id)
# @param [Integer] the secret id begin
# @return [String] the password for the given secret secret = get_secret(scrt_id)
secret_items = secret[:secret][:items][:secret_item]
def get_password(scrt_id) return get_secret_item_by_field_name(secret_items,"Password")[:value]
begin rescue Savon::Error => err
secret = get_secret(scrt_id) raise "There was an error getting the password for secret #{scrt_id}: #{err}"
secret_items = secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item] end
return get_secret_item_by_field_name(secret_items,"Password")[:value] end
rescue Savon::Error => err
raise "There was an error getting the password for secret #{scrt_id}: #{err}" def write_secret_to_file(path, secret_response)
end File.open(File.join(path, secret_response[:file_name]), 'wb') do |file|
end file.puts Base64.decode64(secret_response[:file_attachment])
end
def get_secret_item_by_field_name(secret_items, field_name) end
secret_items.each do |item|
return item if item[:field_name] == field_name def get_secret_item_by_field_name(secret_items, field_name)
end secret_items.each do |item|
end return item if item[:field_name] == field_name
end
## Get the secret item id that relates to a key file or attachment. end
# Will raise on error
# @param [Integer] the secret id ## Get the secret item id that relates to a key file or attachment.
# @param [String] the type of secret item to get, one of privatekey, publickey, attachment # Will raise on error
# @return [Integer] the secret item id # @param [Integer] the secret id
# @param [String] the type of secret item to get, one of privatekey, publickey, attachment
def get_scrt_item_id(scrt_id, type, token) # @return [Integer] the secret item id
secret = get_secret(scrt_id, token)
secret_items = secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item] def get_scrt_item_id(scrt_id, type, token)
begin secret = get_secret(scrt_id, token)
return get_secret_item_by_field_name(secret_items, type)[:id] secret_items = secret[:secret][:items][:secret_item]
rescue begin
raise "Unknown type, #{type}." return get_secret_item_by_field_name(secret_items, type)[:id]
end rescue
end raise "Unknown type, #{type}."
end
## Downloads the private key for a secret and places it where Zanzibar is running, or :path if specified end
# Raise on error
# @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional ## Downloads the private key for a secret and places it where Zanzibar is running, or :path if specified
# Raise on error
def download_private_key(args = {}) # @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional
token = get_token
FileUtils.mkdir_p(args[:path]) if args[:path] def download_private_key(args = {})
path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away. token = get_token
begin FileUtils.mkdir_p(args[:path]) if args[:path]
response = @@client.call(:download_file_attachment_by_item_id, message: { token: token, secretId: args[:scrt_id], secretItemId: args[:scrt_item_id] || get_scrt_item_id(args[:scrt_id], 'Private Key', token)}).hash path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away.
if response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:errors] begin
raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:string]}" response = @@client.call(:download_file_attachment_by_item_id, message: { token: token, secretId: args[:scrt_id], secretItemId: args[:scrt_item_id] || get_scrt_item_id(args[:scrt_id], 'Private Key', token)}).hash[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result]
end raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors]
File.open(File.join(path, response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:file_name]), 'wb') do |file| write_secret_to_file(path, response)
file.puts Base64.decode64(response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:file_attachment]) rescue Savon::Error => err
end raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{err}"
rescue Savon::Error => err end
raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{err}" end
end
end ## Downloads the public key for a secret and places it where Zanzibar is running, or :path if specified
# Raise on error
## Downloads the public key for a secret and places it where Zanzibar is running, or :path if specified # @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional
# Raise on error
# @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional def download_public_key(args = {})
token = get_token
def download_public_key(args = {}) FileUtils.mkdir_p(args[:path]) if args[:path]
token = get_token path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away.
FileUtils.mkdir_p(args[:path]) if args[:path] begin
path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away. response = @@client.call(:download_file_attachment_by_item_id, message: { token: token, secretId: args[:scrt_id], secretItemId: args[:scrt_item_id] || get_scrt_item_id(args[:scrt_id], 'Public Key', token)}).hash[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result]
begin raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors]
response = @@client.call(:download_file_attachment_by_item_id, message: { token: token, secretId: args[:scrt_id], secretItemId: args[:scrt_item_id] || get_scrt_item_id(args[:scrt_id], 'Public Key', token)}).hash write_secret_to_file(path, response)
if response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:errors] rescue Savon::Error => err
raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:string]}" raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{err}"
end end
File.open(File.join(path, response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:file_name]), 'wb') do |file| end
file.puts Base64.decode64(response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:file_attachment])
end ## Downloads an attachment for a secret and places it where Zanzibar is running, or :path if specified
rescue Savon::Error => err # Raise on error
raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{err}" # @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional
end
end def download_attachment(args = {})
token = get_token
## Downloads an attachment for a secret and places it where Zanzibar is running, or :path if specified FileUtils.mkdir_p(args[:path]) if args[:path]
# Raise on error path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away.
# @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional begin
response = @@client.call(:download_file_attachment_by_item_id, message: { token: token, secretId: args[:scrt_id], secretItemId: args[:scrt_item_id] || get_scrt_item_id(args[:scrt_id], 'Attachment', token)}).hash[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result]
def download_attachment(args = {}) raise "There was an error getting the attachment for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors]
token = get_token write_secret_to_file(path, response)
FileUtils.mkdir_p(args[:path]) if args[:path] rescue Savon::Error => err
path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away. raise "There was an error getting the attachment from secret #{args[:scrt_id]}: #{err}"
begin end
response = @@client.call(:download_file_attachment_by_item_id, message: { token: token, secretId: args[:scrt_id], secretItemId: args[:scrt_item_id] || get_scrt_item_id(args[:scrt_id], 'Attachment', token)}).hash end
if response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:errors] end
raise "There was an error getting the attachment for secret #{args[:scrt_id]}: #{response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:string]}" end
end
File.open(File.join(path, response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:file_name]), 'wb') do |file|
file.puts Base64.decode64(response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:file_attachment])
end
rescue Savon::Error => err
raise "There was an error getting the attachment from secret #{args[:scrt_id]}: #{err}"
end
end
end
end

View File

@@ -30,7 +30,7 @@ describe "Zanzibar Test" do
to_return(:body => auth_xml, :status => 200).then. to_return(:body => auth_xml, :status => 200).then.
to_return(:body => secret_xml, :status => 200) to_return(:body => secret_xml, :status => 200)
expect(client.get_secret(1234)[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:name]).to eq("Zanzi Test Secret") expect(client.get_secret(1234)[:secret][:name]).to eq("Zanzi Test Secret")
end end
it 'should get a password' do it 'should get a password' do