Commit 28dde4ec authored by Samir Noir's avatar Samir Noir 🧀
Browse files

Fix http requests done in deployment model

parent 11c6dc00
......@@ -68,12 +68,12 @@ class JobsController < ApplicationController
job = OAR::Job.find(params[:id])
authorize!(job.user)
uri = URI(uri_to(
uri = uri_to(
site_path(
params[:site_id]
)+"/internal/oarapi/jobs/#{params[:id]}.json",
:out
))
)
tls_options = tls_options_for(uri, :out)
headers = { 'Accept' => api_media_type(:json),
'X-Remote-Ident' => @credentials[:cn],
......@@ -114,9 +114,9 @@ class JobsController < ApplicationController
job_to_send = job.to_hash(:destination => "oar-2.4-submission")
Rails.logger.info "Submitting #{job_to_send.inspect}"
uri = URI(uri_to(
uri = uri_to(
site_path(params[:site_id])+"/internal/oarapi/jobs.json", :out
))
)
tls_options = tls_options_for(uri, :out)
headers = { 'X-Remote-Ident' => @credentials[:cn],
'X-Api-User-Cn' => @credentials[:cn],
......
......@@ -30,6 +30,10 @@ module ApplicationHelper
Grid5000::Router.tls_options_for(url, in_or_out)
end
def http_request(method, uri, tls_options, timeout = nil, headers = {}, body = nil)
Grid5000::Router.http_request(method, uri, tls_options, timeout, headers, body)
end
def repository
@repository ||= Grid5000::Repository.new(
File.expand_path(
......@@ -47,30 +51,4 @@ module ApplicationHelper
t.to_s
end
end
def http_request(method, uri, tls_options, timeout = nil, headers = {}, body = nil)
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 5 if timeout
http.use_ssl = true if uri.scheme == 'https'
if tls_options && !tls_options.empty?
http.cert = OpenSSL::X509::Certificate.new(File.read(tls_options[:cert_chain_file]))
http.key = OpenSSL::PKey::RSA.new(File.read(tls_options[:private_key_file]))
http.verify_mode = tls_options[:verify_peer]
end
request = case method
when :post
Net::HTTP::Post.new(uri, headers)
when :get
Net::HTTP::Get.new(uri, headers)
when :delete
Net::HTTP::Delete.new(uri, headers)
else
raise "Unknown http method: #{method}"
end
request.body = body if body
http.request(request)
end
end
......@@ -18,6 +18,8 @@ require 'fileutils'
module Grid5000
# The Deployment class represents a deployment that is launched using the Kadeploy3 tool.
class Deployment < ActiveRecord::Base
include ApplicationHelper
attr_accessor :links
# Ugly hack to make the communication between the controller and the model possible
attr_accessor :base_uri, :user, :tls_options
......@@ -129,15 +131,15 @@ module Grid5000
headers = { 'X-Remote-Ident' => user }
uri = base_uri + uid
response = http_request(:delete, uri, tls_options, 15, headers, body.to_s)
http = http_request(:delete, uri, tls_options, 15, headers)
unless %w{200 201 202 204}.include?(response.status.to_s)
unless %w{200 201 202 204}.include?(http.code.to_s)
error("Unable to contact #{File.join(base_uri,uid)}")
raise self.output+"\n"
end
# Not checked since it avoid touch! to cancel the deployment
#unless %w{200 201 202 204}.include?(http.response_header.status.to_s)
#unless %w{200 201 202 204}.include?(http.response_header.code.to_s)
# fail
# raise "The deployment no longer exists on the Kadeploy server"
#end
......@@ -170,17 +172,17 @@ module Grid5000
Rails.logger.info "Submitting: #{params.inspect} to #{base_uri}"
headers = { 'Content-Type' => Mime::Type.lookup_by_extension(:json).to_s,
'Accept' => Mime::Type.lookup_by_extension,
'Accept' => Mime::Type.lookup_by_extension(:json).to_s,
'X-Remote-Ident' => user,
}
http = http_request(:post, base_uri, tls_options, 20, headers)
http = http_request(:post, base_uri, tls_options, 20, headers, params.to_json)
unless %w{200 201 202 204}.include?(http.status.to_s)
unless %w{200 201 202 204}.include?(http.code.to_s)
error("Unable to contact #{base_uri}")
raise self.output+"\n"
end
if %w{200 201 202 204}.include?(http.status.to_s)
if %w{200 201 202 204}.include?(http.code.to_s)
update_attribute(:uid, JSON.parse(http.body)['wid'])
else
error(get_kaerror(http,http.headers))
......@@ -203,7 +205,7 @@ module Grid5000
raise self.output+"\n"
end
if %w{200 201 202 204}.include?(http.status.to_s)
if %w{200 201 202 204}.include?(http.code.to_s)
item = JSON.parse(http.body)
unless item['error']
......
......@@ -25,15 +25,15 @@ module Grid5000
# resources of a single site (https://rennes.g5k/ ony giving access to
# resources under the /sites/rennes path
class Router
def initialize(where)
@where = where
end
def call(params, request)
self.class.uri_to(request, @where, :in, :absolute)
end
class << self
def uri_to(request, path, in_or_out = :in, relative_or_absolute = :relative)
root_path = if request.env['HTTP_X_API_ROOT_PATH'].blank? || in_or_out == :out
......@@ -70,7 +70,7 @@ module Grid5000
root_uri=URI(base_uri(in_or_out))
if root_uri.path.blank?
root_path=''
else
else
root_path=root_uri.path
end # if root_uri.path.blank?
......@@ -79,23 +79,49 @@ module Grid5000
uri
end # def uri_to()
# FIXME: move Rails.config to Grid5000.config
def base_uri(in_or_out = :in)
Rails.my_config("base_uri_#{in_or_out}".to_sym)
end
def tls_options_for(url, in_or_out = :in)
tls_options={}
tls_options = {}
[:cert_chain_file, :private_key_file, :verify_peer, :fail_if_no_peer_cert,
:cipher_list, :ecdh_curve, :dhparam, :ssl_version].each do |tls_param|
config_key=("uri_#{in_or_out.to_s}_"+tls_param.to_s).to_sym
config_key = ("uri_#{in_or_out.to_s}_" + tls_param.to_s).to_sym
if Rails.my_config(config_key)
tls_options[tls_param]=Rails.my_config(config_key)
tls_options[tls_param] = Rails.my_config(config_key)
end
end
tls_options
end
def http_request(method, uri, tls_options, timeout = nil, headers = {}, body = nil)
uri = URI(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 5 if timeout
http.use_ssl = true if uri.scheme == 'https'
if tls_options && !tls_options.empty?
http.cert = OpenSSL::X509::Certificate.new(File.read(tls_options[:cert_chain_file]))
http.key = OpenSSL::PKey::RSA.new(File.read(tls_options[:private_key_file]))
http.verify_mode = tls_options[:verify_peer]
end
request = case method
when :post
Net::HTTP::Post.new(uri, headers)
when :get
Net::HTTP::Get.new(uri, headers)
when :delete
Net::HTTP::Delete.new(uri, headers)
else
raise "Unknown http method: #{method}"
end
request.body = body if body
http.request(request)
end
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment