20 Commits

Author SHA1 Message Date
Jason Davis-Cooke
8126deded5 Version bump 2015-06-05 10:00:37 -04:00
Jason Davis-Cooke
60545c5b7b Merge pull request #10 from Cimpress-MCP/feature/prefix_dir
Feature/prefix dir
2015-06-05 09:59:28 -04:00
Calvin Leung Huang
8b06192aa1 Update test 2015-06-04 14:55:00 -04:00
Calvin Leung Huang
244b9178b8 Add optional prefix option 2015-06-04 13:05:22 -04:00
Jason Davis-Cooke
4951f13e4f version bump 2015-06-03 16:42:16 -04:00
Ryan Breen
3e2c07defc Merge pull request #8 from Cimpress-MCP/addgitignore
Add a gitignore to secrets_dir
2015-06-03 16:33:29 -04:00
Jason Davis-Cooke
73b939808e Update bundle.rb
we need to puts so we get the requisite newlines
2015-06-03 16:27:03 -04:00
Jason Davis-Cooke
867e14214c Update bundle.rb
*, not .
2015-06-03 16:26:19 -04:00
Jason Davis-Cooke
ff99246b46 Update bundle.rb
Don't gitignore the gitignore
2015-06-03 16:24:40 -04:00
Jason Davis-Cooke
e6ec5e6dbd Add a gitignore to secrets_dir 2015-06-03 16:16:56 -04:00
Norm MacLennan
85b8c66d3d readme - wdsl->wsdl 2015-05-19 21:53:45 -04:00
Jason Davis-Cooke
e0e5bfd276 Merge pull request #6 from Cimpress-MCP/savepasswords
Savepasswords
2015-02-26 19:00:35 +00:00
Jason Davis-Cooke
3969d508f8 version bump 2015-02-26 13:46:32 -05:00
Jason Davis-Cooke
2ee47c2210 Save username and password to a Normariffic yaml file 2015-02-26 11:53:48 -05:00
Jason Davis-Cooke
8778c7b27d remove base64 encoding 2015-02-26 09:35:09 -05:00
Jason Davis-Cooke
431c86bb0e remove incorrect code documentation 2015-02-26 08:57:47 -05:00
Jason Davis-Cooke
179fa24ab9 Save zanzifile passwords to disk 2015-02-26 08:56:43 -05:00
Jason Davis-Cooke
ed496bd416 Save zanzifile passwords to file 2015-02-25 14:28:43 -05:00
Jason Davis-Cooke
4b4549c9c5 require. not include. 2015-02-06 14:56:20 -05:00
Jason Davis-Cooke
9d7b150712 Require pathname 2015-02-06 14:47:42 -05:00
9 changed files with 87 additions and 16 deletions

View File

@@ -106,7 +106,7 @@ Sample `Zanzifile`:
```yaml ```yaml
--- ---
settings: settings:
wsdl: my.scrt.srvr.com/webservices/sswebservice.asmx?wdsl wsdl: my.scrt.srvr.com/webservices/sswebservice.asmx?wsdl
domain: my.domain.net domain: my.domain.net
secret_dir: secrets/ secret_dir: secrets/
ignore_ssl: true ignore_ssl: true

View File

@@ -2,6 +2,7 @@ require 'zanzibar/version'
require 'savon' require 'savon'
require 'io/console' require 'io/console'
require 'fileutils' require 'fileutils'
require 'yaml'
module Zanzibar module Zanzibar
## ##
@@ -123,12 +124,29 @@ module Zanzibar
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
## Get the password, save it to a file, and return the path to the file.
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
def write_secret_to_file(path, secret_response) def write_secret_to_file(path, secret_response)
File.open(File.join(path, secret_response[:file_name]), 'wb') do |file| File.open(File.join(path, secret_response[:file_name]), 'wb') do |file|
file.puts Base64.decode64(secret_response[:file_attachment]) file.puts Base64.decode64(secret_response[:file_attachment])
end end
end end
## Write the password to a file. Intended for use with a Zanzifile
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 user_pass
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

View File

