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

[8.13](backport #38669) add_cloud_metadata: env var override for providers #38965

Merged
merged 2 commits into from
Apr 16, 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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- 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]
- Update to Go 1.21.9. {pulk}38727[38727]
- 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
33 changes: 27 additions & 6 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,25 +27,38 @@ import (
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
var allLocal []string
for name, ff := range cloudMetaProviders {
all = append(all, name)
if ff.Local {
allLocal = append(allLocal, name)
}
}

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,18 +70,25 @@ func TestProvidersFilter(t *testing.T) {
"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 {
out = append(out, str)
}
return out
return append(out, in...)
}

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