github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/image/asc.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package image 16 17 import ( 18 "errors" 19 "net/url" 20 "os" 21 22 "github.com/hashicorp/errwrap" 23 "github.com/rkt/rkt/store/imagestore" 24 ) 25 26 // ascFetcher is an interface used by asc to get the desired signature 27 // file. 28 type ascFetcher interface { 29 // Get fetches the file from passed location. 30 Get(location string) (readSeekCloser, error) 31 } 32 33 // localAscFetcher is an implementation of ascFetcher getting 34 // signature files from a local filesystem. 35 type localAscFetcher struct{} 36 37 func (*localAscFetcher) Get(location string) (readSeekCloser, error) { 38 return os.Open(location) 39 } 40 41 // remoteAscFetcher is an implementation of ascFetcher getting 42 // signature files from remote locations. 43 type remoteAscFetcher struct { 44 // F is a function that actually does the fetching 45 F func(*url.URL, *os.File) error 46 // S is a store - used for getting a temporary file 47 S *imagestore.Store 48 } 49 50 func (f *remoteAscFetcher) Get(location string) (readSeekCloser, error) { 51 var roc *removeOnClose // closed on error 52 var errClose error // error signaling to close roc 53 54 roc, err := getTmpROC(f.S, location) 55 if err != nil { 56 return nil, err 57 } 58 59 defer func() { 60 if errClose != nil { 61 roc.Close() 62 } 63 }() 64 65 u, errClose := url.Parse(location) 66 if errClose != nil { 67 return nil, errwrap.Wrap(errors.New("invalid signature location"), errClose) 68 } 69 70 errClose = f.F(u, roc.File) 71 if errClose != nil { 72 return nil, errClose 73 } 74 75 return roc, nil 76 } 77 78 // asc is an abstraction for getting signature files. 79 type asc struct { 80 // Location is a string passed to the Fetcher. 81 Location string 82 // Fetcher (if available) does the actual fetching of a 83 // signature key. 84 Fetcher ascFetcher 85 } 86 87 // Get fetches a signature file. It returns nil and no error if there 88 // was no fetcher set. 89 func (a *asc) Get() (readSeekCloser, error) { 90 if a.Fetcher != nil { 91 return a.Fetcher.Get(a.Location) 92 } 93 return NopReadSeekCloser(nil), nil 94 }