summaryrefslogtreecommitdiff
path: root/lib/gitlab_keys.rb
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2015-07-16 10:10:46 +0200
committerJacob Vosmaer <contact@jacobvosmaer.nl>2015-07-16 10:10:46 +0200
commit4fd8e44ba12d126b0553b90c099d1b63fa163d00 (patch)
treefab244d21d1792283f53a4dd4bdca4e7970e358f /lib/gitlab_keys.rb
parent4d30c0c5d3d0f23a221ee507b6bd110a539b8570 (diff)
downloadgitlab-shell-4fd8e44ba12d126b0553b90c099d1b63fa163d00.tar.gz
Remove keys from authorized_keys in-place
This will speed up the rm-key operation. The downside is that authorized_keys will not shrink when you remove a key. If this ever becomes a problem it can be fixed by running 'rake gitlab:shell:setup'.
Diffstat (limited to 'lib/gitlab_keys.rb')
-rw-r--r--lib/gitlab_keys.rb15
1 files changed, 7 insertions, 8 deletions
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb
index 10f84bd..a8baf45 100644
--- a/lib/gitlab_keys.rb
+++ b/lib/gitlab_keys.rb
@@ -1,4 +1,3 @@
-require 'tempfile'
require 'timeout'
require_relative 'gitlab_config'
@@ -82,14 +81,14 @@ class GitlabKeys
def rm_key
lock do
$logger.info "Removing key #{@key_id}"
- Tempfile.open('authorized_keys') do |temp|
- open(auth_file, 'r+') do |current|
- current.each do |line|
- temp.puts(line) unless line.start_with?("command=\"#{key_command(@key_id)}\"")
- end
+ open(auth_file, 'r+') do |f|
+ while line = f.gets do
+ next unless line.start_with?("command=\"#{key_command(@key_id)}\"")
+ f.seek(-line.length, IO::SEEK_CUR)
+ # Overwrite the line with #'s. Because the 'line' variable contains
+ # a terminating '\n', we write line.length - 1 '#' characters.
+ f.write('#' * (line.length - 1))
end
- temp.close
- FileUtils.cp(temp.path, auth_file)
end
end
true