resolver.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package auth
  14. import (
  15. "context"
  16. "fmt"
  17. "net/url"
  18. "os"
  19. "github.com/aws/aws-sdk-go-v2/service/sts"
  20. smithyendpoints "github.com/aws/smithy-go/endpoints"
  21. )
  22. const (
  23. // STSEndpointEnv is the environment variable name for the AWS STS endpoint URL.
  24. STSEndpointEnv = "AWS_STS_ENDPOINT"
  25. )
  26. type customEndpointResolver struct{}
  27. // ResolveEndpoint returns a ResolverFunc with
  28. // customizable endpoints.
  29. // should this reside somewhere else since it's specific to sts?
  30. func (c customEndpointResolver) ResolveEndpoint(ctx context.Context, params sts.EndpointParameters) (smithyendpoints.Endpoint, error) {
  31. endpoint := smithyendpoints.Endpoint{}
  32. if v := os.Getenv(STSEndpointEnv); v != "" {
  33. url, err := url.Parse(v)
  34. if err != nil {
  35. return endpoint, fmt.Errorf("failed to parse sts endpoint %s: %w", v, err)
  36. }
  37. endpoint.URI = *url
  38. return endpoint, nil
  39. }
  40. defaultResolver := sts.NewDefaultEndpointResolverV2()
  41. return defaultResolver.ResolveEndpoint(ctx, params)
  42. }