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 openstack ssl provider in add_cloud_metadata #21590

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -451,6 +451,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Cloud Foundry metadata is cached to disk. {pull}20775[20775]
- Add option to select the type of index template to load: legacy, component, index. {pull}21212[21212]
- Release `add_cloudfoundry_metadata` as GA. {pull}21525[21525]
- Add TLS support to `add_cloud_metadata`. {pull}21590[21590]

*Auditbeat*

Expand Down
16 changes: 14 additions & 2 deletions libbeat/processors/add_cloud_metadata/add_cloud_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/libbeat/processors"
jsprocessor "github.com/elastic/beats/v7/libbeat/processors/script/javascript/module/processor"
Expand Down Expand Up @@ -53,6 +54,7 @@ type addCloudMetadata struct {
type initData struct {
fetchers []metadataFetcher
timeout time.Duration
tlsConfig *tlscommon.TLSConfig
overwrite bool
}

Expand All @@ -63,14 +65,24 @@ func New(c *common.Config) (processors.Processor, error) {
return nil, errors.Wrap(err, "failed to unpack add_cloud_metadata config")
}

tlsConfig, err := tlscommon.LoadTLSConfig(config.TLS)
if err != nil {
return nil, errors.Wrap(err, "TLS configuration load")
}

initProviders := selectProviders(config.Providers, cloudMetaProviders)
fetchers, err := setupFetchers(initProviders, c)
if err != nil {
return nil, err
}
p := &addCloudMetadata{
initData: &initData{fetchers, config.Timeout, config.Overwrite},
logger: logp.NewLogger("add_cloud_metadata"),
initData: &initData{
fetchers: fetchers,
timeout: config.Timeout,
tlsConfig: tlsConfig,
overwrite: config.Overwrite,
},
logger: logp.NewLogger("add_cloud_metadata"),
}

go p.init()
Expand Down
10 changes: 7 additions & 3 deletions libbeat/processors/add_cloud_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ package add_cloud_metadata
import (
"fmt"
"time"

"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
)

type config struct {
Timeout time.Duration `config:"timeout"` // Amount of time to wait for responses from the metadata services.
Overwrite bool `config:"overwrite"` // Overwrite if cloud.* fields already exist.
Providers providerList `config:"providers"` // List of providers to probe
Timeout time.Duration `config:"timeout"` // Amount of time to wait for responses from the metadata services.
TLS *tlscommon.Config `config:"ssl"` // TLS configuration
Overwrite bool `config:"overwrite"` // Overwrite if cloud.* fields already exist.
Providers providerList `config:"providers"` // List of providers to probe

jsoriano marked this conversation as resolved.
Show resolved Hide resolved
}

type providerList []string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ The third optional configuration setting is `overwrite`. When `overwrite` is
`true`, `add_cloud_metadata` overwrites existing `cloud.*` fields (`false` by
default).

The `add_cloud_metadata` processor supports SSL options. HTTPS can be enabled by
setting `ssl.enabled` to `true`. See <<configuration-ssl>> for more information.

The metadata that is added to events varies by hosting provider. Below are
examples for each of the supported providers.

Expand Down
10 changes: 8 additions & 2 deletions libbeat/processors/add_cloud_metadata/http_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/pkg/errors"

"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/common/transport/tlscommon"
)

type httpMetadataFetcher struct {
Expand Down Expand Up @@ -129,16 +130,21 @@ func (f *httpMetadataFetcher) fetchRaw(
func getMetadataURLs(c *common.Config, defaultHost string, metadataURIs []string) ([]string, error) {
var urls []string
config := struct {
MetadataHostAndPort string `config:"host"` // Specifies the host and port of the metadata service (for testing purposes only).
MetadataHostAndPort string `config:"host"` // Specifies the host and port of the metadata service (for testing purposes only).
TLSConfig *tlscommon.Config `config:"ssl"`
}{
MetadataHostAndPort: defaultHost,
}
err := c.Unpack(&config)
if err != nil {
return urls, errors.Wrap(err, "failed to unpack add_cloud_metadata config")
}
scheme := "http"
if config.TLSConfig.IsEnabled() {
scheme = "https"
}
for _, uri := range metadataURIs {
urls = append(urls, "http://"+config.MetadataHostAndPort+uri)
urls = append(urls, scheme+"://"+config.MetadataHostAndPort+uri)
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
}
return urls, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
"github.com/elastic/beats/v7/libbeat/logp"
)

func initOpenstackNovaTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func openstackNovaMetadataHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == osMetadataInstanceIDURI {
w.Write([]byte("i-0000ffac"))
return
Expand All @@ -49,13 +49,13 @@ func initOpenstackNovaTestServer() *httptest.Server {
}

http.Error(w, "not found", http.StatusNotFound)
}))
})
}

func TestRetrieveOpenstackNovaMetadata(t *testing.T) {
logp.TestingSetup()

server := initOpenstackNovaTestServer()
server := httptest.NewServer(openstackNovaMetadataHandler())
defer server.Close()

config, err := common.NewConfigFrom(map[string]interface{}{
Expand All @@ -66,6 +66,29 @@ func TestRetrieveOpenstackNovaMetadata(t *testing.T) {
t.Fatal(err)
}

assertOpenstackNova(t, config)
}

func TestRetrieveOpenstackNovaMetadataWithHTTPS(t *testing.T) {
logp.TestingSetup()

server := httptest.NewTLSServer(openstackNovaMetadataHandler())
defer server.Close()

config, err := common.NewConfigFrom(map[string]interface{}{
"host": server.Listener.Addr().String(),
"ssl.enabled": true,
"ssl.verification_mode": "none",
})

if err != nil {
t.Fatal(err)
}

assertOpenstackNova(t, config)
}

func assertOpenstackNova(t *testing.T, config *common.Config) {
p, err := New(config)
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions libbeat/processors/add_cloud_metadata/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (p *addCloudMetadata) fetchMetadata() *result {
Timeout: p.initData.timeout,
KeepAlive: 0,
}).DialContext,
TLSClientConfig: p.initData.tlsConfig.ToConfig(),
},
}

Expand Down