How to use Ruby's XMLRPC::Client::new3 - ruby

I'm trying to sort out how to use the new3 method of the XMLRPC::Client class. This is what I have:
#!/usr/bin/ruby
require "xmlrpc/client"
params = {
host: "https://192.168.1.2",
path: "rpc/api",
port: "443",
proxy_host: "",
proxy_port: "",
user: "username",
password: "password",
use_ssl: "true",
timeout: 300,
}
session = XMLRPC::Client.new3(params)
session.instance_variable_get(:#http).instance_variable_set(:#verify_mode, OpenSSL::SSL::VERIFY_NONE)
sys_ver = session.call('api.systemVersion')
users = session.call("user.listUsers", session)
If I use the new2 method the script will connect and return a 404 error. However, if I use the new3 it returns
/usr/lib/ruby/1.9.1/net/http.rb:762:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `open'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
from /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
from /usr/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `connect'
from /usr/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
from /usr/lib/ruby/1.9.1/net/http.rb:750:in `start'
from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:535:in `do_rpc'
from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:420:in `call2'
from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:410:in `call'
from sat_test.rb:24:in `<main>'
Am I using the new3 method properly?
Also, if I use the new method instead, do I need to set parameter=value for each parameter (proxy_host=, proxy_port=, etc)?

The problem seemed to be more with my script being coded wrong. This is what I have now:
#!/usr/bin/ruby
require "xmlrpc/client"
params = {
host: "REDACTED",
path: "/rpc/api",
use_ssl: "true",
user: "REDACTED",
pass: "REDACTED"
}
client = XMLRPC::Client.new3(params)
client.instance_variable_get(:#http).instance_variable_set(:#verify_mode, OpenSSL::SSL::VERIFY_NONE)
session = client.call('auth.login', params[:user], params[:pass])
sys_ver = session.call('api.systemVersion', session)
users = session.call("user.listUsers", session)
users.each { |uname| puts uname }
I've removed unnecessary arguments in the params hash and made sure everything is properly quoted.
It works and is only leaving me with a 404 error which is a problem with the script. But, at least it connects now and gives me something.

Related

opensearch-ruby error on verifying ssl certificate: certificate verify failed (Hostname mismatch) (Faraday::SSLError)

I have a simple script and I am trying to perform using the opensearch-ruby client a simple search on my index. If I try to perform the query with ssl verification, my request succeeds. However, I am not able to perform the query if I enforce ssl verification. My request is failing saying that the certificate verify failed (Hostname mismatch). Below you can have a look on my script and the stacktrace after trying to execute my script.
Note that when I generate my certificate I use as CN=my.host.com
Do I miss something in my configuration when I am creating my client?
client = OpenSearch::Client.new(host: 'my.host.com',
port: 9200,
user: 'admin',
password: 'mypassword',
scheme: 'https',
transport_options: { ssl: {
verify: true,
ca_file: '/certs/root-ca.pem' } })
index_name = "my-index"
term = "Joe D"
query = {
'multi_match': {
'query': term,
'fields': ['name']
}
}
response = client.search(
body: query,
index: index_name
)
puts JSON.pretty_generate(response)
C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/net-protocol-0.1.2/lib/net/protocol.rb:46:in `connect_nonblock': SSL_connect returned=1 errno=0 state=error: certificate verify failed (Hostname mismatch) (Faraday::SSLError)
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/net-protocol-0.1.2/lib/net/protocol.rb:46:in `ssl_socket_connect'
from C:/Ruby30-x64/lib/ruby/3.0.0/net/http.rb:1038:in `connect'
from C:/Ruby30-x64/lib/ruby/3.0.0/net/http.rb:970:in `do_start'
from C:/Ruby30-x64/lib/ruby/3.0.0/net/http.rb:965:in `start'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/net-http-persistent-4.0.1/lib/net/http/persistent.rb:655:in `start'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/net-http-persistent-4.0.1/lib/net/http/persistent.rb:595:in `connection_for'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/net-http-persistent-4.0.1/lib/net/http/persistent.rb:885:in `request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/faraday-net_http_persistent-1.2.0/lib/faraday/adapter/net_http_persistent.rb:71:in `perform_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:66:in `block in call'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/faraday-1.10.0/lib/faraday/adapter.rb:50:in `connection'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:64:in `call'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/faraday-1.10.0/lib/faraday/rack_builder.rb:154:in `build_response'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/faraday-1.10.0/lib/faraday/connection.rb:516:in `run_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-transport-2.0.0/lib/opensearch/transport/transport/http/faraday.rb:56:in `block in perform_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-transport-2.0.0/lib/opensearch/transport/transport/base.rb:297:in `perform_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-transport-2.0.0/lib/opensearch/transport/transport/http/faraday.rb:45:in `perform_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-transport-2.0.0/lib/opensearch/transport/client.rb:193:in `perform_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-ruby-2.0.2/lib/opensearch.rb:92:in `open_search_validation_request'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-ruby-2.0.2/lib/opensearch.rb:58:in `verify_open_search'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-ruby-2.0.2/lib/opensearch.rb:47:in `method_missing'
from C:/Ruby30-x64/lib/ruby/gems/3.0.0/gems/opensearch-api-2.0.2/lib/opensearch/api/actions/search.rb:106:in `search'
from C:/Users/vasgat/RubymineProjects/OpenSearchTest/myclient.rb:68:in `<main>'

Ruby net/http can't connect to a TLS proxy

Rubys net/http allows you to specify a proxy to route all requests through like this:
Net::HTTP.new("host", "443", "proxyHost", 8080, "proxyUsername", "password")
This works fine when used with HTTP proxy however fails when a TLS proxy used instead of HTTP:
def tls_proxy(data)
uri = URI("https://example.com/post")
client = Net::HTTP.new("host", "443", "proxyHost", 8443, "proxyUsername", "password")
client.ssl_version = :TLSv1_2
client.use_ssl = true
request = Net::HTTP::Post.new(uri.path)
request['Content-type'] = 'application/json'
request.body = data
client.request(request)
end
Uncaught exception: end of file reached
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/protocol.rb:225:in `rbuf_fill'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/protocol.rb:191:in `readuntil'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/protocol.rb:201:in `readline'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http/response.rb:40:in `read_status_line'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http/response.rb:29:in `read_new'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:969:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:930:in `do_start'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:919:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/net/http.rb:1470:in `request'
/Users/laptop/Projects/tls-proxy-ruby/example-https.rb:47:in `tls_proxy'
/Users/laptop/Projects/tls-proxy-ruby/example-https.rb:60:in `<top (required)>'
Is there a way to make it work with TLS proxy or perhaps there is an alternative to this library that supports TLS proxy?
You could try httparty gem instead.
require 'httparty'
options = {
http_proxyaddr: '127.0.0.1',
http_proxyport: '8443',
http_proxyuser: 'johndoe',
http_proxypass: '123456'
}
response = HTTParty.get('https://example.com', options)

SSL_connect returned=1 errno=0 state=unknown state: unknown protocol

After reading through many posts I couldn't find a solution to my problem. I've made a test connection with www.smtper.net which was successful. My error.log only tells me the error:
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=unknown state: unknown protocol
app/controllers/passwords_controller.rb:11:in `create'
Maybe it has something to do with my nginx settings?
Issue
I'm trying to configure my smtp via mailgun (or gmail) but without success.
My configuration in developement.rb looks like:
config.action_mailer.default_url_options = { :host => "xxxxx:8765" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "mail.google.com",
:user_name => "xxx#gmail.com",
:password => "xxxxxx",
:authentication => :plain,
:enable_starttls_auto => true
}
And the error is thrown at the action create, line 11:
AccountMailer.password_reset_email(#account).deliver
I have installed OpenSSL 1.0.1f on my development server
TRACE
/root/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/protocol.rb:44:in `connect_nonblock'
/root/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/protocol.rb:44:in `ssl_socket_connect'
/root/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/smtp.rb:584:in `tlsconnect'
/root/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/smtp.rb:552:in `do_start'
/root/.rbenv/versions/2.5.1/lib/ruby/2.5.0/net/smtp.rb:518:in `start'
mail (2.7.0) lib/mail/network/delivery_methods/smtp.rb:109:in `start_smtp_session'
mail (2.7.0) lib/mail/network/delivery_methods/smtp.rb:100:in `deliver!'
mail (2.7.0) lib/mail/message.rb:2160:in `do_delivery'
mail (2.7.0) lib/mail/message.rb:260:in `block in deliver'
actionmailer (4.2.8) lib/action_mailer/base.rb:543:in `block in deliver_mail'
activesupport (4.2.8) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.8) lib/active_support/notifications.rb:164:in `instrument'
actionmailer (4.2.8) lib/action_mailer/base.rb:541:in `deliver_mail'
mail (2.7.0) lib/mail/message.rb:260:in `deliver'
actionmailer (4.2.8) lib/action_mailer/message_delivery.rb:85:in `deliver_now'
actionmailer (4.2.8) lib/action_mailer/message_delivery.rb:105:in `deliver'
app/controllers/passwords_controller.rb:11:in `create'
What happens if you try this settings and get rid of everything else you currently have:
# /config/environments/development.rb
# Gmail configuration
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: ENV['EMAIL_USER'],
password: ENV['EMAIL_PASS'],
authentication: 'plain',
enable_starttls_auto: true
}
I'm not sure you need to monkey with smtper.net but perhaps see this, even though it's very old, it probably should still work:
http://blog.napcs.com/2013/07/21/rails_ssl_simple_wa/
You'll need to set those environment variables as you obviously should never want to commit credentials in your git repo.
But also see these posts as you may need to do some other things for gmail
(Ruby) Getting Net::SMTP working with Gmail...?
Rails 4, how to correctly configure smtp settings (gmail)

