go.temporal.io/server@v1.23.0/common/persistence/visibility/store/elasticsearch/client/aws.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package client
    26  
    27  import (
    28  	"fmt"
    29  	"net/http"
    30  	"os"
    31  	"strings"
    32  
    33  	"github.com/aws/aws-sdk-go/aws"
    34  	"github.com/aws/aws-sdk-go/aws/credentials"
    35  	"github.com/aws/aws-sdk-go/aws/session"
    36  	elasticaws "github.com/olivere/elastic/v7/aws/v4"
    37  )
    38  
    39  func NewAwsHttpClient(config ESAWSRequestSigningConfig) (*http.Client, error) {
    40  	if !config.Enabled {
    41  		return nil, nil
    42  	}
    43  
    44  	if config.Region == "" {
    45  		config.Region = os.Getenv("AWS_REGION")
    46  		if config.Region == "" {
    47  			return nil, fmt.Errorf("unable to resolve AWS region for obtaining AWS Elastic signing credentials")
    48  		}
    49  	}
    50  
    51  	var awsCredentials *credentials.Credentials
    52  
    53  	switch strings.ToLower(config.CredentialProvider) {
    54  	case "static":
    55  		awsCredentials = credentials.NewStaticCredentials(
    56  			config.Static.AccessKeyID,
    57  			config.Static.SecretAccessKey,
    58  			config.Static.Token,
    59  		)
    60  	case "environment":
    61  		awsCredentials = credentials.NewEnvCredentials()
    62  	case "aws-sdk-default":
    63  		awsSession, err := session.NewSession(&aws.Config{
    64  			Region: &config.Region,
    65  		})
    66  
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  
    71  		awsCredentials = awsSession.Config.Credentials
    72  	default:
    73  		return nil, fmt.Errorf("unknown AWS credential provider specified: %+v. Accepted options are 'static', 'environment' or 'session'", config.CredentialProvider)
    74  	}
    75  
    76  	return elasticaws.NewV4SigningClient(awsCredentials, config.Region), nil
    77  }