Skip to content

Commit

Permalink
Merge pull request #66 from rapid7/bug/MSP-10914/race-condition
Browse files Browse the repository at this point in the history
fix race condition in create credential core

MSP-10914 #land
  • Loading branch information
trosen-r7 committed Jul 30, 2014
2 parents 52882a8 + eacb5ce commit 6d07b7b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions lib/metasploit/credential/creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,27 @@ def create_credential_core(opts={})
origin = opts.fetch(:origin)
workspace_id = opts.fetch(:workspace_id)

private_id = opts[:private].try(:id)
public_id = opts[:public].try(:id)
realm_id = opts[:realm].try(:id)

core = Metasploit::Credential::Core.where(private_id: private_id, public_id: public_id, realm_id: realm_id, workspace_id: workspace_id).first_or_initialize
if core.origin_id.nil?
core.origin = origin
end
if opts[:task_id]
core.tasks << Mdm::Task.find(opts[:task_id])
private_id = opts[:private].try(:id)
public_id = opts[:public].try(:id)
realm_id = opts[:realm].try(:id)

tries = 3
begin
core = Metasploit::Credential::Core.where(private_id: private_id, public_id: public_id, realm_id: realm_id, workspace_id: workspace_id).first_or_initialize
if core.origin_id.nil?
core.origin = origin
end
if opts[:task_id]
core.tasks << Mdm::Task.find(opts[:task_id])
end
core.save!
rescue ActiveRecord::RecordNotUnique
tries -= 1
if tries > 0
retry
end
end
core.save!

core
end

Expand Down
2 changes: 1 addition & 1 deletion lib/metasploit/credential/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Version
# The minor version number, scoped to the {MAJOR} version number.
MINOR = 8
# The patch number, scoped to the {MINOR} version number.
PATCH = 2
PATCH = 3

# The full version string, including the {MAJOR}, {MINOR}, {PATCH}, and optionally, the {PRERELEASE} in the
# {http://semver.org/spec/v2.0.0.html semantic versioning v2.0.0} format.
Expand Down

0 comments on commit 6d07b7b

Please sign in to comment.