forked from go-mgo/mgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 3.6+ user authenticationRestrictions (go-mgo#229)
* Add 3.6 user authenticationRestrictions * Add struct-field comment * Add struct-field comment go-mgo#2 * Add struct-field comment go-mgo#3 * Add documentation link * Fix comment * Fix comment go-mgo#2 * add to README.md * add to README.md go-mgo#2 * add to README.md go-mgo#3 * Add positive/negative authentication restrictions user test * Use denyUser for negative test * Correct message * Fix error match * Fix close on nil/closed session * Simplify test, last change :) * Simplify test, last change :) go-mgo#2 * Simplify test, last change :) go-mgo#3 * Fix := error
- Loading branch information
1 parent
5af851b
commit 593df06
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -341,6 +341,57 @@ func (s *S) TestAuthUpsertUserUpdates(c *C) { | |
c.Assert(err, IsNil) | ||
} | ||
|
||
func (s *S) TestAuthUpsertUserAuthenticationRestrictions(c *C) { | ||
if !s.versionAtLeast(3, 6) { | ||
c.Skip("UpsertUser with user 'authenticationRestrictions' only works on 3.6+") | ||
} | ||
session, err := mgo.Dial("localhost:40002") | ||
c.Assert(err, IsNil) | ||
defer session.Close() | ||
|
||
admindb := session.DB("admin") | ||
err = admindb.Login("root", "rapadura") | ||
|
||
allowUser := &mgo.User{ | ||
Username: "authRestrictionUser", | ||
Password: "123456", | ||
Roles: []mgo.Role{mgo.RoleReadWrite}, | ||
AuthenticationRestrictions: []mgo.AuthenticationRestriction{ | ||
{ | ||
ClientSource: []string{"127.0.0.1"}, | ||
ServerAddress: []string{"127.0.0.1"}, | ||
}, | ||
}, | ||
} | ||
err = admindb.UpsertUser(allowUser) | ||
c.Assert(err, IsNil) | ||
|
||
// Dial again to ensure the positive authentication restriction allows the connection | ||
allowSession, err := mgo.Dial("mongodb://authRestrictionUser:[email protected]:40002/admin") | ||
c.Assert(err, IsNil) | ||
c.Assert(allowSession.Ping(), IsNil) | ||
defer allowSession.Close() | ||
|
||
// this user should fail authentication restrictions | ||
denyUser := &mgo.User{ | ||
Username: "denyUser", | ||
Password: "123456", | ||
Roles: []mgo.Role{mgo.RoleReadWrite}, | ||
AuthenticationRestrictions: []mgo.AuthenticationRestriction{ | ||
{ | ||
ClientSource: []string{"1.2.3.4"}, | ||
ServerAddress: []string{"4.3.2.1"}, | ||
}, | ||
}, | ||
} | ||
err = admindb.UpsertUser(denyUser) | ||
c.Assert(err, IsNil) | ||
|
||
// Dial again to ensure the authentication restriction blocks the connections. | ||
_, err = mgo.Dial("mongodb://denyUser:[email protected]:40002/admin") | ||
c.Assert(err, ErrorMatches, ".*Authentication failed.") | ||
} | ||
|
||
func (s *S) TestAuthAddUser(c *C) { | ||
session, err := mgo.Dial("localhost:40002") | ||
c.Assert(err, IsNil) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters