From 7f357ef60d6e290c591b952140b764e2620d38b6 Mon Sep 17 00:00:00 2001 From: Calvin Leung Huang Date: Wed, 13 Apr 2016 14:38:21 -0400 Subject: [PATCH] Add get_fieldlabel_value --- lib/zanzibar.rb | 21 +++++++++++++++------ lib/zanzibar/actions/get.rb | 8 +++++--- lib/zanzibar/cli.rb | 2 ++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/zanzibar.rb b/lib/zanzibar.rb index 626cc11..9805e2c 100644 --- a/lib/zanzibar.rb +++ b/lib/zanzibar.rb @@ -113,17 +113,26 @@ module Zanzibar raise "There was an error getting the secret with id #{scrt_id}: #{err}" end - ## Retrieve a simple password from a secret + ## Retrieve the value from a field label of a secret # Will raise an error if there are any issues # @param [Integer] the secret id + # @param [String] the field label to get + # @return [String] the value for the given field label + def get_fieldlabel_value(scrt_id, fieldlabel) + secret = get_secret(scrt_id) + secret_items = secret[:secret][:items][:secret_item] + return get_secret_item_by_field_name(secret_items, fieldlabel)[:value] + rescue Savon::Error => err + raise "There was an error getting '#{fieldlabel}' for secret #{scrt_id}: #{err}" + end + + ## Retrieve a simple password from a secret + # Calls get get_fieldlabel_value(), passing in 'Password' as the fieldlabel + # @param [Integer] the secret id # @return [String] the password for the given secret def get_password(scrt_id) - secret = get_secret(scrt_id) - secret_items = secret[: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}" + return get_fieldlabel_value(scrt_id, 'Password') end ## Get the password, save it to a file, and return the path to the file. diff --git a/lib/zanzibar/actions/get.rb b/lib/zanzibar/actions/get.rb index dd70880..fca5c62 100644 --- a/lib/zanzibar/actions/get.rb +++ b/lib/zanzibar/actions/get.rb @@ -20,15 +20,17 @@ module Zanzibar construct_options ensure_options - fetch_secret(@scrt_id, options['filelabel']) + fetch_secret(@scrt_id, options) end - def fetch_secret(scrt_id, label = nil) + def fetch_secret(scrt_id, opts = nil) scrt = ::Zanzibar::Zanzibar.new(@zanzibar_options) - if label + if opts['filelabel'] scrt.download_secret_file(scrt_id: scrt_id, type: label) + elsif opts['fieldlabel'] + scrt.get_fieldlabel_value(scrt_id, opts['fieldlabel']) else scrt.get_password(scrt_id) end diff --git a/lib/zanzibar/cli.rb b/lib/zanzibar/cli.rb index 61d4942..2f9b503 100644 --- a/lib/zanzibar/cli.rb +++ b/lib/zanzibar/cli.rb @@ -76,6 +76,8 @@ module Zanzibar desc: 'Don\'t verify Secret Server\'s SSL certificate' option 'filelabel', type: :string, aliases: :f, desc: 'Specify a file (by label) to download' + option 'fieldlabel', type: :string, aliases: :l, + desc: 'Specify a field (by label) to get' option 'username', type: :string, aliases: :u option 'password', type: :string, aliases: :p def get(scrt_id)