github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/common/elasticsearch/aws.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package elasticsearch
    15  
    16  import (
    17  	"fmt"
    18  	awsauth "github.com/smartystreets/go-aws-auth"
    19  	"net/http"
    20  	"os"
    21  )
    22  
    23  type AWSSigningTransport struct {
    24  	HTTPClient  *http.Client
    25  	Credentials awsauth.Credentials
    26  }
    27  
    28  // RoundTrip implementation
    29  func (a AWSSigningTransport) RoundTrip(req *http.Request) (*http.Response, error) {
    30  	return a.HTTPClient.Do(awsauth.Sign4(req, a.Credentials))
    31  }
    32  
    33  func createAWSClient() (*http.Client, error) {
    34  	id := os.Getenv("AWS_ACCESS_KEY_ID")
    35  	if id == "" {
    36  		id = os.Getenv("AWS_ACCESS_KEY")
    37  	}
    38  
    39  	secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
    40  	if secret == "" {
    41  		secret = os.Getenv("AWS_SECRET_KEY")
    42  	}
    43  
    44  	if id == "" || secret == "" {
    45  		return nil, fmt.Errorf("Failed to configure AWS authentication. Both `AWS_ACCESS_KEY_ID` and " +
    46  			"`AWS_SECRET_ACCESS_KEY` environment veriables required")
    47  	}
    48  
    49  	signingTransport := AWSSigningTransport{
    50  		Credentials: awsauth.Credentials{
    51  			AccessKeyID:     id,
    52  			SecretAccessKey: secret,
    53  		},
    54  		HTTPClient: http.DefaultClient,
    55  	}
    56  	return &http.Client{Transport: http.RoundTripper(signingTransport)}, nil
    57  }