diff --git a/config/mongodb.go b/config/mongodb.go index 2409d40e..6fc0ad71 100644 --- a/config/mongodb.go +++ b/config/mongodb.go @@ -35,11 +35,6 @@ type MongoDB struct { // DisableFullDocumentBeforeChange - This is relevant if you're connecting to Document DB. // BSON field '$changeStream.fullDocumentBeforeChange' is an unknown field. DisableFullDocumentBeforeChange bool `yaml:"disableFullDocumentBeforeChange,omitempty"` - - // Deprecated - use [MongoDB.URI] instead. - Host string `yaml:"host"` - Username string `yaml:"username,omitempty"` - Password string `yaml:"password,omitempty"` } type Collection struct { @@ -62,8 +57,8 @@ func (m MongoDB) GetStreamingBatchSize() int32 { } func (m MongoDB) Validate() error { - if m.Host == "" && m.URI == "" { - return fmt.Errorf("either host or URI must be passed in") + if m.URI == "" { + return fmt.Errorf("URI is empty") } if m.Database == "" { diff --git a/lib/mongo/config.go b/lib/mongo/config.go index f955f070..42f2a57a 100644 --- a/lib/mongo/config.go +++ b/lib/mongo/config.go @@ -9,22 +9,12 @@ import ( ) func OptsFromConfig(cfg config.MongoDB) (*options.ClientOptions, error) { - opts := options.Client() - - if cfg.URI != "" { - opts = opts.ApplyURI(cfg.URI) - } else if cfg.Host != "" { - opts = opts.ApplyURI(cfg.Host) - if cfg.Username != "" && cfg.Password != "" { - opts = opts.SetAuth(options.Credential{ - Username: cfg.Username, - Password: cfg.Password, - }) - } - } else { - return nil, fmt.Errorf("mongoDB requires a URI or host") + if cfg.URI == "" { + return nil, fmt.Errorf("mongoDB requires a URI") } + opts := options.Client().ApplyURI(cfg.URI) + if !cfg.DisableTLS { opts = opts.SetTLSConfig(&tls.Config{}) } diff --git a/lib/mongo/config_test.go b/lib/mongo/config_test.go index 4a9ed487..bd9d26ac 100644 --- a/lib/mongo/config_test.go +++ b/lib/mongo/config_test.go @@ -8,33 +8,10 @@ import ( ) func TestOptsFromConfig(t *testing.T) { - { - cfg := config.MongoDB{ - Host: "localhost", - Username: "user", - Password: "password", - } - - opts, err := OptsFromConfig(cfg) - assert.NoError(t, err) - assert.NotNil(t, opts.TLSConfig) - assert.Equal(t, "user", opts.Auth.Username) - assert.Equal(t, "password", opts.Auth.Password) - } - { - // No username and password - cfg := config.MongoDB{ - Host: "localhost", - } - - opts, err := OptsFromConfig(cfg) - assert.NoError(t, err) - assert.Nil(t, opts.Auth) - } { // Disable TLS cfg := config.MongoDB{ - Host: "localhost", + URI: "mongodb://user:pass@localhost", DisableTLS: true, }