diff --git a/lib/zanzibar.rb b/lib/zanzibar.rb index 00435a9..2e2f4e5 100644 --- a/lib/zanzibar.rb +++ b/lib/zanzibar.rb @@ -146,54 +146,21 @@ module Zanzibar end end - ## Downloads the private key for a secret and places it where Zanzibar is running, or :path if specified + ## Downloads a file for a secret and places it where Zanzibar is running, or :path if specified # Raise on error - # @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional + # @param [Hash] args, :scrt_id, :type (one of "Private Key", "Public Key", "Attachment"), :scrt_item_id - optional, :path - optional - def download_private_key(args = {}) + + def download_secret_file(args = {}) token = get_token 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. 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[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result] - raise "There was an error getting the private 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], args[:type], token)}).hash[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result] + raise "There was an error getting the #{args[:type]} for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors] write_secret_to_file(path, response) rescue Savon::Error => err - raise "There was an error getting the private key for secret #{args[:scrt_id]}: #{err}" - end - end - - ## Downloads the public key for a secret and places it where Zanzibar is running, or :path if specified - # Raise on error - # @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional - - def download_public_key(args = {}) - token = get_token - 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. - 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[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result] - raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors] - write_secret_to_file(path, response) - rescue Savon::Error => err - raise "There was an error getting the public key for secret #{args[:scrt_id]}: #{err}" - end - end - - ## Downloads an attachment for a secret and places it where Zanzibar is running, or :path if specified - # Raise on error - # @param [Hash] args, :scrt_id, :scrt_item_id - optional, :path - optional - - def download_attachment(args = {}) - token = get_token - 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. - 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] - raise "There was an error getting the attachment for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors] - write_secret_to_file(path, response) - 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 #{args[:type]} for secret #{args[:scrt_id]}: #{err}" end end end diff --git a/spec/zanzibar_spec.rb b/spec/zanzibar_spec.rb index a5dd937..2e91d07 100644 --- a/spec/zanzibar_spec.rb +++ b/spec/zanzibar_spec.rb @@ -47,7 +47,7 @@ describe "Zanzibar Test" do to_return(:body => secret_with_key_xml, :status => 200).then. to_return(:body => private_key_xml, :status => 200) - client.download_private_key(:scrt_id => 2345) + client.download_secret_file(:scrt_id => 2345, :type => 'Private Key') expect(File.exist? 'zanzi_key') expect(File.read('zanzi_key')).to eq("-----BEGIN RSA PRIVATE KEY -----\nzanzibarTestPassword\n-----END RSA PRIVATE KEY-----\n") File.delete('zanzi_key') @@ -60,7 +60,7 @@ describe "Zanzibar Test" do to_return(:body => secret_with_key_xml, :status => 200).then. to_return(:body => public_key_xml, :status => 200) - client.download_public_key(:scrt_id => 2345) + client.download_secret_file(:scrt_id => 2345, :type => 'Public Key') expect(File.exist? 'zanzi_key.pub') expect(File.read('zanzi_key.pub')).to eq("1234PublicKey5678==\n") File.delete('zanzi_key.pub') @@ -72,7 +72,7 @@ describe "Zanzibar Test" do to_return(:body => secret_with_attachment_xml, :status => 200).then. to_return(:body => attachment_xml, :status => 200) - client.download_attachment(:scrt_id => 3456) + client.download_secret_file(:scrt_id => 3456, :type => 'Attachment') expect(File.exist? 'attachment.txt') expect(File.read('attachment.txt')).to eq("I am a secret attachment\n") File.delete('attachment.txt')