From 2ee47c2210ea1d48eebbe83d6dbe8935e2857bca Mon Sep 17 00:00:00 2001 From: Jason Davis-Cooke Date: Thu, 26 Feb 2015 11:53:48 -0500 Subject: [PATCH] Save username and password to a Normariffic yaml file --- lib/zanzibar.rb | 14 +++++++++----- lib/zanzibar/actions/bundle.rb | 2 +- spec/lib/zanzibar_spec.rb | 10 +++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/zanzibar.rb b/lib/zanzibar.rb index 39d6f73..f8886bd 100644 --- a/lib/zanzibar.rb +++ b/lib/zanzibar.rb @@ -2,6 +2,7 @@ require 'zanzibar/version' require 'savon' require 'io/console' require 'fileutils' +require 'yaml' module Zanzibar ## @@ -124,9 +125,11 @@ module Zanzibar end ## Get the password, save it to a file, and return the path to the file. - def get_password_and_save(scrt_id, path, name) - password = get_password(scrt_id) - save_password_to_file(password, path, name) + def get_username_and_password_and_save(scrt_id, path, name) + secret_items = get_secret(scrt_id)[:secret][:items][:secret_item] + password = get_secret_item_by_field_name(secret_items, 'Password')[:value] + username = get_secret_item_by_field_name(secret_items, 'Username')[:value] + save_username_and_password_to_file(password, username, path, name) return File.join(path, name) end @@ -137,9 +140,10 @@ module Zanzibar end ## Write the password to a file. Intended for use with a Zanzifile - def save_password_to_file(password, path, name) + def save_username_and_password_to_file(password, username, path, name) + user_pass = {'username' => username.to_s, 'password' => password.to_s}.to_yaml File.open(File.join(path, name), 'wb') do |file| - file.print password + file.print user_pass end end diff --git a/lib/zanzibar/actions/bundle.rb b/lib/zanzibar/actions/bundle.rb index 4ef1f81..ef64c56 100644 --- a/lib/zanzibar/actions/bundle.rb +++ b/lib/zanzibar/actions/bundle.rb @@ -99,7 +99,7 @@ module Zanzibar def download_one_secret(scrt_id, label, path, args, name = nil) if label == 'Password' - path = zanzibar(args).get_password_and_save(scrt_id, path, name) + path = zanzibar(args).get_username_and_password_and_save(scrt_id, path, name) { path: path, hash: Digest::MD5.file(path).hexdigest } else path = zanzibar(args).download_secret_file(scrt_id: scrt_id, diff --git a/spec/lib/zanzibar_spec.rb b/spec/lib/zanzibar_spec.rb index 0ac50f3..fd7c43d 100644 --- a/spec/lib/zanzibar_spec.rb +++ b/spec/lib/zanzibar_spec.rb @@ -104,15 +104,15 @@ describe 'Zanzibar Test' do File.delete('attachment.txt') end - it 'should save a password to a file' do + it 'should save credentials to a file' do stub_request(:any, 'https://www.zanzitest.net/webservices/sswebservice.asmx') .to_return(body: AUTH_XML, status: 200).then .to_return(body: SECRET_XML, status: 200) - client.get_password_and_save(1234, '.', 'zanziTestPassword') - expect(File.exist? 'zanziTestPassword') - expect(File.read('zanziTestPassword')).to eq('zanziUserPassword') - File.delete('zanziTestPassword') + client.get_username_and_password_and_save(1234, '.', 'zanziTestCreds') + expect(File.exist? 'zanziTestCreds') + expect(File.read('zanziTestCreds')).to eq({'username' => 'ZanziUser', 'password' => 'zanziUserPassword'}.to_yaml) + File.delete('zanziTestCreds') end it 'should use environment variables for credentials' do