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

@@ -60,7 +60,7 @@ module Zanzibar
## 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
@@ -80,11 +80,9 @@ module Zanzibar
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
response[:envelope][:body][:authenticate_response][:authenticate_result][:token]
rescue Savon::Error => err rescue Savon::Error => err
raise "There was an error generating the authentiaton token for user #{@@username}: #{err}" raise "There was an error generating the authentiaton token for user #{@@username}: #{err}"
end end
@@ -97,10 +95,8 @@ module Zanzibar
def get_secret(scrt_id, token = nil) def get_secret(scrt_id, token = nil)
begin begin
secret = @@client.call(:get_secret, message: { token: token || get_token, secretId: scrt_id}).hash secret = @@client.call(:get_secret, message: { token: token || get_token, secretId: scrt_id}).hash[:envelope][:body][:get_secret_response][:get_secret_result]
if secret[:envelope][:body][:get_secret_response][:get_secret_result][:errors] raise "There was an error getting secret #{scrt_id}: #{secret[:errors][:string]}" if secret[:errors]
raise "There was an error getting secret #{scrt_id}: #{secret[:envelope][:body][:get_secret_response][:get_secret_result][:errors][:string]}"
end
return secret return secret
rescue Savon::Error => err rescue Savon::Error => err
raise "There was an error getting the secret with id #{scrt_id}: #{err}" raise "There was an error getting the secret with id #{scrt_id}: #{err}"
@@ -115,13 +111,19 @@ module Zanzibar
def get_password(scrt_id) def get_password(scrt_id)
begin begin
secret = get_secret(scrt_id) secret = get_secret(scrt_id)
secret_items = secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item] secret_items = secret[:secret][:items][:secret_item]
return get_secret_item_by_field_name(secret_items,"Password")[:value] return get_secret_item_by_field_name(secret_items,"Password")[:value]
rescue Savon::Error => err rescue Savon::Error => err
raise "There was an error getting the password for secret #{scrt_id}: #{err}" raise "There was an error getting the password for secret #{scrt_id}: #{err}"
end end
end end
def write_secret_to_file(path, secret_response)
File.open(File.join(path, secret_response[:file_name]), 'wb') do |file|
file.puts Base64.decode64(secret_response[:file_attachment])
end
end
def get_secret_item_by_field_name(secret_items, field_name) def get_secret_item_by_field_name(secret_items, field_name)
secret_items.each do |item| secret_items.each do |item|
return item if item[:field_name] == field_name return item if item[:field_name] == field_name
@@ -136,7 +138,7 @@ module Zanzibar
def get_scrt_item_id(scrt_id, type, token) def get_scrt_item_id(scrt_id, type, token)
secret = get_secret(scrt_id, token) secret = get_secret(scrt_id, token)
secret_items = secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item] secret_items = secret[:secret][:items][:secret_item]
begin begin
return get_secret_item_by_field_name(secret_items, type)[:id] return get_secret_item_by_field_name(secret_items, type)[:id]
rescue rescue
@@ -153,13 +155,9 @@ module Zanzibar
FileUtils.mkdir_p(args[:path]) if args[:path] FileUtils.mkdir_p(args[:path]) if args[:path]
path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away. path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away.
begin 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], 'Private Key', token)}).hash 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]
if response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:errors] raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors]
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]}" write_secret_to_file(path, response)
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 rescue Savon::Error => err
raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{err}" raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{err}"
end end
@@ -174,13 +172,9 @@ module Zanzibar
FileUtils.mkdir_p(args[:path]) if args[:path] FileUtils.mkdir_p(args[:path]) if args[:path]
path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away. path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away.
begin 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], 'Public Key', token)}).hash 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]
if response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:errors] raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors]
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]}" write_secret_to_file(path, response)
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 rescue Savon::Error => err
raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{err}" raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{err}"
end end
@@ -195,13 +189,9 @@ module Zanzibar
FileUtils.mkdir_p(args[:path]) if args[:path] FileUtils.mkdir_p(args[:path]) if args[:path]
path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away. path = args[:path] ? args[:path] : '.' ## The File.join below doesn't handle nils well, so let's take that possibility away.
begin 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 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]
if response[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:errors] raise "There was an error getting the attachment for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors]
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]}" write_secret_to_file(path, response)
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 rescue Savon::Error => err
raise "There was an error getting the attachment from secret #{args[:scrt_id]}: #{err}" raise "There was an error getting the attachment from secret #{args[:scrt_id]}: #{err}"
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