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

fix: when using credentials_file make sure defaults are copied to poller #2215

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"bytes"
"context"
"dario.cat/mergo"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/logging"
"os/exec"
Expand Down Expand Up @@ -162,7 +163,7 @@ func (c *Credentials) GetPollerAuth() (PollerAuth, error) {
if auth.IsCert {
return auth, nil
}
if auth.Password != "" {
if auth.Username != "" && auth.Password != "" {
return auth, nil
}

Expand All @@ -186,7 +187,8 @@ func (c *Credentials) GetPollerAuth() (PollerAuth, error) {
if auth.Username != "" {
defaultAuth.Username = auth.Username
}
return defaultAuth, nil
_ = mergo.Merge(&auth, defaultAuth)
return auth, nil
}

func getPollerAuth(c *Credentials, poller *conf.Poller) (PollerAuth, error) {
Expand Down
31 changes: 28 additions & 3 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ Pollers:
credentials_file: testdata/secrets.yaml`,
},

{
name: "poller username from credentials_file, password from poller",
pollerName: "test",
want: PollerAuth{Username: "default-user", Password: "moon"},
defaultDefined: true,
yaml: `
Defaults:
credentials_file: testdata/secrets.yaml
Pollers:
test:
addr: a.b.c
password: moon`,
},

{
name: "poller username from credentials_file",
pollerName: "test2",
Expand Down Expand Up @@ -282,6 +296,20 @@ Pollers:
username: flo
credentials_script:
path: testdata/get_pass
`,
},

{
name: "password with space",
pollerName: "test",
want: PollerAuth{Username: "flo", Password: "abc def"},
wantSchedule: "42m",
yaml: `
Pollers:
test:
addr: a.b.c
username: flo
password: abc def
`,
},
}
Expand Down Expand Up @@ -314,9 +342,6 @@ Pollers:
if tt.want.Password != got.Password {
t.Errorf("got password=[%s], want password=[%s]", got.Password, tt.want.Password)
}
if tt.want.Username != poller.Username {
t.Errorf("poller got username=[%s], want username=[%s]", poller.Username, tt.want.Username)
}
if tt.want.IsCert != got.IsCert {
t.Errorf("got IsCert=[%t], want IsCert=[%t]", got.IsCert, tt.want.IsCert)
}
Expand Down