Skip to content

mongoDB: Use Specific Database for Authentication #1591

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions persistent-mongoDB/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog for persistent-mongoDB

## 2.14.0.0
* MongoDB authentication now allows specifying which database to authenticate against, which may be different from the database that one normally needs to access.

## 2.13.1.0

* Restore update write concern behavior with MongoDB Driver for MongoDB >= 6.0 [#1550](https://github.com/yesodweb/persistent/pull/1550)
Expand Down
14 changes: 7 additions & 7 deletions persistent-mongoDB/Database/Persist/MongoDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ createPipe hostname port = DB.connect (DB.Host hostname port)
createReplicatSet :: (DB.ReplicaSetName, [DB.Host]) -> Database -> Maybe MongoAuth -> IO Connection
createReplicatSet rsSeed dbname mAuth = do
pipe <- DB.openReplicaSet rsSeed >>= DB.primary
testAccess pipe dbname mAuth
testAccess pipe mAuth
return $ Connection pipe dbname

createRsPool :: (Trans.MonadIO m) => Database -> ReplicaSetConfig
Expand All @@ -280,17 +280,17 @@ createRsPool dbname (ReplicaSetConfig rsName rsHosts) mAuth connectionPoolSize s
connectionIdleTime
stripeSize

testAccess :: DB.Pipe -> Database -> Maybe MongoAuth -> IO ()
testAccess pipe dbname mAuth = do
testAccess :: DB.Pipe -> Maybe MongoAuth -> IO ()
testAccess pipe mAuth = do
_ <- case mAuth of
Just (MongoAuth user pass) -> DB.access pipe DB.UnconfirmedWrites dbname (DB.auth user pass)
Just (MongoAuth dbname user pass) -> DB.access pipe DB.UnconfirmedWrites dbname (DB.auth user pass)
Nothing -> return undefined
return ()

createConnection :: Database -> HostName -> DB.PortID -> Maybe MongoAuth -> IO Connection
createConnection dbname hostname port mAuth = do
pipe <- createPipe hostname port
testAccess pipe dbname mAuth
testAccess pipe mAuth
return $ Connection pipe dbname

createMongoDBPool :: (Trans.MonadIO m) => Database -> HostName -> DB.PortID
Expand Down Expand Up @@ -1109,7 +1109,7 @@ dummyFromUnique _ = error "dummyFromUnique"
dummyFromFilts :: [Filter v] -> v
dummyFromFilts _ = error "dummyFromFilts"

data MongoAuth = MongoAuth DB.Username DB.Password deriving Show
data MongoAuth = MongoAuth Database DB.Username DB.Password deriving Show

-- | Information required to connect to a mongo database
data MongoConf = MongoConf
Expand Down Expand Up @@ -1189,7 +1189,7 @@ instance FromJSON MongoConf where
, mgPort = port
, mgAuth =
case (mUser, mPass) of
(Just user, Just pass) -> Just (MongoAuth user pass)
(Just user, Just pass) -> Just (MongoAuth db user pass)
_ -> Nothing
, mgPoolStripes = poolStripes
, mgStripeConnections = stripeConnections
Expand Down
2 changes: 1 addition & 1 deletion persistent-mongoDB/persistent-mongoDB.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-mongoDB
version: 2.13.1.0
version: 2.14.0.0
license: MIT
license-file: LICENSE
author: Greg Weber <[email protected]>
Expand Down
Loading