Posting a gist to github.com in ruby not working - ruby-on-rails-3

I am trying to create a new gist on github.com by posting the the URL. I have done this in C# and it works fine, but when I try to replicate in ruby on rails the post never seems to work I am always just redirected to the gists/new URL which indicates that the post was not accepted. I figure I am just missing something fundamental in ruby.
require 'net/https'
require 'open-uri'
url = URI.parse('https://gist.github.com/gists')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
req = Net::HTTP::Post.new(url.path)
req.form_data = "file_name[gistfile1]=testclip.txt&description=Test Clip&file_contents[gistfile1]=This is my test clip&login=uname&token=secret"
http.start{|h| h.request(req)}['Location']

I'm out on a limb here, but I'm guessing it has to do with the SSL verify mode. You'll either need to keep from verifying:
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
or give it something to verify against:
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.cert = OpenSSL::X509::Certificate.new(ca_cert)
Take a look at the ca_cert method at Defunkt's Gist gem for more info (you'll need a bundle).

Related

404 when executing docker push to gitlab-container-registry

I have installed gitlab-ce 13.2.0 on my server and the container-registry was immediately available.
from a other sever (or my local machine) I can login, but when pushing a image to the container-registry I get a 404-error: error parsing HTTP 404 response body: invalid character '<' looking for beginning of value: "<!DOCTYPE html>\n<html>\n<head>...
in my gitlab.rb I have:
external_url 'https://git.xxxxxxxx.com'
nginx['enable'] = true
nginx['client_max_body_size'] = '250m'
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/trusted-certs/xxxxxxxx.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/trusted-certs/xxxxxxxx.com.key"
nginx['ssl_protocols'] = "TLSv1.1 TLSv1.2"
registry_external_url 'https://git.xxxxxxxx.com'
what is confusing, is that the registry_external_url is the same as the external_url. There are those lines in the gitlab.rb:
### Settings used by GitLab application
# gitlab_rails['registry_enabled'] = true
# gitlab_rails['registry_host'] = "git.xxxxxxxx.com"
# gitlab_rails['registry_port'] = "5005"
# gitlab_rails['registry_path'] = "/var/opt/gitlab/gitlab-rails/shared/registry"
But when I uncomment this, I cannot login.
what can be the problem here?
This is actually because you are using https port without proxying the registry in nginx.
Fix these lines according to the following in gitlab.rb:
registry_nginx['enable'] = true
registry_nginx['listen_https'] = true
registry_nginx['redirect_http_to_https'] = true
registry_external_url 'https://registry.YOUR_DOMAIN.gtld'
You don't need to touch nginx['ssl_*] parameters when you are using letsencrypt since the chef would take care.
How is your image named? Your image name must match exactly not only the registry URL, but project too.
You can't just build "myimage:latest" and push it. It must be like git.xxxxxxxx.com/mygroup/myproject:latest. You can obtain correct name from $CI_REGISTRY_IMAGE predefined variable.

Rails and Devise Missing link to host

I am very confused with how to provide the default_url_options. I am getting this error
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
I am using spreecommerce which uses devise for authentication. This error is occurring durring password reset on my development environment. I have not tested it in a production environment yet.
I am using this in my environments/development
config.default_url_options = { host: 'localhost:3000' }
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
in my rails console when I do Rails.application.routes.default_url_options I get {:host => Rails.application.config.domain}. The same thing happends when I do Rails.applicaiton.default_url_options
None of the solutions I have found have worked.
TL;DR In my case Spree::Store.current vanished - I had to recreate it.
I tried set default_url_options for routes and into environments. But with no luck.
So I got into spree_auth_devise-3.1.0 gem source code:
#confirmation_url = spree.spree_user_confirmation_url(:confirmation_token => token, :host => Spree::Store.current.url)
So for the host, it's using Spree::Store. Then I went into console and got that my Spree::Store.current is empty:
(byebug) Spree::Store.current.url
nil
So simply creating a store with dummy data resolved my problem.
store = Spree::Store.new
store.name = 'test'
store.url = 'http://localhost:3000'
store.code = 'spree'
store.default = true
store.save

