Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send appropriate headers #169

Merged
merged 5 commits into from
Jul 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/docker/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ def build_from_dir(dir, opts = {}, connection = Docker.connection,

private

# A method to build auth headers and merge them into headers sent
# by build_from_dir.
# A method to build the config header and merge it into the
# headers sent by build_from_dir.
def self.build_headers(creds)
credentials = creds || Docker.creds || {}
auth_header = Docker::Util.build_auth_header(credentials.to_json)
config_header = Docker::Util.build_config_header(credentials)

headers = { 'Content-Type' => 'application/tar',
'Transfer-Encoding' => 'chunked' }
headers = headers.merge(auth_header) if auth_header
headers = headers.merge(config_header) if config_header
headers
end

Expand Down
24 changes: 22 additions & 2 deletions lib/docker/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,28 @@ def build_auth_header(credentials)
credentials = credentials.to_json if credentials.is_a?(Hash)
encoded_creds = Base64.encode64(credentials).gsub(/\n/, '')
{
'X-Registry-Auth' => encoded_creds,
'X-Registry-Config' => encoded_creds,
'X-Registry-Auth' => encoded_creds
}
end

def build_config_header(credentials)
if credentials.is_a?(String)
credentials = JSON.parse(credentials, symbolize_names: true)
end
header = {
"configs" => {
credentials[:serveraddress].to_s => {
"username" => credentials[:username].to_s,
"password" => credentials[:password].to_s,
"email" => credentials[:email].to_s
}
}
}.to_json

encoded_header = Base64.encode64(header).gsub(/\n/, '')

{
'X-Registry-Config' => encoded_header
}
end
end
7 changes: 4 additions & 3 deletions spec/docker/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,14 +455,15 @@
{
:username => 'nahiluhmot',
:password => '*********',
:email => '[email protected]'
:email => '[email protected]',
:serveraddress => 'https://index.docker.io/v1'
}
}

before { Docker.creds = creds }

it 'sends Docker.creds', :vcr do
expect(image.info[:headers].keys).to include('X-Registry-Auth')
it 'sends X-Registry-Config header', :vcr do
expect(image.info[:headers].keys).to include('X-Registry-Config')
end
end
end
Expand Down
55 changes: 50 additions & 5 deletions spec/docker/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,70 @@
}
let(:credential_string) { credentials.to_json }
let(:encoded_creds) { Base64.encode64(credential_string).gsub(/\n/, '') }
let(:expected_headers) {
let(:expected_header) {
{
'X-Registry-Auth' => encoded_creds,
'X-Registry-Config' => encoded_creds
'X-Registry-Auth' => encoded_creds
}
}


context 'given credentials as a Hash' do
it 'returns an X-Registry-Auth header encoded' do
expect(subject.build_auth_header(credentials)).to eq(expected_headers)
expect(subject.build_auth_header(credentials)).to eq(expected_header)
end
end

context 'given credentials as a String' do
it 'returns an X-Registry-Auth header encoded' do
expect(
subject.build_auth_header(credential_string)
).to eq(expected_headers)
).to eq(expected_header)
end
end
end

describe '.build_config_header' do
subject { described_class }

let(:credentials) {
{
:username => 'test',
:password => 'password',
:email => '[email protected]',
:serveraddress => 'https://registry.com/'
}
}

let(:credentials_object) {
{
:configs => {
:'https://registry.com/' => {
:username => 'test',
:password => 'password',
:email => '[email protected]',
}
}
}.to_json
}

let(:encoded_creds) { Base64.encode64(credentials_object).gsub(/\n/, '') }
let(:expected_header) {
{
'X-Registry-Config' => encoded_creds
}
}

context 'given credentials as a Hash' do
it 'returns an X-Registry-Config header encoded' do
expect(subject.build_config_header(credentials)).to eq(expected_header)
end
end

context 'given credentials as a String' do
it 'returns an X-Registry-Config header encoded' do
expect(
subject.build_config_header(credentials.to_json)
).to eq(expected_header)
end
end
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.