Skip to content

Commit

Permalink
Remove MongoDB host + username & password support (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie authored Oct 11, 2024
1 parent 033c29c commit a5e9dc4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 45 deletions.
9 changes: 2 additions & 7 deletions config/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 == "" {
Expand Down
18 changes: 4 additions & 14 deletions lib/mongo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Expand Down
25 changes: 1 addition & 24 deletions lib/mongo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down

0 comments on commit a5e9dc4

Please sign in to comment.