Skip to content

Commit

Permalink
Consolidate function
Browse files Browse the repository at this point in the history
  • Loading branch information
legoguy1000 committed Jul 20, 2021
1 parent abe5eaa commit a0a5bcb
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 31 deletions.
5 changes: 2 additions & 3 deletions x-pack/filebeat/input/awscloudwatch/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ func NewInput(cfg *common.Config, connector channel.Connector, context input.Con
config.RegionName = regionName
}

awsConfig, err := awscommon.GetAWSCredentials(config.AwsConfig)
awsConfig, err := awscommon.InitializeAWSConfig(config.AwsConfig)
if err != nil {
return nil, errors.Wrap(err, "getAWSCredentials failed")
return nil, errors.Wrap(err, "InitializeAWSConfig failed")
}
awsConfig = awscommon.EnrichAWSConfigWithProxy(config.AwsConfig, awsConfig)
awsConfig.Region = config.RegionName

closeChannel := make(chan struct{})
Expand Down
9 changes: 4 additions & 5 deletions x-pack/filebeat/input/awss3/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func newInput(config config) (*s3Input, error) {
func (in *s3Input) Name() string { return inputName }

func (in *s3Input) Test(ctx v2.TestContext) error {
_, err := awscommon.GetAWSCredentials(in.config.AWSConfig)
_, err := awscommon.InitializeAWSConfig(in.config.AWSConfig)
if err != nil {
return fmt.Errorf("getAWSCredentials failed: %w", err)
return fmt.Errorf("InitializeAWSConfig failed: %w", err)
}
return nil
}
Expand Down Expand Up @@ -98,11 +98,10 @@ func (in *s3Input) createCollector(ctx v2.Context, pipeline beat.Pipeline) (*s3C
log = log.With("region", regionName)
}

awsConfig, err := awscommon.GetAWSCredentials(in.config.AWSConfig)
awsConfig, err := awscommon.InitializeAWSConfig(in.config.AWSConfig)
if err != nil {
return nil, fmt.Errorf("getAWSCredentials failed: %w", err)
return nil, fmt.Errorf("InitializeAWSConfig failed: %w", err)
}
awsConfig = awscommon.EnrichAWSConfigWithProxy(in.config.AWSConfig, awsConfig)
awsConfig.Region = regionName

visibilityTimeout := int64(in.config.VisibilityTimeout.Seconds())
Expand Down
4 changes: 2 additions & 2 deletions x-pack/filebeat/input/awss3/s3_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func setupCollector(t *testing.T, cfg *common.Config, mock bool) (*s3Collector,
}

config := getConfigForTest(t)
awsConfig, err := awscommon.GetAWSCredentials(config.AWSConfig)
awsConfig, err := awscommon.InitializeAWSConfig(config.AWSConfig)
if err != nil {
t.Fatal("failed GetAWSCredentials with AWS Config: ", err)
t.Fatal("failed InitializeAWSConfig with AWS Config: ", err)
}

s3BucketRegion := os.Getenv("S3_BUCKET_REGION")
Expand Down
2 changes: 1 addition & 1 deletion x-pack/functionbeat/manager/aws/cli_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func NewCLI(
if err := cfg.Unpack(config); err != nil {
return nil, err
}
awsCfg, err := awscommon.GetAWSCredentials(config.Credentials)
awsCfg, err := awscommon.InitializeAWSConfig(config.Credentials)
if err != nil {
return nil, fmt.Errorf("failed to get aws credentials, please check AWS credential in config: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion x-pack/libbeat/autodiscover/providers/aws/ec2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func AutodiscoverBuilder(
return nil, err
}

awsCfg, err := awscommon.GetAWSCredentials(
awsCfg, err := awscommon.InitializeAWSConfig(
awscommon.ConfigAWS{
AccessKeyID: config.AWSConfig.AccessKeyID,
SecretAccessKey: config.AWSConfig.SecretAccessKey,
Expand Down
6 changes: 2 additions & 4 deletions x-pack/libbeat/autodiscover/providers/aws/elb/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ func AutodiscoverBuilder(
return nil, err
}

awsCfg, err := awscommon.GetAWSCredentials(awscommon.ConfigAWS{
awsCfg, err := awscommon.InitializeAWSConfig(awscommon.ConfigAWS{
AccessKeyID: config.AWSConfig.AccessKeyID,
SecretAccessKey: config.AWSConfig.SecretAccessKey,
SessionToken: config.AWSConfig.SessionToken,
ProfileName: config.AWSConfig.ProfileName,
})
awsCfg = awscommon.EnrichAWSConfigWithProxy(config.AWSConfig, awsCfg)

// Construct MetricSet with a full regions list if there is no region specified.
if config.Regions == nil {
Expand All @@ -77,7 +76,7 @@ func AutodiscoverBuilder(

var clients []elasticloadbalancingv2iface.ClientAPI
for _, region := range config.Regions {
awsCfg, err := awscommon.GetAWSCredentials(awscommon.ConfigAWS{
awsCfg, err := awscommon.InitializeAWSConfig(awscommon.ConfigAWS{
AccessKeyID: config.AWSConfig.AccessKeyID,
SecretAccessKey: config.AWSConfig.SecretAccessKey,
SessionToken: config.AWSConfig.SessionToken,
Expand All @@ -86,7 +85,6 @@ func AutodiscoverBuilder(
if err != nil {
logp.Err("error loading AWS config for aws_elb autodiscover provider: %s", err)
}
awsCfg = awscommon.EnrichAWSConfigWithProxy(config.AWSConfig, awsCfg)
awsCfg.Region = region
clients = append(clients, elasticloadbalancingv2.New(awscommon.EnrichAWSConfigWithEndpoint(
config.AWSConfig.Endpoint, "elasticloadbalancing", region, awsCfg)))
Expand Down
27 changes: 14 additions & 13 deletions x-pack/libbeat/common/aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ type ConfigAWS struct {
ProxyUrl *url.URL `config:"proxy_url"`
}

// InitializeAWSConfig function creates the awssdk.Config object from the provided config
func InitializeAWSConfig(config ConfigAWS) (awssdk.Config, error) {
AWSConfig, _ := GetAWSCredentials(config)
if config.ProxyUrl != nil {
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(config.ProxyUrl),
},
}
AWSConfig.HTTPClient = httpClient
}
return AWSConfig, nil
}

// GetAWSCredentials function gets aws credentials from the config.
// If access keys given, use them as credentials.
// If access keys are not given, then load from AWS config file. If credential_profile_name is not
Expand Down Expand Up @@ -131,19 +145,6 @@ func EnrichAWSConfigWithEndpoint(endpoint string, serviceName string, regionName
return awsConfig
}

// EnrichAWSConfigWithProxy function enables proxy support for AWS
func EnrichAWSConfigWithProxy(config ConfigAWS, awsConfig awssdk.Config) awssdk.Config {
if config.ProxyUrl != nil {
httpClient := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(config.ProxyUrl),
},
}
awsConfig.HTTPClient = httpClient
}
return awsConfig
}

// Validate checks for deprecated config option
func (c ConfigAWS) Validate() error {
if c.AWSPartition != "" {
Expand Down
3 changes: 1 addition & 2 deletions x-pack/metricbeat/module/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,10 @@ func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) {
return nil, err
}

awsConfig, err := awscommon.GetAWSCredentials(config.AWSConfig)
awsConfig, err := awscommon.InitializeAWSConfig(config.AWSConfig)
if err != nil {
return nil, fmt.Errorf("failed to get aws credentials, please check AWS credential in config: %w", err)
}
awsConfig = awscommon.EnrichAWSConfigWithProxy(config.AWSConfig, awsConfig)

_, err = awsConfig.Credentials.Retrieve()
if err != nil {
Expand Down

0 comments on commit a0a5bcb

Please sign in to comment.