2016-02-02 08:09:55 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Workhorse, lib: true do
|
2016-09-20 12:21:52 -04:00
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:repository) { project.repository }
|
|
|
|
|
|
|
|
def decode_workhorse_header(array)
|
|
|
|
key, value = array
|
|
|
|
command, encoded_params = value.split(":")
|
|
|
|
params = JSON.parse(Base64.urlsafe_decode64(encoded_params))
|
|
|
|
|
|
|
|
[key, command, params]
|
|
|
|
end
|
2016-02-02 08:09:55 -05:00
|
|
|
|
2016-08-19 13:10:41 -04:00
|
|
|
describe ".send_git_archive" do
|
2016-02-02 08:09:55 -05:00
|
|
|
context "when the repository doesn't have an archive file path" do
|
|
|
|
before do
|
|
|
|
allow(project.repository).to receive(:archive_metadata).and_return(Hash.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error" do
|
2016-09-20 12:21:52 -04:00
|
|
|
expect { described_class.send_git_archive(project.repository, ref: "master", format: "zip") }.to raise_error(RuntimeError)
|
2016-02-02 08:09:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-19 13:10:41 -04:00
|
|
|
|
2016-09-20 12:21:52 -04:00
|
|
|
describe '.send_git_patch' do
|
|
|
|
let(:diff_refs) { double(base_sha: "base", head_sha: "head") }
|
|
|
|
subject { described_class.send_git_patch(repository, diff_refs) }
|
|
|
|
|
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
|
|
|
|
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq("git-format-patch")
|
|
|
|
expect(params).to eq("RepoPath" => repository.path_to_repo, "ShaFrom" => "base", "ShaTo" => "head")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-22 14:55:56 -05:00
|
|
|
describe '.terminal_websocket' do
|
|
|
|
def terminal(ca_pem: nil)
|
|
|
|
out = {
|
|
|
|
subprotocols: ['foo'],
|
|
|
|
url: 'wss://example.com/terminal.ws',
|
|
|
|
headers: { 'Authorization' => ['Token x'] }
|
|
|
|
}
|
|
|
|
out[:ca_pem] = ca_pem if ca_pem
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
def workhorse(ca_pem: nil)
|
|
|
|
out = {
|
|
|
|
'Terminal' => {
|
|
|
|
'Subprotocols' => ['foo'],
|
|
|
|
'Url' => 'wss://example.com/terminal.ws',
|
|
|
|
'Header' => { 'Authorization' => ['Token x'] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out['Terminal']['CAPem'] = ca_pem if ca_pem
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without ca_pem' do
|
|
|
|
subject { Gitlab::Workhorse.terminal_websocket(terminal) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(workhorse) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with ca_pem' do
|
|
|
|
subject { Gitlab::Workhorse.terminal_websocket(terminal(ca_pem: "foo")) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(workhorse(ca_pem: "foo")) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-20 12:21:52 -04:00
|
|
|
describe '.send_git_diff' do
|
|
|
|
let(:diff_refs) { double(base_sha: "base", head_sha: "head") }
|
|
|
|
subject { described_class.send_git_patch(repository, diff_refs) }
|
|
|
|
|
|
|
|
it 'sets the header correctly' do
|
|
|
|
key, command, params = decode_workhorse_header(subject)
|
|
|
|
|
|
|
|
expect(key).to eq("Gitlab-Workhorse-Send-Data")
|
|
|
|
expect(command).to eq("git-format-patch")
|
|
|
|
expect(params).to eq("RepoPath" => repository.path_to_repo, "ShaFrom" => "base", "ShaTo" => "head")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 13:10:41 -04:00
|
|
|
describe ".secret" do
|
|
|
|
subject { described_class.secret }
|
|
|
|
|
|
|
|
before do
|
|
|
|
described_class.instance_variable_set(:@secret, nil)
|
|
|
|
described_class.write_secret
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns 32 bytes' do
|
|
|
|
expect(subject).to be_a(String)
|
|
|
|
expect(subject.length).to eq(32)
|
|
|
|
expect(subject.encoding).to eq(Encoding::ASCII_8BIT)
|
|
|
|
end
|
|
|
|
|
2016-09-13 13:45:02 -04:00
|
|
|
it 'accepts a trailing newline' do
|
|
|
|
open(described_class.secret_path, 'a') { |f| f.write "\n" }
|
|
|
|
expect(subject.length).to eq(32)
|
|
|
|
end
|
|
|
|
|
2016-08-19 13:10:41 -04:00
|
|
|
it 'raises an exception if the secret file cannot be read' do
|
|
|
|
File.delete(described_class.secret_path)
|
|
|
|
expect { subject }.to raise_exception(Errno::ENOENT)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if the secret file contains the wrong number of bytes' do
|
|
|
|
File.truncate(described_class.secret_path, 0)
|
|
|
|
expect { subject }.to raise_exception(RuntimeError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".write_secret" do
|
|
|
|
let(:secret_path) { described_class.secret_path }
|
|
|
|
before do
|
|
|
|
begin
|
|
|
|
File.delete(secret_path)
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
|
|
|
|
described_class.write_secret
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses mode 0600' do
|
|
|
|
expect(File.stat(secret_path).mode & 0777).to eq(0600)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes base64 data' do
|
|
|
|
bytes = Base64.strict_decode64(File.read(secret_path))
|
|
|
|
expect(bytes).not_to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#verify_api_request!' do
|
2016-09-06 11:26:16 -04:00
|
|
|
let(:header_key) { described_class::INTERNAL_API_REQUEST_HEADER }
|
2016-08-19 13:10:41 -04:00
|
|
|
let(:payload) { { 'iss' => 'gitlab-workhorse' } }
|
|
|
|
|
|
|
|
it 'accepts a correct header' do
|
|
|
|
headers = { header_key => JWT.encode(payload, described_class.secret, 'HS256') }
|
|
|
|
expect { call_verify(headers) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the header is not set' do
|
|
|
|
expect { call_verify({}) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the header is not signed' do
|
|
|
|
headers = { header_key => JWT.encode(payload, nil, 'none') }
|
|
|
|
expect { call_verify(headers) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the header is signed with the wrong key' do
|
|
|
|
headers = { header_key => JWT.encode(payload, 'wrongkey', 'HS256') }
|
|
|
|
expect { call_verify(headers) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when the issuer is incorrect' do
|
|
|
|
payload['iss'] = 'somebody else'
|
|
|
|
headers = { header_key => JWT.encode(payload, described_class.secret, 'HS256') }
|
|
|
|
expect { call_verify(headers) }.to raise_jwt_error
|
|
|
|
end
|
|
|
|
|
|
|
|
def raise_jwt_error
|
|
|
|
raise_error(JWT::DecodeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
def call_verify(headers)
|
|
|
|
described_class.verify_api_request!(headers)
|
|
|
|
end
|
|
|
|
end
|
2016-12-26 12:15:40 -05:00
|
|
|
|
|
|
|
describe '.git_http_ok' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { described_class.git_http_ok(repository, user) }
|
|
|
|
|
|
|
|
it { expect(subject).to eq({ GL_ID: "user-#{user.id}", RepoPath: repository.path_to_repo }) }
|
|
|
|
|
|
|
|
context 'when Gitaly socket path is present' do
|
|
|
|
let(:gitaly_socket_path) { '/tmp/gitaly.sock' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.gitaly).to receive(:socket_path).and_return(gitaly_socket_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes Gitaly params in the returned value' do
|
|
|
|
expect(subject).to include({
|
|
|
|
GitalyResourcePath: "/projects/#{repository.project.id}/git-http/info-refs",
|
|
|
|
GitalySocketPath: gitaly_socket_path,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-02 08:09:55 -05:00
|
|
|
end
|