@@ -20,6 +20,7 @@ module Zanzibar
def run def run
ensure_zanzifile ensure_zanzifile
load_required_secrets load_required_secrets
ensure_secrets_path
validate_environment validate_environment
load_resolved_secrets if resolved_file? load_resolved_secrets if resolved_file?
validate_local_secrets unless @update validate_local_secrets unless @update
@@ -42,6 +43,17 @@ module Zanzibar
debug { "#{ZANZIFILE_NAME} located..." } debug { "#{ZANZIFILE_NAME} located..." }
end end
def ensure_secrets_path
## Make sure the directory exists and that a .gitignore is there to ignore it
if @settings['secret_dir']
FileUtils.mkdir_p(@settings['secret_dir'])
File.open("#{@settings['secret_dir']}/.gitignore", 'w') do |file|
file.puts '*'
file.puts '!.gitignore'
end
end
end
def resolved_file? def resolved_file?
File.exist? RESOLVED_NAME File.exist? RESOLVED_NAME
end end
@@ -80,23 +92,29 @@ module Zanzibar
downloaded_secrets = {} downloaded_secrets = {}
remote_secrets.each do |key, secret| remote_secrets.each do |key, secret|
full_path = secret.has_key?('prefix') ? File.join(@settings['secret_dir'], secret['prefix']) : @settings['secret_dir']
downloaded_secrets[key] = download_one_secret(secret['id'], downloaded_secrets[key] = download_one_secret(secret['id'],
secret['label'], secret['label'],
@settings['secret_dir'], full_path,
args) args,
secret['name'] || "#{secret['id']}_password")
debug { "Downloaded secret: #{key} to #{path}..." } debug { "Downloaded secret: #{key} to #{@settings['secret_dir']}..." }
end end
downloaded_secrets downloaded_secrets
end end
def download_one_secret(scrt_id, label, path, args) def download_one_secret(scrt_id, label, path, args, name = nil)
path = zanzibar(args).download_secret_file(scrt_id: scrt_id, if label == 'Password'
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,
type: label, type: label,
path: path) path: path)
{ path: path, hash: Digest::MD5.file(path).hexdigest }
{ path: path, hash: Digest::MD5.file(path).hexdigest } end
end end
def update_resolved_file(new_secrets) def update_resolved_file(new_secrets)

View File

@@ -53,6 +53,7 @@ module Zanzibar
end end
desc 'plunder', "Alias to `#{APPLICATION_NAME} bundle`", :hide => true desc 'plunder', "Alias to `#{APPLICATION_NAME} bundle`", :hide => true
option 'verbose', type: :boolean, default: false, aliases: :v
alias_method :plunder, :bundle alias_method :plunder, :bundle
desc 'install', "Alias to `#{APPLICATION_NAME} bundle`" desc 'install', "Alias to `#{APPLICATION_NAME} bundle`"

View File

@@ -1,3 +1,5 @@
require 'pathname'
# Definitions for various strings used throughout the gem # Definitions for various strings used throughout the gem
module Zanzibar module Zanzibar
APPLICATION_NAME = Pathname.new($PROGRAM_NAME).basename APPLICATION_NAME = Pathname.new($PROGRAM_NAME).basename

View File

@@ -1,4 +1,4 @@
# The version of the gem # The version of the gem
module Zanzibar module Zanzibar
VERSION = '0.1.15' VERSION = '0.1.19'
end end

View File

@@ -4,8 +4,11 @@ settings:
domain: zanzitest.net domain: zanzitest.net
secret_dir: secrets/ secret_dir: secrets/
ignore_ssl: true ignore_ssl: true
secrets:
secrets: secrets:
ssh_key: ssh_key:
id: 2345 id: 2345
label: Private Key label: Private Key
prefix_ssh_key:
id: 2345
label: Private Key
prefix: ssh/

View File

