Skip to content
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

feat(output): add mongodb output plugin #9923

Merged
merged 29 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2bd13fb
initial commit for monogdb output plugin
bustedware Oct 13, 2021
6b9f8de
Merge branch 'influxdata:master' into master
bustedware Oct 13, 2021
1362bba
initial commit for mongodb output unit test. added more error checkin…
bustedware Oct 13, 2021
4e79116
updated go.mod and go.sum files
bustedware Oct 13, 2021
2465282
fixed formatting for mongodb output plugin
bustedware Oct 14, 2021
ee193be
added license for gopkg.in/mgo.v2
bustedware Oct 14, 2021
49761b2
formatting README.md to highlight version requirements
bustedware Oct 14, 2021
0a7c486
update to variable names for consistency
bustedware Oct 14, 2021
efb7fb9
updated to reference new variable names. removed unused serialization…
bustedware Oct 14, 2021
f8fa611
removed unmaintained library from project. updated mongodb_test to us…
bustedware Oct 15, 2021
5eb9553
Merge branch 'influxdata:master' into master
bustedware Oct 15, 2021
0c4e4f6
updated go.mod and go.sum. removed data_format from config and readme…
bustedware Oct 15, 2021
708616e
fixed make check by using primitive.E types
bustedware Oct 16, 2021
4aa3294
- added clientOptions to struct and configured within Init()
bustedware Oct 16, 2021
0929edd
- use proper logging facility
bustedware Oct 16, 2021
ad4011b
modified variable names to pass lint checks. added an error check aro…
bustedware Oct 16, 2021
3078b77
added Dockerfile for running long mongodb_tests.go
bustedware Oct 25, 2021
c06a21e
resolved merge conflict with config.go. added new line characters to …
bustedware Oct 25, 2021
48d6ad7
executed make tidy to resolve go.mod and go.sum conflicts
bustedware Oct 25, 2021
0bb78e9
appears that github.com/aws/aws-sdk-go is no longer used
bustedware Oct 25, 2021
160970d
Update plugins/outputs/mongodb/mongodb.go
bustedware Oct 25, 2021
d92e298
Update plugins/outputs/mongodb/mongodb.go
bustedware Oct 26, 2021
4710145
Apply suggestions from code review
bustedware Oct 27, 2021
2cae811
Merge branch 'influxdata:master' into master
bustedware Oct 27, 2021
4445eb6
finish apply remaining open suggestions. both database and granularit…
bustedware Oct 27, 2021
9e58297
updated README.md to reflect new sensible defaults for database and g…
bustedware Oct 27, 2021
c655015
Update plugins/outputs/mongodb/mongodb.go
bustedware Oct 27, 2021
02152ce
update to use s.ServerSelectTimeout instead of serverSelectionTimeout…
bustedware Oct 27, 2021
ab60455
removed assignment back to s.clientOptions. updated for better usage …
bustedware Oct 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ following works:
- github.com/aristanetworks/glog [Apache License 2.0](https://github.com/aristanetworks/glog/blob/master/LICENSE)
- github.com/aristanetworks/goarista [Apache License 2.0](https://github.com/aristanetworks/goarista/blob/master/COPYING)
- github.com/armon/go-metrics [MIT License](https://github.com/armon/go-metrics/blob/master/LICENSE)
- github.com/aws/aws-sdk-go [Apache License 2.0](https://github.com/aws/aws-sdk-go/blob/master/LICENSE.txt)
- github.com/aws/aws-sdk-go-v2 [Apache License 2.0](https://github.com/aws/aws-sdk-go-v2/blob/main/LICENSE.txt)
- github.com/aws/aws-sdk-go-v2/config [Apache License 2.0](https://github.com/aws/aws-sdk-go-v2/blob/main/config/LICENSE.txt)
- github.com/aws/aws-sdk-go-v2/credentials [Apache License 2.0](https://github.com/aws/aws-sdk-go-v2/blob/main/credentials/LICENSE.txt)
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ require (
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
github.com/yuin/gopher-lua v0.0.0-20200603152657-dc2b0ca8b37e // indirect
go.etcd.io/etcd/api/v3 v3.5.0 // indirect
go.mongodb.org/mongo-driver v1.5.3
go.mongodb.org/mongo-driver v1.7.3
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/collector/model v0.35.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.23.0
Expand Down Expand Up @@ -334,7 +334,6 @@ require (
)

require (
github.com/aws/aws-sdk-go v1.38.3 // indirect
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.2.0 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.4.0 // indirect
github.com/awslabs/kinesis-aggregation/go v0.0.0-20210630091500-54e17340d32f // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ github.com/aws/aws-sdk-go v1.19.48/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpi
github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
github.com/aws/aws-sdk-go v1.38.3 h1:QCL/le04oAz2jELMRSuJVjGT7H+4hhoQc66eMPCfU/k=
github.com/aws/aws-sdk-go v1.38.3/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/aws/aws-sdk-go-v2 v1.1.0/go.mod h1:smfAbmpW+tcRVuNUjo3MOArSZmW72t62rkCzc2i0TWM=
Expand Down Expand Up @@ -2126,8 +2125,8 @@ go.mongodb.org/mongo-driver v1.4.4/go.mod h1:WcMNYLx/IlOxLe6JRJiv2uXuCz6zBLndR4S
go.mongodb.org/mongo-driver v1.4.6/go.mod h1:WcMNYLx/IlOxLe6JRJiv2uXuCz6zBLndR4SoGjYphSc=
go.mongodb.org/mongo-driver v1.5.1/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw=
go.mongodb.org/mongo-driver v1.5.2/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw=
go.mongodb.org/mongo-driver v1.5.3 h1:wWbFB6zaGHpzguF3f7tW94sVE8sFl3lHx8OZx/4OuFI=
go.mongodb.org/mongo-driver v1.5.3/go.mod h1:gRXCHX4Jo7J0IJ1oDQyUxF7jfy19UfxniMS4xxMmUqw=
go.mongodb.org/mongo-driver v1.7.3 h1:G4l/eYY9VrQAK/AUgkV0koQKzQnyddnWxrd/Etf0jIs=
go.mongodb.org/mongo-driver v1.7.3/go.mod h1:NqaYOwnXWr5Pm7AOpO5QFxKJ503nbMse/R79oO62zWg=
go.mozilla.org/pkcs7 v0.0.0-20200128120323-432b2356ecb1/go.mod h1:SNgMg+EgDFwmvSmLRTNKC5fegJjB7v23qTQ0XLGUNHk=
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
Expand Down
5 changes: 4 additions & 1 deletion plugins/common/tls/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/influxdata/telegraf/internal/choice"
"os"
"strings"

"github.com/influxdata/telegraf/internal/choice"
)

// ClientConfig represents the standard client TLS config.
type ClientConfig struct {
TLSCA string `toml:"tls_ca"`
TLSCert string `toml:"tls_cert"`
TLSKey string `toml:"tls_key"`
TLSKeyPwd string `toml:"tls_key_pwd"`
InsecureSkipVerify bool `toml:"insecure_skip_verify"`
ServerName string `toml:"tls_server_name"`

Expand All @@ -27,6 +29,7 @@ type ClientConfig struct {
type ServerConfig struct {
TLSCert string `toml:"tls_cert"`
TLSKey string `toml:"tls_key"`
TLSKeyPwd string `toml:"tls_key_pwd"`
TLSAllowedCACerts []string `toml:"tls_allowed_cacerts"`
TLSCipherSuites []string `toml:"tls_cipher_suites"`
TLSMinVersion string `toml:"tls_min_version"`
Expand Down
21 changes: 21 additions & 0 deletions plugins/common/tls/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestClientConfig(t *testing.T) {
TLSKey: pki.ClientKeyPath(),
},
},
{
name: "success with tls key password set",
client: tls.ClientConfig{
TLSCA: pki.CACertPath(),
TLSCert: pki.ClientCertPath(),
TLSKey: pki.ClientKeyPath(),
TLSKeyPwd: "",
},
},
{
name: "invalid ca",
client: tls.ClientConfig{
Expand Down Expand Up @@ -137,6 +146,18 @@ func TestServerConfig(t *testing.T) {
TLSMaxVersion: pki.TLSMaxVersion(),
},
},
{
name: "success with tls key password set",
server: tls.ServerConfig{
TLSCert: pki.ServerCertPath(),
TLSKey: pki.ServerKeyPath(),
TLSKeyPwd: "",
TLSAllowedCACerts: []string{pki.CACertPath()},
TLSCipherSuites: []string{pki.CipherSuite()},
TLSMinVersion: pki.TLSMinVersion(),
TLSMaxVersion: pki.TLSMaxVersion(),
},
},
{
name: "missing tls cipher suites is okay",
server: tls.ServerConfig{
Expand Down
1 change: 1 addition & 0 deletions plugins/outputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/outputs/librato"
_ "github.com/influxdata/telegraf/plugins/outputs/logzio"
_ "github.com/influxdata/telegraf/plugins/outputs/loki"
_ "github.com/influxdata/telegraf/plugins/outputs/mongodb"
_ "github.com/influxdata/telegraf/plugins/outputs/mqtt"
_ "github.com/influxdata/telegraf/plugins/outputs/nats"
_ "github.com/influxdata/telegraf/plugins/outputs/newrelic"
Expand Down
43 changes: 43 additions & 0 deletions plugins/outputs/mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# MongoDB Output Plugin

This plugin sends metrics to MongoDB and automatically creates the collections as time series collections when they don't already exist.
**Please note:** Requires MongoDB 5.0+ for Time Series Collections

### Configuration:

```toml
# A plugin that can transmit logs to mongodb
[[outputs.mongodb]]
# connection string examples for mongodb
dsn = "mongodb://localhost:27017"
# dsn = "mongodb://mongod1:27017,mongod2:27017,mongod3:27017/admin&replicaSet=myReplSet&w=1"

# overrides serverSelectionTimeoutMS in dsn if set
# timeout = "30s"

# default authentication, optional
# authentication = "NONE"

# for SCRAM-SHA-256 authentication
# authentication = "SCRAM"
# username = "root"
# password = "***"

# for x509 certificate authentication
# authentication = "X509"
# tls_ca = "ca.pem"
# tls_key = "client.pem"
# # tls_key_pwd = "changeme" # required for encrypted tls_key
# insecure_skip_verify = false

# database to store measurements and time series collections
database = "telegraf"

# granularity can be seconds, minutes, or hours.
# configuring this value will be based on your input collection frequency.
# see https://docs.mongodb.com/manual/core/timeseries-collections/#create-a-time-series-collection
granularity = "seconds"

# optionally set a TTL to automatically expire documents from the measurement collections.
# ttl = "360h"
```
22 changes: 22 additions & 0 deletions plugins/outputs/mongodb/dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM docker.io/library/mongo:latest

RUN apt-get update && \
apt-get install -y openssh-client

WORKDIR /var/log
RUN mkdir -p mongodb_noauth/ mongodb_scram/ mongodb_x509/ mongodb_x509_expire/

WORKDIR /opt
COPY ./testutil/pki/tls-certs.sh .
RUN mkdir -p data/noauth data/scram data/x509 data/x509_expire
RUN /opt/tls-certs.sh

COPY ./plugins/outputs/mongodb/dev/mongodb.sh .
RUN chmod +x mongodb.sh

EXPOSE 27017
EXPOSE 27018
EXPOSE 27019
EXPOSE 27020

CMD ./mongodb.sh
34 changes: 34 additions & 0 deletions plugins/outputs/mongodb/dev/mongodb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# no auth
mongod --dbpath data/noauth --fork --logpath /var/log/mongodb_noauth/mongod.log --bind_ip 0.0.0.0 --port 27017

# scram auth
mongod --dbpath data/scram --fork --logpath /var/log/mongodb_scram/mongod.log --bind_ip 0.0.0.0 --port 27018
mongo localhost:27018/admin --eval "db.createUser({user:\"root\", pwd:\"changeme\", roles:[{role:\"root\",db:\"admin\"}]})"
mongo localhost:27018/admin --eval "db.shutdownServer()"
mongod --dbpath data/scram --fork --logpath /var/log/mongodb_scram/mongod.log --auth --setParameter authenticationMechanisms=SCRAM-SHA-256 --bind_ip 0.0.0.0 --port 27018

# get client certificate subject for creating x509 authenticating user
dn=$(openssl x509 -in ./private/client.pem -noout -subject -nameopt RFC2253 | sed 's/subject=//g')

# x509 auth
mongod --dbpath data/x509 --fork --logpath /var/log/mongodb_x509/mongod.log --bind_ip 0.0.0.0 --port 27019
mongo localhost:27019/admin --eval "db.getSiblingDB(\"\$external\").runCommand({createUser:\"$dn\",roles:[{role:\"root\",db:\"admin\"}]})"
mongo localhost:27019/admin --eval "db.shutdownServer()"
mongod --dbpath data/x509 --fork --logpath /var/log/mongodb_x509/mongod.log --auth --setParameter authenticationMechanisms=MONGODB-X509 --tlsMode preferTLS --tlsCAFile certs/cacert.pem --tlsCertificateKeyFile private/server.pem --bind_ip 0.0.0.0 --port 27019

# x509 auth short expirey
# mongodb will not start with an expired certificate. service must be started before certificate expires. tests should be run after certificate expiry
mongod --dbpath data/x509_expire --fork --logpath /var/log/mongodb_x509_expire/mongod.log --bind_ip 0.0.0.0 --port 27020
mongo localhost:27020/admin --eval "db.getSiblingDB(\"\$external\").runCommand({createUser:\"$dn\",roles:[{role:\"root\",db:\"admin\"}]})"
mongo localhost:27020/admin --eval "db.shutdownServer()"
mongod --dbpath data/x509_expire --fork --logpath /var/log/mongodb_x509_expire/mongod.log --auth --setParameter authenticationMechanisms=MONGODB-X509 --tlsMode preferTLS --tlsCAFile certs/cacert.pem --tlsCertificateKeyFile private/serverexp.pem --bind_ip 0.0.0.0 --port 27020

# note about key size and mongodb
# x509 must be 2048 bytes or stronger in order for mongodb to start. otherwise you will receive similar error below
# {"keyFile":"/opt/private/server.pem","error":"error:140AB18F:SSL routines:SSL_CTX_use_certificate:ee key too small"}

# copy key files to /opt/export. docker volume should point /opt/export to outputs/mongodb/dev in order to run non short x509 tests
cp /opt/certs/cacert.pem /opt/private/client.pem /opt/private/clientenc.pem /opt/export

while true; do sleep 1; done # leave container running.
Loading