forked from open-telemetry/opentelemetry-collector-contrib
/
conn.go
385 lines (343 loc) · 13.4 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright The OpenTelemetry Authors
// Portions of this file Copyright 2018-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package awsutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net/http"
"net/url"
"os"
"time"
override "github.com/amazon-contributing/opentelemetry-collector-contrib/override/aws"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
"go.uber.org/zap"
"golang.org/x/net/http2"
)
type ConnAttr interface {
newAWSSession(logger *zap.Logger, roleArn string, region string, profile string, sharedCredentialsFile []string) (*session.Session, error)
getEC2Region(s *session.Session) (string, error)
}
// Conn implements connAttr interface.
type Conn struct{}
func (c *Conn) getEC2Region(s *session.Session) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), override.TimePerCall)
defer cancel()
region, err := ec2metadata.New(s, &aws.Config{
Retryer: override.IMDSRetryer,
EC2MetadataEnableFallback: aws.Bool(false),
}).RegionWithContext(ctx)
if err == nil {
return region, err
}
ctxFallbackEnable, cancelFallbackEnable := context.WithTimeout(context.Background(), override.TimePerCall)
defer cancelFallbackEnable()
return ec2metadata.New(s, &aws.Config{}).RegionWithContext(ctxFallbackEnable)
}
// AWS STS endpoint constants
const (
STSEndpointPrefix = "https://sts."
STSEndpointSuffix = ".amazonaws.com"
STSAwsCnPartitionIDSuffix = ".amazonaws.com.cn" // AWS China partition.
)
// newHTTPClient returns new HTTP client instance with provided configuration.
func newHTTPClient(logger *zap.Logger, maxIdle int, requestTimeout int, noVerify bool,
proxyAddress string, certificateFilePath string) (*http.Client, error) {
logger.Debug("Using proxy address: ",
zap.String("proxyAddr", proxyAddress),
)
rootCA, certPoolError := loadCertPool(certificateFilePath)
if certificateFilePath != "" && certPoolError != nil {
logger.Warn("could not create root ca from", zap.String("file", certificateFilePath), zap.Error(certPoolError))
}
tlsConfig := &tls.Config{
RootCAs: rootCA,
InsecureSkipVerify: noVerify,
}
finalProxyAddress := getProxyAddress(proxyAddress)
proxyURL, err := getProxyURL(finalProxyAddress)
if err != nil {
logger.Error("unable to obtain proxy URL", zap.Error(err))
return nil, err
}
transport := &http.Transport{
MaxIdleConnsPerHost: maxIdle,
TLSClientConfig: tlsConfig,
Proxy: http.ProxyURL(proxyURL),
}
// is not enabled by default as we configure TLSClientConfig for supporting SSL to data plane.
// http2.ConfigureTransport will setup transport layer to use HTTP2
if err = http2.ConfigureTransport(transport); err != nil {
logger.Error("unable to configure http2 transport", zap.Error(err))
return nil, err
}
http := &http.Client{
Transport: transport,
Timeout: time.Second * time.Duration(requestTimeout),
}
return http, err
}
func loadCertPool(bundleFile string) (*x509.CertPool, error) {
bundleBytes, err := os.ReadFile(bundleFile)
if err != nil {
return nil, err
}
p := x509.NewCertPool()
if !p.AppendCertsFromPEM(bundleBytes) {
return nil, errors.New("unable to append certs")
}
return p, nil
}
func getProxyAddress(proxyAddress string) string {
var finalProxyAddress string
switch {
case proxyAddress != "":
finalProxyAddress = proxyAddress
case proxyAddress == "" && os.Getenv("HTTPS_PROXY") != "":
finalProxyAddress = os.Getenv("HTTPS_PROXY")
default:
finalProxyAddress = ""
}
return finalProxyAddress
}
func getProxyURL(finalProxyAddress string) (*url.URL, error) {
var proxyURL *url.URL
var err error
if finalProxyAddress != "" {
proxyURL, err = url.Parse(finalProxyAddress)
} else {
proxyURL = nil
err = nil
}
return proxyURL, err
}
// GetAWSConfigSession returns AWS config and session instances.
func GetAWSConfigSession(logger *zap.Logger, cn ConnAttr, cfg *AWSSessionSettings) (*aws.Config, *session.Session, error) {
var s *session.Session
var err error
var awsRegion string
http, err := newHTTPClient(logger, cfg.NumberOfWorkers, cfg.RequestTimeoutSeconds, cfg.NoVerifySSL, cfg.ProxyAddress, cfg.CertificateFilePath)
if err != nil {
logger.Error("unable to obtain proxy URL", zap.Error(err))
return nil, nil, err
}
regionEnv := os.Getenv("AWS_REGION")
switch {
case cfg.Region == "" && regionEnv != "":
awsRegion = regionEnv
logger.Debug("Fetch region from environment variables", zap.String("region", awsRegion))
case cfg.Region != "":
awsRegion = cfg.Region
logger.Debug("Fetch region from commandline/config file", zap.String("region", awsRegion))
case !cfg.NoVerifySSL:
var es *session.Session
es, err = GetDefaultSession(logger, session.Options{})
if err != nil {
logger.Error("Unable to retrieve default session", zap.Error(err))
} else {
awsRegion, err = cn.getEC2Region(es)
if err != nil {
logger.Error("Unable to retrieve the region from the EC2 instance", zap.Error(err))
} else {
logger.Debug("Fetch region from ec2 metadata", zap.String("region", awsRegion))
}
}
}
if awsRegion == "" {
msg := "Cannot fetch region variable from config file, environment variables and ec2 metadata."
logger.Error(msg)
return nil, nil, awserr.New("NoAwsRegion", msg, nil)
}
s, err = cn.newAWSSession(logger, cfg.RoleARN, awsRegion, cfg.Profile, cfg.SharedCredentialsFile)
if err != nil {
return nil, nil, err
}
config := &aws.Config{
Region: aws.String(awsRegion),
DisableParamValidation: aws.Bool(true),
MaxRetries: aws.Int(cfg.MaxRetries),
Endpoint: aws.String(cfg.Endpoint),
HTTPClient: http,
CredentialsChainVerboseErrors: aws.Bool(true),
}
// do not overwrite for sts assume role
if cfg.RoleARN == "" && len(override.GetCredentialsChainOverride().GetCredentialsChain()) > 0 {
config.Credentials = credentials.NewCredentials(&credentials.ChainProvider{
Providers: customCredentialProvider(cfg, config),
})
}
return config, s, nil
}
func customCredentialProvider(cfg *AWSSessionSettings, config *aws.Config) []credentials.Provider {
defaultCredProviders := defaults.CredProviders(config, defaults.Handlers())
overrideCredProviders := override.GetCredentialsChainOverride().GetCredentialsChain()
credProviders := make([]credentials.Provider, 0)
// if is for differently configured shared creds file location
// else if is for diff profile but no change in creds file ex run in containers
if cfg.SharedCredentialsFile != nil && len(cfg.SharedCredentialsFile) > 0 {
for _, file := range cfg.SharedCredentialsFile {
credProviders = append(credProviders, &credentials.SharedCredentialsProvider{Filename: file, Profile: cfg.Profile})
}
} else if cfg.Profile != "" {
credProviders = append(credProviders, &credentials.SharedCredentialsProvider{Filename: "", Profile: cfg.Profile})
}
credProviders = append(credProviders, defaultCredProviders...)
for _, provider := range overrideCredProviders {
for _, file := range cfg.SharedCredentialsFile {
credProviders = append(credProviders, provider(file))
}
}
return credProviders
}
// ProxyServerTransport configures HTTP transport for TCP Proxy Server.
func ProxyServerTransport(logger *zap.Logger, config *AWSSessionSettings) (*http.Transport, error) {
tls := &tls.Config{
InsecureSkipVerify: config.NoVerifySSL,
}
proxyAddr := getProxyAddress(config.ProxyAddress)
proxyURL, err := getProxyURL(proxyAddr)
if err != nil {
logger.Error("unable to obtain proxy URL", zap.Error(err))
return nil, err
}
// Connection timeout in seconds
idleConnTimeout := time.Duration(config.RequestTimeoutSeconds) * time.Second
transport := &http.Transport{
MaxIdleConns: config.NumberOfWorkers,
MaxIdleConnsPerHost: config.NumberOfWorkers,
IdleConnTimeout: idleConnTimeout,
Proxy: http.ProxyURL(proxyURL),
TLSClientConfig: tls,
// If not disabled the transport will add a gzip encoding header
// to requests with no `accept-encoding` header value. The header
// is added after we sign the request which invalidates the
// signature.
DisableCompression: true,
}
return transport, nil
}
func (c *Conn) newAWSSession(logger *zap.Logger, roleArn string, region string, profile string, sharedCredentialsFile []string) (*session.Session, error) {
var s *session.Session
var err error
if roleArn == "" {
// if an empty or nil list of sharedCredentialsFile is passed use the sdk default
if sharedCredentialsFile == nil || len(sharedCredentialsFile) < 1 {
sharedCredentialsFile = nil
}
options := session.Options{
Profile: profile,
SharedConfigFiles: sharedCredentialsFile,
}
s, err = GetDefaultSession(logger, options)
if err != nil {
return s, err
}
} else {
stsCreds, _ := getSTSCreds(logger, region, roleArn)
s, err = session.NewSession(&aws.Config{
Credentials: stsCreds,
})
if err != nil {
logger.Error("Error in creating session object : ", zap.Error(err))
return s, err
}
}
return s, nil
}
// getSTSCreds gets STS credentials from regional endpoint. ErrCodeRegionDisabledException is received if the
// STS regional endpoint is disabled. In this case STS credentials are fetched from STS primary regional endpoint
// in the respective AWS partition.
func getSTSCreds(logger *zap.Logger, region string, roleArn string) (*credentials.Credentials, error) {
t, err := GetDefaultSession(logger, session.Options{})
if err != nil {
return nil, err
}
stsCred := getSTSCredsFromRegionEndpoint(logger, t, region, roleArn)
// Make explicit call to fetch credentials.
_, err = stsCred.Get()
if err != nil {
var awsErr awserr.Error
if errors.As(err, &awsErr) {
err = nil
if awsErr.Code() == sts.ErrCodeRegionDisabledException {
logger.Error("Region ", zap.String("region", region), zap.Error(awsErr))
stsCred = getSTSCredsFromPrimaryRegionEndpoint(logger, t, roleArn, region)
}
}
}
return stsCred, err
}
// getSTSCredsFromRegionEndpoint fetches STS credentials for provided roleARN from regional endpoint.
// AWS STS recommends that you provide both the Region and endpoint when you make calls to a Regional endpoint.
// Reference: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_enable-regions.html#id_credentials_temp_enable-regions_writing_code
func getSTSCredsFromRegionEndpoint(logger *zap.Logger, sess *session.Session, region string,
roleArn string) *credentials.Credentials {
regionalEndpoint := getSTSRegionalEndpoint(region)
// if regionalEndpoint is "", the STS endpoint is Global endpoint for classic regions except ap-east-1 - (HKG)
// for other opt-in regions, region value will create STS regional endpoint.
// This will be only in the case, if provided region is not present in aws_regions.go
c := &aws.Config{Region: aws.String(region), Endpoint: ®ionalEndpoint}
st := sts.New(sess, c)
logger.Info("STS Endpoint ", zap.String("endpoint", st.Endpoint))
return stscreds.NewCredentialsWithClient(st, roleArn)
}
// getSTSCredsFromPrimaryRegionEndpoint fetches STS credentials for provided roleARN from primary region endpoint in
// the respective partition.
func getSTSCredsFromPrimaryRegionEndpoint(logger *zap.Logger, t *session.Session, roleArn string,
region string) *credentials.Credentials {
logger.Info("Credentials for provided RoleARN being fetched from STS primary region endpoint.")
partitionID := getPartition(region)
switch partitionID {
case endpoints.AwsPartitionID:
return getSTSCredsFromRegionEndpoint(logger, t, endpoints.UsEast1RegionID, roleArn)
case endpoints.AwsCnPartitionID:
return getSTSCredsFromRegionEndpoint(logger, t, endpoints.CnNorth1RegionID, roleArn)
case endpoints.AwsUsGovPartitionID:
return getSTSCredsFromRegionEndpoint(logger, t, endpoints.UsGovWest1RegionID, roleArn)
}
return nil
}
func getSTSRegionalEndpoint(r string) string {
p := getPartition(r)
var e string
if p == endpoints.AwsPartitionID || p == endpoints.AwsUsGovPartitionID {
e = STSEndpointPrefix + r + STSEndpointSuffix
} else if p == endpoints.AwsCnPartitionID {
e = STSEndpointPrefix + r + STSAwsCnPartitionIDSuffix
}
return e
}
func GetDefaultSession(logger *zap.Logger, options session.Options) (*session.Session, error) {
result, serr := session.NewSessionWithOptions(options)
if serr != nil {
logger.Error("Error in creating session object ", zap.Error(serr))
return result, serr
}
return result, nil
}
// getPartition return AWS Partition for the provided region.
func getPartition(region string) string {
p, _ := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region)
return p.ID()
}