forked from coreos/ignition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
477 lines (432 loc) · 14.4 KB
/
url.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package resource
import (
"bytes"
"compress/gzip"
"context"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"syscall"
configErrors "github.com/coreos/ignition/config/shared/errors"
"github.com/coreos/ignition/internal/distro"
"github.com/coreos/ignition/internal/log"
"github.com/coreos/ignition/internal/systemd"
"github.com/coreos/ignition/internal/util"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/pin/tftp"
"github.com/vincent-petithory/dataurl"
)
var (
ErrSchemeUnsupported = errors.New("unsupported source scheme")
ErrPathNotAbsolute = errors.New("path is not absolute")
ErrNotFound = errors.New("resource not found")
ErrFailed = errors.New("failed to fetch resource")
ErrCompressionUnsupported = errors.New("compression is not supported with that scheme")
// ConfigHeaders are the HTTP headers that should be used when the Ignition
// config is being fetched
ConfigHeaders = http.Header{
"Accept-Encoding": []string{"identity"},
"Accept": []string{"application/vnd.coreos.ignition+json; version=2.2.0, application/vnd.coreos.ignition+json; version=1; q=0.5, */*; q=0.1"},
}
)
// Fetcher holds settings for fetching resources from URLs
type Fetcher struct {
// The logger object to use when logging information.
Logger *log.Logger
// client is the http client that will be used when fetching http(s)
// resources. If left nil, one will be created and used, but this means any
// timeouts Ignition was configured to used will be ignored.
client *HttpClient
// The AWS Session to use when fetching resources from S3. If left nil, the
// first S3 object that is fetched will initialize the field. This can be
// used to set credentials.
AWSSession *session.Session
// The region where the EC2 machine trying to fetch is.
// This is used as a hint to fetch the S3 bucket from the right partition and region.
S3RegionHint string
}
type FetchOptions struct {
// Headers are the http headers that will be used when fetching http(s)
// resources. They have no effect on other fetching schemes.
Headers http.Header
// Hash is the hash to use when calculating a fetched resource's hash. If
// left as nil, no hash will be calculated.
Hash hash.Hash
// The expected sum to be produced by the given hasher. If the Hash field is
// nil, this field is ignored.
ExpectedSum []byte
// Compression specifies the type of compression to use when decompressing
// the fetched object. If left empty, no decompression will be used.
Compression string
}
// FetchToBuffer will fetch the given url into a temporrary file, and then read
// in the contents of the file and delete it. It will return the downloaded
// contents, or an error if one was encountered.
func (f *Fetcher) FetchToBuffer(u url.URL, opts FetchOptions) ([]byte, error) {
file, err := ioutil.TempFile("", "ignition")
if err != nil {
return nil, err
}
defer os.Remove(file.Name())
defer file.Close()
err = f.Fetch(u, file, opts)
if err != nil {
return nil, err
}
_, err = file.Seek(0, os.SEEK_SET)
if err != nil {
return nil, err
}
res, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return res, nil
}
// Fetch calls the appropriate FetchFrom* function based on the scheme of the
// given URL. The results will be decompressed if compression is set in opts,
// and written into dest. If opts.Hash is set the data stream will also be
// hashed and compared against opts.ExpectedSum, and any match failures will
// result in an error being returned.
//
// Fetch expects dest to be an empty file and for the cursor in the file to be
// at the beginning. Since some url schemes (ex: s3) use chunked downloads and
// fetch chunks out of order, Fetch's behavior when dest is not an empty file is
// undefined.
func (f *Fetcher) Fetch(u url.URL, dest *os.File, opts FetchOptions) error {
switch u.Scheme {
case "http", "https":
return f.FetchFromHTTP(u, dest, opts)
case "tftp":
return f.FetchFromTFTP(u, dest, opts)
case "data":
return f.FetchFromDataURL(u, dest, opts)
case "oem":
return f.FetchFromOEM(u, dest, opts)
case "s3":
return f.FetchFromS3(u, dest, opts)
case "":
return nil
default:
return ErrSchemeUnsupported
}
}
// FetchFromTFTP fetches a resource from u via TFTP into dest, returning an
// error if one is encountered.
func (f *Fetcher) FetchFromTFTP(u url.URL, dest *os.File, opts FetchOptions) error {
if !strings.ContainsRune(u.Host, ':') {
u.Host = u.Host + ":69"
}
c, err := tftp.NewClient(u.Host)
if err != nil {
return err
}
wt, err := c.Receive(u.Path, "octet")
if err != nil {
return err
}
// The TFTP library takes an io.Writer to send data in to, but to decompress
// the stream the gzip library wraps an io.Reader, so let's create a pipe to
// connect these two things
pReader, pWriter := io.Pipe()
doneChan := make(chan error, 2)
checkForDoneChanErr := func(err error) error {
// If an error is encountered while decompressing or copying data out of
// the pipe, there's probably an error from writing into the pipe that
// will better describe what went wrong. This function does a
// non-blocking read of doneChan, overriding the returned error val if
// there's anything in doneChan.
select {
case writeErr := <-doneChan:
if writeErr != nil {
return writeErr
}
return err
default:
return err
}
}
// A goroutine is used to handle writing the fetched data into the pipe
// while also copying it out of the pipe concurrently
go func() {
_, err := wt.WriteTo(pWriter)
doneChan <- err
err = pWriter.Close()
doneChan <- err
}()
err = f.decompressCopyHashAndVerify(dest, pReader, opts)
if err != nil {
return checkForDoneChanErr(err)
}
// receive the error from wt.WriteTo()
err = <-doneChan
if err != nil {
return err
}
// receive the error from pWriter.Close()
err = <-doneChan
if err != nil {
return err
}
return nil
}
// FetchFromHTTP fetches a resource from u via HTTP(S) into dest, returning an
// error if one is encountered.
func (f *Fetcher) FetchFromHTTP(u url.URL, dest *os.File, opts FetchOptions) error {
// for the case when "config is not valid"
// this if necessary if not spawned through kola (e.g. Packet Dashboard)
if f.client == nil {
logger := log.New(true)
f.Logger = &logger
f.newHttpClient()
}
dataReader, status, ctxCancel, err := f.client.getReaderWithHeader(u.String(), opts.Headers)
if ctxCancel != nil {
// whatever context getReaderWithHeader created for the request should
// be cancelled once we're done reading the response
defer ctxCancel()
}
if err != nil {
return err
}
defer dataReader.Close()
switch status {
case http.StatusOK, http.StatusNoContent:
break
case http.StatusNotFound:
return ErrNotFound
default:
return ErrFailed
}
return f.decompressCopyHashAndVerify(dest, dataReader, opts)
}
// FetchFromDataURL writes the data stored in the dataurl u into dest, returning
// an error if one is encountered.
func (f *Fetcher) FetchFromDataURL(u url.URL, dest *os.File, opts FetchOptions) error {
if opts.Compression != "" {
return ErrCompressionUnsupported
}
url, err := dataurl.DecodeString(u.String())
if err != nil {
return err
}
return f.decompressCopyHashAndVerify(dest, bytes.NewBuffer(url.Data), opts)
}
// FetchFromOEM gets data off the oem partition as described by u and writes it
// into dest, returning an error if one is encountered.
func (f *Fetcher) FetchFromOEM(u url.URL, dest *os.File, opts FetchOptions) error {
path := filepath.Clean(u.Path)
if !filepath.IsAbs(path) {
f.Logger.Err("oem path is not absolute: %q", u.Path)
return ErrPathNotAbsolute
}
// check if present in OEM lookaside dir, if so use it.
absPath := filepath.Join(distro.OEMLookasideDir(), path)
if fi, err := os.Open(absPath); err == nil {
defer fi.Close()
return f.decompressCopyHashAndVerify(dest, fi, opts)
} else if !os.IsNotExist(err) {
f.Logger.Err("failed to read oem config: %v", err)
return ErrFailed
}
f.Logger.Info("oem config not found in %q, looking on oem partition",
distro.OEMLookasideDir())
oemMountPath, err := ioutil.TempDir("/mnt", "oem")
if err != nil {
f.Logger.Err("failed to create mount path for oem partition: %v")
return ErrFailed
}
// try oemMountPath, requires mounting it.
if err := f.mountOEM(oemMountPath); err != nil {
f.Logger.Err("failed to mount oem partition: %v", err)
return ErrFailed
}
defer os.Remove(oemMountPath)
defer f.umountOEM(oemMountPath)
absPath = filepath.Join(oemMountPath, path)
fi, err := os.Open(absPath)
if err != nil {
f.Logger.Err("failed to read oem config: %v", err)
return ErrFailed
}
defer fi.Close()
return f.decompressCopyHashAndVerify(dest, fi, opts)
}
// FetchFromS3 gets data from an S3 bucket as described by u and writes it into
// dest, returning an error if one is encountered. It will attempt to acquire
// IAM credentials from the EC2 metadata service, and if this fails will attempt
// to fetch the object with anonymous credentials.
func (f *Fetcher) FetchFromS3(u url.URL, dest *os.File, opts FetchOptions) error {
if opts.Compression != "" {
return ErrCompressionUnsupported
}
ctx := context.Background()
if f.client != nil && f.client.timeout != 0 {
var cancelFn context.CancelFunc
ctx, cancelFn = context.WithTimeout(ctx, f.client.timeout)
defer cancelFn()
}
if f.AWSSession == nil {
var err error
f.AWSSession, err = session.NewSession(&aws.Config{
Credentials: credentials.AnonymousCredentials,
})
if err != nil {
return err
}
}
sess := f.AWSSession.Copy()
// Determine the partition and region this bucket is in
regionHint := "us-east-1"
if f.S3RegionHint != "" {
regionHint = f.S3RegionHint
}
region, err := s3manager.GetBucketRegion(ctx, sess, u.Host, regionHint)
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NotFound" {
return fmt.Errorf("couldn't determine the region for bucket %q: %v", u.Host, err)
}
return err
}
sess.Config.Region = aws.String(region)
input := &s3.GetObjectInput{
Bucket: &u.Host,
Key: &u.Path,
}
err = f.fetchFromS3WithCreds(ctx, dest, input, sess)
if err != nil {
return err
}
if opts.Hash != nil {
opts.Hash.Reset()
_, err = dest.Seek(0, os.SEEK_SET)
if err != nil {
return err
}
_, err = io.Copy(opts.Hash, dest)
if err != nil {
return err
}
calculatedSum := opts.Hash.Sum(nil)
if !bytes.Equal(calculatedSum, opts.ExpectedSum) {
return util.ErrHashMismatch{
Calculated: hex.EncodeToString(calculatedSum),
Expected: hex.EncodeToString(opts.ExpectedSum),
}
}
f.Logger.Debug("file matches expected sum of: %s", hex.EncodeToString(opts.ExpectedSum))
}
return nil
}
func (f *Fetcher) fetchFromS3WithCreds(ctx context.Context, dest *os.File, input *s3.GetObjectInput, sess *session.Session) error {
downloader := s3manager.NewDownloader(sess)
_, err := downloader.DownloadWithContext(ctx, dest, input)
if err != nil {
if awserrval, ok := err.(awserr.Error); ok && awserrval.Code() == "EC2RoleRequestError" {
// If this error was due to an EC2 role request error, try again
// with the anonymous credentials.
sess.Config.Credentials = credentials.AnonymousCredentials
return f.fetchFromS3WithCreds(ctx, dest, input, sess)
}
return err
}
return nil
}
// uncompress will wrap the given io.Reader in a decompresser specified in the
// FetchOptions, and return an io.ReadCloser with the decompressed data stream.
func (f *Fetcher) uncompress(r io.Reader, opts FetchOptions) (io.ReadCloser, error) {
switch opts.Compression {
case "":
return ioutil.NopCloser(r), nil
case "gzip":
return gzip.NewReader(r)
default:
return nil, configErrors.ErrCompressionInvalid
}
}
// decompressCopyHashAndVerify will decompress src if necessary, copy src into
// dest until src returns an io.EOF while also calculating a hash if one is set,
// and will return an error if there's any problems with any of this or if the
// hash doesn't match the expected hash in the opts.
func (f *Fetcher) decompressCopyHashAndVerify(dest io.Writer, src io.Reader, opts FetchOptions) error {
decompressor, err := f.uncompress(src, opts)
if err != nil {
return err
}
defer decompressor.Close()
if opts.Hash != nil {
opts.Hash.Reset()
dest = io.MultiWriter(dest, opts.Hash)
}
_, err = io.Copy(dest, decompressor)
if err != nil {
return err
}
if opts.Hash != nil {
calculatedSum := opts.Hash.Sum(nil)
if !bytes.Equal(calculatedSum, opts.ExpectedSum) {
return util.ErrHashMismatch{
Calculated: hex.EncodeToString(calculatedSum),
Expected: hex.EncodeToString(opts.ExpectedSum),
}
}
f.Logger.Debug("file matches expected sum of: %s", hex.EncodeToString(opts.ExpectedSum))
}
return nil
}
// mountOEM waits for the presence of and mounts the oem partition at
// oemMountPath. oemMountPath will be created if it does not exist.
func (f *Fetcher) mountOEM(oemMountPath string) error {
dev := []string{distro.OEMDevicePath()}
if err := systemd.WaitOnDevices(dev, "oem-cmdline"); err != nil {
f.Logger.Err("failed to wait for oem device: %v", err)
return err
}
if err := os.MkdirAll(oemMountPath, 0700); err != nil {
f.Logger.Err("failed to create oem mount point: %v", err)
return err
}
if err := f.Logger.LogOp(
func() error {
return syscall.Mount(dev[0], oemMountPath, "ext4", 0, "")
},
"mounting %q at %q", distro.OEMDevicePath(), oemMountPath,
); err != nil {
return fmt.Errorf("failed to mount device %q at %q: %v",
distro.OEMDevicePath(), oemMountPath, err)
}
return nil
}
// umountOEM unmounts the oem partition at oemMountPath.
func (f *Fetcher) umountOEM(oemMountPath string) {
f.Logger.LogOp(
func() error { return syscall.Unmount(oemMountPath, 0) },
"unmounting %q", oemMountPath,
)
}