-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathclient.go
357 lines (305 loc) · 9.86 KB
/
client.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package getter
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
urlhelper "github.com/hashicorp/go-getter/v2/helper/url"
"github.com/hashicorp/go-multierror"
safetemp "github.com/hashicorp/go-safetemp"
)
// ErrSymlinkCopy means that a copy of a symlink was encountered on a request with DisableSymlinks enabled.
var ErrSymlinkCopy = errors.New("copying of symlinks has been disabled")
// Client is a client for downloading things.
//
// Top-level functions such as Get are shortcuts for interacting with a client.
// Using a client directly allows more fine-grained control over how downloading
// is done, as well as customizing the protocols supported.
type Client struct {
// Decompressors is the map of decompressors supported by this client.
// If this is nil, then the default value is the Decompressors global.
Decompressors map[string]Decompressor
// Getters is the list of protocols supported by this client. If this
// is nil, then the default Getters variable will be used.
Getters []Getter
// Disable symlinks is used to prevent copying or writing files through symlinks for Get requests.
// When set to true any copying or writing through symlinks will result in a ErrSymlinkCopy error.
DisableSymlinks bool
}
// GetResult is the result of a Client.Get
type GetResult struct {
// Local destination of the gotten object.
Dst string
}
// Get downloads the configured source to the destination.
func (c *Client) Get(ctx context.Context, req *Request) (*GetResult, error) {
if err := c.configure(); err != nil {
return nil, err
}
// Pass along the configured Getter client in the context for usage with the X-Terraform-Get feature.
ctx = NewContextWithClient(ctx, c)
// Store this locally since there are cases we swap this
if req.GetMode == ModeInvalid {
req.GetMode = ModeAny
}
// Client setting takes precedence for all requests
if c.DisableSymlinks {
req.DisableSymlinks = true
}
// If there is a subdir component, then we download the root separately
// and then copy over the proper subdir.
req.Src, req.subDir = SourceDirSubdir(req.Src)
if req.subDir != "" {
// Check if the subdirectory is attempting to traverse upwards, outside of
// the cloned repository path.
req.subDir = filepath.Clean(req.subDir)
if containsDotDot(req.subDir) {
return nil, fmt.Errorf("subdirectory component contain path traversal out of the repository")
}
// Prevent absolute paths, remove a leading path separator from the subdirectory
if req.subDir[0] == os.PathSeparator {
req.subDir = req.subDir[1:]
}
td, tdcloser, err := safetemp.Dir("", "getter")
if err != nil {
return nil, err
}
defer tdcloser.Close()
req.realDst = req.Dst
req.Dst = td
}
var multierr []error
for _, g := range c.Getters {
shouldDownload, err := Detect(req, g)
if err != nil {
return nil, err
}
if !shouldDownload {
// the request should not be processed by that getter
continue
}
result, getErr := c.get(ctx, req, g)
if getErr != nil {
if getErr.Fatal {
return nil, getErr.Err
}
multierr = append(multierr, getErr.Err)
continue
}
return result, nil
}
if len(multierr) == 1 {
// This is for keeping the error original format
return nil, multierr[0]
}
if multierr != nil {
var result *multierror.Error
result = multierror.Append(result, multierr...)
return nil, fmt.Errorf("error downloading '%s': %s", req.Src, result.Error())
}
return nil, fmt.Errorf("error downloading '%s'", req.Src)
}
// getError is the Error response object returned by get(context.Context, *Request, Getter)
// to tell the client whether to halt (Fatal) Get or to keep trying to get an artifact.
type getError struct {
// When Fatal is true something went wrong with get(context.Context, *Request, Getter)
// and the client should halt and return the Err.
Fatal bool
Err error
}
func (ge *getError) Error() string {
return ge.Err.Error()
}
func (c *Client) get(ctx context.Context, req *Request, g Getter) (*GetResult, *getError) {
u, err := urlhelper.Parse(req.Src)
req.u = u
if err != nil {
return nil, &getError{true, err}
}
// We have magic query parameters that we use to signal different features
q := req.u.Query()
// Determine if we have an archive type
archiveV := q.Get("archive")
if archiveV != "" {
// Delete the parameter since it is a magic parameter we don't
// want to pass on to the Getter
q.Del("archive")
req.u.RawQuery = q.Encode()
// If we can parse the value as a bool and it is false, then
// set the archive to "-" which should never map to a decompressor
if b, err := strconv.ParseBool(archiveV); err == nil && !b {
archiveV = "-"
}
} else {
// We don't appear to... but is it part of the filename?
matchingLen := 0
for k := range c.Decompressors {
if strings.HasSuffix(req.u.Path, "."+k) && len(k) > matchingLen {
archiveV = k
matchingLen = len(k)
}
}
}
// If we have a decompressor, then we need to change the destination
// to download to a temporary path. We unarchive this into the final,
// real path.
var decompressDst string
var decompressDir bool
decompressor := c.Decompressors[archiveV]
if decompressor != nil {
// Create a temporary directory to store our archive. We delete
// this at the end of everything.
td, err := ioutil.TempDir("", "getter")
if err != nil {
return nil, &getError{true, fmt.Errorf(
"Error creating temporary directory for archive: %s", err)}
}
defer os.RemoveAll(td)
// Swap the download directory to be our temporary path and
// store the old values.
decompressDst = req.Dst
decompressDir = req.GetMode != ModeFile
req.Dst = filepath.Join(td, "archive")
req.GetMode = ModeFile
}
// Determine checksum if we have one
checksum, err := c.GetChecksum(ctx, req)
if err != nil {
return nil, &getError{true, fmt.Errorf("invalid checksum: %s", err)}
}
// Delete the query parameter if we have it.
q.Del("checksum")
req.u.RawQuery = q.Encode()
if req.GetMode == ModeAny {
// Ask the getter which client mode to use
req.GetMode, err = g.Mode(ctx, req.u)
if err != nil {
return nil, &getError{false, err}
}
// Destination is the base name of the URL path in "any" mode when
// a file source is detected.
if req.GetMode == ModeFile {
filename := filepath.Base(req.u.Path)
// Determine if we have a custom file name
if v := q.Get("filename"); v != "" {
// Delete the query parameter if we have it.
q.Del("filename")
req.u.RawQuery = q.Encode()
filename = v
}
if containsDotDot(filename) {
return nil, &getError{true, fmt.Errorf("filename query parameter contain path traversal")}
}
req.Dst = filepath.Join(req.Dst, filename)
}
}
// If we're not downloading a directory, then just download the file
// and return.
if req.GetMode == ModeFile {
getFile := true
if checksum != nil {
if err := checksum.Checksum(req.Dst); err == nil {
// don't get the file if the checksum of dst is correct
getFile = false
}
}
if getFile {
if err := g.GetFile(ctx, req); err != nil {
return nil, &getError{false, err}
}
if checksum != nil {
if err := checksum.Checksum(req.Dst); err != nil {
return nil, &getError{true, err}
}
}
}
if decompressor != nil {
// We have a decompressor, so decompress the current destination
// into the final destination with the proper mode.
err := decompressor.Decompress(decompressDst, req.Dst, decompressDir, req.umask())
if err != nil {
return nil, &getError{true, err}
}
// Swap the information back
req.Dst = decompressDst
if decompressDir {
req.GetMode = ModeAny
} else {
req.GetMode = ModeFile
}
}
// We check the dir value again because it can be switched back
// if we were unarchiving. If we're still only Get-ing a file, then
// we're done.
if req.GetMode == ModeFile {
return &GetResult{req.Dst}, nil
}
}
// If we're at this point we're either downloading a directory or we've
// downloaded and unarchived a directory and we're just checking subdir.
// In the case we have a decompressor we don't Get because it was Get
// above.
if decompressor == nil {
// If we're getting a directory, then this is an error. You cannot
// checksum a directory. TODO: test
if checksum != nil {
return nil, &getError{true, fmt.Errorf(
"checksum cannot be specified for directory download")}
}
// We're downloading a directory, which might require a bit more work
// if we're specifying a subdir.
if err := g.Get(ctx, req); err != nil {
return nil, &getError{false, err}
}
}
// If we have a subdir, copy that over
if req.subDir != "" {
if err := os.RemoveAll(req.realDst); err != nil {
return nil, &getError{true, err}
}
if err := os.MkdirAll(req.realDst, req.Mode(0755)); err != nil {
return nil, &getError{true, err}
}
// Process any globs
subDir, err := SubdirGlob(req.Dst, req.subDir)
if err != nil {
return nil, &getError{true, err}
}
err = copyDir(ctx, req.realDst, subDir, false, req.DisableSymlinks, req.umask())
if err != nil {
return nil, &getError{false, err}
}
return &GetResult{req.realDst}, nil
}
return &GetResult{req.Dst}, nil
}
func (c *Client) checkArchive(req *Request) string {
q := req.u.Query()
archiveV := q.Get("archive")
if archiveV != "" {
// Delete the paramter since it is a magic parameter we don't
// want to pass on to the Getter
q.Del("archive")
req.u.RawQuery = q.Encode()
// If we can parse the value as a bool and it is false, then
// set the archive to "-" which should never map to a decompressor
if b, err := strconv.ParseBool(archiveV); err == nil && !b {
archiveV = "-"
}
}
if archiveV == "" {
// We don't appear to... but is it part of the filename?
matchingLen := 0
for k := range c.Decompressors {
if strings.HasSuffix(req.u.Path, "."+k) && len(k) > matchingLen {
archiveV = k
matchingLen = len(k)
}
}
}
return archiveV
}