Commit 4038ef2e authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'authorized_keys_lock' into 'master'

Authorized keys lock
parents d8600696 19a5c54a
...@@ -2,3 +2,4 @@ config.yml ...@@ -2,3 +2,4 @@ config.yml
tmp/* tmp/*
*.log *.log
/*.log.* /*.log.*
authorized_keys.lock
v1.9.4
- Use lock file when modify authorized_keys
v1.9.3 v1.9.3
- Ignore force push detection for new branch or branch remove push - Ignore force push detection for new branch or branch remove push
......
...@@ -36,6 +36,7 @@ class GitlabKeys ...@@ -36,6 +36,7 @@ class GitlabKeys
end end
def batch_add_keys def batch_add_keys
lock do
open(auth_file, 'a') do |file| open(auth_file, 'a') do |file|
stdin.each_line do |input| stdin.each_line do |input|
tokens = input.strip.split("\t") tokens = input.strip.split("\t")
...@@ -45,6 +46,7 @@ class GitlabKeys ...@@ -45,6 +46,7 @@ class GitlabKeys
file.puts(key_line(key_id, public_key)) file.puts(key_line(key_id, public_key))
end end
end end
end
true true
end end
...@@ -57,6 +59,7 @@ class GitlabKeys ...@@ -57,6 +59,7 @@ class GitlabKeys
end end
def rm_key def rm_key
lock do
$logger.info "Removing key #{@key_id}" $logger.info "Removing key #{@key_id}"
Tempfile.open('authorized_keys') do |temp| Tempfile.open('authorized_keys') do |temp|
open(auth_file, 'r+') do |current| open(auth_file, 'r+') do |current|
...@@ -67,6 +70,7 @@ class GitlabKeys ...@@ -67,6 +70,7 @@ class GitlabKeys
temp.close temp.close
FileUtils.cp(temp.path, auth_file) FileUtils.cp(temp.path, auth_file)
end end
end
true true
end end
...@@ -74,4 +78,20 @@ class GitlabKeys ...@@ -74,4 +78,20 @@ class GitlabKeys
open(auth_file, 'w') { |file| file.puts '# Managed by gitlab-shell' } open(auth_file, 'w') { |file| file.puts '# Managed by gitlab-shell' }
true true
end end
def lock(timeout = 10)
File.open(lock_file, "w+") do |f|
begin
f.flock File::LOCK_EX
Timeout::timeout(timeout) { yield }
ensure
f.flock File::LOCK_UN
end
end
end
def lock_file
@lock_file ||= File.join(ROOT_PATH, "authorized_keys.lock")
end
end end
...@@ -145,6 +145,42 @@ describe GitlabKeys do ...@@ -145,6 +145,42 @@ describe GitlabKeys do
end end
end end
describe :lock do
it "should raise exception if operation lasts more then timeout" do
key = GitlabKeys.new
expect do
key.send :lock, 1 do
sleep 2
end
end.to raise_error
end
it "should actually lock file" do
$global = ""
key = GitlabKeys.new
thr1 = Thread.new do
key.send :lock do
# Put bigger sleep here to test if main thread will
# wait for lock file released before executing code
sleep 1
$global << "foo"
end
end
# make sure main thread start lock command after
# thread above
sleep 0.5
key.send :lock do
$global << "bar"
end
thr1.join
$global.should == "foobar"
end
end
def build_gitlab_keys(*args) def build_gitlab_keys(*args)
argv(*args) argv(*args)
GitlabKeys.new GitlabKeys.new
......
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