Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle user create race condition #20097

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/models/authenticator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,18 @@ def authorize(taskid, username, *args)
end

user.lastlogon = Time.now.utc
user.save!
if user.new_record?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that we only lock on a new user, which should ideally be less common than just logging in. This is great!

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

_log.info("Authorized User: [#{user.userid}]")
task.userid = user.userid
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 @@ -272,6 +272,18 @@ def authenticate
'X-Remote-User-Email' => 'Sally@example.com')
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 => 'sally@example.com').count }.from(1))
end
end

context "when user record with userid in upn format already exists" do
let!(:sally_username) { FactoryBot.create(:user, :userid => 'sAlly') }
let!(:sally_dn) { FactoryBot.create(:user, :userid => dn) }
Expand Down