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

Amazon Elasticsearch support #5466

Closed
wants to merge 2 commits into from
Closed
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
82 changes: 82 additions & 0 deletions libbeat/outputs/elasticsearch/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package elasticsearch

import (
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/signer/v4"
"io/ioutil"
"bytes"
"github.com/elastic/beats/libbeat/logp"
"time"
"io"
"strings"
)

type AwsData struct {
Config aws.Config
Signer v4.Signer
}

// Calculate signature for request and append headers
func (conn *Connection) performAwsSignature(req *http.Request) {

if conn.Aws == nil {
return
}

var reader io.ReadSeeker
if req.Body != nil {
body := req.Body
buf, err := ioutil.ReadAll(body)
if err != nil {
logp.Err("Error performing AWS signature: %s", err)
return
}
reader = bytes.NewReader(buf)
req.Body = ioutil.NopCloser(reader)
} else {
reader = nil
}

// AWS-SDK creates invalid signature when host contains a port
// See https://github.com/aws/aws-sdk-go/issues/1537
// Host manipulations can be removed when it's fixed
host := req.Host
colon := strings.IndexByte(req.Host, ':')
if colon >= 0 {
req.Host = req.Host[:colon]
}
_, err := conn.Aws.Signer.Sign(req, reader, "es", *conn.Aws.Config.Region, time.Now())
if err != nil {
logp.Err("Error performing AWS signature: %s", err)
}
req.Host = host
}

func (c *elasticsearchConfig) Aws() *AwsData {
if c.AwsConfig.Enabled {
credentials := defaults.CredChain(defaults.Config(), defaults.Handlers())
if _, err := credentials.Get(); err != nil {
logp.Err("Error loading AWS credentials: %s", err)
return nil
}

region := c.AwsConfig.Region
if region == "" {
region = "us-east-1"
}

config := aws.NewConfig().WithCredentials(credentials).WithRegion(region)
return &AwsData{
Config: *config,
Signer: v4.Signer{
Credentials: config.Credentials,
Logger: aws.NewDefaultLogger(), // TODO remove debug logging
Debug: aws.LogDebugWithSigning,
},
}
}

return nil
}
8 changes: 8 additions & 0 deletions libbeat/outputs/elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ClientSettings struct {
Timeout time.Duration
CompressionLevel int
Stats *outputs.Stats
Aws *AwsData
}

type connectCallback func(client *Client) error
Expand All @@ -72,6 +73,7 @@ type Connection struct {

encoder bodyEncoder
version string
Aws *AwsData
}

var (
Expand Down Expand Up @@ -168,6 +170,7 @@ func NewClient(
Timeout: s.Timeout,
},
encoder: encoder,
Aws: s.Aws,
},
tlsConfig: s.TLS,
index: s.Index,
Expand Down Expand Up @@ -219,6 +222,7 @@ func (client *Client) Clone() *Client {
Headers: client.Headers,
Timeout: client.http.Timeout,
CompressionLevel: client.compressionLevel,
Aws: client.Aws,
},
nil, // XXX: do not pass connection callback?
)
Expand Down Expand Up @@ -721,6 +725,10 @@ func (conn *Connection) execHTTPRequest(req *http.Request) (int, []byte, error)
req.Host = host
}

if conn.Aws != nil {
conn.performAwsSignature(req)
}

resp, err := conn.http.Do(req)
if err != nil {
return 0, nil, err
Expand Down
10 changes: 10 additions & 0 deletions libbeat/outputs/elasticsearch/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type elasticsearchConfig struct {
MaxRetries int `config:"max_retries"`
Timeout time.Duration `config:"timeout"`
Backoff Backoff `config:"backoff"`
AwsConfig AwsConfig `config:"aws"`
}

type AwsConfig struct {
Enabled bool `config:"enabled"`
Region string `config:"region"`
}

type Backoff struct {
Expand Down Expand Up @@ -49,6 +55,10 @@ var (
Init: 1 * time.Second,
Max: 60 * time.Second,
},
AwsConfig: AwsConfig{
Enabled: false,
Region: "",
},
}
)

Expand Down
1 change: 1 addition & 0 deletions libbeat/outputs/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func makeES(
Timeout: config.Timeout,
CompressionLevel: config.CompressionLevel,
Stats: stats,
Aws: config.Aws(),
}, &connectCallbackRegistry)
if err != nil {
return outputs.Fail(err)
Expand Down
202 changes: 202 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/NOTICE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading