Mechanize getting "Errno::ECONNRESET: Connection reset by peer - SSL_connect" - ssl

I'm unable to get Mechanize to load a page that used to work -- it's reliably failing with a Errno: ECONNRESET: Connection reset by peer - SSL_connect message. Any suggestions as to what I should try or details I should look at? (Please see "what I've tried" below...)
Update 1
Taking a hint from a related S.O. post, I tried accessing the site directly with Net::HTTP. When I set http.ssl_version = :TLSv1, I get a redirect rather than an error (as it should be). So my question becomes: how can I configure Net::HTTP's underlying ssl_version parameters from within Mechanize?
Thanks...
The symptoms:
$ rails console
>> a = Mechanize.new
=> #<Mechanize:0x007fd26789b8e0 ...>
>> p = a.get("http://sce.com")
# (...after a long pause...)
Errno::ECONNRESET: Connection reset by peer - SSL_connect
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
from /sandbox/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /sandbox/usr/lib/ruby/2.0.0/net/http.rb:857:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:691:in `start'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/net-http-persistent-2.9/lib/net/http/persistent.rb:631:in `connection_for'
...
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:257:in `fetch'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:974:in `response_redirect'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize/http/agent.rb:298:in `fetch'
from /sandbox/usr/lib/ruby/gems/2.0.0/gems/mechanize-2.7.2/lib/mechanize.rb:432:in `get'
from (irb):3
The environment:
$ rake about
About your application's environment
Ruby version 2.0.0 (x86_64-darwin12.4.0)
RubyGems version 2.1.9
Rack version 1.5
Rails version 4.0.0
JavaScript Runtime JavaScriptCore
Active Record version 4.0.0
Action Pack version 4.0.0
Action Mailer version 4.0.0
Active Support version 4.0.0
Middleware ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd423c50e0>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
Application root /Users/me/MyProject
Environment development
Database adapter postgresql
Database schema version 20131017201057
$ openssl version
OpenSSL 1.0.1e 11 Feb 2013
$ system_profiler -detailLevel mini SPSoftwareDataType
System Software Overview:
System Version: OS X 10.8.5 (12F45)
Kernel Version: Darwin 12.5.0
Time since boot: 8 days 7:40
What I've tried:
I've tried the same URL from a Firefox web browser. It works.
I've explicitly set the Mechanize request_headers to exactly mimic the Firefox browser. No change.
I've updated my ssl/cert.pem file (a second time) as described in this S.O post. No change.
I've tried different sites: www.pge.com and www.sdge.com. Both work. There's something different about the www.sce.com site.
Success with Net::HTTP
Here's an example of Net::HTTP working:
$ irb
>> require 'net/https'
=> true
>> require 'uri'
=> false
>> uri = URI.parse("https://www.sce.com/")
=> #<URI::HTTPS:0x007facab8f6ba0 URL:https://www.sce.com/>
>> http = Net::HTTP.new(uri.host, uri.port)
=> #<Net::HTTP www.sce.com:443 open=false>
>> http.use_ssl = true
=> true
>> http.ssl_version = :TLSv1 # <= this line makes all the difference
=> :TLSv1
>> r = http.start { |agent| p agent.get(uri.path) }
=> #<Net::HTTPFound 302 Found readbody=true>
>> r.to_hash
=> {"content-language"=>["en-US"], "date"=>["Fri, 18 Oct 2013 01:00:07 GMT"], "location"=>["https://www.sce.com/wps/portal/home/!ut/p/b1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOIt3Q1cPbz8DTzdQwKNDTyNAw38gh0djQ0MzIAKIoEKDHAARwNC-sP1o8BK8Jjg55Gfm6pfkBthoOuoqAgAgIrzaA!!/dl4/d5/L2dBISEvZ0FBIS9nQSEh/"], "p3p"=>["CP=\"NON CUR OTPi OUR NOR UNI\""], "server"=>["IBM_HTTP_Server"], "transfer-encoding"=>["chunked"], "x-powered-by"=>["Servlet/3.0"], "set-cookie"=>["PD_STATEFUL_d55ece64-8d9a-11e2-84a1-0050560010d6=%2Fwps; Path=/", "session_www=740796608.47873.0000; path=/"]}

The folks on the Mechanize mailing list kindly provided the answer:
agent = Mechanize.new do |a|
a.ssl_version = :TLSv1
end

Related

Rails 5 Server Issues (Windows 10)

Newbie to Ruby on Rails here, I just did a fresh install of Ruby & Rails on Windows 10 (Bash on Ubuntu shell). I seems to having issues on starting the Rails server. Already checked the other threads on the issue, but everyone else is using OSX. :( Hope someone can help!
When running rails s,
root#TEAMROCKETHQ:~/newapp# rails s
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/railties-5.0.2/lib/rails/app_loader.rb:40: warning: Insecure world writable dir /root/.rbenv/versions in PATH, mode 040777
=> Booting Puma
=> Rails 5.0.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.0 (ruby 2.4.0-p0), codename: Sassy Salamander
* Min threads: 5, max threads: 5
Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
For the first line issue, I tried:
sudo chmod 775 /usr/local
it seems it didn't do anything. Maybe I'm reading the error wrong, but I don't know... When I go to localhost:3000, it returns this error:
=> Booting Puma
=> Rails 5.0.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.0 (ruby 2.4.0-p0), codename: Sassy Salamander
* Min threads: 5, max threads: 5
Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
2017-03-09 23:17:26 -0600: Read error: #<Errno::EINVAL: Invalid argument - getsockopt(2)>
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.8.0/lib/puma/server.rb:124:in `getsockopt'
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.8.0/lib/puma/server.rb:124:in `closed_socket?'
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.8.0/lib/puma/server.rb:563:in `handle_request'
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.8.0/lib/puma/server.rb:425:in `process_client'
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.8.0/lib/puma/server.rb:289:in `block in run'
/root/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/puma-3.8.0/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
Webpage:
The localhost page isn't working
localhost is currently unable to handle this request.
HTTP ERROR 500
It is because puma 3.8.0.
Put gem 'puma', '3.7.1' in your gem file, and bundle install again.
And gem uninstall puma at version 3.8.0, wait until there is a fix.