Error after trying to access xml (Zlib::BufError) (Ruby)

I keep getting and error whenever I try and access an XML file in Ruby. This is my code:
require 'rubygems'
require 'net/http'
require 'uri'
require 'json'
require 'open-uri'
url = 'http://access.alchemyapi.com/calls'
service = '/text/TextGetRankedTaxonomy'
apikey = '?apikey=4317fce9281094613deee9ebcc5aaf5238cd0748'
thething = '&text='
text = 'men%27s%20white%20crew%20neck%20shirt'
fullurl = url + service + apikey + thething + text
opener = open(fullurl) {|f| f.read }
Here is the error:
C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:357:in `finish': buffer error (Zlib::BufError)
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:357:in `finish'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:262:in `ensure in inflater'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:262:in `inflater'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:274:in `read_body_0'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:201:in `read_body'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:226:in `body'
from C:/Ruby21/lib/ruby/2.1.0/net/http/response.rb:163:in `reading_body'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1420:in `block in transport_request'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1411:in `catch'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1411:in `transport_request'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:1384:in `request'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:509:in `block in post_form'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:853:in `start'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:583:in `start'
from C:/Ruby21/lib/ruby/2.1.0/net/http.rb:507:in `post_form'
from C:/Users/KVadher/Desktop/test151:11:in `<main>'
Is there anything I can do to either solve the error or run past it?
There is something wrong with the way this API server is encoding the data. To bypass this, you can simple say in the HTTP header that you don't accept any kind of encoding:
opener = open(fullurl, 'Accept-Encoding' => '') {|f| f.read }