@@ -24,9 +24,12 @@ describe Zanzibar::Cli do
FakeFS::FileSystem.clone files FakeFS::FileSystem.clone files
stub_request(:any, 'https://www.zanzitest.net/webservices/sswebservice.asmx') stub_request(:any, 'https://www.zanzitest.net/webservices/sswebservice.asmx')
.to_return(body: AUTH_XML, status: 200).then .to_return({body: AUTH_XML, status: 200}).then
.to_return(body: SECRET_WITH_KEY_XML, status: 200).then .to_return({body: SECRET_WITH_KEY_XML, status: 200}).then
.to_return(body: PRIVATE_KEY_XML, status: 200) .to_return({body: PRIVATE_KEY_XML, status: 200}).then
.to_return({body: AUTH_XML, status: 200}).then
.to_return({body: SECRET_WITH_KEY_XML, status: 200}).then
.to_return({body: PRIVATE_KEY_XML, status: 200})
Dir.chdir File.join(source_root, 'spec', 'files') Dir.chdir File.join(source_root, 'spec', 'files')
end end
@@ -50,6 +53,18 @@ describe Zanzibar::Cli do
expect(FakeFS::FileTest.file? File.join('secrets', 'zanzi_key')).to be(true) expect(FakeFS::FileTest.file? File.join('secrets', 'zanzi_key')).to be(true)
end end
it 'should download a file to a prefix' do
expect(FakeFS::FileTest.file? File.join('secrets/ssh', 'zanzi_key')).to be(false)
expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout
expect(FakeFS::FileTest.file? File.join('secrets/ssh', 'zanzi_key')).to be(true)
end
it 'should create a .gitignore' do
expect(FakeFS::FileTest.file? File.join('secrets', '.gitignore')).to be(false)
expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout
expect(FakeFS::FileTest.file? File.join('secrets', '.gitignore')).to be(true)
end
it 'should create a resolved file' do it 'should create a resolved file' do
expect(FakeFS::FileTest.file? Zanzibar::RESOLVED_NAME).to be(false) expect(FakeFS::FileTest.file? Zanzibar::RESOLVED_NAME).to be(false)
expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout
@@ -58,7 +73,7 @@ describe Zanzibar::Cli do
it 'should not redownload files it already has' do it 'should not redownload files it already has' do
expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout
expect(WebMock).to have_requested(:post, 'https://www.zanzitest.net/webservices/sswebservice.asmx').times(3) expect(WebMock).to have_requested(:post, 'https://www.zanzitest.net/webservices/sswebservice.asmx').times(6)
WebMock.reset! WebMock.reset!
@@ -68,16 +83,19 @@ describe Zanzibar::Cli do
it 'should redownload on update action' do it 'should redownload on update action' do
expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout expect { subject.bundle }.to output(/Finished downloading secrets/).to_stdout
expect(WebMock).to have_requested(:post, 'https://www.zanzitest.net/webservices/sswebservice.asmx').times(3) expect(WebMock).to have_requested(:post, 'https://www.zanzitest.net/webservices/sswebservice.asmx').times(6)
WebMock.reset! WebMock.reset!
stub_request(:any, 'https://www.zanzitest.net/webservices/sswebservice.asmx') stub_request(:any, 'https://www.zanzitest.net/webservices/sswebservice.asmx')
.to_return({body: AUTH_XML, status: 200}).then
.to_return({body: SECRET_WITH_KEY_XML, status: 200}).then
.to_return({body: PRIVATE_KEY_XML, status: 200}).then
.to_return(body: AUTH_XML, status: 200).then .to_return(body: AUTH_XML, status: 200).then
.to_return(body: SECRET_WITH_KEY_XML, status: 200).then .to_return(body: SECRET_WITH_KEY_XML, status: 200).then
.to_return(body: PRIVATE_KEY_XML, status: 200) .to_return(body: PRIVATE_KEY_XML, status: 200)
expect { subject.update }.to output(/Finished downloading secrets/).to_stdout expect { subject.update }.to output(/Finished downloading secrets/).to_stdout
expect(WebMock).to have_requested(:post, 'https://www.zanzitest.net/webservices/sswebservice.asmx').times(3) expect(WebMock).to have_requested(:post, 'https://www.zanzitest.net/webservices/sswebservice.asmx').times(6)
end end
it 'should reject a malformed Zanzifile' do it 'should reject a malformed Zanzifile' do

View File

@@ -104,6 +104,17 @@ describe 'Zanzibar Test' do
File.delete('attachment.txt') File.delete('attachment.txt')
end end
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_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 it 'should use environment variables for credentials' do
ENV['ZANZIBAR_USER'] = 'environment_user' ENV['ZANZIBAR_USER'] = 'environment_user'
ENV['ZANZIBAR_PASSWORD'] = 'environment_password' ENV['ZANZIBAR_PASSWORD'] = 'environment_password'