File: customizations.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (169 lines) | stat: -rw-r--r-- 5,550 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package rds

import (
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awsutil"
	"github.com/aws/aws-sdk-go/aws/endpoints"
	"github.com/aws/aws-sdk-go/aws/request"
)

func init() {
	ops := []string{
		opCopyDBSnapshot,
		opCreateDBInstanceReadReplica,
		opCopyDBClusterSnapshot,
		opCreateDBCluster,
		opStartDBInstanceAutomatedBackupsReplication,
	}
	initRequest = func(r *request.Request) {
		for _, operation := range ops {
			if r.Operation.Name == operation {
				r.Handlers.Build.PushFront(fillPresignedURL)
			}
		}
	}
}

func fillPresignedURL(r *request.Request) {
	fns := map[string]func(r *request.Request){
		opCopyDBSnapshot:                             copyDBSnapshotPresign,
		opCreateDBInstanceReadReplica:                createDBInstanceReadReplicaPresign,
		opCopyDBClusterSnapshot:                      copyDBClusterSnapshotPresign,
		opCreateDBCluster:                            createDBClusterPresign,
		opStartDBInstanceAutomatedBackupsReplication: startDBInstanceAutomatedBackupsReplicationPresign,
	}
	if !r.ParamsFilled() {
		return
	}
	if f, ok := fns[r.Operation.Name]; ok {
		f(r)
	}
}

func copyDBSnapshotPresign(r *request.Request) {
	originParams := r.Params.(*CopyDBSnapshotInput)

	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
		return
	}

	originParams.DestinationRegion = r.Config.Region

	// preSignedUrl is not required for instances in the same region.
	if *originParams.SourceRegion == *originParams.DestinationRegion {
		return
	}

	newParams := awsutil.CopyOf(r.Params).(*CopyDBSnapshotInput)
	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
}

func createDBInstanceReadReplicaPresign(r *request.Request) {
	originParams := r.Params.(*CreateDBInstanceReadReplicaInput)

	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
		return
	}

	originParams.DestinationRegion = r.Config.Region
	// preSignedUrl is not required for instances in the same region.
	if *originParams.SourceRegion == *originParams.DestinationRegion {
		return
	}

	newParams := awsutil.CopyOf(r.Params).(*CreateDBInstanceReadReplicaInput)
	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
}

func copyDBClusterSnapshotPresign(r *request.Request) {
	originParams := r.Params.(*CopyDBClusterSnapshotInput)

	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
		return
	}

	originParams.DestinationRegion = r.Config.Region
	// preSignedUrl is not required for instances in the same region.
	if *originParams.SourceRegion == *originParams.DestinationRegion {
		return
	}

	newParams := awsutil.CopyOf(r.Params).(*CopyDBClusterSnapshotInput)
	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
}

func createDBClusterPresign(r *request.Request) {
	originParams := r.Params.(*CreateDBClusterInput)

	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
		return
	}

	originParams.DestinationRegion = r.Config.Region
	// preSignedUrl is not required for instances in the same region.
	if *originParams.SourceRegion == *originParams.DestinationRegion {
		return
	}

	newParams := awsutil.CopyOf(r.Params).(*CreateDBClusterInput)
	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
}

func startDBInstanceAutomatedBackupsReplicationPresign(r *request.Request) {
	originParams := r.Params.(*StartDBInstanceAutomatedBackupsReplicationInput)

	if originParams.SourceRegion == nil || originParams.PreSignedUrl != nil || originParams.DestinationRegion != nil {
		return
	}

	originParams.DestinationRegion = r.Config.Region
	// preSignedUrl is not required for instances in the same region.
	if *originParams.SourceRegion == *originParams.DestinationRegion {
		return
	}

	newParams := awsutil.CopyOf(r.Params).(*StartDBInstanceAutomatedBackupsReplicationInput)
	originParams.PreSignedUrl = presignURL(r, originParams.SourceRegion, newParams)
}

// presignURL will presign the request by using SoureRegion to sign with. SourceRegion is not
// sent to the service, and is only used to not have the SDKs parsing ARNs.
func presignURL(r *request.Request, sourceRegion *string, newParams interface{}) *string {
	cfg := r.Config.Copy(aws.NewConfig().
		WithEndpoint("").
		WithRegion(aws.StringValue(sourceRegion)))

	clientInfo := r.ClientInfo
	resolved, err := r.Config.EndpointResolver.EndpointFor(
		EndpointsID, aws.StringValue(cfg.Region),
		func(opt *endpoints.Options) {
			opt.DisableSSL = aws.BoolValue(cfg.DisableSSL)
			opt.UseDualStack = aws.BoolValue(cfg.UseDualStack)
			opt.UseDualStackEndpoint = cfg.UseDualStackEndpoint
			opt.UseFIPSEndpoint = cfg.UseFIPSEndpoint
			opt.Logger = r.Config.Logger
			opt.LogDeprecated = r.Config.LogLevel.Matches(aws.LogDebugWithDeprecated)
		},
	)
	if err != nil {
		r.Error = err
		return nil
	}

	clientInfo.Endpoint = resolved.URL
	clientInfo.SigningRegion = resolved.SigningRegion

	// Presign a request with modified params
	req := request.New(*cfg, clientInfo, r.Handlers, r.Retryer, r.Operation, newParams, r.Data)
	req.Operation.HTTPMethod = "GET"
	uri, err := req.Presign(5 * time.Minute) // 5 minutes should be enough.
	if err != nil {                          // bubble error back up to original request
		r.Error = err
		return nil
	}

	// We have our URL, set it on params
	return &uri
}