Skip to content

Commit 7a5d857

Browse files
authored
Add support for new SSL configuration to mongodb (#2522)
closes #2519
1 parent 13f314a commit 7a5d857

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ be deprecated eventually.
5757
- [#2071](https://github.com/influxdata/telegraf/issues/2071): Use official docker SDK.
5858
- [#1678](https://github.com/influxdata/telegraf/pull/1678): Add AMQP consumer input plugin
5959
- [#2501](https://github.com/influxdata/telegraf/pull/2501): Support DEAD(X) state in system input plugin.
60+
- [#2522](https://github.com/influxdata/telegraf/pull/2522): Add support for mongodb client certificates.
6061

6162
### Bugfixes
6263

plugins/inputs/mongodb/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
## 10.0.0.1:10000, etc.
1212
servers = ["127.0.0.1:27017"]
1313
gather_perdb_stats = false
14+
15+
## Optional SSL Config
16+
# ssl_ca = "/etc/telegraf/ca.pem"
17+
# ssl_cert = "/etc/telegraf/cert.pem"
18+
# ssl_key = "/etc/telegraf/key.pem"
19+
## Use SSL but skip chain & host verification
20+
# insecure_skip_verify = false
1421
```
1522

16-
For authenticated mongodb istances use connection mongdb connection URI
23+
For authenticated mongodb instances use `mongodb://` connection URI
1724

1825
```toml
1926
[[inputs.mongodb]]

plugins/inputs/mongodb/mongodb.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/influxdata/telegraf"
13+
"github.com/influxdata/telegraf/internal"
1314
"github.com/influxdata/telegraf/internal/errchan"
1415
"github.com/influxdata/telegraf/plugins/inputs"
1516
"gopkg.in/mgo.v2"
@@ -20,6 +21,15 @@ type MongoDB struct {
2021
Ssl Ssl
2122
mongos map[string]*Server
2223
GatherPerdbStats bool
24+
25+
// Path to CA file
26+
SSLCA string `toml:"ssl_ca"`
27+
// Path to host cert file
28+
SSLCert string `toml:"ssl_cert"`
29+
// Path to cert key file
30+
SSLKey string `toml:"ssl_key"`
31+
// Use SSL but skip chain & host verification
32+
InsecureSkipVerify bool
2333
}
2434

2535
type Ssl struct {
@@ -35,6 +45,13 @@ var sampleConfig = `
3545
## 10.0.0.1:10000, etc.
3646
servers = ["127.0.0.1:27017"]
3747
gather_perdb_stats = false
48+
49+
## Optional SSL Config
50+
# ssl_ca = "/etc/telegraf/ca.pem"
51+
# ssl_cert = "/etc/telegraf/cert.pem"
52+
# ssl_key = "/etc/telegraf/key.pem"
53+
## Use SSL but skip chain & host verification
54+
# insecure_skip_verify = false
3855
`
3956

4057
func (m *MongoDB) SampleConfig() string {
@@ -105,8 +122,11 @@ func (m *MongoDB) gatherServer(server *Server, acc telegraf.Accumulator) error {
105122
dialInfo.Direct = true
106123
dialInfo.Timeout = 5 * time.Second
107124

125+
var tlsConfig *tls.Config
126+
108127
if m.Ssl.Enabled {
109-
tlsConfig := &tls.Config{}
128+
// Deprecated SSL config
129+
tlsConfig = &tls.Config{}
110130
if len(m.Ssl.CaCerts) > 0 {
111131
roots := x509.NewCertPool()
112132
for _, caCert := range m.Ssl.CaCerts {
@@ -119,6 +139,13 @@ func (m *MongoDB) gatherServer(server *Server, acc telegraf.Accumulator) error {
119139
} else {
120140
tlsConfig.InsecureSkipVerify = true
121141
}
142+
} else {
143+
tlsConfig, err = internal.GetTLSConfig(
144+
m.SSLCert, m.SSLKey, m.SSLCA, m.InsecureSkipVerify)
145+
}
146+
147+
// If configured to use TLS, add a dial function
148+
if tlsConfig != nil {
122149
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) {
123150
conn, err := tls.Dial("tcp", addr.String(), tlsConfig)
124151
if err != nil {

0 commit comments

Comments
 (0)