add tls connect options
This commit is contained in:
parent
2b6be44627
commit
98893c5c57
|
@ -15,3 +15,4 @@
|
|||
# vendor/
|
||||
|
||||
.idea
|
||||
*.pem
|
||||
|
|
|
@ -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))
|
||||
}
|
113
mongodb.go
113
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
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue