forked from domodwyer/mgo
-
Notifications
You must be signed in to change notification settings - Fork 230
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
Merge Development #79
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
de872a6
Merge branch 'master' into development
domodwyer 1519fd3
Merge branch 'master' into development
domodwyer 454da02
add DropAllIndexes() method (#25)
feliixx 93aaa6e
readme: credit @feliixx for #25 (#26)
domodwyer 165af68
send metadata during handshake (#28)
feliixx a76b1a0
Update README to add appName (#32)
domodwyer 25200e4
add method CreateView() (#33)
feliixx 1f4c10f
readme: credit @feliixx in the README (#36)
domodwyer 934a190
Don't panic on indexed int64 fields (#23)
domodwyer b37e3c1
Add collation option to collection.Create() (#37)
feliixx aead58f
Test against MongoDB 3.4.x (#35)
feliixx 950ed5a
Introduce constants for BSON element types (#41)
bozaro d21a525
bson.Unmarshal returns time in UTC (#42)
9d743b4
readme: add missing features / credit
domodwyer c86ed84
Merge pull request #45 from globalsign/feature/update-readme
97bd0cd
fix golint, go vet and gofmt warnings (#44)
feliixx fd79249
readme: credit @feliixx (#46)
domodwyer dba7b4c
Fix GetBSON() method usage (#40)
bozaro 12fb1c2
readme: credit @bozaro (#47)
domodwyer 199dc25
Improve cursorData struct unmarshaling speed (#49)
bozaro 345ab0b
readme: credit @bozaro and @idy (#53)
domodwyer 0454966
do not lock while writing to a socket (#52) (#54)
domodwyer 663dfe5
Add proper DN construction (#55)
csucu 7cd0b89
reduce memory allocation in bulk op (#56)
feliixx ea8e8e6
readme: credit @feliixx (#58)
domodwyer 90c056c
MongoDB 3.6: implement the new wire protocol (#61)
feliixx 1ac9b5d
Merge branch 'master' into development
domodwyer a104bfb
Recover from persistent "i/o timeout" or "Closed explicitly" pool err…
bachue f9be6c5
development: revert #61 (#73)
domodwyer 138ba2f
readme: credit @bachue (#74)
domodwyer 9acbd68
auth: add an example for x509 authentication (#75)
domodwyer eeedc17
session: add example concurrent usage (#78)
domodwyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package mgo | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"io/ioutil" | ||
"net" | ||
"sync" | ||
) | ||
|
||
func ExampleCredential_x509Authentication() { | ||
// MongoDB follows RFC2253 for the ordering of the DN - if the order is | ||
// incorrect when creating the user in Mongo, the client will not be able to | ||
// connect. | ||
// | ||
// The best way to generate the DN with the correct ordering is with | ||
// openssl: | ||
// | ||
// openssl x509 -in client.crt -inform PEM -noout -subject -nameopt RFC2253 | ||
// subject= CN=Example App,OU=MongoDB Client Authentication,O=GlobalSign,C=GB | ||
// | ||
// | ||
// And then create the user in MongoDB with the above DN: | ||
// | ||
// db.getSiblingDB("$external").runCommand({ | ||
// createUser: "CN=Example App,OU=MongoDB Client Authentication,O=GlobalSign,C=GB", | ||
// roles: [ | ||
// { role: 'readWrite', db: 'bananas' }, | ||
// { role: 'userAdminAnyDatabase', db: 'admin' } | ||
// ], | ||
// writeConcern: { w: "majority" , wtimeout: 5000 } | ||
// }) | ||
// | ||
// | ||
// References: | ||
// - https://docs.mongodb.com/manual/tutorial/configure-x509-client-authentication/ | ||
// - https://docs.mongodb.com/manual/core/security-x.509/ | ||
// | ||
|
||
// Read in the PEM encoded X509 certificate. | ||
// | ||
// See the client.pem file at the path below. | ||
clientCertPEM, err := ioutil.ReadFile("harness/certs/client.pem") | ||
|
||
// Read in the PEM encoded private key. | ||
clientKeyPEM, err := ioutil.ReadFile("harness/certs/client.key") | ||
|
||
// Parse the private key, and the public key contained within the | ||
// certificate. | ||
clientCert, err := tls.X509KeyPair(clientCertPEM, clientKeyPEM) | ||
|
||
// Parse the actual certificate data | ||
clientCert.Leaf, err = x509.ParseCertificate(clientCert.Certificate[0]) | ||
|
||
// Use the cert to set up a TLS connection to Mongo | ||
tlsConfig := &tls.Config{ | ||
Certificates: []tls.Certificate{clientCert}, | ||
|
||
// This is set to true so the example works within the test | ||
// environment. | ||
// | ||
// DO NOT set InsecureSkipVerify to true in a production | ||
// environment - if you use an untrusted CA/have your own, load | ||
// its certificate into the RootCAs value instead. | ||
// | ||
// RootCAs: myCAChain, | ||
InsecureSkipVerify: true, | ||
} | ||
|
||
// Connect to Mongo using TLS | ||
host := "localhost:40003" | ||
session, err := DialWithInfo(&DialInfo{ | ||
Addrs: []string{host}, | ||
DialServer: func(addr *ServerAddr) (net.Conn, error) { | ||
return tls.Dial("tcp", host, tlsConfig) | ||
}, | ||
}) | ||
|
||
// Authenticate using the certificate | ||
cred := &Credential{Certificate: tlsConfig.Certificates[0].Leaf} | ||
if err := session.Login(cred); err != nil { | ||
panic(err) | ||
} | ||
|
||
// Done! Use mgo as normal from here. | ||
// | ||
// You should actually check the error code at each step. | ||
_ = err | ||
} | ||
|
||
func ExampleSession_concurrency() { | ||
// This example shows the best practise for concurrent use of a mgo session. | ||
// | ||
// Internally mgo maintains a connection pool, dialling new connections as | ||
// required. | ||
// | ||
// Some general suggestions: | ||
// - Define a struct holding the original session, database name and | ||
// collection name instead of passing them explicitly. | ||
// - Define an interface abstracting your data access instead of exposing | ||
// mgo to your application code directly. | ||
// - Limit concurrency at the application level, not with SetPoolLimit(). | ||
|
||
// This will be our concurrent worker | ||
var doStuff = func(wg *sync.WaitGroup, session *Session) { | ||
defer wg.Done() | ||
|
||
// Copy the session - if needed this will dial a new connection which | ||
// can later be reused. | ||
// | ||
// Calling close returns the connection to the pool. | ||
conn := session.Copy() | ||
defer conn.Close() | ||
|
||
// Do something(s) with the connection | ||
_, _ = conn.DB("").C("my_data").Count() | ||
} | ||
|
||
/////////////////////////////////////////////// | ||
|
||
// Dial a connection to Mongo - this creates the connection pool | ||
session, err := Dial("localhost:40003/my_database") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Concurrently do things, passing the session to the worker | ||
wg := &sync.WaitGroup{} | ||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go doStuff(wg, session) | ||
} | ||
wg.Wait() | ||
|
||
session.Close() | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,5 @@ | |
. ../.env | ||
|
||
exec mongod $COMMONDOPTS \ | ||
--shardsvr \ | ||
--port 40002 \ | ||
--auth |
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
. ../.env | ||
|
||
exec mongod $COMMONDOPTS \ | ||
--shardsvr \ | ||
--port 40003 \ | ||
--auth \ | ||
--sslMode preferSSL \ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we upgrade to 3.6.2 while we are at it ?