Puppet: rake spec could not find class ::splunk

I'm trying to tests my puppet modules, but I get an error message that it can't find class.
I've written a couple of internally used Puppet modules; now that I have my head around puppet, I want to write tests. Since I ran into issues, I decided to essentially start over with a new clean module to figure out what I need to do.
$puppet module generate wet-splunk
<<snip interactive questions>>
$cd splunk
$rake spec
<path snip>ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F
Failures:
1) splunk with defaults for all parameters should contain Class[splunk]
Failure/Error: it { should contain_class('splunk') }
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
# ./spec/classes/init_spec.rb:5:in `block (3 levels) in <top (required)>'
Finished in 0.21712 seconds (files took 3.14 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:5 # splunk with defaults for all parameters should contain Class[splunk]
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color failed
Checking the files though I think they contain what they should:
manifests/init.pp
# <snip documentation boilterplate>
class splunk {
}
spec/classes/init_spec.rb
require 'spec_helper'
describe 'splunk' do
context 'with defaults for all parameters' do
it { should contain_class('splunk') }
end
end
The Could not find class ::splunk is the part that has me confused.
The way I read the spec file is it looking inside init for class{'splunk': ... } That could be me misreading it as it seems odd they would start with a broken spec file, but just in case I tried testing for package instead.
manifests/init.pp
class splunk {
package{'splunk':
ensure => 'present',
}
}
spec/classes/init_spec.rb
require 'spec_helper'
describe 'splunk' do
context 'with defaults for all parameters' do
it { should contain_package('splunk') }
end
end
I still get a similar error message:
$ rake spec
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F
Failures:
1) splunk with defaults for all parameters should contain Package[splunk]
Failure/Error: it { should contain_package('splunk') }
Puppet::PreformattedError:
Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
# ./spec/classes/init_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.21168 seconds (files took 3.17 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:6 # splunk with defaults for all parameters should contain Package[splunk]
Sorry to ask such a basic question; I really appreciate your help. I'd really love links to good tutorials on testing Puppet code; I'd love to use Vagrant to have some confidence my code is actually going to do what I want it to.
Update:
I believe I have followed #Peter_Souter's suggestions; I still get the error that it can't find the class. I'm posting the contents of the files referenced to try and validate I did the right thing and in case there are other errors.
$ cat .fixtures.yml
fixtures:
symlinks:
"splunk": "#{source_dir}"
$ cat spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
$ cat Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]
desc "Validate manifests, templates, and ruby files"
task :validate do
Dir['manifests/**/*.pp'].each do |manifest|
sh "puppet parser validate --noop #{manifest}"
end
Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
end
Dir['templates/**/*.erb'].each do |template|
sh "erb -P -x -T '-' #{template} | ruby -c"
end
end
$ cat Gemfile
source 'https://rubygems.org'
puppetversion = ENV.key?('PUPPET_VERSION') ? "#{ENV['PUPPET_VERSION']}" : ['>= 3.3']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.8.2'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet', '~> 2.1', :require => false
gem 'rspec-core', '3.1.7', :require => false
The way that rspec-puppet simulates the catalog for testing, it makes a fixtures directory with the required modules in it. So right now it's running it on an empty directory so it can't find your module. We generally fix that with the puppetlabs-spec-helper which makes a simlink to your current repository in that fixtures directory.
I'd recommend adding the following into your files:
# .fixtures.yml
fixtures:
symlinks:
"splunk": "#{source_dir}"
# spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
# Rakefile
require 'puppetlabs_spec_helper/rake_tasks'
# Gemfile
gem 'rspec-puppet', '~> 2.1', :require => false
gem 'rspec-core', '3.1.7', :require => false
Can you try that and tell me if it works for you?
If somebody stumble around this Thread as I do. The for me correct answer is given in:
Why do I get puppet-rspec 'class does not exist' when it does?
means you have to do:
puppet module generate foo-bar
and then to fix this issue
bundle exec rspec-puppet-init
It's not nice but better then handling with hidden dot files.

