From 3f8f24c40edda023fc7806aae9cf88d6aab801bc Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Mon, 23 Jan 2012 15:10:30 -0800 Subject: [PATCH] Build certificates from execute, not while processing arguments --- lib/rubygems/commands/cert_command.rb | 44 +++++++++++-------- .../test_gem_commands_cert_command.rb | 8 ++-- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/rubygems/commands/cert_command.rb b/lib/rubygems/commands/cert_command.rb index e5f6c43c934f..8d8c9089aad4 100644 --- a/lib/rubygems/commands/cert_command.rb +++ b/lib/rubygems/commands/cert_command.rb @@ -5,7 +5,7 @@ class Gem::Commands::CertCommand < Gem::Command def initialize super 'cert', 'Manage RubyGems certificates and signing settings', - :add => [], :remove => [], :list => [] + :add => [], :remove => [], :list => [], :build => [] OptionParser.accept OpenSSL::X509::Certificate do |certificate| OpenSSL::X509::Certificate.new File.read certificate @@ -36,34 +36,23 @@ def initialize add_option('-b', '--build EMAIL_ADDR', 'Build private key and self-signed', - 'certificate for EMAIL_ADDR.') do |email_address, options| - name = Gem::Security.email_to_name email_address - - key = Gem::Security.create_key - - cert = Gem::Security.create_cert_self_signed name, key - - key_path = Gem::Security.write key, "gem-private_key.pem" - cert_path = Gem::Security.write cert, "gem-public_cert.pem" - - say "Certificate: #{cert_path}" - say "Private Key: #{key_path}" - say "Don't forget to move the key file to somewhere private..." + 'certificate for EMAIL_ADDR') do |email_address, options| + options[:build] << Gem::Security.email_to_name(email_address) end add_option('-C', '--certificate CERT', OpenSSL::X509::Certificate, - 'Certificate for --sign command.') do |cert, options| + 'Signing certificate for --sign') do |cert, options| options[:issuer_cert] = cert end add_option('-K', '--private-key KEY', OpenSSL::PKey::RSA, - 'Private key for --sign command.') do |key, options| + 'Signing key for --sign') do |key, options| options[:issuer_key] = key end - add_option('-s', '--sign NEWCERT', - 'Sign a certificate with my key and', - 'certificate.') do |cert_file, options| + add_option('-s', '--sign CERT', + 'Signs CERT with the key from -K', + 'and the certificate from -C') do |cert_file, options| cert = OpenSSL::X509::Certificate.new File.read cert_file permissions = File.stat(cert_file).mode & 0777 @@ -97,6 +86,23 @@ def execute say certificate.subject.to_s end end + + options[:build].each do |name| + build name + end + end + + def build name + key = Gem::Security.create_key + + cert = Gem::Security.create_cert_self_signed name, key + + key_path = Gem::Security.write key, "gem-private_key.pem" + cert_path = Gem::Security.write cert, "gem-public_cert.pem" + + say "Certificate: #{cert_path}" + say "Private Key: #{key_path}" + say "Don't forget to move the key file to somewhere private!" end def certificates_matching filter diff --git a/test/rubygems/test_gem_commands_cert_command.rb b/test/rubygems/test_gem_commands_cert_command.rb index 1bcc6438617c..bbade9c371fd 100644 --- a/test/rubygems/test_gem_commands_cert_command.rb +++ b/test/rubygems/test_gem_commands_cert_command.rb @@ -95,10 +95,10 @@ def test_execute_add_twice end def test_execute_build + @cmd.handle_options %W[--build nobody@example.com] + use_ui @ui do - Dir.chdir @tempdir do - @cmd.send :handle_options, %W[--build nobody@example.com] - end + @cmd.execute end output = @ui.output.split "\n" @@ -108,7 +108,7 @@ def test_execute_build assert_equal "Private Key: #{File.join @tempdir, 'gem-private_key.pem'}", output.shift - assert_equal "Don't forget to move the key file to somewhere private...", + assert_equal "Don't forget to move the key file to somewhere private!", output.shift assert_empty output