Commit f59cd678 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'ash.mckenzie/minor-tidy-up' into 'master'

Refactor for re-usability for future MR's

See merge request gitlab-org/gitlab-shell!210
parents 6413c4be c92bfcef
...@@ -67,3 +67,6 @@ Style/RegexpLiteral: ...@@ -67,3 +67,6 @@ Style/RegexpLiteral:
Style/SpecialGlobalVars: Style/SpecialGlobalVars:
Enabled: false Enabled: false
Style/FrozenStringLiteralComment:
Enabled: false
source "http://rubygems.org" source "http://rubygems.org"
group :development, :test do group :development, :test do
gem 'guard' gem 'guard', '~> 1.5.0'
gem 'guard-rspec' gem 'guard-rspec', '~> 2.1.0'
gem 'rspec', '~> 2.14.0' gem 'listen', '~> 0.5.0'
gem 'rspec', '~> 2.0'
gem 'rspec-its', '~> 1.0.0'
gem 'rubocop', '0.49.1', require: false gem 'rubocop', '0.49.1', require: false
gem 'simplecov', require: false gem 'simplecov', '~> 0.9.0', require: false
gem 'vcr' gem 'vcr', '~> 2.4.0'
gem 'webmock' gem 'webmock', '~> 1.9.0'
end end
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
addressable (2.3.2) addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0) ast (2.4.0)
coderay (1.0.8) coderay (1.1.2)
crack (0.3.2) crack (0.4.3)
diff-lcs (1.2.5) safe_yaml (~> 1.0.0)
diff-lcs (1.3)
docile (1.1.5) docile (1.1.5)
guard (1.5.4) guard (1.5.4)
listen (>= 0.4.2) listen (>= 0.4.2)
...@@ -16,28 +18,31 @@ GEM ...@@ -16,28 +18,31 @@ GEM
guard (>= 1.1) guard (>= 1.1)
rspec (~> 2.11) rspec (~> 2.11)
listen (0.5.3) listen (0.5.3)
lumberjack (1.0.2) lumberjack (1.0.13)
method_source (0.8.1) method_source (0.9.0)
multi_json (1.10.1) multi_json (1.13.1)
parallel (1.12.1) parallel (1.12.1)
parser (2.5.0.2) parser (2.5.1.2)
ast (~> 2.4.0) ast (~> 2.4.0)
powerpack (0.1.1) powerpack (0.1.2)
pry (0.9.10) pry (0.11.3)
coderay (~> 1.0.5) coderay (~> 1.1.0)
method_source (~> 0.8) method_source (~> 0.9.0)
slop (~> 3.3.1) public_suffix (3.0.2)
rainbow (2.2.2) rainbow (2.2.2)
rake rake
rake (12.3.0) rake (12.3.1)
rspec (2.14.1) rspec (2.99.0)
rspec-core (~> 2.14.0) rspec-core (~> 2.99.0)
rspec-expectations (~> 2.14.0) rspec-expectations (~> 2.99.0)
rspec-mocks (~> 2.14.0) rspec-mocks (~> 2.99.0)
rspec-core (2.14.8) rspec-core (2.99.2)
rspec-expectations (2.14.5) rspec-expectations (2.99.2)
diff-lcs (>= 1.1.3, < 2.0) diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6) rspec-its (1.0.1)
rspec-core (>= 2.99.0.beta1)
rspec-expectations (>= 2.99.0.beta1)
rspec-mocks (2.99.4)
rubocop (0.49.1) rubocop (0.49.1)
parallel (~> 1.10) parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0) parser (>= 2.3.3.1, < 3.0)
...@@ -46,30 +51,32 @@ GEM ...@@ -46,30 +51,32 @@ GEM
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1) unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0) ruby-progressbar (1.9.0)
simplecov (0.9.1) safe_yaml (1.0.4)
simplecov (0.9.2)
docile (~> 1.1.0) docile (~> 1.1.0)
multi_json (~> 1.0) multi_json (~> 1.0)
simplecov-html (~> 0.8.0) simplecov-html (~> 0.9.0)
simplecov-html (0.8.0) simplecov-html (0.9.0)
slop (3.3.3) thor (0.20.0)
thor (0.19.1) unicode-display_width (1.4.0)
unicode-display_width (1.3.0)
vcr (2.4.0) vcr (2.4.0)
webmock (1.9.0) webmock (1.9.3)
addressable (>= 2.2.7) addressable (>= 2.2.7)
crack (>= 0.1.7) crack (>= 0.3.2)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
guard guard (~> 1.5.0)
guard-rspec guard-rspec (~> 2.1.0)
rspec (~> 2.14.0) listen (~> 0.5.0)
rspec (~> 2.0)
rspec-its (~> 1.0.0)
rubocop (= 0.49.1) rubocop (= 0.49.1)
simplecov simplecov (~> 0.9.0)
vcr vcr (~> 2.4.0)
webmock webmock (~> 1.9.0)
BUNDLED WITH BUNDLED WITH
1.16.1 1.16.3
...@@ -7,13 +7,15 @@ require_relative 'gitlab_logger' ...@@ -7,13 +7,15 @@ require_relative 'gitlab_logger'
require_relative 'gitlab_access' require_relative 'gitlab_access'
require_relative 'gitlab_lfs_authentication' require_relative 'gitlab_lfs_authentication'
require_relative 'httpunix' require_relative 'httpunix'
require_relative 'http_helper'
class GitlabNet # rubocop:disable Metrics/ClassLength class GitlabNet # rubocop:disable Metrics/ClassLength
include HTTPHelper
class ApiUnreachableError < StandardError; end class ApiUnreachableError < StandardError; end
class NotFound < StandardError; end class NotFound < StandardError; end
CHECK_TIMEOUT = 5 CHECK_TIMEOUT = 5
READ_TIMEOUT = 300
def check_access(cmd, gl_repository, repo, actor, changes, protocol, env: {}) def check_access(cmd, gl_repository, repo, actor, changes, protocol, env: {})
changes = changes.join("\n") unless changes.is_a?(String) changes = changes.join("\n") unless changes.is_a?(String)
...@@ -33,7 +35,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -33,7 +35,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
params[:user_id] = actor.gsub("user-", "") params[:user_id] = actor.gsub("user-", "")
end end
url = "#{host}/allowed" url = "#{internal_api_endpoint}/allowed"
resp = post(url, params) resp = post(url, params)
if resp.code == '200' if resp.code == '200'
...@@ -50,7 +52,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -50,7 +52,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def discover(key) def discover(key)
key_id = key.gsub("key-", "") key_id = key.gsub("key-", "")
resp = get("#{host}/discover?key_id=#{key_id}") resp = get("#{internal_api_endpoint}/discover?key_id=#{key_id}")
JSON.parse(resp.body) rescue nil JSON.parse(resp.body) rescue nil
end end
...@@ -60,7 +62,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -60,7 +62,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
key_id: key.gsub('key-', '') key_id: key.gsub('key-', '')
} }
resp = post("#{host}/lfs_authenticate", params) resp = post("#{internal_api_endpoint}/lfs_authenticate", params)
if resp.code == '200' if resp.code == '200'
GitlabLfsAuthentication.build_from_json(resp.body) GitlabLfsAuthentication.build_from_json(resp.body)
...@@ -68,14 +70,14 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -68,14 +70,14 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end end
def broadcast_message def broadcast_message
resp = get("#{host}/broadcast_message") resp = get("#{internal_api_endpoint}/broadcast_message")
JSON.parse(resp.body) rescue {} JSON.parse(resp.body) rescue {}
end end
def merge_request_urls(gl_repository, repo_path, changes) def merge_request_urls(gl_repository, repo_path, changes)
changes = changes.join("\n") unless changes.is_a?(String) changes = changes.join("\n") unless changes.is_a?(String)
changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '') changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '')
url = "#{host}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}" url = "#{internal_api_endpoint}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}"
url += "&gl_repository=#{URI.escape(gl_repository)}" if gl_repository url += "&gl_repository=#{URI.escape(gl_repository)}" if gl_repository
resp = get(url) resp = get(url)
...@@ -89,11 +91,11 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -89,11 +91,11 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end end
def check def check
get("#{host}/check", read_timeout: CHECK_TIMEOUT) get("#{internal_api_endpoint}/check", options: { read_timeout: CHECK_TIMEOUT })
end end
def authorized_key(key) def authorized_key(key)
resp = get("#{host}/authorized_keys?key=#{URI.escape(key, '+/=')}") resp = get("#{internal_api_endpoint}/authorized_keys?key=#{URI.escape(key, '+/=')}")
JSON.parse(resp.body) if resp.code == "200" JSON.parse(resp.body) if resp.code == "200"
rescue rescue
nil nil
...@@ -101,7 +103,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -101,7 +103,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def two_factor_recovery_codes(key) def two_factor_recovery_codes(key)
key_id = key.gsub('key-', '') key_id = key.gsub('key-', '')
resp = post("#{host}/two_factor_recovery_codes", key_id: key_id) resp = post("#{internal_api_endpoint}/two_factor_recovery_codes", key_id: key_id)
JSON.parse(resp.body) if resp.code == '200' JSON.parse(resp.body) if resp.code == '200'
rescue rescue
...@@ -110,7 +112,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -110,7 +112,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def notify_post_receive(gl_repository, repo_path) def notify_post_receive(gl_repository, repo_path)
params = { gl_repository: gl_repository, project: repo_path } params = { gl_repository: gl_repository, project: repo_path }
resp = post("#{host}/notify_post_receive", params) resp = post("#{internal_api_endpoint}/notify_post_receive", params)
resp.code == '200' resp.code == '200'
rescue rescue
...@@ -123,7 +125,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -123,7 +125,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
identifier: identifier, identifier: identifier,
changes: changes changes: changes
} }
resp = post("#{host}/post_receive", params) resp = post("#{internal_api_endpoint}/post_receive", params)
raise NotFound if resp.code == '404' raise NotFound if resp.code == '404'
...@@ -131,7 +133,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -131,7 +133,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end end
def pre_receive(gl_repository) def pre_receive(gl_repository)
resp = post("#{host}/pre_receive", gl_repository: gl_repository) resp = post("#{internal_api_endpoint}/pre_receive", gl_repository: gl_repository)
raise NotFound if resp.code == '404' raise NotFound if resp.code == '404'
...@@ -143,107 +145,4 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -143,107 +145,4 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def sanitize_path(repo) def sanitize_path(repo)
repo.delete("'") repo.delete("'")
end end
def config
@config ||= GitlabConfig.new
end
def host
"#{config.gitlab_url}/api/v4/internal"
end
def http_client_for(uri, options = {})
http = if uri.is_a?(URI::HTTPUNIX)
Net::HTTPUNIX.new(uri.hostname)
else
Net::HTTP.new(uri.host, uri.port)
end
http.read_timeout = options[:read_timeout] || read_timeout
if uri.is_a?(URI::HTTPS)
http.use_ssl = true
http.cert_store = cert_store
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config.http_settings['self_signed_cert']
end
http
end
def http_request_for(method, uri, params = {})
request_klass = method == :get ? Net::HTTP::Get : Net::HTTP::Post
request = request_klass.new(uri.request_uri)
user = config.http_settings['user']
password = config.http_settings['password']
request.basic_auth(user, password) if user && password
request.set_form_data(params.merge(secret_token: secret_token))
if uri.is_a?(URI::HTTPUNIX)
# The HTTPUNIX HTTP client does not set a correct Host header. This can
# lead to 400 Bad Request responses.
request['Host'] = 'localhost'
end
request
end
def request(method, url, params = {}, options = {})
$logger.debug('Performing request', method: method.to_s.upcase, url: url)
uri = URI.parse(url)
http = http_client_for(uri, options)
request = http_request_for(method, uri, params)
begin
start_time = Time.new
response = http.start { http.request(request) }
rescue => e
$logger.warn('Failed to connect to internal API', method: method.to_s.upcase, url: url, error: e)
raise ApiUnreachableError
ensure
$logger.info('finished HTTP request', method: method.to_s.upcase, url: url, duration: Time.new - start_time)
end
if response.code == "200"
$logger.debug('Received response', code: response.code, body: response.body)
else
$logger.error('API call failed', method: method.to_s.upcase, url: url, code: response.code, body: response.body)
end
response
end
def get(url, options = {})
request(:get, url, {}, options)
end
def post(url, params)
request(:post, url, params)
end
def cert_store
@cert_store ||= begin
store = OpenSSL::X509::Store.new
store.set_default_paths
ca_file = config.http_settings['ca_file']
store.add_file(ca_file) if ca_file
ca_path = config.http_settings['ca_path']
store.add_path(ca_path) if ca_path
store
end
end
def secret_token
@secret_token ||= File.read config.secret_file
end
def read_timeout
config.http_settings['read_timeout'] || READ_TIMEOUT
end
end end
module HTTPHelper
READ_TIMEOUT = 300
protected
def config
@config ||= GitlabConfig.new
end
def base_api_endpoint
"#{config.gitlab_url}/api/v4"
end
def internal_api_endpoint
"#{base_api_endpoint}/internal"
end
def http_client_for(uri, options = {})
http = if uri.is_a?(URI::HTTPUNIX)
Net::HTTPUNIX.new(uri.hostname)
else
Net::HTTP.new(uri.host, uri.port)
end
http.read_timeout = options[:read_timeout] || read_timeout
if uri.is_a?(URI::HTTPS)
http.use_ssl = true
http.cert_store = cert_store
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config.http_settings['self_signed_cert']
end
http
end
def http_request_for(method, uri, params: {}, headers: {}, options: {})
request_klass = method == :get ? Net::HTTP::Get : Net::HTTP::Post
request = request_klass.new(uri.request_uri, headers)
user = config.http_settings['user']
password = config.http_settings['password']
request.basic_auth(user, password) if user && password
if options[:json]
request.body = options[:json].merge(secret_token: secret_token).to_json
else
request.set_form_data(params.merge(secret_token: secret_token))
end
if uri.is_a?(URI::HTTPUNIX)
# The HTTPUNIX HTTP client does not set a correct Host header. This can
# lead to 400 Bad Request responses.
request['Host'] = 'localhost'
end
request
end
def request(method, url, params: {}, headers: {}, options: {})
$logger.debug('Performing request', method: method.to_s.upcase, url: url)
uri = URI.parse(url)
http = http_client_for(uri, options)
request = http_request_for(method, uri,
params: params,
headers: headers,
options: options)
begin
start_time = Time.new
response = http.start { http.request(request) }
rescue => e
$logger.warn('Failed to connect', method: method.to_s.upcase, url: url, error: e)
raise GitlabNet::ApiUnreachableError
ensure
$logger.info('finished HTTP request', method: method.to_s.upcase, url: url, duration: Time.new - start_time)
end
if response.code == "200"
$logger.debug('Received response', code: response.code, body: response.body)
else
$logger.error('Call failed', method: method.to_s.upcase, url: url, code: response.code, body: response.body)
end
response
end
def get(url, headers: {}, options: {})
request(:get, url, headers: headers, options: options)
end
def post(url, params, headers: {}, options: {})
request(:post, url, params: params, headers: headers, options: options)
end
def cert_store
@cert_store ||= begin
store = OpenSSL::X509::Store.new
store.set_default_paths
ca_file = config.http_settings['ca_file']
store.add_file(ca_file) if ca_file
ca_path = config.http_settings['ca_path']
store.add_path(ca_path) if ca_path
store
end
end
def secret_token
@secret_token ||= File.read config.secret_file
end
def read_timeout
config.http_settings['read_timeout'] || READ_TIMEOUT
end
end
...@@ -36,7 +36,7 @@ describe GitlabAccess do ...@@ -36,7 +36,7 @@ describe GitlabAccess do
context "access is granted" do context "access is granted" do
it "returns true" do it "returns true" do
expect(subject.exec).to be_true expect(subject.exec).to be_truthy
end end
end end
...@@ -54,7 +54,7 @@ describe GitlabAccess do ...@@ -54,7 +54,7 @@ describe GitlabAccess do
end end
it "returns false" do it "returns false" do
expect(subject.exec).to be_false expect(subject.exec).to be_falsey
end end
end end
...@@ -65,7 +65,7 @@ describe GitlabAccess do ...@@ -65,7 +65,7 @@ describe GitlabAccess do
end end
it "returns false" do it "returns false" do
expect(subject.exec).to be_false expect(subject.exec).to be_falsey
end end
end end
end end
......
...@@ -62,7 +62,7 @@ describe GitlabKeys do ...@@ -62,7 +62,7 @@ describe GitlabKeys do
end end
it "should return true" do it "should return true" do
gitlab_keys.send(:add_key).should be_true gitlab_keys.send(:add_key).should be_truthy
end end
end end
end end
...@@ -133,7 +133,7 @@ describe GitlabKeys do ...@@ -133,7 +133,7 @@ describe GitlabKeys do
end end
it "should return true" do it "should return true" do
gitlab_keys.send(:batch_add_keys).should be_true gitlab_keys.send(:batch_add_keys).should be_truthy
end end
end end
end end
...@@ -174,7 +174,7 @@ describe GitlabKeys do ...@@ -174,7 +174,7 @@ describe GitlabKeys do
end end
it "should return true" do it "should return true" do
gitlab_keys.send(:rm_key).should be_true gitlab_keys.send(:rm_key).should be_truthy
end end
end end
...@@ -201,7 +201,7 @@ describe GitlabKeys do ...@@ -201,7 +201,7 @@ describe GitlabKeys do
it "should return true" do it "should return true" do
gitlab_keys.stub(:open) gitlab_keys.stub(:open)
gitlab_keys.send(:clear).should be_true gitlab_keys.send(:clear).should be_truthy
end end
end end
......
...@@ -3,6 +3,10 @@ require_relative '../lib/gitlab_metrics' ...@@ -3,6 +3,10 @@ require_relative '../lib/gitlab_metrics'
describe GitlabMetrics do describe GitlabMetrics do
describe '.measure' do describe '.measure' do
before do
$logger = double('logger').as_null_object
end
it 'returns the return value of the block' do it 'returns the return value of the block' do
val = described_class.measure('foo') { 10 } val = described_class.measure('foo') { 10 }
...@@ -10,7 +14,7 @@ describe GitlabMetrics do ...@@ -10,7 +14,7 @@ describe GitlabMetrics do
end end
it 'writes the metrics data to a log file' do it 'writes the metrics data to a log file' do
expect(described_class.logger).to receive(:debug). expect($logger).to receive(:debug).
with('metrics', a_metrics_log_message('foo')) with('metrics', a_metrics_log_message('foo'))
described_class.measure('foo') { 10 } described_class.measure('foo') { 10 }
......
This diff is collapsed.
...@@ -4,6 +4,7 @@ require_relative '../lib/gitlab_access_status' ...@@ -4,6 +4,7 @@ require_relative '../lib/gitlab_access_status'
describe GitlabShell do describe GitlabShell do
before do before do
$logger = double('logger').as_null_object
FileUtils.mkdir_p(tmp_repos_path) FileUtils.mkdir_p(tmp_repos_path)
end end
...@@ -428,7 +429,7 @@ describe GitlabShell do ...@@ -428,7 +429,7 @@ describe GitlabShell do
it "refuses to assign the path" do it "refuses to assign the path" do
$stderr.should_receive(:puts).with("GitLab: Invalid repository path") $stderr.should_receive(:puts).with("GitLab: Invalid repository path")
expect(subject.exec(ssh_cmd)).to be_false expect(subject.exec(ssh_cmd)).to be_falsey
end end
end end
end end
......
ROOT_PATH = File.expand_path(File.join(File.dirname(__FILE__), "..")) ROOT_PATH = File.expand_path(File.join(File.dirname(__FILE__), ".."))
require 'rspec/its'
require 'simplecov' require 'simplecov'
SimpleCov.start SimpleCov.start
......
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