|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package endpoints contains aws endpoint related utilities. |
| 18 | +package endpointsv2 |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "errors" |
| 23 | + "net/url" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 27 | + smithyendpoints "github.com/aws/smithy-go/endpoints" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + errServiceEndpointFormat = errors.New("must be formatted as ${ServiceID}=${URL}") |
| 32 | + errServiceEndpointSigningRegion = errors.New("must be formatted as ${SigningRegion}:${ServiceID1}=${URL1},${ServiceID2}=${URL2...}") |
| 33 | + errServiceEndpointURL = errors.New("must use a valid URL as a service-endpoint") |
| 34 | + errServiceEndpointDuplicateServiceID = errors.New("same serviceID defined twice for signing region") |
| 35 | + // ServiceEndpointsMap Can be made private after Go SDK V2 migration |
| 36 | + ServiceEndpointsMap map[string]ServiceEndpoint |
| 37 | +) |
| 38 | + |
| 39 | +// ServiceEndpointsMap Can be made private after Go SDK V2 migration |
| 40 | +// serviceEndpointV2 defines a tuple containing AWS Service resolution information for SDK V2. |
| 41 | +type ServiceEndpoint struct { |
| 42 | + ServiceID string |
| 43 | + URL string |
| 44 | + SigningRegion string |
| 45 | +} |
| 46 | + |
| 47 | +// ParseFlag parses the command line flag of service endponts in the format ${SigningRegion1}:${ServiceID1}=${URL1},${ServiceID2}=${URL2}...;${SigningRegion2}... |
| 48 | +// returning a set of ServiceEndpoints. |
| 49 | +func ParseFlag(serviceEndpoints string) error { |
| 50 | + if serviceEndpoints == "" { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + signingRegionConfigs := strings.Split(serviceEndpoints, ";") |
| 55 | + for _, regionConfig := range signingRegionConfigs { |
| 56 | + components := strings.SplitN(regionConfig, ":", 2) |
| 57 | + if len(components) != 2 { |
| 58 | + return errServiceEndpointSigningRegion |
| 59 | + } |
| 60 | + signingRegion := components[0] |
| 61 | + servicePairs := strings.Split(components[1], ",") |
| 62 | + for _, servicePair := range servicePairs { |
| 63 | + kv := strings.Split(servicePair, "=") |
| 64 | + if len(kv) != 2 { |
| 65 | + return errServiceEndpointFormat |
| 66 | + } |
| 67 | + var serviceID = kv[0] |
| 68 | + if _, ok := ServiceEndpointsMap[serviceID]; ok { |
| 69 | + return errServiceEndpointDuplicateServiceID |
| 70 | + } |
| 71 | + |
| 72 | + URL, err := url.ParseRequestURI(kv[1]) |
| 73 | + if err != nil { |
| 74 | + return errServiceEndpointURL |
| 75 | + } |
| 76 | + ServiceEndpointsMap[serviceID] = ServiceEndpoint{ |
| 77 | + ServiceID: serviceID, |
| 78 | + URL: URL.String(), |
| 79 | + SigningRegion: signingRegion, |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// Custom EndpointResolverV2 ResolveEndpoint handlers |
| 87 | + |
| 88 | +// MultiServiceEndpointResolver imeplements EndpointResolverV2 interface for services |
| 89 | +type MultiServiceEndpointResolver struct { |
| 90 | + endpoints map[string]ServiceEndpoint |
| 91 | +} |
| 92 | + |
| 93 | +// NewMultiServiceEndpointResolver returns new MultiServiceEndpointResolver |
| 94 | +func NewMultiServiceEndpointResolver() *MultiServiceEndpointResolver { |
| 95 | + return &MultiServiceEndpointResolver{ |
| 96 | + endpoints: ServiceEndpointsMap, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// S3EndpointResolver imeplements EndpointResolverV2 interface for S3 |
| 101 | +type S3EndpointResolver struct { |
| 102 | + *MultiServiceEndpointResolver |
| 103 | +} |
| 104 | + |
| 105 | +// ResolveEndpoint for S3 |
| 106 | +func (s *S3EndpointResolver) ResolveEndpoint(ctx context.Context, params s3.EndpointParameters) (smithyendpoints.Endpoint, error) { |
| 107 | + // If custom endpoint not found, return default endpoint for the service |
| 108 | + if _, ok := s.endpoints[s3.ServiceID]; !ok { |
| 109 | + return s3.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params) |
| 110 | + } |
| 111 | + |
| 112 | + endpoint := ServiceEndpointsMap[s3.ServiceID] |
| 113 | + params.Endpoint = &endpoint.URL |
| 114 | + params.Region = &endpoint.SigningRegion |
| 115 | + return s3.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params) |
| 116 | +} |
0 commit comments