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 GetAWSCredentials function in libbeat common #12727

Merged
merged 15 commits into from
Jul 22, 2019
Merged
Prev Previous commit
Next Next commit
Fix ec2_test.go
kaiyan-sheng committed Jul 16, 2019

Verified

This commit was signed with the committer’s verified signature.
tdimitrov Tsvetomir Dimitrov
commit 0e34950ad8372ad08719bdd2a32c0b58b01fb5de
5 changes: 5 additions & 0 deletions x-pack/libbeat/common/aws/credentials.go
Original file line number Diff line number Diff line change
@@ -10,13 +10,18 @@ import (
"github.com/aws/aws-sdk-go-v2/aws/external"
)

// ConfigAWS is a structure defined for AWS credentials
type ConfigAWS struct {
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
AccessKeyID string `config:"access_key_id"`
SecretAccessKey string `config:"secret_access_key"`
SessionToken string `config:"session_token"`
ProfileName string `config:"credential_profile_name"`
}

// GetAWSCredentials function gets aws credentials from the config.
// If access_key_id and secret_access_key are given, then use them as credentials.
// If not, then load from aws config file. If credential_profile_name is not
// given, then load default profile from the aws config file.
func GetAWSCredentials(config ConfigAWS) (awssdk.Config, error) {
kaiyan-sheng marked this conversation as resolved.
Show resolved Hide resolved
// Check if accessKeyID and secretAccessKey is given from configuration
if config.AccessKeyID != "" && config.SecretAccessKey != "" {
8 changes: 1 addition & 7 deletions x-pack/metricbeat/module/aws/ec2/ec2_test.go
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ import (

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/x-pack/metricbeat/module/aws"
)

// MockEC2Client struct is used for unit tests.
@@ -126,11 +125,6 @@ func TestGetInstanceIDs(t *testing.T) {
}

func TestCreateCloudWatchEvents(t *testing.T) {
mockModuleConfig := aws.Config{
Period: 300 * time.Second,
DefaultRegion: regionName,
}

expectedEvent := mb.Event{
RootFields: common.MapStr{
"service": common.MapStr{"name": "ec2"},
@@ -199,7 +193,7 @@ func TestCreateCloudWatchEvents(t *testing.T) {
}

metricSet := MetricSet{}
events, err := metricSet.createCloudWatchEvents(getMetricDataOutput, instancesOutputs, mockModuleConfig.DefaultRegion)
events, err := metricSet.createCloudWatchEvents(getMetricDataOutput, instancesOutputs, "us-west-1")
assert.NoError(t, err)
assert.Equal(t, 1, len(events))
assert.Equal(t, expectedEvent.RootFields, events[instanceID].RootFields)