This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
httpfetcher.go
193 lines (166 loc) · 4.72 KB
/
httpfetcher.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
// Copyright 2015 The rkt Authors
//
// 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 image
import (
"errors"
"io"
"net/url"
"time"
rktflag "github.com/rkt/rkt/rkt/flag"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/pkg/keystore"
"github.com/rkt/rkt/rkt/config"
"github.com/rkt/rkt/store/imagestore"
)
// httpFetcher is used to download images from http or https URLs.
type httpFetcher struct {
InsecureFlags *rktflag.SecFlags
S *imagestore.Store
Ks *keystore.Keystore
Rem *imagestore.Remote
NoCache bool
Debug bool
Headers map[string]config.Headerer
}
// Hash fetches the URL, optionally verifies it against passed asc,
// stores it in the store and returns the hash.
func (f *httpFetcher) Hash(u *url.URL, a *asc) (string, error) {
ensureLogger(f.Debug)
urlStr := u.String()
if !f.NoCache && f.Rem != nil {
if useCached(f.Rem.DownloadTime, f.Rem.CacheMaxAge) {
diag.Printf("image for %s isn't expired, not fetching.", urlStr)
return f.Rem.BlobKey, nil
}
}
diag.Printf("fetching image from %s", urlStr)
aciFile, cd, err := f.fetchURL(u, a, eTag(f.Rem))
if err != nil {
return "", err
}
defer aciFile.Close()
if key := maybeUseCached(f.Rem, cd); key != "" {
// TODO(krnowak): that does not update the store with
// the new CacheMaxAge and Download Time, so it will
// query the server every time after initial
// CacheMaxAge is exceeded
return key, nil
}
key, err := f.S.WriteACI(aciFile, imagestore.ACIFetchInfo{
Latest: false,
})
if err != nil {
return "", err
}
// TODO(krnowak): What's the point of the second parameter?
// The SigURL field in imagestore.Remote seems to be completely
// unused.
newRem := imagestore.NewRemote(urlStr, a.Location)
newRem.BlobKey = key
newRem.DownloadTime = time.Now()
if cd != nil {
newRem.ETag = cd.ETag
newRem.CacheMaxAge = cd.MaxAge
}
err = f.S.WriteRemote(newRem)
if err != nil {
return "", err
}
return key, nil
}
func (f *httpFetcher) fetchURL(u *url.URL, a *asc, etag string) (readSeekCloser, *cacheData, error) {
if f.InsecureFlags.SkipTLSCheck() && f.Ks != nil {
log.Printf("warning: TLS verification has been disabled")
}
if f.InsecureFlags.SkipImageCheck() && f.Ks != nil {
log.Printf("warning: image signature verification has been disabled")
}
if f.InsecureFlags.SkipImageCheck() || f.Ks == nil {
o := f.httpOps()
aciFile, cd, err := o.DownloadImageWithETag(u, etag)
if err != nil {
return nil, nil, err
}
return aciFile, cd, err
}
return f.fetchVerifiedURL(u, a, etag)
}
func (f *httpFetcher) fetchVerifiedURL(u *url.URL, a *asc, etag string) (readSeekCloser, *cacheData, error) {
var aciFile readSeekCloser // closed on error
var errClose error // error signaling to close aciFile
o := f.httpOps()
f.maybeOverrideAscFetcherWithRemote(o, u, a)
ascFile, retry, err := o.DownloadSignature(a)
if err != nil {
return nil, nil, err
}
defer ascFile.Close()
aciFile, cd, err := o.DownloadImageWithETag(u, etag)
if err != nil {
return nil, nil, err
}
defer func() {
if errClose != nil {
aciFile.Close()
}
}()
if cd.UseCached {
aciFile.Close()
return NopReadSeekCloser(nil), cd, nil
}
if retry {
ascFile.Close()
ascFile, errClose = o.DownloadSignatureAgain(a)
if errClose != nil {
ascFile = NopReadSeekCloser(nil)
return nil, nil, errClose
}
}
errClose = f.validate(aciFile, ascFile)
if errClose != nil {
return nil, nil, errClose
}
return aciFile, cd, nil
}
func (f *httpFetcher) httpOps() *httpOps {
return &httpOps{
InsecureSkipTLSVerify: f.InsecureFlags.SkipTLSCheck(),
S: f.S,
Headers: f.Headers,
Debug: f.Debug,
}
}
func (f *httpFetcher) validate(aciFile, ascFile io.ReadSeeker) error {
v, err := newValidator(aciFile)
if err != nil {
return err
}
entity, err := v.ValidateWithSignature(f.Ks, ascFile)
if err != nil {
return err
}
if _, err := aciFile.Seek(0, 0); err != nil {
return errwrap.Wrap(errors.New("error seeking ACI file"), err)
}
printIdentities(entity)
return nil
}
func (f *httpFetcher) maybeOverrideAscFetcherWithRemote(o *httpOps, u *url.URL, a *asc) {
if a.Fetcher != nil {
return
}
u2 := ascURLFromImgURL(u)
a.Location = u2.String()
a.Fetcher = o.AscRemoteFetcher()
}