Commit d9a7a1f8 authored by MARGERY David's avatar MARGERY David
Browse files

[bug #10556] Correct call to :out urls from proxy

When called from a proxy, the X_API_ROOT_PATH used as entry point in the
proxy should not influence the call made to other parts of the API
parent 7ad88013
......@@ -16,12 +16,14 @@ module Grid5000
# Computes the URI to a specific path.
# X-Api-root-Path is the entry point for all versions of the API
# X-Api-version is the version string by which the server is reached
# X-Api-Path-Prefix is the entry point to access this service for a
# X-Api-Path-Prefix was used in the transition from sid/grid5000/sites to /sid/sites
# and is kept should the API serve more than on platform
# X-Api-Mount-Path (subset of the API path to take out of the URI path).
# It is the entry point to access this service for a
# given version. To use if a server does not wish to show the top level
# hieararchy exposed by the services, for example to only publish the
# resources of a single site (https://rennes.g5k/ ony giving access to
# resources under the /sites/rennes path
# X-Api-Mount-Path (subset of the API path to take out of the URI path).
class Router
def initialize(where)
......@@ -34,7 +36,7 @@ module Grid5000
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?
root_path = if request.env['HTTP_X_API_ROOT_PATH'].blank? || in_or_out == :out
nil
else
File.join("/", (request.env['HTTP_X_API_ROOT_PATH'] || ""))
......
......@@ -21,35 +21,35 @@ describe Grid5000::Router do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_VERSION' => 'sid'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/sid/sites/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/sid/sites/rennes/jobs"
end
it "should take into account X-Api-Path-Prefix header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_PATH_PREFIX' => 'grid5000'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/grid5000/sites/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/grid5000/sites/rennes/jobs"
end
it "should take into account X-Api-Root-Path header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_ROOT_PATH' => 'proxies/grid5000'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/proxies/grid5000/sites/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/proxies/grid5000/sites/rennes/jobs"
end
it "should take into account X-Api-Mount-Path header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_MOUNT_PATH' => 'sites/rennes'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/jobs"
end
it "should only substitute X-Api-Mount-Path header at the start of url" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_MOUNT_PATH' => '/rennes'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/sites/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/sites/rennes/jobs"
end
it "should take into account both X-Api-Version and X-Api-Path-Prefix headers" do
......@@ -57,7 +57,7 @@ describe Grid5000::Router do
'HTTP_X_API_VERSION' => 'sid',
'HTTP_X_API_PATH_PREFIX' => 'grid5000'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/sid/grid5000/sites/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/sid/grid5000/sites/rennes/jobs"
end
it "Should properly combine X-API-[Mount-Path,Version,Path-Prefix] headers" do
......@@ -66,7 +66,7 @@ describe Grid5000::Router do
'HTTP_X_API_VERSION' => 'sid',
'HTTP_X_API_PATH_PREFIX' => 'g5k-api'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/sid/g5k-api/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/sid/g5k-api/jobs"
end
it "Should properly combine X-API-[Root-Path,Version,Path-Prefix] headers" do
......@@ -75,7 +75,7 @@ describe Grid5000::Router do
'HTTP_X_API_VERSION' => 'sid',
'HTTP_X_API_PATH_PREFIX' => 'g5k-api'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/proxies/grid5000/sid/g5k-api/sites/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/proxies/grid5000/sid/g5k-api/sites/rennes/jobs"
end
it "Should properly combine all X-API headers supported" do
......@@ -85,7 +85,7 @@ describe Grid5000::Router do
'HTTP_X_API_VERSION' => 'sid',
'HTTP_X_API_PATH_PREFIX' => 'g5k-api'
})
Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/sites/fr/grid5000/sid/g5k-api/rennes/jobs"
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs")).to eq "/sites/fr/grid5000/sid/g5k-api/rennes/jobs"
end
end
......@@ -168,25 +168,63 @@ describe Grid5000::Router do
end
end
it "should take into account the parameters of the config file with empty path" do
Rails.my_config("base_uri_out".to_sym).should == "http://api-out.local"
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_VERSION' => 'sid'
})
Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs/374172.json", :out).should == "http://api-out.local/sid/sites/rennes/internal/oarapi/jobs/374172.json"
describe "uri_to called for :out parameters (:should imply :abolute in practise)" do
before do
@server_url="http://api-out.local"
Api::Application::CONFIG["base_uri_out"] = @server_url
expect(Rails.my_config("base_uri_out".to_sym)).to eq @server_url
end
it "should take into account base_uri_out from config file with empty path" do
expect(Rails.my_config("base_uri_out".to_sym)).to eq "http://api-out.local"
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_VERSION' => 'sid'
})
expect(Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs/374172.json", :out)).to eq "http://api-out.local/sid/sites/rennes/internal/oarapi/jobs/374172.json"
end
it "should take into account base_uri_out from config file with path component(for dev environment)" do
Api::Application::CONFIG["base_uri_out"] = "http://api-out.local/sid"
expect(Rails.my_config("base_uri_out".to_sym)).to eq "http://api-out.local/sid"
request = double(Rack::MockRequest, :env => {})
expect(Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs/374172.json", :out)).to eq "http://api-out.local/sid/sites/rennes/internal/oarapi/jobs/374172.json"
end
it "should take into account X-Api-Version header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_VERSION' => 'sid'
})
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs", :out)).to eq "#{@server_url}/sid/sites/rennes/jobs"
end
it "should take into account X-Api-Path-Prefix header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_PATH_PREFIX' => 'grid5000'
})
expect(Grid5000::Router.uri_to(request, "/sites/rennes/jobs", :out)).to eq "#{@server_url}/grid5000/sites/rennes/jobs"
end
it "should not take into account X-Api-Root-Path header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_ROOT_PATH' => 'proxies/grid5000'
})
expect(Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs.json", :out)).to eq "#{@server_url}/sites/rennes/internal/oarapi/jobs.json"
end
it "should not take into account X-Api-Mount-Path header" do
request = double(Rack::MockRequest, :env => {
'HTTP_X_API_MOUNT_PATH' => 'sites/rennes'
})
expect(Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs.json", :out)).to eq "#{@server_url}/internal/oarapi/jobs.json"
skip "not sure what to expect here : should the API expect base_uri_out to be a mounted server or the server used to access all the sites ?"
end
end
it "should take into account the parameters of the config file with path (for dev environment)" do
Api::Application::CONFIG["base_uri_out"] = "http://api-out.local/sid"
Rails.my_config("base_uri_out".to_sym).should == "http://api-out.local/sid"
request = double(Rack::MockRequest, :env => {})
Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs/374172.json", :out).should == "http://api-out.local/sid/sites/rennes/internal/oarapi/jobs/374172.json"
end
it "should take into account tls options" do
Api::Application::CONFIG["uri_out_verify_peer"] = true
Api::Application::CONFIG["uri_out_private_key_file"] = "/etc/ssl/certs/private/api.out.local.pem"
expect(tls_options_for("https://api-out.local/", :out)).to include ({private_key_file: "/etc/ssl/certs/private/api.out.local.pem"} )
expect(tls_options_for("https://api-out.local/", :out)).to include ({verify_peer: true} )
describe "tls_options_for called for :out parameter" do
it "should take into account tls options from config file when connecting :out" do
Api::Application::CONFIG["uri_out_verify_peer"] = true
Api::Application::CONFIG["uri_out_private_key_file"] = "/etc/ssl/certs/private/api.out.local.pem"
expect(tls_options_for("https://api-out.local/", :out)).to include ({private_key_file: "/etc/ssl/certs/private/api.out.local.pem"} )
expect(tls_options_for("https://api-out.local/", :out)).to include ({verify_peer: true} )
end
end
end
Markdown is supported
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