Skip to content

Commit

Permalink
[8.13](backport #38669) add_cloud_metadata: env var override for prov…
Browse files Browse the repository at this point in the history
…iders (#38965)

* add_cloud_metadata: env var override for providers (#38669)

* add_cloud_metadata: env var override for providers

Add support for configuring the add_cloud_metadata providers
with an environment variable, $BEATS_ADD_CLOUD_METADATA_PROVIDERS.

This may be useful when deploying Elastic Agent standalone in a
cloud provider managed Kubernetes cluster, where the cloud provider
is known at deployment time.

* Fix changelog

* appease golangci-lint

* More appeasement

(cherry picked from commit d0399e0)

* Update CHANGELOG.next.asciidoc

---------

Co-authored-by: Andrew Wilkins <[email protected]>
  • Loading branch information
mergify[bot] and axw authored Apr 16, 2024
1 parent 11346e1 commit 6951e5e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
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

0 comments on commit 6951e5e

Please sign in to comment.