Trouble running Jasmine specs headless

I'm trying to get Jasmine to run in the command line (headless), but have had no success. With our current configuration:
Gemfile:
gem 'jasmine', :git => "git://github.com/pivotal/jasmine-gem.git"
gem 'jasmine-headless-webkit', :git => "git://github.com/johnbintz/jasmine-headless-webkit.git"
My current configuration of jasmine specs runs in the browser. rake jasmine:ci, however, opens a browser, and then aborts shortly thereafter with this error message:
/Users/`<me>`/.rvm/gems/ruby-2.0.0-p353/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/firefox/launcher.rb:79:in `connect_until_stable': unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) (Selenium::WebDriver::Error::WebDriverError)
I looked at the github page for jasmine-headless-webkit, and saw that the project is listed as dead. So! I tried to replace the jasmine & jasmine-headless-webkit gem with jasmine-rails, and I got this error:
ERROR: TypeError: 'undefined' is not an object (evaluating 'jasmine.Matchers.prototype')
TRACE:
-> file:///Users/<me>/<myapp>/tmp/jasmine/assets/jasmine-specs.js: 711
-> file:///Users/<me>/<myapp>/tmp/jasmine/assets/jasmine-specs.js: 736
-> file:///Users/<me>/<myapp>/tmp/jasmine/assets/jasmine-specs.js: 738
-> file:///Users/<me>/<myapp>/tmp/jasmine/assets/jasmine-specs.js: 862
rake aborted!
Error executing command: "/Users/<me>/.phantomjs/1.9.7/darwin/bin/phantomjs" "/Users/<me>/.rvm/gems/ruby-2.0.0-p353/gems/jasmine-rails-0.9.0/lib/jasmine_rails/../assets/javascripts/jasmine-runner.js" "/Users/<me>/<myapp>/tmp/jasmine/runner.html?spec="
Since most users seemed to want to run their jasmine specs using phantom, as part of the 2.0 release we made that the default. The ability to run jasmine:ci with selenium was extracted into its own gem.

osx 10.9 php 5.4 pecl_http - class HttpRequest not found

I am running php 5.4.17 on OS X 10.9.1.
I have installed pear using this command:
wget http://pear.php.net/go-pear.phar
php -d detect_unicode=0 go-pear.phar
Then I have installed pecl_http with:
pecl install pecl_http
I have added extension lines to my php.ini and add 'x' permission to library binaries. If I run php -i I get:
http
HTTP Support => enabled
Extension Version => 2.0.4
Used Library => Compiled => Linked
libz => 1.2.5 => 1.2.5
libcurl => 7.30.0 => 7.30.0
libevent => disabled => disabled
Directive => Local Value => Master Value
http.etag.mode => crc32b => crc32b
But if I try to run php script with HttpRequest inside I still get:
Fatal error: Class 'HttpRequest' not found
Can somebody give me a clue what am I doing wrong?
You are using pecl_http 2.0.4 and probably refer to documentation from http://www.php.net/manual/en/class.httprequest.php which is for older version. There is a post that's already explained your problem:
PECL_HTTP not recognised php ubuntu
In fact, you should refer to the documentation defined here:
http://devel-m6w6.rhcloud.com/mdref/http
To verify: if (class_exists('http\Client\Request')) printf 'pecl_http v2 is installed'

Rails - Curb gem - Failed to allocate memory

During a running task this exception is raised by the Curb gem (https://github.com/taf2/curb).
failed to allocate memory
It's strange because before the Exception the gem works properly (with others curl calls).
Any ideas?
I'm using Ruby 1.9.3 on Ubuntu server (and same error in dev mode locally - Mac OS X)
EDIT
In my task I do very much (+10k) calls like this
req = Curl::Easy.new do |curl|
curl.ssl_verify_peer = false
curl.url = url
curl.headers['Content-type'] = 'application/json'
end
req.perform