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(inputs.redfish): Allow secrets for username/password configuration #14702

Merged
merged 7 commits into from
Feb 8, 2024
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
10 changes: 9 additions & 1 deletion plugins/inputs/redfish/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.

[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins

## Secret-store support

This plugin supports secrets from secret-stores for the `username` and
`password` options. See the [secret-store documentation][SECRETSTORE] for more
details on how to use them.

[SECRETSTORE]: ../../../docs/CONFIGURATION.md#secret-store-secrets

## Configuration

```toml @sample.conf
Expand All @@ -24,7 +32,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Redfish API Base URL.
address = "https://127.0.0.1:5000"

## Credentials for the Redfish API.
## Credentials for the Redfish API. Can also use secrets.
username = "root"
password = "password123456"

Expand Down
22 changes: 18 additions & 4 deletions plugins/inputs/redfish/redfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ var sampleConfig string

type Redfish struct {
Address string `toml:"address"`
Username string `toml:"username"`
Password string `toml:"password"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
ComputerSystemID string `toml:"computer_system_id"`
IncludeMetrics []string `toml:"include_metrics"`
IncludeTagSets []string `toml:"include_tag_sets"`
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *Redfish) Init() error {
return errors.New("did not provide IP")
}

if r.Username == "" && r.Password == "" {
if r.Username.Empty() && r.Password.Empty() {
return errors.New("did not provide username and password")
}

Expand Down Expand Up @@ -221,7 +221,21 @@ func (r *Redfish) getData(address string, payload interface{}) error {
return err
}

req.SetBasicAuth(r.Username, r.Password)
username, err := r.Username.Get()
if err != nil {
return fmt.Errorf("getting username failed: %w", err)
}
user := username.String()
username.Destroy()

password, err := r.Password.Get()
if err != nil {
return fmt.Errorf("getting password failed: %w", err)
}
pass := password.String()
password.Destroy()

req.SetBasicAuth(user, pass)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("OData-Version", "4.0")
Expand Down
29 changes: 15 additions & 14 deletions plugins/inputs/redfish/redfish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
)

Expand Down Expand Up @@ -422,8 +423,8 @@ func TestDellApis(t *testing.T) {
}
plugin := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "System.Embedded.1",
IncludeMetrics: []string{"thermal", "power"},
}
Expand Down Expand Up @@ -601,8 +602,8 @@ func TestHPApis(t *testing.T) {

hpPlugin := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "1",
IncludeMetrics: []string{"thermal", "power"},
}
Expand Down Expand Up @@ -698,8 +699,8 @@ func TestHPilo4Apis(t *testing.T) {

hpPlugin := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "1",
IncludeMetrics: []string{"thermal"},
}
Expand Down Expand Up @@ -739,8 +740,8 @@ func TestInvalidUsernameorPassword(t *testing.T) {

r := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "System.Embedded.1",
IncludeMetrics: []string{"thermal", "power"},
}
Expand Down Expand Up @@ -841,8 +842,8 @@ func TestInvalidDellJSON(t *testing.T) {

plugin := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "System.Embedded.1",
IncludeMetrics: []string{"thermal", "power"},
}
Expand Down Expand Up @@ -912,8 +913,8 @@ func TestInvalidHPJSON(t *testing.T) {

plugin := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "System.Embedded.2",
IncludeMetrics: []string{"thermal", "power"},
}
Expand Down Expand Up @@ -1196,8 +1197,8 @@ func TestIncludeTagSetsConfiguration(t *testing.T) {

hpPlugin := &Redfish{
Address: ts.URL,
Username: "test",
Password: "test",
Username: config.NewSecret([]byte("test")),
Password: config.NewSecret([]byte("test")),
ComputerSystemID: "1",
IncludeTagSets: []string{"chassis", "chassis.location"},
IncludeMetrics: []string{"thermal", "power"},
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/redfish/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Redfish API Base URL.
address = "https://127.0.0.1:5000"

## Credentials for the Redfish API.
## Credentials for the Redfish API. Can also use secrets.
username = "root"
password = "password123456"

Expand Down
Loading