From f81fae24144a8e3ab41cd44de7e3819c0c783888 Mon Sep 17 00:00:00 2001 From: Levi Harrison Date: Tue, 22 Nov 2022 10:09:14 -0500 Subject: [PATCH] Add common HTTP client to AWS SDs (#11611) * Common client in EC2 and Lightsail Signed-off-by: Levi Harrison * Azure -> AWS Signed-off-by: Levi Harrison Signed-off-by: Levi Harrison --- config/config_test.go | 14 +++--- discovery/aws/ec2.go | 13 ++++- discovery/aws/lightsail.go | 13 ++++- docs/configuration/configuration.md | 78 +++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 10 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index ddf65a068..9ee8fe1a0 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -586,6 +586,7 @@ var expectedConf = &Config{ Values: []string{"web", "db"}, }, }, + HTTPClientConfig: config.DefaultHTTPClientConfig, }, }, }, @@ -602,12 +603,13 @@ var expectedConf = &Config{ ServiceDiscoveryConfigs: discovery.Configs{ &aws.LightsailSDConfig{ - Region: "us-east-1", - AccessKey: "access", - SecretKey: "mysecret", - Profile: "profile", - RefreshInterval: model.Duration(60 * time.Second), - Port: 80, + Region: "us-east-1", + AccessKey: "access", + SecretKey: "mysecret", + Profile: "profile", + RefreshInterval: model.Duration(60 * time.Second), + Port: 80, + HTTPClientConfig: config.DefaultHTTPClientConfig, }, }, }, diff --git a/discovery/aws/ec2.go b/discovery/aws/ec2.go index 7519f58da..ca9921159 100644 --- a/discovery/aws/ec2.go +++ b/discovery/aws/ec2.go @@ -66,8 +66,9 @@ const ( // DefaultEC2SDConfig is the default EC2 SD configuration. var DefaultEC2SDConfig = EC2SDConfig{ - Port: 80, - RefreshInterval: model.Duration(60 * time.Second), + Port: 80, + RefreshInterval: model.Duration(60 * time.Second), + HTTPClientConfig: config.DefaultHTTPClientConfig, } func init() { @@ -91,6 +92,8 @@ type EC2SDConfig struct { RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` Port int `yaml:"port"` Filters []*EC2Filter `yaml:"filters"` + + HTTPClientConfig config.HTTPClientConfig `yaml:",inline"` } // Name returns the name of the EC2 Config. @@ -171,11 +174,17 @@ func (d *EC2Discovery) ec2Client(ctx context.Context) (*ec2.EC2, error) { creds = nil } + client, err := config.NewClientFromConfig(d.cfg.HTTPClientConfig, "ec2_sd") + if err != nil { + return nil, err + } + sess, err := session.NewSessionWithOptions(session.Options{ Config: aws.Config{ Endpoint: &d.cfg.Endpoint, Region: &d.cfg.Region, Credentials: creds, + HTTPClient: client, }, Profile: d.cfg.Profile, }) diff --git a/discovery/aws/lightsail.go b/discovery/aws/lightsail.go index 016d78a67..e671769ca 100644 --- a/discovery/aws/lightsail.go +++ b/discovery/aws/lightsail.go @@ -56,8 +56,9 @@ const ( // DefaultLightsailSDConfig is the default Lightsail SD configuration. var DefaultLightsailSDConfig = LightsailSDConfig{ - Port: 80, - RefreshInterval: model.Duration(60 * time.Second), + Port: 80, + RefreshInterval: model.Duration(60 * time.Second), + HTTPClientConfig: config.DefaultHTTPClientConfig, } func init() { @@ -74,6 +75,8 @@ type LightsailSDConfig struct { RoleARN string `yaml:"role_arn,omitempty"` RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` Port int `yaml:"port"` + + HTTPClientConfig config.HTTPClientConfig `yaml:",inline"` } // Name returns the name of the Lightsail Config. @@ -144,11 +147,17 @@ func (d *LightsailDiscovery) lightsailClient() (*lightsail.Lightsail, error) { creds = nil } + client, err := config.NewClientFromConfig(d.cfg.HTTPClientConfig, "lightsail_sd") + if err != nil { + return nil, err + } + sess, err := session.NewSessionWithOptions(session.Options{ Config: aws.Config{ Endpoint: &d.cfg.Endpoint, Region: &d.cfg.Region, Credentials: creds, + HTTPClient: client, }, Profile: d.cfg.Profile, }) diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 7345aca4a..c6edcf233 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -1064,6 +1064,45 @@ See below for the configuration options for EC2 discovery: filters: [ - name: values: , [...] ] + +# Authentication information used to authenticate to the EC2 API. +# Note that `basic_auth`, `authorization` and `oauth2` options are +# mutually exclusive. +# `password` and `password_file` are mutually exclusive. + +# Optional HTTP basic authentication information, currently not supported by AWS. +basic_auth: + [ username: ] + [ password: ] + [ password_file: ] + +# Optional `Authorization` header configuration, currently not supported by AWS. +authorization: + # Sets the authentication type. + [ type: | default: Bearer ] + # Sets the credentials. It is mutually exclusive with + # `credentials_file`. + [ credentials: ] + # Sets the credentials to the credentials read from the configured file. + # It is mutuall exclusive with `credentials`. + [ credentials_file: ] + +# Optional OAuth 2.0 configuration, currently not supported by AWS. +oauth2: + [ ] + +# Optional proxy URL. +[ proxy_url: ] + +# Configure whether HTTP requests follow HTTP 3xx redirects. +[ follow_redirects: | default = true ] + +# Whether to enable HTTP2. +[ enable_http2: | default: true ] + +# TLS configuration. +tls_config: + [ ] ``` The [relabeling phase](#relabel_config) is the preferred and more powerful @@ -2066,6 +2105,45 @@ See below for the configuration options for Lightsail discovery: # The port to scrape metrics from. If using the public IP address, this must # instead be specified in the relabeling rule. [ port: | default = 80 ] + +# Authentication information used to authenticate to the Lightsail API. +# Note that `basic_auth`, `authorization` and `oauth2` options are +# mutually exclusive. +# `password` and `password_file` are mutually exclusive. + +# Optional HTTP basic authentication information, currently not supported by AWS. +basic_auth: + [ username: ] + [ password: ] + [ password_file: ] + +# Optional `Authorization` header configuration, currently not supported by AWS. +authorization: + # Sets the authentication type. + [ type: | default: Bearer ] + # Sets the credentials. It is mutually exclusive with + # `credentials_file`. + [ credentials: ] + # Sets the credentials to the credentials read from the configured file. + # It is mutuall exclusive with `credentials`. + [ credentials_file: ] + +# Optional OAuth 2.0 configuration, currently not supported by AWS. +oauth2: + [ ] + +# Optional proxy URL. +[ proxy_url: ] + +# Configure whether HTTP requests follow HTTP 3xx redirects. +[ follow_redirects: | default = true ] + +# Whether to enable HTTP2. +[ enable_http2: | default: true ] + +# TLS configuration. +tls_config: + [ ] ``` ### ``