File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
source/includes/connection-snippets/x509-snippets Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ // begin x509 connection
2
+ package main
3
+
4
+ import (
5
+ "context"
6
+ "fmt"
7
+ "log"
8
+ "go.mongodb.org/mongo-driver/mongo"
9
+ "go.mongodb.org/mongo-driver/mongo/options"
10
+ "go.mongodb.org/mongo-driver/bson"
11
+ )
12
+
13
+ func main () {
14
+ ctx := context .TODO ()
15
+ certificateKeyFilePath := "/etc/certs/mongodb/client.pem"
16
+ uri := "mongodb+srv://<cluster-url>/test?authSource=$external&tlsCertificateKeyFile=%s&retryWrites=true&w=majority&authMechanism=MONGODB-X509"
17
+ uri = fmt .Sprintf (uri , certificateKeyFilePath )
18
+ clientOpts := options .Client ().ApplyURI (uri )
19
+
20
+ client , err := mongo .Connect (ctx , clientOpts )
21
+ if err != nil { log .Fatal (err ) }
22
+ defer client .Disconnect (ctx )
23
+
24
+ collection := client .Database ("testDB" ).Collection ("testCol" )
25
+ docCount , err := collection .CountDocuments (ctx , bson.D {})
26
+ if err != nil { log .Fatal (err ) }
27
+ fmt .Println (docCount )
28
+ }
29
+ // end x509 connection
You can’t perform that action at this time.
0 commit comments