From 0d5466549e9c37e1f419d0bf5c6ffa2801165916 Mon Sep 17 00:00:00 2001 From: Midhun Krishna Date: Tue, 9 Feb 2021 15:26:51 +0530 Subject: [PATCH] Fixes domain name duplication in url When a bucket url like bucket.s3.amazonaws.com with bucket name bucket is present in params passed into request_params method, it should not dupplicate the subdomain. current: bucket.bucket.s3.amazonaws.com fix: bucket.s3.amazonaws.com Fix test failure due to Hash#slice not present for ruby 2.3 --- lib/fog/aws/storage.rb | 2 + tests/models/storage/directory_tests.rb | 115 +++++++++++++++++++++++- tests/requests/storage/bucket_tests.rb | 2 +- 3 files changed, 116 insertions(+), 3 deletions(-) diff --git a/lib/fog/aws/storage.rb b/lib/fog/aws/storage.rb index 97091124d..d28779aae 100644 --- a/lib/fog/aws/storage.rb +++ b/lib/fog/aws/storage.rb @@ -298,6 +298,8 @@ module Fog host = params.fetch(:cname, bucket_name) elsif path_style path = bucket_to_path bucket_name, path + elsif host.start_with?("#{bucket_name}.") + # no-op else host = [bucket_name, host].join('.') end diff --git a/tests/models/storage/directory_tests.rb b/tests/models/storage/directory_tests.rb index 211b9e163..efb6e21be 100644 --- a/tests/models/storage/directory_tests.rb +++ b/tests/models/storage/directory_tests.rb @@ -1,4 +1,117 @@ Shindo.tests("Storage[:aws] | directory", ["aws"]) do + tests('Fog::Storage[:aws]', "#request_params") do + def slice(hash, *args) + hash.select { |k, _| args.include?(k) } + end + + instance = Fog::Storage[:aws] + method = instance.method(:request_params) + + params = {bucket_name: 'profile-uploads', host: 'profile-uploads.s3.us-west-2.amazonaws.com'} + tests("given #{params}, request_params[:host]").returns("profile-uploads.s3.us-west-2.amazonaws.com") do + method.call(params)[:host] + end + + params = {bucket_name: 'profile-uploads.johnsmith.net', cname: 'profile-uploads.johnsmith.net', virtual_host: true} + tests("given #{params}, request_params[:host]").returns("profile-uploads.johnsmith.net") do + method.call(params)[:host] + end + + params = {bucket_name: 'profile-uploads.johnsmith.net', cname: 'profile-uploads.johnsmith.net', virtual_host: false} + tests("given #{params}, request_params[:host], request_params[:path]"). + returns({host: "s3.amazonaws.com", path: "/profile-uploads.johnsmith.net/"}) do + slice(method.call(params), :host, :path) + end + + params = {bucket_name: 'profile-uploads.johnsmith.net', bucket_cname: 'profile-uploads.johnsmith.net'} + tests("given #{params}, request_params[:host]").returns("profile-uploads.johnsmith.net") do + method.call(params)[:host] + end + + params = {bucket_name: 'profile-uploads'} + tests("given #{params}, request_params[:path], request_params[:host]"). + returns({path: "/", host: "profile-uploads.s3.amazonaws.com"}) do + slice(method.call(params), :path, :host) + end + + params = {bucket_name: 'profile-uploads', path_style: true} + tests("given #{params}, request_params[:path], request_params[:host]"). + returns({path: "/profile-uploads/", host: "s3.amazonaws.com"}) do + slice(method.call(params), :path, :host) + end + + params = {bucket_name: 'profile-uploads', path_style: false} + tests("given #{params}, request_params[:path], request_params[:host]"). + returns({path: "/", host: "profile-uploads.s3.amazonaws.com"}) do + slice(method.call(params), :path, :host) + end + + params = {scheme: 'https', bucket_name: 'profile.uploads', path_style: false} + tests("given #{params}, request_params[:path], request_params[:host]"). + returns(path: "/profile.uploads/", host: "s3.amazonaws.com") do + slice(method.call(params), :path, :host) + end + + params = {:headers=>{:"Content-Type"=>"application/json"}} + tests("given #{params}, request_params[:headers]").returns({:"Content-Type"=>"application/json"}) do + method.call(params)[:headers] + end + + params = {headers: {}} + tests("given #{params}, request_params[:headers]").returns({}) do + method.call(params)[:headers] + end + + params = {scheme: 'http'} + tests("given #{params}, request_params[:scheme]").returns('http') do + method.call(params)[:scheme] + end + + params = {} + tests("given #{params}, request_params[:scheme]").returns('https') do + method.call(params)[:scheme] + end + + params = {scheme: 'http', port: 8080} + tests("given #{params} (default scheme), request_params[:port]").returns(8080) do + method.call(params)[:port] + end + + params = {scheme: 'https', port: 443} + tests("given #{params}, request_params[:port]").returns(nil) do + method.call(params)[:port] + end + + params = {} + tests("given #{params}, request_params[:host]").returns("s3.amazonaws.com") do + method.call(params)[:host] + end + + params = {region: 'us-east-1'} + tests("given #{params}, request_params[:host]").returns("s3.amazonaws.com") do + method.call(params)[:host] + end + + params = {region: 'us-west-2'} + tests("given #{params}, request_params[:host]").returns("s3.us-west-2.amazonaws.com") do + method.call(params)[:host] + end + + params= {region: 'us-east-1', host: 's3.us-west-2.amazonaws.com'} + tests("given #{params}, request_params[:host]").returns("s3.us-west-2.amazonaws.com") do + method.call(params)[:host] + end + + params = {object_name: 'image.png'} + tests("given #{params}, request_params[:host]").returns("/image.png") do + method.call(params)[:path] + end + + params = {object_name: 'image.png', path: '/images/image.png'} + tests("given #{params}, request_params[:host]").returns("/images/image.png") do + method.call(params)[:path] + end + end directory_attributes = { :key => uniq_id('fogdirectorytests') @@ -85,7 +198,5 @@ Shindo.tests("Storage[:aws] | directory", ["aws"]) do @instance.versioning? end end - end - end diff --git a/tests/requests/storage/bucket_tests.rb b/tests/requests/storage/bucket_tests.rb index a9a9d5235..4f871e4b0 100644 --- a/tests/requests/storage/bucket_tests.rb +++ b/tests/requests/storage/bucket_tests.rb @@ -158,7 +158,7 @@ Shindo.tests('Fog::Storage[:aws] | bucket requests', ["aws"]) do Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, :IndexDocument => 'index.html') end - tests("#put_bucket_website('#{@aws_bucket_name}', :RedirectAllRequestsTo => 'redirect.example..com')").succeeds do + tests("#put_bucket_website('#{@aws_bucket_name}', :RedirectAllRequestsTo => 'redirect.example.com')").succeeds do Fog::Storage[:aws].put_bucket_website(@aws_bucket_name, :RedirectAllRequestsTo => 'redirect.example.com') end