From adae2dcdccafd8b8da768085eeba75fa59ef71db Mon Sep 17 00:00:00 2001 From: Norm MacLennan Date: Thu, 15 Jan 2015 07:24:14 -0500 Subject: [PATCH] bit of refactoring and adding zamioculcas bin --- .rspec | 2 + .travis.yml | 5 + Gemfile | 11 +- Rakefile | 13 +- bin/zamioculcas | 70 + lib/zanzibar.rb | 57 +- .../responses/attachment_response.xml | 24 +- .../responses/authenticate_response.xml | 0 .../download_private_key_response.xml | 24 +- .../download_public_key_response.xml | 0 .../responses/get_secret_response.xml | 0 .../get_secret_with_attachment_response.xml | 114 +- .../get_secret_with_keys_response.xml | 94 +- {test => spec}/scrt.wsdl | 1258 ++++++++--------- {test => spec}/spec/spec_helper.rb | 0 {test => spec}/zanzibar_spec.rb | 18 +- test/.rspec | 2 - zanzibar.gemspec | 1 + 18 files changed, 888 insertions(+), 805 deletions(-) create mode 100644 .rspec create mode 100644 .travis.yml create mode 100644 bin/zamioculcas rename {test => spec}/responses/attachment_response.xml (98%) rename {test => spec}/responses/authenticate_response.xml (100%) rename {test => spec}/responses/download_private_key_response.xml (98%) rename {test => spec}/responses/download_public_key_response.xml (100%) rename {test => spec}/responses/get_secret_response.xml (100%) rename {test => spec}/responses/get_secret_with_attachment_response.xml (97%) rename {test => spec}/responses/get_secret_with_keys_response.xml (97%) rename {test => spec}/scrt.wsdl (97%) rename {test => spec}/spec/spec_helper.rb (100%) rename {test => spec}/zanzibar_spec.rb (81%) delete mode 100644 test/.rspec diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..65448f4 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec/spec_helper diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..e2f0ec2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: ruby +rvm: + - 1.9.3 + - 2.0.0 + - 2.1.0 diff --git a/Gemfile b/Gemfile index 3ce429f..4f7af18 100644 --- a/Gemfile +++ b/Gemfile @@ -1,9 +1,14 @@ source 'https://rubygems.org' gem 'savon' -gem 'savon_spec' -gem 'rspec' -gem 'webmock' + +group :test do + gem 'rake' + gem 'savon_spec' + gem 'rspec' + gem 'webmock' + gem 'zanzibar', path: '.' +end # Specify your gem's dependencies in zanzibar.gemspec gemspec diff --git a/Rakefile b/Rakefile index 899c1b2..132e5fd 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,11 @@ require "bundler/gem_tasks" +require "bundler/setup" # load up our gem environment (incl. local zanzibar) +require 'rspec/core/rake_task' +require 'zanzibar/version' -task 'test' do - Dir.chdir('test') - system("rspec zanzibar_spec.rb") -end +RSpec::Core::RakeTask.new(:test) -task 'install_dependencies' do - system('bundle install') +task :install_local do + system "rake build" + system "gem install ./pkg/zanzibar-#{Zanzibar::VERSION}.gem" end diff --git a/bin/zamioculcas b/bin/zamioculcas new file mode 100644 index 0000000..c2231ec --- /dev/null +++ b/bin/zamioculcas @@ -0,0 +1,70 @@ +#! ruby + +require 'zanzibar' +require 'optparse' + +options = { + :domain => 'local' +} + +OptionParser.new do |opts| + opts.banner = "Usage: zamioculcas -d domain [-w wsdl] [-k] [-p] [secret_id]" + + opts.on("-d", "--domain DOMAIN", "Specify domain") do |v| + options[:domain] = v + end + + opts.on("-w", "--wsdl WSDL", "Specify WSDL location") do |v| + options[:wsdl] = v + end + + opts.on("-s", "--server SERVER", "Secret server hostname or IP") do |v| + options[:server] = v + end + + opts.on("-k", "--no-check-certificate", "Don't run SSL certificate checks") do |v| + options[:globals] = {:ssl_verify_mode => :none} + end + + opts.on("-p", "--password PASSWORD", "Specify password") do |v| + options[:pwd] = v + end + + opts.on("-t", "--type TYPE", "Specify the type of secret") do |v| + options[:type] = v + end + + opts.on("-u", "--user USER", "Specify the username") do |v| + options[:username] = v + end + +end.parse! + +raise OptionParser::MissingArgument if options[:server].nil? +options[:type] = "password" if options[:type].nil? + +unless STDIN.tty? || options[:pwd] + options[:pwd] = $stdin.read.strip +end + +secret_id = Integer(ARGV.pop) +if(!secret_id) + fail "no secret!" +end + +unless options[:wsdl] || options[:server].nil? + options[:wsdl] = "https://#{options[:server]}/webservices/sswebservice.asmx?wsdl" +end + +scrt = Zanzibar::Zanzibar.new(options) + +case options[:type] +when "password" + $stdout.write "#{scrt.get_password(secret_id)}\n" +when "privatekey" + scrt.download_private_key(:scrt_id=>secret_id) +when "publickey" + scrt.download_public_key(:scrt_id=>secret_id) +else + $stderr.write "#{options[:type]} is not a known type." +end diff --git a/lib/zanzibar.rb b/lib/zanzibar.rb index fc3ebfb..309d223 100644 --- a/lib/zanzibar.rb +++ b/lib/zanzibar.rb @@ -10,9 +10,16 @@ module Zanzibar class Zanzibar ## - # @param args{:domain, :wsdl, :pwd, :globals{}} + # @param args{:domain, :wsdl, :pwd, :username, :globals{}} def initialize(args = {}) + + if args[:username] + @@username = args[:username] + else + @@username = ENV['USER'] + end + if args[:wsdl] @@wsdl = args[:wsdl] else @@ -46,7 +53,7 @@ module Zanzibar # @return [String] the password for the current user def prompt_for_password - puts "Please enter password for #{ENV['USER']}:" + puts "Please enter password for #{@@username}:" return STDIN.noecho(&:gets).chomp end @@ -73,13 +80,13 @@ module Zanzibar def get_token begin - response = @@client.call(:authenticate, message: { username: ENV['USER'], password: @@password, organization: "", domain: @@domain }).hash + response = @@client.call(:authenticate, message: { username: @@username, password: @@password, organization: "", domain: @@domain }).hash if response[:envelope][:body][:authenticate_response][:authenticate_result][:errors] - raise "Error generating the authentication token for user #{ENV['USER']}: #{response[:envelope][:body][:authenticate_response][:authenticate_result][:errors][:string]}" + raise "Error generating the authentication token for user #{@@username}: #{response[:envelope][:body][:authenticate_response][:authenticate_result][:errors][:string]}" end response[:envelope][:body][:authenticate_response][:authenticate_result][:token] rescue Savon::Error => err - raise "There was an error generating the authentiaton token for user #{ENV['USER']}: #{err}" + raise "There was an error generating the authentiaton token for user #{@@username}: #{err}" end end @@ -108,12 +115,19 @@ module Zanzibar def get_password(scrt_id) begin secret = get_secret(scrt_id) - return secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item][1][:value] + secret_items = secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item] + return get_secret_item_by_field_name(secret_items,"Password")[:value] rescue Savon::Error => err raise "There was an error getting the password for secret #{scrt_id}: #{err}" end end + 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 + ## Get the secret item id that relates to a key file or attachment. # Will raise on error # @param [Integer] the secret id @@ -122,25 +136,12 @@ module Zanzibar def get_scrt_item_id(scrt_id, type, token) secret = get_secret(scrt_id, token) - case type - when 'privatekey' - ## Get private key item id - secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item].each do |item| - return item[:id] if item[:field_name] == 'Private Key' - end - when 'publickey' - ## Get public key item id - secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item].each do |item| - return item[:id] if item[:field_name] == 'Public Key' - end - when 'attachment' - ## Get attachment item id. This currently only supports secrets with one attachment. - secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item].each do |item| - return item[:id] if item[:field_name] == 'Attachment' - end - else - raise "Unknown type, #{type}." - end + secret_items = secret[:envelope][:body][:get_secret_response][:get_secret_result][:secret][:items][:secret_item] + begin + return get_secret_item_by_field_name(secret_items, type)[:id] + rescue + raise "Unknown type, #{type}." + end end ## Downloads the private key for a secret and places it where Zanzibar is running, or :path if specified @@ -152,7 +153,7 @@ module Zanzibar 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], 'privatekey', 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 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[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:string]}" end @@ -173,7 +174,7 @@ module Zanzibar 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], 'publickey', 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 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[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:string]}" end @@ -194,7 +195,7 @@ module Zanzibar 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 + 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 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[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result][:string]}" end diff --git a/test/responses/attachment_response.xml b/spec/responses/attachment_response.xml similarity index 98% rename from test/responses/attachment_response.xml rename to spec/responses/attachment_response.xml index bf07e26..ae0248a 100644 --- a/test/responses/attachment_response.xml +++ b/spec/responses/attachment_response.xml @@ -1,12 +1,12 @@ - - - - - - - SSBhbSBhIHNlY3JldCBhdHRhY2htZW50 - attachment.txt - - - - + + + + + + + SSBhbSBhIHNlY3JldCBhdHRhY2htZW50 + attachment.txt + + + + diff --git a/test/responses/authenticate_response.xml b/spec/responses/authenticate_response.xml similarity index 100% rename from test/responses/authenticate_response.xml rename to spec/responses/authenticate_response.xml diff --git a/test/responses/download_private_key_response.xml b/spec/responses/download_private_key_response.xml similarity index 98% rename from test/responses/download_private_key_response.xml rename to spec/responses/download_private_key_response.xml index 588ffb6..ca4b7bb 100644 --- a/test/responses/download_private_key_response.xml +++ b/spec/responses/download_private_key_response.xml @@ -1,12 +1,12 @@ - - - - - - - LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVkgLS0tLS0KemFuemliYXJUZXN0UGFzc3dvcmQKLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0= - zanzi_key - - - - + + + + + + + LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVkgLS0tLS0KemFuemliYXJUZXN0UGFzc3dvcmQKLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0= + zanzi_key + + + + diff --git a/test/responses/download_public_key_response.xml b/spec/responses/download_public_key_response.xml similarity index 100% rename from test/responses/download_public_key_response.xml rename to spec/responses/download_public_key_response.xml diff --git a/test/responses/get_secret_response.xml b/spec/responses/get_secret_response.xml similarity index 100% rename from test/responses/get_secret_response.xml rename to spec/responses/get_secret_response.xml diff --git a/test/responses/get_secret_with_attachment_response.xml b/spec/responses/get_secret_with_attachment_response.xml similarity index 97% rename from test/responses/get_secret_with_attachment_response.xml rename to spec/responses/get_secret_with_attachment_response.xml index 9a4a41c..c6400cc 100644 --- a/test/responses/get_secret_with_attachment_response.xml +++ b/spec/responses/get_secret_with_attachment_response.xml @@ -1,57 +1,57 @@ - - - - - - - - Zanzi Secret Attachment - - - N/A - 20144 - 284 - Username - false - false - false - Username - - - N/A - 20145 - 285 - Password - false - false - true - Password - - - - 20148 - 287 - Attachment - true - false - false - Attachment - - - 3456 - 6028 - 85 - false - true - - - - - - false - - - - - - + + + + + + + + Zanzi Secret Attachment + + + N/A + 20144 + 284 + Username + false + false + false + Username + + + N/A + 20145 + 285 + Password + false + false + true + Password + + + + 20148 + 287 + Attachment + true + false + false + Attachment + + + 3456 + 6028 + 85 + false + true + + + + + + false + + + + + + diff --git a/test/responses/get_secret_with_keys_response.xml b/spec/responses/get_secret_with_keys_response.xml similarity index 97% rename from test/responses/get_secret_with_keys_response.xml rename to spec/responses/get_secret_with_keys_response.xml index 3d0cb4a..6aaa8a1 100644 --- a/test/responses/get_secret_with_keys_response.xml +++ b/spec/responses/get_secret_with_keys_response.xml @@ -1,47 +1,47 @@ - - - - - - - - Zanzibar Test Keys - - - - 15214 - 486 - Private Key - true - false - false - Private Key - - - - 15215 - 487 - Public Key - true - false - false - Public Key - - - 2345 - 6054 - 85 - false - true - - - - - - false - - - - - - + + + + + + + + Zanzibar Test Keys + + + + 15214 + 486 + Private Key + true + false + false + Private Key + + + + 15215 + 487 + Public Key + true + false + false + Public Key + + + 2345 + 6054 + 85 + false + true + + + + + + false + + + + + + diff --git a/test/scrt.wsdl b/spec/scrt.wsdl similarity index 97% rename from test/scrt.wsdl rename to spec/scrt.wsdl index 61bb79a..4ee82ae 100644 --- a/test/scrt.wsdl +++ b/spec/scrt.wsdl @@ -1,629 +1,629 @@ - - - Webservice for standard integration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Webservice for standard integration. - - - - - - - - - - - - - - + + + Webservice for standard integration. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Webservice for standard integration. + + + + + + + + + + + + + + diff --git a/test/spec/spec_helper.rb b/spec/spec/spec_helper.rb similarity index 100% rename from test/spec/spec_helper.rb rename to spec/spec/spec_helper.rb diff --git a/test/zanzibar_spec.rb b/spec/zanzibar_spec.rb similarity index 81% rename from test/zanzibar_spec.rb rename to spec/zanzibar_spec.rb index 3c4de9e..22f5cb1 100644 --- a/test/zanzibar_spec.rb +++ b/spec/zanzibar_spec.rb @@ -1,4 +1,4 @@ -require '../lib/zanzibar.rb' +require 'zanzibar' require 'savon' require 'webmock' require 'rspec' @@ -8,14 +8,14 @@ include WebMock::API describe "Zanzibar Test" do - client = Zanzibar::Zanzibar.new(:domain => "zanzitest.net", :pwd=>'password', :wsdl => "scrt.wsdl") - auth_xml = File.read('responses/authenticate_response.xml') - secret_xml = File.read('responses/get_secret_response.xml') - secret_with_key_xml = File.read('responses/get_secret_with_keys_response.xml') - secret_with_attachment_xml = File.read('responses/get_secret_with_attachment_response.xml') - private_key_xml = File.read('responses/download_private_key_response.xml') - public_key_xml = File.read('responses/download_public_key_response.xml') - attachment_xml = File.read('responses/attachment_response.xml') + client = Zanzibar::Zanzibar.new(:domain => "zanzitest.net", :pwd=>'password', :wsdl => "spec/scrt.wsdl") + auth_xml = File.read('spec/responses/authenticate_response.xml') + secret_xml = File.read('spec/responses/get_secret_response.xml') + secret_with_key_xml = File.read('spec/responses/get_secret_with_keys_response.xml') + secret_with_attachment_xml = File.read('spec/responses/get_secret_with_attachment_response.xml') + private_key_xml = File.read('spec/responses/download_private_key_response.xml') + public_key_xml = File.read('spec/responses/download_public_key_response.xml') + attachment_xml = File.read('spec/responses/attachment_response.xml') it 'should return an auth token' do diff --git a/test/.rspec b/test/.rspec deleted file mode 100644 index 83e16f8..0000000 --- a/test/.rspec +++ /dev/null @@ -1,2 +0,0 @@ ---color ---require spec_helper diff --git a/zanzibar.gemspec b/zanzibar.gemspec index 8e223d1..8a92012 100644 --- a/zanzibar.gemspec +++ b/zanzibar.gemspec @@ -18,6 +18,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] + spec.add_dependency "rubyntlm", "~> 0.4.0" spec.add_development_dependency "bundler", "~> 1.7" spec.add_development_dependency "rake", "~> 10.0" spec.add_runtime_dependency "savon", "~> 2.8.0"