Skip to content

Commit

Permalink
Handle user create race condition
Browse files Browse the repository at this point in the history
Fixes #20041
  • Loading branch information
jvlcek committed Apr 23, 2020
1 parent 87de914 commit d0739e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
18 changes: 15 additions & 3 deletions app/models/authenticator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def authenticate(username, password, request = nil, options = {})
options[:require_user] ||= false
options[:authorize_only] ||= false
fail_message = _("Authentication failed")

user_or_taskid = nil

begin
Expand Down Expand Up @@ -137,8 +136,21 @@ def authorize(taskid, username, *args)
end

user.lastlogon = Time.now.utc
user.save!

if user.new_record?
User.with_lock do
begin
user.save!
rescue ActiveRecord::RecordInvalid # Try update when catching race condition on create.
userid, user = find_or_initialize_user(identity, username)
update_user_attributes(user, userid, identity)
user.miq_groups = matching_groups
user.save!
end
end
else
user.save!
end

_log.info("Authorized User: [#{user.userid}]")
task.userid = user.userid
task.update_status("Finished", "Ok", "User authorized successfully")
Expand Down
12 changes: 12 additions & 0 deletions spec/models/authenticator/httpd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,18 @@ def authenticate
expect { authenticate }.to(change { User.where(:userid => 'bob@example.com').count }.from(0).to(1))
end

context "with a race condition on create user" do
before do
authenticate
end

it "update the exiting user" do
allow(User).to receive(:lookup_by_userid).and_return(nil)
allow(User).to receive(:in_my_region).and_return(User.none, User.all)
expect { authenticate }.not_to(change { User.where(:userid => 'bob@example.com').count }.from(1))
end
end

context "with no matching groups" do
let(:headers) { super().merge('X-Remote-User-Groups' => 'bubble:trouble') }

Expand Down

0 comments on commit d0739e5

Please sign in to comment.