Rspec for REST API call

Im trying to implement rspec for a http call... But I have not succeeded so far..
http = Net::HTTP.new("secured site url", "443")
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.get("/query params",initheader = {'Accept' =>'application/xml'})
return response
I would need an rspec for this..Though Im new to this, I tried many ways but it says expected: 1 time received: 0 times
Here is all what I have written :
it "should be success" do
#mock_http = mock("http")
#mock_http.should_receive(:new).with("secured site url")
end
Please correct me if Im wrong.. Any help is appreciated! Thanks
Of course you have to activate the method in which the new statement is called so your code should look something like:
it "should be success" do
#mock_http = mock("http")
Net::HTTP.should_receive(:new).with("secured site url", "443")
some_method
end
def some_method
http = Net::HTTP.new("secured site url", "443")
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.get("/query params",initheader = {'Accept' =>'application/xml'})
return response
end

getting bad request from ruby on rails ssl post

Looked at: Escaping parameters in set_form_data POST, and Ruby on Rails HTTPS Post Bad Request
def send_request(params)
host = "https://hc.mercurydev.net"
uri = URI.parse(host)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
xml = build_xml_for_checkout(params)
http.start do |http|
req = Net::HTTP::Post.new('http://www.mercurypay.com/InitializePayment')
header = {'Host' => 'hc.mercurydev.net', 'Content-Type' => 'text/xml; charset=utf-8', 'Content-Length' => 'length', 'SOAPAction' => 'http://www.mercurypay.com/InitializePayment'}
req.set_form_data(header, xml)
response = http.request(req)
end
end
This is the first time I have had to build my own post request in ruby, I am sure that I am missing something simple, but what?
-- I have confirmation that the xml is 100% good, so it has to be something in my header or in how I'm building the request.
Edit: After looking at ruby-api-doc: I came up with this:
uri = URI.parse(host)
Net::HTTP.start(uri.hostname, uri.port,
:use_ssl => uri.scheme == 'https').start do |http|
req = Net::HTTP::Post.new uri.path
req.body = xml
req.set_form_data(header)
res = http.request req
end
Which returns (even if I restart my server) that I have an open HTTP session.
So ended up with this:
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Post.new(uri.request_uri)
req.body = xml.to_s
req.initialize_http_header(header)
response = http.request(req)
The key that I was missing was the initialize_http_header() once I got that in there the soap api accepted my submission and then is was just an issue of making sure my syntax was correct.

How to perform 301 before force_ssl? Rails 3.1

I need to make http://example.com go to https://www.example.com. right now it's warning in the browser. I followed: http://www.simonecarletti.com/blog/2011/05/configuring-rails-3-https-ssl/
loading from /lib/middleware (Rails 3.1)
class WwwMiddlewareRedirect
def initialize(app)
#app = app
end
def call(env)
request = Rack::Request.new(env)
if request.host.starts_with?("example.com")
[301, {"Location" => request.url.sub("//", "//www.")}, self]
else
#app.call(env)
end
end
def each(&block)
end
end
production env
Example::Application.configure do
config.force_ssl = true
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
config.middleware.use "WwwMiddlewareRedirect"
end
If you can get your webserver to do the redirect, that's usually best. If you're on Heroku or a similar platform where you can't get the webserver to redirect for you, make these changes:
Change the middleware line to be:
config.middleware.insert_before Rack::SSL, "WwwMiddlewareRedirect"
Then in your gemfile, include:
gem 'rack-ssl', :require => 'rack/ssl'
Unless I'm misunderstanding, I think you are using the < 3.1 instructions for a 3.1 app. In 3.1 you only need to set force_ssl to true and it will result in the behavior you desire.