summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/gitlab_keys.rb9
-rw-r--r--spec/gitlab_keys_spec.rb15
2 files changed, 17 insertions, 7 deletions
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb
index 2ea5117..1185b0d 100644
--- a/lib/gitlab_keys.rb
+++ b/lib/gitlab_keys.rb
@@ -36,8 +36,13 @@ class GitlabKeys
def rm_key
$logger.info "Removing key #{@key_id}"
Tempfile.open('authorized_keys') do |temp|
- cmd = "sed '/shell #{@key_id}\"/d' #{auth_file} > #{temp.path} && mv #{temp.path} #{auth_file}"
- system(cmd)
+ open(auth_file, 'r+') do |current|
+ current.each do |line|
+ temp.puts(line) unless line.include?("/bin/gitlab-shell #{@key_id}\"")
+ end
+ end
+ temp.close
+ FileUtils.cp(temp.path, auth_file)
end
end
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb
index cbe09bd..3688a96 100644
--- a/spec/gitlab_keys_spec.rb
+++ b/spec/gitlab_keys_spec.rb
@@ -34,14 +34,15 @@ describe GitlabKeys do
describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
- let(:temp_file) { mock(:temp_file, path: 'tmp_path') }
- before { Tempfile.should_receive(:open).and_yield(temp_file) }
it "should receive valid cmd" do
- auth_file = GitlabConfig.new.auth_file
- valid_cmd = "sed '/shell key-741\"/d' #{auth_file} > tmp_path && mv tmp_path #{auth_file}"
- gitlab_keys.should_receive(:system).with(valid_cmd)
+ FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
+ open(tmp_authorized_keys_path, 'w') do |auth_file|
+ auth_file.puts ['first key', '/bin/gitlab-shell key-741"', 'third key']
+ end
+ gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
gitlab_keys.send :rm_key
+ File.read(tmp_authorized_keys_path).should == "first key\nthird key\n"
end
it "should log an rm-key event" do
@@ -87,4 +88,8 @@ describe GitlabKeys do
ARGV[i] = arg
end
end
+
+ def tmp_authorized_keys_path
+ File.join(ROOT_PATH, 'tmp', 'authorized_keys')
+ end
end