Debian Codegraph @

aws/aws.go

raw
1 // Copyright 2018 The Go Cloud Development Kit Authors
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 // https://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
15 // Package aws provides fundamental Wire providers for Amazon Web Services (AWS).
16 package aws // import "gocloud.dev/aws"
17
18 import (
19 "context"
20 "fmt"
21 "net/url"
22 "strconv"
23 "strings"
24
25 "github.com/aws/aws-sdk-go-v2/aws"
26 "github.com/aws/aws-sdk-go-v2/aws/ratelimit"
27 "github.com/aws/aws-sdk-go-v2/aws/retry"
28 "github.com/aws/aws-sdk-go-v2/config"
29 )
30
31 const (
32 requestChecksumCalculationParamKey = "request_checksum_calculation"
33 responseChecksumValidationParamKey = "response_checksum_validation"
34 )
35
36 // parseRequestChecksumCalculation parses request checksum calculation mode values.
37 // Supports AWS SDK documented values: "when_supported", "when_required".
38 func parseRequestChecksumCalculation(value string) (aws.RequestChecksumCalculation, error) {
39 switch strings.ToLower(value) {
40 case "when_supported":
41 return aws.RequestChecksumCalculationWhenSupported, nil
42 case "when_required":
43 return aws.RequestChecksumCalculationWhenRequired, nil
44 default:
45 return aws.RequestChecksumCalculationWhenSupported, fmt.Errorf("invalid value for %q: %q. Valid values are: when_supported, when_required", requestChecksumCalculationParamKey, value)
46 }
47 }
48
49 // parseResponseChecksumValidation parses response checksum validation mode values.
50 // Supports AWS SDK documented values: "when_supported", "when_required".
51 func parseResponseChecksumValidation(value string) (aws.ResponseChecksumValidation, error) {
52 switch strings.ToLower(value) {
53 case "when_supported":
54 return aws.ResponseChecksumValidationWhenSupported, nil
55 case "when_required":
56 return aws.ResponseChecksumValidationWhenRequired, nil
57 default:
58 return aws.ResponseChecksumValidationWhenSupported, fmt.Errorf("invalid value for %q: %q. Valid values are: when_supported, when_required", responseChecksumValidationParamKey, value)
59 }
60 }
61
62 // NewDefaultV2Config returns a aws.Config for AWS SDK v2, using the default options.
63 func NewDefaultV2Config(ctx context.Context) (aws.Config, error) {
64 return config.LoadDefaultConfig(ctx)
65 }
66
67 // V2ConfigFromURLParams returns an aws.Config for AWS SDK v2 initialized based on the URL
68 // parameters in q. It is intended to be used by URLOpeners for AWS services if
69 // UseV2 returns true.
70 //
71 // https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws#Config
72 //
73 // It returns an error if q contains any unknown query parameters; callers
74 // should remove any query parameters they know about from q before calling
75 // V2ConfigFromURLParams.
76 //
77 // The following query options are supported:
78 // - region: The AWS region for requests; sets WithRegion.
79 // - anonymous: A value of "true" forces use of anonymous credentials.
80 // - profile: The shared config profile to use; sets SharedConfigProfile.
81 // - endpoint: The AWS service endpoint to send HTTP request.
82 // - hostname_immutable: Make the hostname immutable, only works if endpoint is also set.
83 // - dualstack: A value of "true" enables dual stack (IPv4 and IPv6) endpoints.
84 // - fips: A value of "true" enables the use of FIPS endpoints.
85 // - rate_limiter_capacity: A integer value configures the capacity of a token bucket used
86 // in client-side rate limits. If no value is set, the client-side rate limiting is disabled.
87 // See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/retries-timeouts/#client-side-rate-limiting.
88 // - request_checksum_calculation: Request checksum calculation mode (when_supported, when_required)
89 // - response_checksum_validation: Response checksum validation mode (when_supported, when_required)
90 func V2ConfigFromURLParams(ctx context.Context, q url.Values) (aws.Config, error) {
91 var endpoint string
92 var hostnameImmutable bool
93 var rateLimitCapacity int64
94 var opts []func(*config.LoadOptions) error
95 for param, values := range q {
96 value := values[0]
97 switch param {
98 case "hostname_immutable":
99 var err error
100 hostnameImmutable, err = strconv.ParseBool(value)
101 if err != nil {
102 return aws.Config{}, fmt.Errorf("invalid value for hostname_immutable: %w", err)
103 }
104 case "region":
105 opts = append(opts, config.WithRegion(value))
106 case "endpoint":
107 endpoint = value
108 case "profile":
109 opts = append(opts, config.WithSharedConfigProfile(value))
110 case "dualstack":
111 dualStack, err := strconv.ParseBool(value)
112 if err != nil {
113 return aws.Config{}, fmt.Errorf("invalid value for dualstack: %w", err)
114 }
115 if dualStack {
116 opts = append(opts, config.WithUseDualStackEndpoint(aws.DualStackEndpointStateEnabled))
117 }
118 case "fips":
119 fips, err := strconv.ParseBool(value)
120 if err != nil {
121 return aws.Config{}, fmt.Errorf("invalid value for fips: %w", err)
122 }
123 if fips {
124 opts = append(opts, config.WithUseFIPSEndpoint(aws.FIPSEndpointStateEnabled))
125 }
126 case "rate_limiter_capacity":
127 var err error
128 rateLimitCapacity, err = strconv.ParseInt(value, 10, 32)
129 if err != nil {
130 return aws.Config{}, fmt.Errorf("invalid value for capacity: %w", err)
131 }
132 case "anonymous":
133 anon, err := strconv.ParseBool(value)
134 if err != nil {
135 return aws.Config{}, fmt.Errorf("invalid value for anonymous: %w", err)
136 }
137 if anon {
138 opts = append(opts, config.WithCredentialsProvider(aws.AnonymousCredentials{}))
139 }
140 case requestChecksumCalculationParamKey:
141 value, err := parseRequestChecksumCalculation(value)
142 if err != nil {
143 return aws.Config{}, err
144 }
145
146 opts = append(opts, config.WithRequestChecksumCalculation(value))
147 case responseChecksumValidationParamKey:
148 value, err := parseResponseChecksumValidation(value)
149 if err != nil {
150 return aws.Config{}, err
151 }
152
153 opts = append(opts, config.WithResponseChecksumValidation(value))
154 case "awssdk":
155 // ignore, should be handled before this
156 default:
157 return aws.Config{}, fmt.Errorf("unknown query parameter %q", param)
158 }
159 }
160 if endpoint != "" {
161 customResolver := aws.EndpointResolverWithOptionsFunc(
162 func(service, region string, options ...any) (aws.Endpoint, error) {
163 return aws.Endpoint{
164 PartitionID: "aws",
165 URL: endpoint,
166 SigningRegion: region,
167 HostnameImmutable: hostnameImmutable,
168 }, nil
169 })
170 opts = append(opts, config.WithEndpointResolverWithOptions(customResolver))
171 }
172
173 var rateLimiter retry.RateLimiter
174 rateLimiter = ratelimit.None
175 if rateLimitCapacity > 0 {
176 rateLimiter = ratelimit.NewTokenRateLimit(uint(rateLimitCapacity))
177 }
178 opts = append(opts, config.WithRetryer(func() aws.Retryer {
179 return retry.NewStandard(func(so *retry.StandardOptions) {
180 so.RateLimiter = rateLimiter
181 })
182 }))
183
184 return config.LoadDefaultConfig(ctx, opts...)
185 }