Skip to content

Commit 831d6f5

Browse files
committed
Properly handle container registry redirects to fix metadata stored on a S3 backend
The previous behavior would include the Authorization header, which would make fetching an S3 blob fail quietly. Closes #22403 Update sh-fix-container-registry-s3-redirects.yml
1 parent 39baadb commit 831d6f5

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Properly handle container registry redirects to fix metadata stored on a S3 backend
3+
merge_request:
4+
author:

lib/container_registry/client.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ def response_body(response, allow_redirect: false)
7575
def redirect_response(location)
7676
return unless location
7777

78-
# We explicitly remove authorization token
79-
faraday_blob.get(location) do |req|
80-
req['Authorization'] = ''
81-
end
78+
faraday_redirect.get(location)
8279
end
8380

8481
def faraday
@@ -93,5 +90,14 @@ def faraday_blob
9390
initialize_connection(conn, @options)
9491
end
9592
end
93+
94+
# Create a new request to make sure the Authorization header is not inserted
95+
# via the Faraday middleware
96+
def faraday_redirect
97+
@faraday_redirect ||= Faraday.new(@base_uri) do |conn|
98+
conn.request :json
99+
conn.adapter :net_http
100+
end
101+
end
96102
end
97103
end

spec/lib/container_registry/blob_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
context 'for a valid address' do
9999
before do
100100
stub_request(:get, location).
101-
with(headers: { 'Authorization' => nil }).
101+
with { |request| !request.headers.include?('Authorization') }.
102102
to_return(
103103
status: 200,
104104
headers: { 'Content-Type' => 'application/json' },
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# coding: utf-8
2+
require 'spec_helper'
3+
4+
describe ContainerRegistry::Client do
5+
let(:token) { '12345' }
6+
let(:options) { { token: token } }
7+
let(:client) { described_class.new("http://container-registry", options) }
8+
9+
describe '#blob' do
10+
it 'GET /v2/:name/blobs/:digest' do
11+
stub_request(:get, "http://container-registry/v2/group/test/blobs/sha256:0123456789012345").
12+
with(headers: {
13+
'Accept' => 'application/octet-stream',
14+
'Authorization' => "bearer #{token}"
15+
}).
16+
to_return(status: 200, body: "Blob")
17+
18+
expect(client.blob('group/test', 'sha256:0123456789012345')).to eq('Blob')
19+
end
20+
21+
it 'follows 307 redirect for GET /v2/:name/blobs/:digest' do
22+
stub_request(:get, "http://container-registry/v2/group/test/blobs/sha256:0123456789012345").
23+
with(headers: {
24+
'Accept' => 'application/octet-stream',
25+
'Authorization' => "bearer #{token}"
26+
}).
27+
to_return(status: 307, body: "", headers: { Location: 'http://redirected' })
28+
# We should probably use hash_excluding here, but that requires an update to WebMock:
29+
# https://github.com/bblimke/webmock/blob/master/lib/webmock/matchers/hash_excluding_matcher.rb
30+
stub_request(:get, "http://redirected/").
31+
with { |request| !request.headers.include?('Authorization') }.
32+
to_return(status: 200, body: "Successfully redirected")
33+
34+
response = client.blob('group/test', 'sha256:0123456789012345')
35+
36+
expect(response).to eq('Successfully redirected')
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)