ssl_certifier 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe OpenURI do
4
+ context "SSL operations" do
5
+ before(:each) do
6
+ @proxies = %w[http_proxy HTTP_PROXY https_proxy HTTPS_PROXY ftp_proxy FTP_PROXY no_proxy]
7
+ @old_proxies = @proxies.map {|k| ENV[k] }
8
+ @proxies.each {|k| ENV[k] = nil }
9
+ end
10
+
11
+ after(:each) do
12
+ @proxies.each_with_index {|k, i| ENV[k] = @old_proxies[i] }
13
+ end
14
+
15
+ it 'should validate with ca_cert specified' do
16
+ with_https do |srv, dr, url|
17
+ cacert_filename = "#{dr}/cacert.pem"
18
+ open(cacert_filename, "w") {|f| f << CA_CERT }
19
+ open("#{dr}/data", "w") {|f| f << "ddd" }
20
+ open("#{url}/data", :ssl_ca_cert => cacert_filename) do |f|
21
+ f.status[0].should == "200"
22
+ f.read.should == "ddd"
23
+ end
24
+ open("#{url}/data", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) do |f|
25
+ f.status[0].should == "200"
26
+ f.read.should == "ddd"
27
+ end
28
+
29
+ lambda { open("#{url}/data") {} }.should raise_error(OpenSSL::SSL::SSLError)
30
+ end
31
+ end
32
+
33
+ it 'should work via proxy' do
34
+ with_https do |srv, dr, url|
35
+ cacert_filename = "#{dr}/cacert.pem"
36
+ open(cacert_filename, "w") {|f| f << CA_CERT }
37
+ cacert_directory = "#{dr}/certs"
38
+ Dir.mkdir cacert_directory
39
+ hashed_name = "%08x.0" % OpenSSL::X509::Certificate.new(CA_CERT).subject.hash
40
+ open("#{cacert_directory}/#{hashed_name}", "w") {|f| f << CA_CERT }
41
+
42
+ prxy = WEBrick::HTTPProxyServer.new({
43
+ :ServerType => Thread,
44
+ :Logger => WEBrick::Log.new(NullLog),
45
+ :AccessLog => [[sio=StringIO.new, WEBrick::AccessLog::COMMON_LOG_FORMAT]],
46
+ :BindAddress => '127.0.0.1',
47
+ :Port => 0})
48
+ _, p_port, _, p_host = prxy.listeners[0].addr
49
+
50
+ begin
51
+ th = prxy.start
52
+ open("#{dr}/proxy", "w") {|f| f << "proxy" }
53
+
54
+ open("#{url}/proxy", :proxy=>"http://#{p_host}:#{p_port}/", :ssl_ca_cert => cacert_filename) do |f|
55
+ f.status[0].should == "200"
56
+ f.read.should == "proxy"
57
+ end
58
+ sio.string.should match %r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ]
59
+ sio.truncate(0); sio.rewind
60
+
61
+ open("#{url}/proxy", :proxy=>"http://#{p_host}:#{p_port}/", :ssl_ca_cert => cacert_directory) do |f|
62
+ f.status[0].should == "200"
63
+ f.read.should == "proxy"
64
+ end
65
+ sio.string.should match %r[CONNECT #{url.sub(%r{\Ahttps://}, '')} ]
66
+ sio.truncate(0); sio.rewind
67
+ ensure
68
+ prxy.shutdown
69
+ end
70
+ end
71
+ end
72
+
73
+ #TODO: make this a better URL
74
+ it 'should validate without ca_cert specified' do
75
+ with_https do |srv, dr, url|
76
+ open("https://github.com/wingrunr21/ssl_certifier/raw/master/spec/data.txt") do |f|
77
+ f.status[0].should == "200"
78
+ f.read.should == "ddd"
79
+ end
80
+ open("https://github.com/wingrunr21/ssl_certifier/raw/master/spec/data.txt", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE) do |f|
81
+ f.status[0].should == "200"
82
+ f.read.should == "ddd"
83
+ end
84
+
85
+ #lambda { open("#{url}/data") {} }.should_not raise_error(OpenSSL::SSL::SSLError)
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,73 @@
1
+ require 'open-uri'
2
+ require 'openssl'
3
+ require 'webrick'
4
+ require 'webrick/https'
5
+ require 'webrick/httpproxy'
6
+ require 'stringio'
7
+ require 'zlib'
8
+ require 'ssl_certifier'
9
+
10
+ dr = File.dirname(File.expand_path(__FILE__))
11
+
12
+ #Read in various files needed for SSL
13
+ SERVER_CERT = File.read(File.join(dr, 'certs', 'server_cert.pem'))
14
+ SERVER_KEY = File.read(File.join(dr, 'certs', 'server_key'))
15
+ CA_CERT = File.read(File.join(dr, 'certs', 'ca_cert.pem'))
16
+
17
+ #NullLog
18
+ NullLog = Object.new
19
+ def NullLog.<<(arg)
20
+ end
21
+
22
+ #Various with methods from the open-uri unit tests
23
+ def with_http
24
+ Dir.mktmpdir {|dr|
25
+ srv = WEBrick::HTTPServer.new({
26
+ :DocumentRoot => dr,
27
+ :ServerType => Thread,
28
+ :Logger => WEBrick::Log.new(NullLog),
29
+ :AccessLog => [[NullLog, ""]],
30
+ :BindAddress => '127.0.0.1',
31
+ :Port => 0})
32
+ _, port, _, host = srv.listeners[0].addr
33
+ begin
34
+ th = srv.start
35
+ yield srv, dr, "http://#{host}:#{port}"
36
+ ensure
37
+ srv.shutdown
38
+ end
39
+ }
40
+ end
41
+
42
+ def with_env(h)
43
+ begin
44
+ old = {}
45
+ h.each_key {|k| old[k] = ENV[k] }
46
+ h.each {|k, v| ENV[k] = v }
47
+ yield
48
+ ensure
49
+ h.each_key {|k| ENV[k] = old[k] }
50
+ end
51
+ end
52
+
53
+ def with_https
54
+ Dir.mktmpdir {|dr|
55
+ srv = WEBrick::HTTPServer.new({
56
+ :DocumentRoot => dr,
57
+ :ServerType => Thread,
58
+ :Logger => WEBrick::Log.new(NullLog),
59
+ :AccessLog => [[NullLog, ""]],
60
+ :SSLEnable => true,
61
+ :SSLCertificate => OpenSSL::X509::Certificate.new(SERVER_CERT),
62
+ :SSLPrivateKey => OpenSSL::PKey::RSA.new(SERVER_KEY),
63
+ :BindAddress => '127.0.0.1',
64
+ :Port => 0})
65
+ _, port, _, host = srv.listeners[0].addr
66
+ begin
67
+ th = srv.start
68
+ yield srv, dr, "https://#{host}:#{port}"
69
+ ensure
70
+ srv.shutdown
71
+ end
72
+ }
73
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ssl_certifier/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ssl_certifier"
7
+ s.version = SslCertifier::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Stafford Brunk"]
10
+ s.email = ["wingrunr21@gmail.com"]
11
+ s.homepage = "https://www.github.com/wingrunr21/ssl_certifier"
12
+ s.summary = %q{Adds root certificates to the OpenURI module so that SSL connections work properly in Ruby 1.9}
13
+ s.description = %q{Adds root certificates to the OpenURI module so that SSL connections work properly in Ruby 1.9. This gem allows for SSL connections to function properly even when Ruby does not have access to the operating system's default root certificates}
14
+
15
+ s.rubyforge_project = "ssl_certifier"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency "rspec", "~> 2.5.0"
23
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssl_certifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stafford Brunk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-17 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &2154644380 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.5.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2154644380
26
+ description: Adds root certificates to the OpenURI module so that SSL connections
27
+ work properly in Ruby 1.9. This gem allows for SSL connections to function properly
28
+ even when Ruby does not have access to the operating system's default root certificates
29
+ email:
30
+ - wingrunr21@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - .gemtest
36
+ - .gitignore
37
+ - Gemfile
38
+ - README.rdoc
39
+ - Rakefile
40
+ - certs/cacert.pem
41
+ - lib/ssl_certifier.rb
42
+ - lib/ssl_certifier/open-uri.rb
43
+ - lib/ssl_certifier/version.rb
44
+ - spec/certs/ca_cert.pem
45
+ - spec/certs/server_cert.pem
46
+ - spec/certs/server_key
47
+ - spec/data.txt
48
+ - spec/open-uri_spec.rb
49
+ - spec/open-uri_ssl_spec.rb
50
+ - spec/spec_helper.rb
51
+ - ssl_certifier.gemspec
52
+ has_rdoc: true
53
+ homepage: https://www.github.com/wingrunr21/ssl_certifier
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project: ssl_certifier
73
+ rubygems_version: 1.6.2
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Adds root certificates to the OpenURI module so that SSL connections work
77
+ properly in Ruby 1.9
78
+ test_files:
79
+ - spec/certs/ca_cert.pem
80
+ - spec/certs/server_cert.pem
81
+ - spec/certs/server_key
82
+ - spec/data.txt
83
+ - spec/open-uri_spec.rb
84
+ - spec/open-uri_ssl_spec.rb
85
+ - spec/spec_helper.rb