Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Fixing issue with mongodb autoconfig authentication on reconnects
Browse files Browse the repository at this point in the history
Change-Id: I7d8d2fdbc98db81cf6f3068fe0229d225d7d32d0
  • Loading branch information
Thomas Risberg committed Apr 2, 2012
1 parent 84a478d commit 1b566bc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion auto-reconfiguration/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
cf-autoconfig (0.0.2)
cf-autoconfig (0.0.3)
cf-runtime (= 0.0.1)

GEM
Expand Down
27 changes: 13 additions & 14 deletions auto-reconfiguration/lib/cfautoconfig/document/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Mongo
def self.included( base )
base.send( :alias_method, :original_initialize, :initialize)
base.send( :alias_method, :initialize, :initialize_with_cf )
base.send( :alias_method, :original_apply_saved_authentication, :apply_saved_authentication)
base.send( :alias_method, :apply_saved_authentication, :apply_saved_authentication_with_cf )
base.send( :alias_method, :original_db, :db)
base.send( :alias_method, :db, :db_with_cf )
base.send( :alias_method, :original_shortcut, :[])
Expand All @@ -19,39 +21,36 @@ def initialize_with_cf(host = nil, port = nil, opts = {})
@service_props = CFRuntime::CloudApp.service_props('mongodb')
puts "Auto-reconfiguring MongoDB"
@auto_config = true
mongo_host = @service_props[:host]
mongo_port = @service_props[:port]
original_initialize mongo_host, mongo_port, opts
original_initialize @service_props[:host], @service_props[:port], opts
add_auth(@service_props[:db], @service_props[:username], @service_props[:password])
else
puts "Found #{service_names.length} mongo services. Skipping auto-reconfiguration."
@auto_config = false
original_initialize host, port, opts
end
end

def db_with_cf(db_name, opts = {})

def apply_saved_authentication_with_cf(opts = {})
add_auth(@service_props[:db], @service_props[:username], @service_props[:password])
original_apply_saved_authentication opts
end

def db_with_cf(db_name, opts = {})
if @auto_config && db_name != 'admin'
add_auth(@service_props[:db], @service_props[:username], @service_props[:password])
db = original_db @service_props[:db], opts
authenticate_with_cf(db)
else
original_db db_name, opts
end
end

def shortcut_with_cf(db_name)
if @auto_config && db_name != 'admin'
add_auth(@service_props[:db], @service_props[:username], @service_props[:password])
db = original_shortcut(@service_props[:db])
authenticate_with_cf(db)
else
db = original_shortcut(db_name)
end
end

def authenticate_with_cf(db)
mongo_username = @service_props[:username]
mongo_password = @service_props[:password]
db.authenticate mongo_username, mongo_password
db
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Connection
if method_defined?(:initialize_with_cf)
undef_method :initialize_with_cf
alias :initialize :original_initialize
undef_method :apply_saved_authentication_with_cf
alias :apply_saved_authentication :original_apply_saved_authentication
undef_method :db_with_cf
alias :db :original_db
undef_method :shortcut_with_cf
Expand Down
2 changes: 1 addition & 1 deletion auto-reconfiguration/lib/cfautoconfig/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AutoReconfiguration
VERSION = '0.0.2'
VERSION = '0.0.3'
end
35 changes: 18 additions & 17 deletions auto-reconfiguration/spec/unit/document/mongo_configurer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def validate_db_name(db_name)
db_name
end
end
class DB
def authenticate(username, password, save_auth=true)
class Connection
def add_auth(database, username, password)
database.should == 'db'
username.should == '8d93ae0a'
password.should == '7cf3c0e3'
true
Expand All @@ -44,8 +45,9 @@ def validate_db_name(db_name)
db_name
end
end
class DB
def authenticate(username, password, save_auth=true)
class Connection
def add_auth(database, username, password)
database.should == 'db'
username.should == '8d93ae0a'
password.should == '7cf3c0e3'
true
Expand All @@ -64,9 +66,11 @@ def validate_db_name(db_name)
db_name
end
end
class DB
def authenticate(username, password, save_auth=true)
raise(Mongo::AuthenticationError, "Authenticate should not have been called!")
class Connection
def add_auth(database, username, password)
if (database == 'admin')
raise(Mongo::AuthenticationError, "Authenticate should not have been called!")
end
end
end
end
Expand All @@ -82,9 +86,11 @@ def validate_db_name(db_name)
db_name
end
end
class DB
def authenticate(username, password, save_auth=true)
raise(Mongo::AuthenticationError, "Authenticate should not have been called!")
class Connection
def add_auth(database, username, password)
if (database == 'admin')
raise(Mongo::AuthenticationError, "Authenticate should not have been called!")
end
end
end
end
Expand Down Expand Up @@ -118,11 +124,6 @@ def validate_db_name(db_name)
db_name
end
end
class DB
def authenticate(username, password, save_auth=true)
raise(Mongo::AuthenticationError, "Authenticate should not have been called!")
end
end
end
mongo = Mongo::Connection.new('127.0.0.1', 27017, {:connect => false})
db = mongo.db('test')
Expand All @@ -142,8 +143,8 @@ def validate_db_name(db_name)
db_name
end
end
class DB
def authenticate(username, password, save_auth=true)
class Connection
def add_auth(database, username, password)
raise(Mongo::AuthenticationError, "Authenticate should not have been called!")
end
end
Expand Down

0 comments on commit 1b566bc

Please sign in to comment.