resolver.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package auth
  13. import (
  14. "context"
  15. "fmt"
  16. "net/url"
  17. "os"
  18. "github.com/aws/aws-sdk-go-v2/service/sts"
  19. smithyendpoints "github.com/aws/smithy-go/endpoints"
  20. )
  21. const (
  22. STSEndpointEnv = "AWS_STS_ENDPOINT"
  23. )
  24. type customEndpointResolver struct{}
  25. // ResolveEndpoint returns a ResolverFunc with
  26. // customizable endpoints.
  27. // should this reside somewhere else since it's specific to sts?
  28. func (c customEndpointResolver) ResolveEndpoint(ctx context.Context, params sts.EndpointParameters) (smithyendpoints.Endpoint, error) {
  29. endpoint := smithyendpoints.Endpoint{}
  30. if v := os.Getenv(STSEndpointEnv); v != "" {
  31. url, err := url.Parse(v)
  32. if err != nil {
  33. return endpoint, fmt.Errorf("failed to parse sts endpoint %s: %w", v, err)
  34. }
  35. endpoint.URI = *url
  36. return endpoint, nil
  37. }
  38. defaultResolver := sts.NewDefaultEndpointResolverV2()
  39. return defaultResolver.ResolveEndpoint(ctx, params)
  40. }