diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..8d96aa0 --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,20 @@ +--- +engines: + bundler-audit: + enabled: true + duplication: + enabled: true + config: + languages: + - ruby + fixme: + enabled: true + rubocop: + enabled: true +ratings: + paths: + - Gemfile.lock + - "**.rb" +exclude_paths: +- spec/ +- templates/ diff --git a/.rubocop.yml b/.rubocop.yml index c6ea525..814c731 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,3 +3,7 @@ Metrics/ClassLength: Metrics/LineLength: Max: 175 + +AllCops: + Exclude: + - 'spec/**/*' diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5face3f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,20 @@ +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] +### Changed +- Upgraded dependencies + - `rubyntlm`: ~>0.4.0 to ~>0.6.0 + - `savon`: ~>2.10.0 to ~>2.11.0 + - `rubocop`: ~>0.28.0 to ~>0.39.0 +- Lots of rubocop-y code cleanup +- Converted from code climate classic to code climate platform +- Added rake task to run code climate platform locally + +## [0.1.27] - 2016-04-15 +### Added +- `zanzibar get` can fetch field values for fields other than password + - This ability has not been added to Zanzifiles yet. + +[Unreleased]: https://github.com/Cimpress-MCP/Zanzibar/compare/v0.1.27...HEAD diff --git a/Rakefile b/Rakefile index e8da831..8618a2a 100644 --- a/Rakefile +++ b/Rakefile @@ -7,5 +7,15 @@ require 'rubocop/rake_task' task default: [:test] RSpec::Core::RakeTask.new(:test) - RuboCop::RakeTask.new + +task :cc_local do + command = 'docker run ' + command << '--interactive --tty --rm ' + command << '--env CODECLIMATE_CODE="$PWD" ' + command << '--volume "$PWD":/code ' + command << '--volume /var/run/docker.sock:/var/run/docker.sock ' + command << '--volume /tmp/cc:/tmp/cc ' + command << 'codeclimate/codeclimate analyze' + sh command +end diff --git a/lib/zanzibar.rb b/lib/zanzibar.rb index 5900d4b..c859ff1 100644 --- a/lib/zanzibar.rb +++ b/lib/zanzibar.rb @@ -12,33 +12,33 @@ module Zanzibar # @param args{:domain, :wsdl, :pwd, :username, :globals{}} def initialize(args = {}) - if args[:username] - @@username = args[:username] - elsif ENV['ZANZIBAR_USER'] - @@username = ENV['ZANZIBAR_USER'] - else - @@username = ENV['USER'] - end + @@username = if args[:username] + args[:username] + elsif ENV['ZANZIBAR_USER'] + ENV['ZANZIBAR_USER'] + else + ENV['USER'] + end - if args[:wsdl] - @@wsdl = args[:wsdl] - else - @@wsdl = get_wsdl_location - end + @@wsdl = if args[:wsdl] + args[:wsdl] + else + get_wsdl_location + end - if args[:pwd] - @@password = args[:pwd] - elsif ENV['ZANZIBAR_PASSWORD'] - @@password = ENV['ZANZIBAR_PASSWORD'] - else - @@password = prompt_for_password - end + @@password = if args[:pwd] + args[:pwd] + elsif ENV['ZANZIBAR_PASSWORD'] + ENV['ZANZIBAR_PASSWORD'] + else + prompt_for_password + end - if args[:domain] - @@domain = args[:domain] - else - @@domain = prompt_for_domain - end + @@domain = if args[:domain] + args[:domain] + else + prompt_for_domain + end args[:globals] = {} unless args[:globals] init_client(args[:globals]) end @@ -67,7 +67,7 @@ module Zanzibar def prompt_for_password puts "Please enter password for #{@@username}:" STDIN.noecho(&:gets).chomp.tap do - puts "Using password to login..." + puts 'Using password to login...' end end @@ -93,8 +93,8 @@ module Zanzibar def get_token response = @@client.call(:authenticate, message: { username: @@username, password: @@password, organization: '', domain: @@domain }) - .hash[:envelope][:body][:authenticate_response][:authenticate_result] - fail "Error generating the authentication token for user #{@@username}: #{response[:errors][:string]}" if response[:errors] + .hash[:envelope][:body][:authenticate_response][:authenticate_result] + raise "Error generating the authentication token for user #{@@username}: #{response[:errors][:string]}" if response[:errors] response[:token] rescue Savon::Error => err raise "There was an error generating the authentiaton token for user #{@@username}: #{err}" @@ -107,7 +107,7 @@ module Zanzibar def get_secret(scrt_id, token = nil) secret = @@client.call(:get_secret, message: { token: token || get_token, secretId: scrt_id }).hash[:envelope][:body][:get_secret_response][:get_secret_result] - fail "There was an error getting secret #{scrt_id}: #{secret[:errors][:string]}" if secret[:errors] + raise "There was an error getting secret #{scrt_id}: #{secret[:errors][:string]}" if secret[:errors] return secret rescue Savon::Error => err raise "There was an error getting the secret with id #{scrt_id}: #{err}" @@ -131,7 +131,7 @@ module Zanzibar # @param [Integer] the secret id # @return [String] the password for the given secret def get_password(scrt_id) - return get_fieldlabel_value(scrt_id) + get_fieldlabel_value(scrt_id) end ## Get the password, save it to a file, and return the path to the file. @@ -140,7 +140,7 @@ module Zanzibar 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) + File.join(path, name) end def write_secret_to_file(path, secret_response) @@ -151,7 +151,7 @@ module Zanzibar ## 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 + 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 @@ -190,8 +190,8 @@ module Zanzibar 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], args[:type], token) }) - .hash[:envelope][:body][:download_file_attachment_by_item_id_response][:download_file_attachment_by_item_id_result] - fail "There was an error getting the #{args[:type]} for secret #{args[:scrt_id]}: #{response[:errors][:string]}" if response[:errors] + .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) return File.join(path, response[:file_name]) rescue Savon::Error => err diff --git a/lib/zanzibar/actions/bundle.rb b/lib/zanzibar/actions/bundle.rb index b9ada9c..aa27a86 100644 --- a/lib/zanzibar/actions/bundle.rb +++ b/lib/zanzibar/actions/bundle.rb @@ -39,7 +39,7 @@ module Zanzibar end def ensure_zanzifile - fail Error, NO_ZANZIFILE_ERROR unless File.exist? ZANZIFILE_NAME + raise Error, NO_ZANZIFILE_ERROR unless File.exist? ZANZIFILE_NAME debug { "#{ZANZIFILE_NAME} located..." } end @@ -47,7 +47,7 @@ module Zanzibar ## Make sure the directory exists and that a .gitignore is there to ignore it if @settings['secret_dir'] FileUtils.mkdir_p(@settings['secret_dir']) - if !File.exist? "#{@settings['secret_dir']}/.gitignore" + unless File.exist? "#{@settings['secret_dir']}/.gitignore" File.open("#{@settings['secret_dir']}/.gitignore", 'w') do |file| file.puts '*' file.puts '!.gitignore' @@ -69,7 +69,7 @@ module Zanzibar def validate_environment return unless @settings.empty? || @remote_secrets.empty? - fail Error, INVALID_ZANZIFILE_ERROR + raise Error, INVALID_ZANZIFILE_ERROR end def load_resolved_secrets @@ -94,7 +94,7 @@ module Zanzibar downloaded_secrets = {} remote_secrets.each do |key, secret| - full_path = secret.has_key?('prefix') ? File.join(@settings['secret_dir'], secret['prefix']) : @settings['secret_dir'] + full_path = secret.key?('prefix') ? File.join(@settings['secret_dir'], secret['prefix']) : @settings['secret_dir'] downloaded_secrets[key] = download_one_secret(secret['id'], secret['label'], full_path, @@ -110,13 +110,13 @@ module Zanzibar def download_one_secret(scrt_id, label, path, args, name = nil) 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, - path: path) - { path: path, hash: Digest::MD5.file(path).hexdigest } + type: label, + path: path) end + + { path: path, hash: Digest::MD5.file(path).hexdigest } end def update_resolved_file(new_secrets) diff --git a/lib/zanzibar/actions/get.rb b/lib/zanzibar/actions/get.rb index 5683477..d9f3a43 100644 --- a/lib/zanzibar/actions/get.rb +++ b/lib/zanzibar/actions/get.rb @@ -32,7 +32,6 @@ module Zanzibar else scrt.get_fieldlabel_value(scrt_id, @zanzibar_options[:fieldlabel]) end - end def construct_options @@ -55,7 +54,7 @@ module Zanzibar def ensure_options return if @zanzibar_options[:wsdl] - fail Error, NO_WSDL_ERROR + raise Error, NO_WSDL_ERROR end end end diff --git a/lib/zanzibar/actions/init.rb b/lib/zanzibar/actions/init.rb index efa6c59..de314d7 100644 --- a/lib/zanzibar/actions/init.rb +++ b/lib/zanzibar/actions/init.rb @@ -17,7 +17,7 @@ module Zanzibar def check_for_zanzifile return unless File.exist?(ZANZIFILE_NAME) && !options['force'] - fail Error, ALREADY_EXISTS_ERROR + raise Error, ALREADY_EXISTS_ERROR end def write_template diff --git a/lib/zanzibar/cli.rb b/lib/zanzibar/cli.rb index 2f9b503..5550eff 100644 --- a/lib/zanzibar/cli.rb +++ b/lib/zanzibar/cli.rb @@ -52,12 +52,12 @@ module Zanzibar run_action { bundle! } 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 plunder bundle desc 'install', "Alias to `#{APPLICATION_NAME} bundle`" - alias_method :install, :bundle + alias install bundle desc 'update', "Redownload all secrets in your #{ZANZIFILE_NAME}" option 'verbose', type: :boolean, default: false, aliases: :v diff --git a/lib/zanzibar/defaults.rb b/lib/zanzibar/defaults.rb index 34cb371..20902c9 100644 --- a/lib/zanzibar/defaults.rb +++ b/lib/zanzibar/defaults.rb @@ -3,14 +3,14 @@ require 'pathname' # Definitions for various strings used throughout the gem module Zanzibar APPLICATION_NAME = Pathname.new($PROGRAM_NAME).basename - ZANZIFILE_NAME = 'Zanzifile' - RESOLVED_NAME = 'Zanzifile.resolved' - TEMPLATE_NAME = 'templates/Zanzifile.erb' - DEFAULT_SERVER = 'secret.example.com' - DEFAULT_WSDL = 'https://%s/webservices/sswebservice.asmx?wsdl' + ZANZIFILE_NAME = 'Zanzifile'.freeze + RESOLVED_NAME = 'Zanzifile.resolved'.freeze + TEMPLATE_NAME = 'templates/Zanzifile.erb'.freeze + DEFAULT_SERVER = 'secret.example.com'.freeze + DEFAULT_WSDL = 'https://%s/webservices/sswebservice.asmx?wsdl'.freeze - ALREADY_EXISTS_ERROR = "#{ZANZIFILE_NAME} already exists! Aborting..." - NO_WSDL_ERROR = 'Could not construct WSDL URL. Please provide either --server or --wsdl' - NO_ZANZIFILE_ERROR = "You don't have a #{ZANZIFILE_NAME}! Run `#{APPLICATION_NAME} init` first!" - INVALID_ZANZIFILE_ERROR = "Unable to load your #{ZANZIFILE_NAME}. Please ensure it is valid YAML." + ALREADY_EXISTS_ERROR = "#{ZANZIFILE_NAME} already exists! Aborting...".freeze + NO_WSDL_ERROR = 'Could not construct WSDL URL. Please provide either --server or --wsdl'.freeze + NO_ZANZIFILE_ERROR = "You don't have a #{ZANZIFILE_NAME}! Run `#{APPLICATION_NAME} init` first!".freeze + INVALID_ZANZIFILE_ERROR = "Unable to load your #{ZANZIFILE_NAME}. Please ensure it is valid YAML.".freeze end diff --git a/lib/zanzibar/version.rb b/lib/zanzibar/version.rb index 6ec333a..908af5a 100644 --- a/lib/zanzibar/version.rb +++ b/lib/zanzibar/version.rb @@ -1,4 +1,4 @@ # The version of the gem module Zanzibar - VERSION = '0.1.27' + VERSION = '0.2.0'.freeze end diff --git a/spec/lib/zanzibar/actions/bundle_spec.rb b/spec/lib/zanzibar/actions/bundle_spec.rb index 7dca6c0..fc8e9f1 100644 --- a/spec/lib/zanzibar/actions/bundle_spec.rb +++ b/spec/lib/zanzibar/actions/bundle_spec.rb @@ -24,12 +24,12 @@ describe Zanzibar::Cli do FakeFS::FileSystem.clone files 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: SECRET_WITH_KEY_XML, status: 200}).then - .to_return({body: PRIVATE_KEY_XML, status: 200}) + .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: SECRET_WITH_KEY_XML, status: 200).then + .to_return(body: PRIVATE_KEY_XML, status: 200) Dir.chdir File.join(source_root, 'spec', 'files') end @@ -87,9 +87,9 @@ describe Zanzibar::Cli do WebMock.reset! 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: 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: SECRET_WITH_KEY_XML, status: 200).then .to_return(body: PRIVATE_KEY_XML, status: 200) diff --git a/spec/lib/zanzibar_spec.rb b/spec/lib/zanzibar_spec.rb index fd7c43d..cbafca9 100644 --- a/spec/lib/zanzibar_spec.rb +++ b/spec/lib/zanzibar_spec.rb @@ -109,10 +109,10 @@ describe 'Zanzibar Test' do .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') + 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 diff --git a/zanzibar.gemspec b/zanzibar.gemspec index 7014ec6..6118cd9 100644 --- a/zanzibar.gemspec +++ b/zanzibar.gemspec @@ -14,13 +14,13 @@ Gem::Specification.new do |spec| spec.license = 'Apache 2.0' spec.files = `git ls-files -z`.split("\x0") - spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) } - spec.test_files = spec.files.grep(/^(test|spec|features)\//) + spec.executables = spec.files.grep(%r{^bin\/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)\/}) spec.require_paths = ['lib'] spec.add_development_dependency 'bundler', '~> 1.7' spec.add_development_dependency 'rake', '~> 10.0' - spec.add_development_dependency 'rubocop', '~> 0.28.0' + spec.add_development_dependency 'rubocop', '~> 0.39.0' spec.add_development_dependency 'savon_spec', '~> 0.1.6' spec.add_development_dependency 'rspec', '~> 3.1.0' spec.add_development_dependency 'webmock', '~> 1.20.4' @@ -28,7 +28,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'fakefs', '~> 0.6.4' spec.add_development_dependency 'simplecov', '~> 0.9.1' - spec.add_runtime_dependency 'savon', '~> 2.10.0' - spec.add_runtime_dependency 'rubyntlm', '~> 0.4.0' + spec.add_runtime_dependency 'savon', '~> 2.11.0' + spec.add_runtime_dependency 'rubyntlm', '~> 0.6.0' spec.add_runtime_dependency 'thor', '~> 0.19.0' end