This repository was archived by the owner on Jul 2, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsecure-mgo.go
More file actions
99 lines (83 loc) · 2.63 KB
/
Copy pathsecure-mgo.go
File metadata and controls
99 lines (83 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package mgosec
import (
"crypto/tls"
"fmt"
"net"
"strings"
"time"
mgo "github.com/globalsign/mgo"
)
//AuthMechanism represents a MongoDB authentication mechanism
type AuthMechanism string
const (
//ScramSha1 = SCRAM-SHA-1
ScramSha1 AuthMechanism = "SCRAM-SHA-1"
//MongoCR = MONGODB-CR
MongoCR AuthMechanism = "MONGODB-CR"
//PlainAuth = PLAIN
PlainAuth AuthMechanism = "PLAIN"
//X500 = MONGODB-X500
X500 AuthMechanism = "MONGODB-X500"
//GssAPI = GSSAPI
GssAPI AuthMechanism = "GSSAPI"
//None is used to show that authentication is disabled
None AuthMechanism = ""
)
var authMechanisms = []AuthMechanism{ScramSha1, MongoCR, PlainAuth, X500, GssAPI, None}
//Dial dials a MongoDB server with the provided MongoDB connection string,
//as supplied to the mongo utility, an auth mechanism (see AuthMechanism),
//and a TLS configuration
func Dial(connString string, authMechanism AuthMechanism, conf *tls.Config) (*mgo.Session, error) {
dialInfo, err := mgo.ParseURL(connString)
if err != nil {
return nil, err
}
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
return tls.Dial("tcp", addr.String(), conf)
}
dialInfo.Timeout = 5 * time.Second
if authMechanism == None {
dialInfo.Username = ""
dialInfo.Password = ""
}
dialInfo.Mechanism = string(authMechanism)
return mgo.DialWithInfo(dialInfo)
}
//DialInsecure dials a MongoDB server with the provided MongoDB connection string,
//as supplied to the mongo utility, an auth mechanism (see AuthMechanism).
//Note: this method does not encrypt any information placed on the wire,
//including authentication details
func DialInsecure(connString string, authMechanism AuthMechanism) (*mgo.Session, error) {
dialInfo, err := mgo.ParseURL(connString)
if err != nil {
return nil, err
}
dialInfo.Timeout = 5 * time.Second
if authMechanism == None {
dialInfo.Username = ""
dialInfo.Password = ""
}
dialInfo.Mechanism = string(authMechanism)
return mgo.DialWithInfo(dialInfo)
}
//ParseAuthMechanism parses a string representation of a MongoDB
//authentication mechanism and returns the AuthMechanism which matches
func ParseAuthMechanism(mechanism string) (AuthMechanism, error) {
mechanism = strings.ToUpper(mechanism)
/*
The following code was ineffectual, but it was released into production, so it is now commented out.
strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}
return r
}, mechanism)
*/
for _, authMechanism := range authMechanisms {
if mechanism == string(authMechanism) {
return authMechanism, nil
}
}
return None, fmt.Errorf("%s did not match an existing MongoDB "+
"authentication mechanism", mechanism)
}