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

add_cloud_metadata: env var override for providers #38669

Merged
merged 10 commits into from
Apr 16, 2024
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Setting environmental variable ELASTIC_NETINFO:false in Elastic Agent pod will d
- Raise up logging level to warning when attempting to configure beats with unknown fields from autodiscovered events/environments
- elasticsearch output now supports `idle_connection_timeout`. {issue}35616[35615] {pull}36843[36843]
Setting environmental variable ELASTIC_NETINFO:false in Elastic Agent pod will disable the netinfo.enabled option of add_host_metadata processor
- The environment variable `BEATS_ADD_CLOUD_METADATA_PROVIDERS` overrides configured/default `add_cloud_metadata` providers {pull}38669[38669]

*Auditbeat*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ cloud or on-premise).
The second optional setting is `providers`. The `providers` settings accepts a
list of cloud provider names to be used. If `providers` is not configured, then
all providers that do not access a remote endpoint are enabled by default.
The list of providers may alternatively be configured with the environment
variable `BEATS_ADD_CLOUD_METADATA_PROVIDERS`, by setting it to a comma-separated
list of provider names.

List of names the `providers` setting supports:

Expand Down
17 changes: 17 additions & 0 deletions libbeat/processors/add_cloud_metadata/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"fmt"
"net"
"net/http"
"os"
"strings"
"time"

conf "github.com/elastic/elastic-agent-libs/config"
Expand Down Expand Up @@ -73,6 +75,21 @@ func selectProviders(configList providerList, providers map[string]provider) map
}

func providersFilter(configList providerList, allProviders map[string]provider) func(string) bool {
if v, ok := os.LookupEnv("BEATS_ADD_CLOUD_METADATA_PROVIDERS"); ok {
// We allow users to override the config and defaults with
// this environment variable as a workaround in case the
// configured/default providers misbehave.
configList = nil
for _, name := range strings.Split(v, ",") {
configList = append(configList, strings.TrimSpace(name))
}
if len(configList) == 0 {
// User explicitly disabled all providers.
return func(string) bool {
return false
}
}
}
if len(configList) == 0 {
return func(name string) bool {
ff, ok := allProviders[name]
Expand Down
26 changes: 26 additions & 0 deletions libbeat/processors/add_cloud_metadata/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package add_cloud_metadata

import (
"os"
"sort"
"testing"

Expand All @@ -26,11 +27,15 @@
conf "github.com/elastic/elastic-agent-libs/config"
)

func init() {
os.Unsetenv("BEATS_ADD_CLOUD_METADATA_PROVIDERS")
}

func TestProvidersFilter(t *testing.T) {
var all []string

Check failure on line 35 in libbeat/processors/add_cloud_metadata/providers_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Consider pre-allocating `all` (prealloc)
var allLocal []string
for name, ff := range cloudMetaProviders {
all = append(all, name)

Check failure on line 38 in libbeat/processors/add_cloud_metadata/providers_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

SA4010: this result of append is never used, except maybe in other appends (staticcheck)
if ff.Local {
allLocal = append(allLocal, name)
}
Expand All @@ -38,13 +43,24 @@

cases := map[string]struct {
config map[string]interface{}
env string
fail bool
expected []string
}{
"all with local access only if not configured": {
config: map[string]interface{}{},
expected: allLocal,
},
"BEATS_ADD_CLOUD_METADATA_PROVIDERS overrides default": {
config: map[string]interface{}{},
env: "alibaba, digitalocean",
expected: []string{"alibaba", "digitalocean"},
},
"none if BEATS_ADD_CLOUD_METADATA_PROVIDERS is explicitly set to an empty list": {
config: map[string]interface{}{},
env: " ",
expected: nil,
},
"fail to load if unknown name is used": {
config: map[string]interface{}{
"providers": []string{"unknown"},
Expand All @@ -56,10 +72,17 @@
"providers": []string{"aws", "gcp", "digitalocean"},
},
},
"BEATS_ADD_CLOUD_METADATA_PROVIDERS overrides selected": {
config: map[string]interface{}{
"providers": []string{"aws", "gcp", "digitalocean"},
},
env: "alibaba, digitalocean",
expected: []string{"alibaba", "digitalocean"},
},
}

copyStrings := func(in []string) (out []string) {
for _, str := range in {

Check failure on line 85 in libbeat/processors/add_cloud_metadata/providers_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

S1011: should replace loop with `out = append(out, in...)` (gosimple)
out = append(out, str)
}
return out
Expand All @@ -68,6 +91,9 @@
for name, test := range cases {
t.Run(name, func(t *testing.T) {
rawConfig := conf.MustNewConfigFrom(test.config)
if test.env != "" {
t.Setenv("BEATS_ADD_CLOUD_METADATA_PROVIDERS", test.env)
}

config := defaultConfig()
err := rawConfig.Unpack(&config)
Expand Down
Loading