-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Metricbeat: MongoDB TLS connection support #7401
Changes from 2 commits
38acc63
b37a82f
280223a
b06eb0c
fbea1e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,21 @@ | |
# password configuration options. | ||
hosts: ["localhost:27017"] | ||
|
||
# Optional SSL. By default is off. | ||
#ssl.enabled: true | ||
|
||
# Mode of verification of server certificate ('none' or 'full') | ||
#ssl.verification_mode: 'full' | ||
|
||
# List of root certificates for HTTPS server verifications | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should say TLS or SSL instead of HTTPS (since there's no HTTP connection here)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, fixed. |
||
#ssl.certificate_authorities: ["/etc/pki/root/ca.pem"] | ||
|
||
# Certificate for SSL client authentication | ||
#ssl.certificate: "/etc/pki/client/cert.pem" | ||
|
||
# Client Certificate Key | ||
#ssl.key: "/etc/pki/client/cert.key" | ||
|
||
# Username to use when connecting to MongoDB. Empty by default. | ||
#username: user | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package mongodb | ||
|
||
import ( | ||
"crypto/tls" | ||
"net" | ||
|
||
"gopkg.in/mgo.v2" | ||
|
||
"github.com/elastic/beats/libbeat/common/transport/tlscommon" | ||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
) | ||
|
||
// ModuleConfig contains the common configuration for this module | ||
type ModuleConfig struct { | ||
TLS *tlscommon.Config `config:"ssl"` | ||
} | ||
|
||
// MetricSet type defines all fields of the MetricSet | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
DialInfo *mgo.DialInfo | ||
} | ||
|
||
// NewMetricSet creates a new instance of the MetricSet | ||
func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) { | ||
var config ModuleConfig | ||
err := base.Module().UnpackConfig(&config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dialInfo, err := mgo.ParseURL(base.HostData().URI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dialInfo.Timeout = base.Module().Config().Timeout | ||
|
||
if config.TLS.IsEnabled() { | ||
tlsConfig, err := tlscommon.LoadTLSConfig(config.TLS) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dialInfo.DialServer = func(addr *mgo.ServerAddr) (net.Conn, error) { | ||
hostname, _, err := net.SplitHostPort(base.HostData().Host) | ||
if err != nil { | ||
logp.Warn("Failed to obtain hostname from `%s`: %s", hostname, err) | ||
hostname = "" | ||
} | ||
return tls.Dial("tcp", addr.String(), tlsConfig.BuildModuleConfig(hostname)) | ||
} | ||
} | ||
|
||
return &MetricSet{ | ||
BaseMetricSet: base, | ||
DialInfo: dialInfo, | ||
}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ import ( | |
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/module/mongodb" | ||
|
||
"gopkg.in/mgo.v2" | ||
"gopkg.in/mgo.v2/bson" | ||
) | ||
|
||
|
@@ -47,24 +46,18 @@ func init() { | |
// additional entries. These variables can be used to persist data or configuration between | ||
// multiple fetch calls. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
dialInfo *mgo.DialInfo | ||
*mongodb.MetricSet | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be an invalid/stupid question because I'm still new to beats + golang but why do we need to embed the func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return mongodb.NewMetricSet(base)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no stupid questions, this is a good one :) Here all the metricsets share the same fields, so I use the same builder, but they need different implementations for the |
||
} | ||
|
||
// New creates a new instance of the MetricSet | ||
// Part of new is also setting up the configuration by processing additional | ||
// configuration entries if needed. | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
dialInfo, err := mgo.ParseURL(base.HostData().URI) | ||
ms, err := mongodb.NewMetricSet(base) | ||
if err != nil { | ||
return nil, err | ||
} | ||
dialInfo.Timeout = base.Module().Config().Timeout | ||
|
||
return &MetricSet{ | ||
BaseMetricSet: base, | ||
dialInfo: dialInfo, | ||
}, nil | ||
return &MetricSet{ms}, nil | ||
} | ||
|
||
// Fetch methods implements the data gathering and data conversion to the right format | ||
|
@@ -73,7 +66,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | |
func (m *MetricSet) Fetch() (common.MapStr, error) { | ||
|
||
// instantiate direct connections to each of the configured Mongo hosts | ||
mongoSession, err := mongodb.NewDirectSession(m.dialInfo) | ||
mongoSession, err := mongodb.NewDirectSession(m.DialInfo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message is autogenerated after adding the
ssl
settings tofields.yml
. I think it shouldn't mention by default the http options, as for example this module uses TLS but doesn't use HTTP. To be fixed in a future PR.