Commit 633042fd authored by Jacob Vosmaer's avatar Jacob Vosmaer

Remove sed invocation from GitlabKeys

parent 261cb1fe
...@@ -36,8 +36,13 @@ class GitlabKeys ...@@ -36,8 +36,13 @@ class GitlabKeys
def rm_key def rm_key
$logger.info "Removing key #{@key_id}" $logger.info "Removing key #{@key_id}"
Tempfile.open('authorized_keys') do |temp| Tempfile.open('authorized_keys') do |temp|
cmd = "sed '/shell #{@key_id}\"/d' #{auth_file} > #{temp.path} && mv #{temp.path} #{auth_file}" open(auth_file, 'r+') do |current|
system(cmd) 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
end end
......
...@@ -34,14 +34,15 @@ describe GitlabKeys do ...@@ -34,14 +34,15 @@ describe GitlabKeys do
describe :rm_key do describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') } 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 it "should receive valid cmd" do
auth_file = GitlabConfig.new.auth_file FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
valid_cmd = "sed '/shell key-741\"/d' #{auth_file} > tmp_path && mv tmp_path #{auth_file}" open(tmp_authorized_keys_path, 'w') do |auth_file|
gitlab_keys.should_receive(:system).with(valid_cmd) 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 gitlab_keys.send :rm_key
File.read(tmp_authorized_keys_path).should == "first key\nthird key\n"
end end
it "should log an rm-key event" do it "should log an rm-key event" do
...@@ -87,4 +88,8 @@ describe GitlabKeys do ...@@ -87,4 +88,8 @@ describe GitlabKeys do
ARGV[i] = arg ARGV[i] = arg
end end
end end
def tmp_authorized_keys_path
File.join(ROOT_PATH, 'tmp', 'authorized_keys')
end
end end
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