Error while using Net::HTTP GET request

I am new to ruby . I have the following Code snippet which performs a GET operation and retrieves the names of my facebook groups
def get_groups
query=("SELECT gid,name FROM group where gid in(SELECT gid from group_member where uid=me)")
uri=URI("https://graph.facebook.com/fql")
params={'q'=>query,'access_token'=>TOKEN}
uri.query=URI.encode_www_form(params)
response=Net::HTTP.get_response(uri)
result=json.loads(response.text)
return result['data']
end
But when I execute the above code I get the following errors:
/usr/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)
from /usr/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill'
from /usr/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil'
from /usr/lib/ruby/1.9.1/net/protocol.rb:132:in `readline'
from /usr/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line'
from /usr/lib/ruby/1.9.1/net/http.rb:2551:in `read_new'
from /usr/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request'
from /usr/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
from /usr/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
from /usr/lib/ruby/1.9.1/net/http.rb:1293:in `request'
from /usr/lib/ruby/1.9.1/net/http.rb:1195:in `request_get'
from /usr/lib/ruby/1.9.1/net/http.rb:455:in `block in get_response'
from /usr/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /usr/lib/ruby/1.9.1/net/http.rb:454:in `get_response'
from fb.rb:12:in `get_groups'
from fb.rb:32:in `<main>'
What is the mistake that I am making here?
I believe the issue here is you're using an HTTP library to access an HTTPS service. These are fundamentally different things. Here's an HTTPS example:
require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.example.com', 443)
http.use_ssl = true
http.ssl_version = :TLSv1
http.ciphers = "ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:-LOW"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
req = Net::HTTP::Get.new('/', {'Content-Type' =>'application/json'})
http.start {|http| http.request(req) }
It seems like you have SSL certificate validation issue. Try to turn off SSL validation.
Net::HTTP.verify_mode = OpenSSL::SSL::VERIFY_NONE
If above helps you, possible root of the issue is current OpenSSL installation. Try to update it to the latest version.

Resources