From 2b6be446270d76d93e60d11b919e17b589606119 Mon Sep 17 00:00:00 2001 From: Sinh Date: Tue, 22 Feb 2022 15:24:31 +0700 Subject: [PATCH 1/2] add tls connect options --- mongodb.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/mongodb.go b/mongodb.go index 5741b17..fcefd52 100644 --- a/mongodb.go +++ b/mongodb.go @@ -7,10 +7,41 @@ import ( "github.com/logrusorgru/aurora" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/mongo/readpref" ) +// ConnectTLSOpts ... +type ConnectTLSOpts struct { + Host string + DBName string + ReplSet string + CaFilePath string + CertificateKeyFilePath string + CertificateKeyFilePassword string +} + var db *mongo.Database +// ConnectWithTLS ... +func ConnectWithTLS(opts ConnectTLSOpts) (*mongo.Database, error) { + ctx := context.Background() + uri := fmt.Sprintf("%s/?tls=true&tlsCAFile=%s&tlsCertificateKeyFile=%s&tlsCertificateKeyFilePassword=%s", opts.Host, opts.CaFilePath, opts.CertificateKeyFilePath, opts.CertificateKeyFilePassword) + readPref := readpref.SecondaryPreferred() + credential := options.Credential{ + AuthMechanism: "MONGODB-X509", + } + clientOpts := options.Client().SetAuth(credential).SetReadPreference(readPref).SetReplicaSet(opts.ReplSet).ApplyURI(uri) + client, err := mongo.Connect(ctx, clientOpts) + if err != nil { + return nil, err + } + if err := client.Ping(ctx, readpref.SecondaryPreferred()); err != nil { + return nil, err + } + db = client.Database(opts.DBName) + return db, err +} + // Connect to mongo server func Connect(host, user, password, dbName, mechanism, source string) (*mongo.Database, error) { connectOptions := options.ClientOptions{} @@ -31,7 +62,7 @@ func Connect(host, user, password, dbName, mechanism, source string) (*mongo.Dat return nil, err } - fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + host)) + fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + host + " --- DB: " + dbName)) // Set data db = client.Database(dbName) From 98893c5c5715178e129204fb3b2887a664683e38 Mon Sep 17 00:00:00 2001 From: Sinh Date: Thu, 24 Feb 2022 15:00:42 +0700 Subject: [PATCH 2/2] add tls connect options --- .gitignore | 3 +- base64.go | 12 +++++ mongodb.go | 113 ++++++++++++++++++++++++++++++------------------ mongodb_test.go | 47 ++++++++++++++++++++ util.go | 21 +++++++++ 5 files changed, 153 insertions(+), 43 deletions(-) create mode 100644 base64.go create mode 100644 mongodb_test.go create mode 100644 util.go diff --git a/.gitignore b/.gitignore index bbfd2cb..37c7aee 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -.idea \ No newline at end of file +.idea +*.pem diff --git a/base64.go b/base64.go new file mode 100644 index 0000000..260a517 --- /dev/null +++ b/base64.go @@ -0,0 +1,12 @@ +package mongodb + +import "encoding/base64" + +func base64DecodeToBytes(text string) []byte { + s, _ := base64.StdEncoding.DecodeString(text) + return s +} + +func base64DecodeToString(text string) string { + return string(base64DecodeToBytes(text)) +} diff --git a/mongodb.go b/mongodb.go index fcefd52..f83c888 100644 --- a/mongodb.go +++ b/mongodb.go @@ -10,27 +10,81 @@ import ( "go.mongodb.org/mongo-driver/mongo/readpref" ) +// Config ... +type Config struct { + Host string + DBName string + + TLS *ConnectTLSOpts + Standalone *ConnectStandaloneOpts +} + // ConnectTLSOpts ... type ConnectTLSOpts struct { - Host string - DBName string - ReplSet string - CaFilePath string - CertificateKeyFilePath string - CertificateKeyFilePassword string + ReplSet string + CaFile string + CertKeyFile string + CertKeyFilePassword string +} + +// ConnectStandaloneOpts ... +type ConnectStandaloneOpts struct { + AuthMechanism string + AuthSource string + Username string + Password string } var db *mongo.Database -// ConnectWithTLS ... -func ConnectWithTLS(opts ConnectTLSOpts) (*mongo.Database, error) { - ctx := context.Background() - uri := fmt.Sprintf("%s/?tls=true&tlsCAFile=%s&tlsCertificateKeyFile=%s&tlsCertificateKeyFilePassword=%s", opts.Host, opts.CaFilePath, opts.CertificateKeyFilePath, opts.CertificateKeyFilePassword) - readPref := readpref.SecondaryPreferred() - credential := options.Credential{ - AuthMechanism: "MONGODB-X509", +// Connect to mongo server +func Connect(cfg Config) (*mongo.Database, error) { + if cfg.TLS != nil && cfg.TLS.ReplSet != "" { + return connectWithTLS(cfg) } - clientOpts := options.Client().SetAuth(credential).SetReadPreference(readPref).SetReplicaSet(opts.ReplSet).ApplyURI(uri) + connectOptions := options.ClientOptions{} + opts := cfg.Standalone + // Set auth if existed + if opts.Username != "" && opts.Password != "" { + connectOptions.Auth = &options.Credential{ + AuthMechanism: opts.AuthMechanism, + AuthSource: opts.AuthSource, + Username: opts.Username, + Password: opts.Password, + } + } + + // Connect + client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(cfg.Host)) + if err != nil { + fmt.Println("Error when connect to MongoDB database", cfg.Host, err) + return nil, err + } + + fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + cfg.Host + " --- DB: " + cfg.DBName)) + + // Set data + db = client.Database(cfg.DBName) + return db, nil +} + +func connectWithTLS(cfg Config) (*mongo.Database, error) { + ctx := context.Background() + opts := cfg.TLS + + caFile, err := initFileFromBase64String("ca.pem", opts.CaFile) + if err != nil { + return nil, err + } + certFile, err := initFileFromBase64String("cert.pem", opts.CertKeyFile) + if err != nil { + return nil, err + } + pwd := base64DecodeToString(opts.CertKeyFilePassword) + s := "%s/?tls=true&tlsCAFile=./%s&tlsCertificateKeyFile=./%s&tlsCertificateKeyFilePassword=%s&authMechanism=MONGODB-X509" + uri := fmt.Sprintf(s, cfg.Host, caFile.Name(), certFile.Name(), pwd) + readPref := readpref.SecondaryPreferred() + clientOpts := options.Client().SetReadPreference(readPref).SetReplicaSet(opts.ReplSet).ApplyURI(uri) client, err := mongo.Connect(ctx, clientOpts) if err != nil { return nil, err @@ -38,37 +92,12 @@ func ConnectWithTLS(opts ConnectTLSOpts) (*mongo.Database, error) { if err := client.Ping(ctx, readpref.SecondaryPreferred()); err != nil { return nil, err } - db = client.Database(opts.DBName) + db = client.Database(cfg.DBName) + + fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + cfg.Host + " --- DB: " + cfg.DBName)) return db, err } -// Connect to mongo server -func Connect(host, user, password, dbName, mechanism, source string) (*mongo.Database, error) { - connectOptions := options.ClientOptions{} - // Set auth if existed - if user != "" && password != "" { - connectOptions.Auth = &options.Credential{ - AuthMechanism: mechanism, - AuthSource: source, - Username: user, - Password: password, - } - } - - // Connect - client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(host)) - if err != nil { - fmt.Println("Error when connect to MongoDB database", host, err) - return nil, err - } - - fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + host + " --- DB: " + dbName)) - - // Set data - db = client.Database(dbName) - return db, nil -} - // GetInstance ... func GetInstance() *mongo.Database { return db diff --git a/mongodb_test.go b/mongodb_test.go new file mode 100644 index 0000000..ae6273b --- /dev/null +++ b/mongodb_test.go @@ -0,0 +1,47 @@ +package mongodb + +import ( + "testing" + + "go.mongodb.org/mongo-driver/mongo" +) + +func Test_connectWithTLS(t *testing.T) { + type args struct { + cfg Config + } + tests := []struct { + name string + args args + want *mongo.Database + wantErr bool + }{ + { + name: "should connect success", + args: args{ + cfg: Config{ + Host: "mongodb://vps1:27017,vps2:27017,vps3:27017", + DBName: "unibag", + TLS: &ConnectTLSOpts{ + ReplSet: "sellyrs", + CaFile: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQzRENDQXNTZ0F3SUJBZ0lVWllwWEJPK3JUTVFDOGJzc05UU1ljQU02cmxZd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1lqRUxNQWtHQTFVRUF3d0NRMEV4RGpBTUJnTlZCQXNNQlUxUFRrZFBNUk13RVFZRFZRUUtEQXBDVlZOVQpSVVJYUVZKRk1SUXdFZ1lEVlFRSERBdFFRVWxPUlZOV1NVeE1SVEVMTUFrR0ExVUVDQXdDVDBneEN6QUpCZ05WCkJBWVRBbFZUTUI0WERUSXlNREl5TWpBME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dYSVY1QjNQcElNbG5MYmtyUUxTK3N2ald6Sk5tM0dSRXQ5RndhUkxFdUZVV3hTL1NTMDByaElqb0NxCm9Ea0NnZ0VCQU5NYWI0S2pVMVNQTlRXNnhOK2tQdGlKWlFndXRHeXRUVnA4Ynl4STFmQmlvZGdLZWI3M1M4alYKZ3E4cjJpVHNQT3NyTWNUZG94eDhmUWgzdVlDWWdvWDdsZ2N2V1psek1FOGE3RFZGRGFiRnZNVjlGWWpka2VpagowQ2doYXhjRytONTRBcFBxNzVyMUwxM1VBa04weDBvNE1tVmxpRWNHRWx6c1oydHpUaS9KWTk3d2JpZ2lreGZ5CmQvdENSNFFRTUlCNExHa0VHK2N1ZERpdHRic0NZOHZ2S2Q1R2E0S0tERHduN0pSeUxibXhYVXBCRUhUQ0ZsZngKeldnT2c5MmpkM2lYOFdxdks1V1VVd3h6S3lERXR5bnkyb3ZIV3JjNWdRUW9EN09scG1SdjNBVE5aVDMzeTZyQgphM25hQnZVZmorSzJORCtUbUVJbnRwNlcrZjQ3S3YwPQotLS0tLUVORCBQUklWQVRFIEtFWS0tLS0t", + CertKeyFilePassword: "MTIzNDQzMjE=", + }, + Standalone: nil, + }, + }, + want: nil, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := connectWithTLS(tt.args.cfg) + if (err != nil) != tt.wantErr { + t.Errorf("connectWithTLS() error = %v, wantErr %v", err, tt.wantErr) + return + } + }) + } +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..6939507 --- /dev/null +++ b/util.go @@ -0,0 +1,21 @@ +package mongodb + +import ( + "fmt" + "os" +) + +func initFileFromBase64String(filename, value string) (*os.File, error) { + f, err := os.Create(filename) + if err != nil { + fmt.Println("mongodb.initFileFromBase64String - err: ", err) + return nil, err + } + b := base64DecodeToBytes(value) + if _, err := f.Write(b); err != nil { + fmt.Println("mongodb.initFileFromBase64String - write file err: ", err) + return nil, err + } + f.Sync() + return f, nil +}