diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action.rb | 7 | ||||
-rw-r--r-- | lib/action/api_2fa_recovery.rb | 54 | ||||
-rw-r--r-- | lib/action/base.rb | 30 | ||||
-rw-r--r-- | lib/action/git_lfs_authenticate.rb | 26 | ||||
-rw-r--r-- | lib/action/gitaly.rb | 119 |
5 files changed, 236 insertions, 0 deletions
diff --git a/lib/action.rb b/lib/action.rb new file mode 100644 index 0000000..1f9cc6c --- /dev/null +++ b/lib/action.rb @@ -0,0 +1,7 @@ +require_relative 'action/base' +require_relative 'action/gitaly' +require_relative 'action/git_lfs_authenticate' +require_relative 'action/api_2fa_recovery' + +module Action +end diff --git a/lib/action/api_2fa_recovery.rb b/lib/action/api_2fa_recovery.rb new file mode 100644 index 0000000..827f8aa --- /dev/null +++ b/lib/action/api_2fa_recovery.rb @@ -0,0 +1,54 @@ +require_relative '../action' +require_relative '../gitlab_logger' + +module Action + class API2FARecovery < Base + def initialize(key_id) + @key_id = key_id + end + + def execute(_, _) + recover + end + + private + + attr_reader :key_id + + def continue?(question) + puts "#{question} (yes/no)" + STDOUT.flush # Make sure the question gets output before we wait for input + response = STDIN.gets.chomp + puts '' # Add a buffer in the output + response == 'yes' + end + + def recover + continue = continue?( + "Are you sure you want to generate new two-factor recovery codes?\n" \ + "Any existing recovery codes you saved will be invalidated." + ) + + unless continue + puts 'New recovery codes have *not* been generated. Existing codes will remain valid.' + return + end + + resp = api.two_factor_recovery_codes(key_id) + if resp['success'] + codes = resp['recovery_codes'].join("\n") + $logger.info('API 2FA recovery success', user: user.log_username) + puts "Your two-factor authentication recovery codes are:\n\n" \ + "#{codes}\n\n" \ + "During sign in, use one of the codes above when prompted for\n" \ + "your two-factor code. Then, visit your Profile Settings and add\n" \ + "a new device so you do not lose access to your account again." + true + else + $logger.info('API 2FA recovery error', user: user.log_username) + puts "An error occurred while trying to generate new recovery codes.\n" \ + "#{resp['message']}" + end + end + end +end diff --git a/lib/action/base.rb b/lib/action/base.rb new file mode 100644 index 0000000..1f24c8c --- /dev/null +++ b/lib/action/base.rb @@ -0,0 +1,30 @@ +require 'json' + +require_relative '../gitlab_config' +require_relative '../gitlab_net' +require_relative '../gitlab_metrics' +require_relative '../user' + +module Action + class Base + def self.create_from_json(_) + raise NotImplementedError + end + + private + + attr_reader :key_id + + def config + @config ||= GitlabConfig.new + end + + def api + @api ||= GitlabNet.new + end + + def user + @user ||= User.new(key_id, audit_usernames: config.audit_usernames) + end + end +end diff --git a/lib/action/git_lfs_authenticate.rb b/lib/action/git_lfs_authenticate.rb new file mode 100644 index 0000000..d38d845 --- /dev/null +++ b/lib/action/git_lfs_authenticate.rb @@ -0,0 +1,26 @@ +require_relative '../action' +require_relative '../gitlab_logger' + +module Action + class GitLFSAuthenticate < Base + def initialize(key_id, repo_name) + @key_id = key_id + @repo_name = repo_name + end + + def execute(_, _) + GitlabMetrics.measure('lfs-authenticate') do + $logger.info('Processing LFS authentication', user: user.log_username) + lfs_access = api.lfs_authenticate(key_id, repo_name) + return unless lfs_access + + puts lfs_access.authentication_payload + end + true + end + + private + + attr_reader :key_id, :repo_name + end +end diff --git a/lib/action/gitaly.rb b/lib/action/gitaly.rb new file mode 100644 index 0000000..65397e6 --- /dev/null +++ b/lib/action/gitaly.rb @@ -0,0 +1,119 @@ +require_relative '../action' +require_relative '../gitlab_logger' +require_relative '../gitlab_net' + +module Action + class Gitaly < Base + REPOSITORY_PATH_NOT_PROVIDED = "Repository path not provided. Please make sure you're using GitLab v8.10 or later.".freeze + MIGRATED_COMMANDS = { + 'git-upload-pack' => File.join(ROOT_PATH, 'bin', 'gitaly-upload-pack'), + 'git-upload-archive' => File.join(ROOT_PATH, 'bin', 'gitaly-upload-archive'), + 'git-receive-pack' => File.join(ROOT_PATH, 'bin', 'gitaly-receive-pack') + }.freeze + + def initialize(key_id, gl_repository, gl_username, repository_path, gitaly) + @key_id = key_id + @gl_repository = gl_repository + @gl_username = gl_username + @repository_path = repository_path + @gitaly = gitaly + end + + def self.create_from_json(key_id, json) + new(key_id, + json['gl_repository'], + json['gl_username'], + json['repository_path'], + json['gitaly']) + end + + def execute(command, args) + raise ArgumentError, REPOSITORY_PATH_NOT_PROVIDED unless repository_path + raise InvalidRepositoryPathError unless valid_repository? + + $logger.info('Performing Gitaly command', user: user.log_username) + process(command, args) + end + + private + + attr_reader :gl_repository, :gl_username, :repository_path, :gitaly + + def process(command, args) + executable = command + args = [repository_path] + + if MIGRATED_COMMANDS.key?(executable) && gitaly + executable = MIGRATED_COMMANDS[executable] + gitaly_address = gitaly['address'] + args = [gitaly_address, JSON.dump(gitaly_request)] + end + + args_string = [File.basename(executable), *args].join(' ') + $logger.info('executing git command', command: args_string, user: user.log_username) + + exec_cmd(executable, *args) + end + + def exec_cmd(*args) + env = exec_env + env['GITALY_TOKEN'] = gitaly['token'] if gitaly && gitaly.include?('token') + + if git_trace_available? + env.merge!( + 'GIT_TRACE' => config.git_trace_log_file, + 'GIT_TRACE_PACKET' => config.git_trace_log_file, + 'GIT_TRACE_PERFORMANCE' => config.git_trace_log_file + ) + end + + # We use 'chdir: ROOT_PATH' to let the next executable know where config.yml is. + Kernel.exec(env, *args, unsetenv_others: true, chdir: ROOT_PATH) + end + + def exec_env + { + 'HOME' => ENV['HOME'], + 'PATH' => ENV['PATH'], + 'LD_LIBRARY_PATH' => ENV['LD_LIBRARY_PATH'], + 'LANG' => ENV['LANG'], + 'GL_ID' => key_id, + 'GL_PROTOCOL' => GitlabNet::GL_PROTOCOL, + 'GL_REPOSITORY' => gl_repository, + 'GL_USERNAME' => gl_username + } + end + + def gitaly_request + # The entire gitaly_request hash should be built in gitlab-ce and passed + # on as-is. For now we build a fake one on the spot. + { + 'repository' => gitaly['repository'], + 'gl_repository' => gl_repository, + 'gl_id' => key_id, + 'gl_username' => gl_username + } + end + + def valid_repository? + File.absolute_path(repository_path) == repository_path + end + + def git_trace_available? + return false unless config.git_trace_log_file + + if Pathname(config.git_trace_log_file).relative? + $logger.warn('git trace log path must be absolute, ignoring', git_trace_log_file: config.git_trace_log_file) + return false + end + + begin + File.open(config.git_trace_log_file, 'a') { nil } + return true + rescue => ex + $logger.warn('Failed to open git trace log file', git_trace_log_file: config.git_trace_log_file, error: ex.to_s) + return false + end + end + end +end |