Commit df89099e authored by Douwe Maan's avatar Douwe Maan

Merge branch 'sh-bump-ruby-version' into 'master'

Upgrade to Ruby 2.5.3

See merge request gitlab-org/gitlab-shell!257
parents 528960c8 46e81d93
image: "ruby:2.3" image: "ruby:2.5"
before_script: before_script:
- export PATH=~/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin - export PATH=~/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
...@@ -30,18 +30,18 @@ rubocop: ...@@ -30,18 +30,18 @@ rubocop:
except: except:
- tags - tags
#ruby 2.2 #ruby 2.4
rspec:ruby2.2: rspec:ruby2.4:
image: ruby:2.2 image: ruby:2.4
<<: *rspec_definition <<: *rspec_definition
tags: tags:
- ruby - ruby
except: except:
- tags - tags
#ruby 2.1 #ruby 2.3
rspec:ruby2.1: rspec:ruby2.3:
image: ruby:2.1 image: ruby:2.3
<<: *rspec_definition <<: *rspec_definition
tags: tags:
- ruby - ruby
...@@ -77,7 +77,7 @@ go:1.10: ...@@ -77,7 +77,7 @@ go:1.10:
go:1.11: go:1.11:
<<: *go_definition <<: *go_definition
image: golang:1.10 image: golang:1.11
codequality: codequality:
image: docker:stable image: docker:stable
......
...@@ -6,6 +6,7 @@ AllCops: ...@@ -6,6 +6,7 @@ AllCops:
- 'tmp/**/*' - 'tmp/**/*'
- 'bin/**/*' - 'bin/**/*'
- 'hooks/**/*' - 'hooks/**/*'
- 'support/**/*'
- 'Guardfile' - 'Guardfile'
Layout/DotPosition: Layout/DotPosition:
...@@ -35,6 +36,9 @@ Metrics/CyclomaticComplexity: ...@@ -35,6 +36,9 @@ Metrics/CyclomaticComplexity:
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:
Enabled: false Enabled: false
Naming/AccessorMethodName:
Enabled: false
Style/Documentation: Style/Documentation:
Enabled: false Enabled: false
...@@ -47,9 +51,6 @@ Style/StringLiterals: ...@@ -47,9 +51,6 @@ Style/StringLiterals:
Style/GlobalVars: Style/GlobalVars:
Enabled: false Enabled: false
Style/AccessorMethodName:
Enabled: false
Style/GuardClause: Style/GuardClause:
Enabled: false Enabled: false
......
source 'https://rubygems.org' source 'https://rubygems.org'
group :development do
gem 'pry-byebug', '~> 3.6', require: false
end
group :development, :test do group :development, :test do
gem 'listen', '~> 0.5.0' gem 'listen', '~> 0.5.0'
gem 'rspec', '~> 3.8.0' gem 'rspec', '~> 3.8.0'
gem 'rspec-parameterized', '~> 0.4.0' gem 'rspec-parameterized', '~> 0.4.0'
gem 'rubocop', '0.49.1', require: false gem 'rubocop', '0.61', require: false
gem 'simplecov', '~> 0.9.0', require: false gem 'simplecov', '~> 0.16.1', require: false
gem 'vcr', '~> 4.0.0' gem 'vcr', '~> 4.0.0'
gem 'webmock', '~> 3.4.0' gem 'webmock', '~> 3.4.0'
end end
...@@ -8,26 +8,27 @@ GEM ...@@ -8,26 +8,27 @@ GEM
addressable (2.5.2) addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0) public_suffix (>= 2.0.2, < 4.0)
ast (2.4.0) ast (2.4.0)
binding_of_caller (0.8.0) binding_ninja (0.2.2)
debug_inspector (>= 0.0.1) byebug (10.0.2)
coderay (1.1.2) coderay (1.1.2)
concord (0.1.5) concord (0.1.5)
adamantium (~> 0.2.0) adamantium (~> 0.2.0)
equalizer (~> 0.0.9) equalizer (~> 0.0.9)
crack (0.4.3) crack (0.4.3)
safe_yaml (~> 1.0.0) safe_yaml (~> 1.0.0)
debug_inspector (0.0.3)
diff-lcs (1.3) diff-lcs (1.3)
docile (1.1.5) docile (1.3.1)
equalizer (0.0.11) equalizer (0.0.11)
hashdiff (0.3.7) hashdiff (0.3.7)
ice_nine (0.11.2) ice_nine (0.11.2)
jaro_winkler (1.5.1)
json (2.1.0)
listen (0.5.3) listen (0.5.3)
memoizable (0.4.2) memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1) thread_safe (~> 0.3, >= 0.3.1)
multi_json (1.13.1) method_source (0.9.2)
parallel (1.12.1) parallel (1.12.1)
parser (2.5.1.2) parser (2.5.3.0)
ast (~> 2.4.0) ast (~> 2.4.0)
powerpack (0.1.2) powerpack (0.1.2)
proc_to_ast (0.1.0) proc_to_ast (0.1.0)
...@@ -35,46 +36,51 @@ GEM ...@@ -35,46 +36,51 @@ GEM
parser parser
unparser unparser
procto (0.0.3) procto (0.0.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.6.0)
byebug (~> 10.0)
pry (~> 0.10)
public_suffix (3.0.3) public_suffix (3.0.3)
rainbow (2.2.2) rainbow (3.0.0)
rake
rake (12.3.1)
rspec (3.8.0) rspec (3.8.0)
rspec-core (~> 3.8.0) rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0) rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0) rspec-mocks (~> 3.8.0)
rspec-core (3.8.0) rspec-core (3.8.0)
rspec-support (~> 3.8.0) rspec-support (~> 3.8.0)
rspec-expectations (3.8.1) rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0) rspec-support (~> 3.8.0)
rspec-mocks (3.8.0) rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.8.0) rspec-support (~> 3.8.0)
rspec-parameterized (0.4.0) rspec-parameterized (0.4.1)
binding_of_caller binding_ninja (>= 0.2.1)
parser parser
proc_to_ast proc_to_ast
rspec (>= 2.13, < 4) rspec (>= 2.13, < 4)
unparser unparser
rspec-support (3.8.0) rspec-support (3.8.0)
rubocop (0.49.1) rubocop (0.61.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10) parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0) parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1) powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0) rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1) unicode-display_width (~> 1.4.0)
ruby-progressbar (1.9.0) ruby-progressbar (1.10.0)
safe_yaml (1.0.4) safe_yaml (1.0.4)
simplecov (0.9.2) simplecov (0.16.1)
docile (~> 1.1.0) docile (~> 1.1)
multi_json (~> 1.0) json (>= 1.8, < 3)
simplecov-html (~> 0.9.0) simplecov-html (~> 0.10.0)
simplecov-html (0.9.0) simplecov-html (0.10.2)
thread_safe (0.3.6) thread_safe (0.3.6)
unicode-display_width (1.4.0) unicode-display_width (1.4.0)
unparser (0.2.8) unparser (0.4.2)
abstract_type (~> 0.0.7) abstract_type (~> 0.0.7)
adamantium (~> 0.2.0) adamantium (~> 0.2.0)
concord (~> 0.1.5) concord (~> 0.1.5)
...@@ -93,12 +99,13 @@ PLATFORMS ...@@ -93,12 +99,13 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
listen (~> 0.5.0) listen (~> 0.5.0)
pry-byebug (~> 3.6)
rspec (~> 3.8.0) rspec (~> 3.8.0)
rspec-parameterized (~> 0.4.0) rspec-parameterized (~> 0.4.0)
rubocop (= 0.49.1) rubocop (= 0.61)
simplecov (~> 0.9.0) simplecov (~> 0.16.1)
vcr (~> 4.0.0) vcr (~> 4.0.0)
webmock (~> 3.4.0) webmock (~> 3.4.0)
BUNDLED WITH BUNDLED WITH
1.16.3 1.17.1
...@@ -96,12 +96,13 @@ module Action ...@@ -96,12 +96,13 @@ module Action
def print_flush(str) def print_flush(str)
return false unless str return false unless str
$stdout.print(Base64.decode64(str)) $stdout.print(Base64.decode64(str))
$stdout.flush $stdout.flush
end end
def inform_client(str) def inform_client(str)
$stderr.puts(format_gitlab_output(str)) warn(format_gitlab_output(str))
end end
def format_gitlab_output(str) def format_gitlab_output(str)
......
...@@ -31,10 +31,10 @@ class GitlabAccess ...@@ -31,10 +31,10 @@ class GitlabAccess
true true
rescue GitlabNet::ApiUnreachableError rescue GitlabNet::ApiUnreachableError
$stderr.puts "GitLab: Failed to authorize your Git request: internal API unreachable" warn "GitLab: Failed to authorize your Git request: internal API unreachable"
false false
rescue AccessDeniedError => ex rescue AccessDeniedError => ex
$stderr.puts "GitLab: #{ex.message}" warn "GitLab: #{ex.message}"
false false
end end
......
require 'timeout' require 'timeout'
require 'open3'
require_relative 'gitlab_config' require_relative 'gitlab_config'
require_relative 'gitlab_logger' require_relative 'gitlab_logger'
...@@ -14,7 +15,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength ...@@ -14,7 +15,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
end end
def self.command_key(key_id) def self.command_key(key_id)
unless /\A[a-z0-9-]+\z/ =~ key_id unless /\A[a-z0-9-]+\z/ =~ key_id # rubocop:disable Performance/RegexpMatch
raise KeyError, "Invalid key_id: #{key_id.inspect}" raise KeyError, "Invalid key_id: #{key_id.inspect}"
end end
...@@ -107,7 +108,9 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength ...@@ -107,7 +108,9 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
open_auth_file('r') do |f| open_auth_file('r') do |f|
f.each_line do |line| f.each_line do |line|
matchd = line.match(/key-(\d+)/) matchd = line.match(/key-(\d+)/)
next unless matchd next unless matchd
puts matchd[1] puts matchd[1]
end end
end end
...@@ -138,6 +141,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength ...@@ -138,6 +141,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
open_auth_file('r+') do |f| open_auth_file('r+') do |f|
while line = f.gets # rubocop:disable Lint/AssignmentInCondition while line = f.gets # rubocop:disable Lint/AssignmentInCondition
next unless line.start_with?("command=\"#{self.class.command_key(@key_id)}\"") next unless line.start_with?("command=\"#{self.class.command_key(@key_id)}\"")
f.seek(-line.length, IO::SEEK_CUR) f.seek(-line.length, IO::SEEK_CUR)
# Overwrite the line with #'s. Because the 'line' variable contains # Overwrite the line with #'s. Because the 'line' variable contains
# a terminating '\n', we write line.length - 1 '#' characters. # a terminating '\n', we write line.length - 1 '#' characters.
...@@ -155,20 +159,24 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength ...@@ -155,20 +159,24 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
def check_permissions def check_permissions
open_auth_file(File::RDWR | File::CREAT) { true } open_auth_file(File::RDWR | File::CREAT) { true }
rescue => ex rescue StandardError => ex
puts "error: could not open #{auth_file}: #{ex}" puts "error: could not open #{auth_file}: #{ex}"
if File.exist?(auth_file)
system('ls', '-l', auth_file) cmd = if File.exist?(auth_file)
else %W{ls -l #{auth_file}}
# Maybe the parent directory is not writable? else
system('ls', '-ld', File.dirname(auth_file)) # Maybe the parent directory is not writable?
end %W{ls -ld #{File.dirname(auth_file)}}
end
output, = Open3.capture2e(cmd.join(' '))
puts output
false false
end end
def lock(timeout = 10) def lock(timeout = 10)
File.open(lock_file, "w+") do |f| File.open(lock_file, "w+") do |f|
begin begin # rubocop:disable Style/RedundantBegin
f.flock File::LOCK_EX f.flock File::LOCK_EX
Timeout.timeout(timeout) { yield } Timeout.timeout(timeout) { yield }
ensure ensure
...@@ -182,7 +190,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength ...@@ -182,7 +190,7 @@ class GitlabKeys # rubocop:disable Metrics/ClassLength
end end
def open_auth_file(mode) def open_auth_file(mode)
open(auth_file, mode, 0o600) do |file| File.open(auth_file, mode, 0o600) do |file|
file.chmod(0o600) file.chmod(0o600)
yield file yield file
end end
......
...@@ -13,7 +13,7 @@ class GitlabLfsAuthentication ...@@ -13,7 +13,7 @@ class GitlabLfsAuthentication
def self.build_from_json(json) def self.build_from_json(json)
values = JSON.parse(json) values = JSON.parse(json)
new(values['username'], values['lfs_token'], values['repository_http_path']) new(values['username'], values['lfs_token'], values['repository_http_path'])
rescue rescue StandardError
nil nil
end end
......
...@@ -7,15 +7,14 @@ require_relative 'gitlab_config' ...@@ -7,15 +7,14 @@ require_relative 'gitlab_config'
def convert_log_level(log_level) def convert_log_level(log_level)
Logger.const_get(log_level.upcase) Logger.const_get(log_level.upcase)
rescue NameError rescue NameError
$stderr.puts "WARNING: Unrecognized log level #{log_level.inspect}." warn "WARNING: Unrecognized log level #{log_level.inspect}. Falling back to INFO."
$stderr.puts "WARNING: Falling back to INFO."
Logger::INFO Logger::INFO
end end
class GitlabLogger class GitlabLogger
# Emulate the quoting logic of logrus # Emulate the quoting logic of logrus
# https://github.com/sirupsen/logrus/blob/v1.0.5/text_formatter.go#L143-L156 # https://github.com/sirupsen/logrus/blob/v1.0.5/text_formatter.go#L143-L156
SHOULD_QUOTE = /[^a-zA-Z0-9\-._\/@^+]/ SHOULD_QUOTE = /[^a-zA-Z0-9\-._\/@^+]/.freeze
LEVELS = { LEVELS = {
Logger::INFO => 'info'.freeze, Logger::INFO => 'info'.freeze,
......
# frozen_string_literal: true
require 'net/http' require 'net/http'
require 'openssl' require 'openssl'
require 'json' require 'json'
...@@ -11,7 +13,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -11,7 +13,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
include HTTPHelper include HTTPHelper
CHECK_TIMEOUT = 5 CHECK_TIMEOUT = 5
API_INACCESSIBLE_MESSAGE = 'API is not accessible'.freeze API_INACCESSIBLE_MESSAGE = 'API is not accessible'
def check_access(cmd, gl_repository, repo, who, changes, protocol, env: {}) def check_access(cmd, gl_repository, repo, who, changes, protocol, env: {})
changes = changes.join("\n") unless changes.is_a?(String) changes = changes.join("\n") unless changes.is_a?(String)
...@@ -76,8 +78,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -76,8 +78,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
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 = "#{internal_api_endpoint}/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)
if resp.code == '200' if resp.code == '200'
...@@ -85,7 +87,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -85,7 +87,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
else else
[] []
end end
rescue rescue StandardError
[] []
end end
...@@ -94,9 +96,9 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -94,9 +96,9 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end end
def authorized_key(key) def authorized_key(key)
resp = get("#{internal_api_endpoint}/authorized_keys?key=#{URI.escape(key, '+/=')}") resp = get("#{internal_api_endpoint}/authorized_keys?key=#{URI.escape(key, '+/=')}") # rubocop:disable Lint/UriEscapeUnescape
JSON.parse(resp.body) if resp.code == "200" JSON.parse(resp.body) if resp.code == "200"
rescue rescue StandardError
nil nil
end end
...@@ -106,7 +108,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -106,7 +108,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
resp = post("#{internal_api_endpoint}/two_factor_recovery_codes", id_sym => id) resp = post("#{internal_api_endpoint}/two_factor_recovery_codes", id_sym => id)
JSON.parse(resp.body) if resp.code == '200' JSON.parse(resp.body) if resp.code == '200'
rescue rescue StandardError
{} {}
end end
...@@ -115,7 +117,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -115,7 +117,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
resp = post("#{internal_api_endpoint}/notify_post_receive", params) resp = post("#{internal_api_endpoint}/notify_post_receive", params)
resp.code == '200' resp.code == '200'
rescue rescue StandardError
false false
end end
...@@ -143,11 +145,15 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -143,11 +145,15 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def self.parse_who(who) def self.parse_who(who)
if who.start_with?("key-") if who.start_with?("key-")
value = who.gsub("key-", "") value = who.gsub("key-", "")
raise ArgumentError, "who='#{who}' is invalid!" unless value =~ /\A[0-9]+\z/ raise ArgumentError, "who='#{who}' is invalid!" unless value =~ /\A[0-9]+\z/
[:key_id, 'key_id', value] [:key_id, 'key_id', value]
elsif who.start_with?("user-") elsif who.start_with?("user-")
value = who.gsub("user-", "") value = who.gsub("user-", "")
raise ArgumentError, "who='#{who}' is invalid!" unless value =~ /\A[0-9]+\z/ raise ArgumentError, "who='#{who}' is invalid!" unless value =~ /\A[0-9]+\z/
[:user_id, 'user_id', value] [:user_id, 'user_id', value]
elsif who.start_with?("username-") elsif who.start_with?("username-")
[:username, 'username', who.gsub("username-", "")] [:username, 'username', who.gsub("username-", "")]
...@@ -161,4 +167,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -161,4 +167,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
def sanitize_path(repo) def sanitize_path(repo)
repo.delete("'") repo.delete("'")
end end
def uri_escape(str)
URI.escape(str) # rubocop:disable Lint/UriEscapeUnescape
end
end end
...@@ -25,6 +25,7 @@ class GitlabPostReceive ...@@ -25,6 +25,7 @@ class GitlabPostReceive
end end
return false unless response return false unless response
print_broadcast_message(response['broadcast_message']) if response['broadcast_message'] print_broadcast_message(response['broadcast_message']) if response['broadcast_message']
print_merge_request_links(response['merge_request_urls']) if response['merge_request_urls'] print_merge_request_links(response['merge_request_urls']) if response['merge_request_urls']
puts response['redirected_message'] if response['redirected_message'] puts response['redirected_message'] if response['redirected_message']
...@@ -43,6 +44,7 @@ class GitlabPostReceive ...@@ -43,6 +44,7 @@ class GitlabPostReceive
def print_merge_request_links(merge_request_urls) def print_merge_request_links(merge_request_urls)
return if merge_request_urls.empty? return if merge_request_urls.empty?
puts puts
merge_request_urls.each { |mr| print_merge_request_link(mr) } merge_request_urls.each { |mr| print_merge_request_link(mr) }
end end
......
...@@ -81,23 +81,23 @@ class GitlabShell # rubocop:disable Metrics/ClassLength ...@@ -81,23 +81,23 @@ class GitlabShell # rubocop:disable Metrics/ClassLength
true true
rescue GitlabNet::ApiUnreachableError rescue GitlabNet::ApiUnreachableError
$stderr.puts "GitLab: Failed to authorize your Git request: internal API unreachable" warn "GitLab: Failed to authorize your Git request: internal API unreachable"
false false
rescue AccessDeniedError => ex rescue AccessDeniedError => ex
$logger.warn('Access denied', command: origin_cmd, user: log_username) $logger.warn('Access denied', command: origin_cmd, user: log_username)
$stderr.puts "GitLab: #{ex.message}" warn "GitLab: #{ex.message}"
false false
rescue DisallowedCommandError rescue DisallowedCommandError
$logger.warn('Denied disallowed command', command: origin_cmd, user: log_username) $logger.warn('Denied disallowed command', command: origin_cmd, user: log_username)
$stderr.puts "GitLab: Disallowed command" warn "GitLab: Disallowed command"
false false
rescue InvalidRepositoryPathError rescue InvalidRepositoryPathError
$stderr.puts "GitLab: Invalid repository path" warn "GitLab: Invalid repository path"
false false
rescue Action::Custom::BaseError => ex rescue Action::Custom::BaseError => ex
$logger.warn('Custom action error', exception: ex.class, message: ex.message, $logger.warn('Custom action error', exception: ex.class, message: ex.message,
command: origin_cmd, user: log_username) command: origin_cmd, user: log_username)
$stderr.puts ex.message warn ex.message
false false
end end
...@@ -121,6 +121,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength ...@@ -121,6 +121,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength
case @command case @command
when GIT_LFS_AUTHENTICATE_COMMAND when GIT_LFS_AUTHENTICATE_COMMAND
raise DisallowedCommandError unless args.count >= 2 raise DisallowedCommandError unless args.count >= 2
@repo_name = args[1] @repo_name = args[1]
case args[2] case args[2]
when 'download' when 'download'
...@@ -132,6 +133,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength ...@@ -132,6 +133,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength
end end
else else
raise DisallowedCommandError unless args.count == 2 raise DisallowedCommandError unless args.count == 2
@repo_name = args.last @repo_name = args.last
end end
...@@ -201,7 +203,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength ...@@ -201,7 +203,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength
begin begin
if defined?(@who) if defined?(@who)
@user = api.discover(@who) @user = api.discover(@who)
@gl_id = "user-#{@user['id']}" if @user && @user.key?('id') @gl_id = "user-#{@user['id']}" if @user&.key?('id')
else else
@user = api.discover(@gl_id) @user = api.discover(@gl_id)
end end
......
...@@ -74,7 +74,7 @@ module HTTPHelper ...@@ -74,7 +74,7 @@ module HTTPHelper
begin begin
start_time = Time.new start_time = Time.new
response = http.start { http.request(request) } response = http.start { http.request(request) }
rescue => e rescue StandardError => e
$logger.warn('Failed to connect', method: method.to_s.upcase, url: url, error: e) $logger.warn('Failed to connect', method: method.to_s.upcase, url: url, error: e)
raise GitlabNet::ApiUnreachableError raise GitlabNet::ApiUnreachableError
ensure ensure
......
...@@ -12,13 +12,14 @@ module URI ...@@ -12,13 +12,14 @@ module URI
def hostname def hostname
# decode %XX from path to file # decode %XX from path to file
v = host v = host
URI.decode(v) URI.decode(v) # rubocop:disable Lint/UriEscapeUnescape
end end
# port is not allowed in URI # port is not allowed in URI
DEFAULT_PORT = nil DEFAULT_PORT = nil
def set_port(v) def set_port(value)
return v unless v return value unless value
raise InvalidURIError, "http+unix:// cannot contain port" raise InvalidURIError, "http+unix:// cannot contain port"
end end
end end
......
...@@ -5,6 +5,8 @@ require 'stringio' ...@@ -5,6 +5,8 @@ require 'stringio'
describe GitlabKeys do describe GitlabKeys do
before do before do
$logger = double('logger').as_null_object $logger = double('logger').as_null_object
# The default 'auth_file' value from config.yml.example is '/home/git/.ssh/authorized_keys'
allow(GitlabConfig).to receive_message_chain(:new, :auth_file).and_return('/home/git/.ssh/authorized_keys')
end end
describe '.command' do describe '.command' do
...@@ -148,7 +150,9 @@ describe GitlabKeys do ...@@ -148,7 +150,9 @@ describe GitlabKeys do
context "without file writing" do context "without file writing" do
before do before do
expect(gitlab_keys).to receive(:open).and_yield(double(:file, puts: nil, chmod: nil)) file = double(:file, puts: nil, chmod: nil, flock: nil)
expect(File).to receive(:open).with(tmp_authorized_keys_path + '.lock', 'w+').and_yield(file)
expect(File).to receive(:open).with(tmp_authorized_keys_path, "a", 0o600).and_yield(file)
end end
it "should log an add-key event" do it "should log an add-key event" do
...@@ -189,7 +193,8 @@ describe GitlabKeys do ...@@ -189,7 +193,8 @@ describe GitlabKeys do
context "without file writing" do context "without file writing" do
before do before do
allow(gitlab_keys).to receive(:open) allow(File).to receive(:open).with("#{ROOT_PATH}/config.yml", 'r:bom|utf-8').and_call_original
allow(File).to receive(:open).with('/home/git/.ssh/authorized_keys', 'r+', 384)
allow(gitlab_keys).to receive(:lock).and_yield allow(gitlab_keys).to receive(:lock).and_yield
end end
...@@ -225,7 +230,8 @@ describe GitlabKeys do ...@@ -225,7 +230,8 @@ describe GitlabKeys do
let(:gitlab_keys) { build_gitlab_keys('clear') } let(:gitlab_keys) { build_gitlab_keys('clear') }
it "should return true" do it "should return true" do
allow(gitlab_keys).to receive(:open) allow(File).to receive(:open).with("#{ROOT_PATH}/config.yml", 'r:bom|utf-8').and_call_original
allow(File).to receive(:open).with('/home/git/.ssh/authorized_keys', 'w', 384)
expect(gitlab_keys.send(:clear)).to be_truthy expect(gitlab_keys.send(:clear)).to be_truthy
end end
end end
...@@ -240,7 +246,7 @@ describe GitlabKeys do ...@@ -240,7 +246,7 @@ describe GitlabKeys do
it 'returns false if opening raises an exception' do it 'returns false if opening raises an exception' do
expect(gitlab_keys).to receive(:open_auth_file).and_raise("imaginary error") expect(gitlab_keys).to receive(:open_auth_file).and_raise("imaginary error")
expect(gitlab_keys.exec).to eq(false) expect { expect(gitlab_keys.exec).to eq(false) }.to output(/imaginary error/).to_stdout
end end
it 'creates the keys file if it does not exist' do it 'creates the keys file if it does not exist' do
......
...@@ -3,11 +3,13 @@ require_relative '../lib/gitlab_logger' ...@@ -3,11 +3,13 @@ require_relative '../lib/gitlab_logger'
require 'securerandom' require 'securerandom'
describe :convert_log_level do describe :convert_log_level do
subject { convert_log_level :extreme } it "returns desired Logger::<type>" do
expect(convert_log_level(:debug)).to eq(Logger::DEBUG)
end
it "converts invalid log level to Logger::INFO" do it "converts invalid log level to Logger::INFO" do
expect($stderr).to receive(:puts).at_least(:once) expect { convert_log_level(:extreme) }.to output(/WARNING: Unrecognized log level :extreme. Falling back to INFO./).to_stderr
is_expected.to eq(Logger::INFO) expect(convert_log_level(:extreme)).to eq(Logger::INFO)
end end
end end
......
...@@ -67,7 +67,7 @@ describe GitlabNet, vcr: true do ...@@ -67,7 +67,7 @@ describe GitlabNet, vcr: true do
describe '#lfs_authenticate' do describe '#lfs_authenticate' do
context 'lfs authentication succeeded' do context 'lfs authentication succeeded' do
let(:repository_http_path) { URI.join(internal_api_endpoint.sub('api/v4', ''), project).to_s } let(:repository_http_path) { URI.join(internal_api_endpoint.sub('/api/v4/internal', ''), project).to_s }
context 'for download operation' do context 'for download operation' do
it 'should return the correct data' do it 'should return the correct data' do
......
require_relative 'spec_helper' require_relative 'spec_helper'
require 'open3'
describe 'bin/gitlab-shell' do describe 'bin/gitlab-shell' do
def original_root_path def original_root_path
...@@ -131,12 +132,13 @@ describe 'bin/gitlab-shell' do ...@@ -131,12 +132,13 @@ describe 'bin/gitlab-shell' do
def run!(args) def run!(args)
cmd = [ cmd = [
'SSH_CONNECTION=fake',
gitlab_shell_path, gitlab_shell_path,
args args
].flatten.compact ].flatten.join(' ')
output = IO.popen({'SSH_CONNECTION' => 'fake'}, cmd, &:read) stdout, _stderr, status = Open3.capture3(cmd)
[output, $?] [stdout, status]
end end
end end
...@@ -46,6 +46,6 @@ http_interactions: ...@@ -46,6 +46,6 @@ http_interactions:
- '1.436040' - '1.436040'
body: body:
encoding: UTF-8 encoding: UTF-8
string: '""' string: '{'
http_version: http_version:
recorded_at: Fri, 20 Jul 2018 06:18:58 GMT recorded_at: Fri, 20 Jul 2018 06:18:58 GMT
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