|
| 1 | +package docdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go/aws" |
| 7 | + "github.com/aws/aws-sdk-go/aws/awsutil" |
| 8 | + "github.com/aws/aws-sdk-go/aws/endpoints" |
| 9 | + "github.com/aws/aws-sdk-go/aws/request" |
| 10 | +) |
| 11 | + |
| 12 | +func init() { |
| 13 | + ops := []string{ |
| 14 | + opCopyDBClusterSnapshot, |
| 15 | + opCreateDBCluster, |
| 16 | + } |
| 17 | + initRequest = func(r *request.Request) { |
| 18 | + for _, operation := range ops { |
| 19 | + if r.Operation.Name == operation { |
| 20 | + r.Handlers.Build.PushFront(fillPresignedURL) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func fillPresignedURL(r *request.Request) { |
| 27 | + fns := map[string]func(r *request.Request){ |
| 28 | + opCopyDBClusterSnapshot: copyDBClusterSnapshotPresign, |
| 29 | + opCreateDBCluster: createDBClusterPresign, |
| 30 | + } |
| 31 | + if !r.ParamsFilled() { |
| 32 | + return |
| 33 | + } |
| 34 | + if f, ok := fns[r.Operation.Name]; ok { |
| 35 | + f(r) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func copyDBClusterSnapshotPresign(r *request.Request) { |
| 40 | + originParams := r.Params.(*CopyDBClusterSnapshotInput) |
| 41 | + |
| 42 | + if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + originParams.DestinationRegion = r.Config.Region |
| 47 | + // preSignedUrl is not required for instances in the same region. |
| 48 | + if *originParams.SourceRegion == *originParams.DestinationRegion { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + newParams := awsutil.CopyOf(r.Params).(*CopyDBClusterSnapshotInput) |
| 53 | + originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams) |
| 54 | +} |
| 55 | + |
| 56 | +func createDBClusterPresign(r *request.Request) { |
| 57 | + originParams := r.Params.(*CreateDBClusterInput) |
| 58 | + |
| 59 | + if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + originParams.DestinationRegion = r.Config.Region |
| 64 | + // preSignedUrl is not required for instances in the same region. |
| 65 | + if *originParams.SourceRegion == *originParams.DestinationRegion { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + newParams := awsutil.CopyOf(r.Params).(*CreateDBClusterInput) |
| 70 | + originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams) |
| 71 | +} |
| 72 | + |
| 73 | +// presignURL will presign the request by using SoureRegion to sign with. SourceRegion is not |
| 74 | +// sent to the service, and is only used to not have the SDKs parsing ARNs. |
| 75 | +func presignURL(r *request.Request, sourceRegion *string, newParams interface{}) *string { |
| 76 | + cfg := r.Config.Copy(aws.NewConfig(). |
| 77 | + WithEndpoint(""). |
| 78 | + WithRegion(aws.StringValue(sourceRegion))) |
| 79 | + |
| 80 | + clientInfo := r.ClientInfo |
| 81 | + resolved, err := r.Config.EndpointResolver.EndpointFor( |
| 82 | + EndpointsID, aws.StringValue(cfg.Region), |
| 83 | + func(opt *endpoints.Options) { |
| 84 | + opt.DisableSSL = aws.BoolValue(cfg.DisableSSL) |
| 85 | + opt.UseDualStack = aws.BoolValue(cfg.UseDualStack) |
| 86 | + }, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + r.Error = err |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + clientInfo.Endpoint = resolved.URL |
| 94 | + clientInfo.SigningRegion = resolved.SigningRegion |
| 95 | + |
| 96 | + // Presign a request with modified params |
| 97 | + req := request.New(*cfg, clientInfo, r.Handlers, r.Retryer, r.Operation, newParams, r.Data) |
| 98 | + req.Operation.HTTPMethod = "GET" |
| 99 | + uri, err := req.Presign(5 * time.Minute) // 5 minutes should be enough. |
| 100 | + if err != nil { // bubble error back up to original request |
| 101 | + r.Error = err |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + // We have our URL, set it on params |
| 106 | + return &uri |
| 107 | +} |
0 commit comments