This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
asc.go
94 lines (79 loc) · 2.43 KB
/
asc.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
// 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"
"net/url"
"os"
"github.com/hashicorp/errwrap"
"github.com/rkt/rkt/store/imagestore"
)
// ascFetcher is an interface used by asc to get the desired signature
// file.
type ascFetcher interface {
// Get fetches the file from passed location.
Get(location string) (readSeekCloser, error)
}
// localAscFetcher is an implementation of ascFetcher getting
// signature files from a local filesystem.
type localAscFetcher struct{}
func (*localAscFetcher) Get(location string) (readSeekCloser, error) {
return os.Open(location)
}
// remoteAscFetcher is an implementation of ascFetcher getting
// signature files from remote locations.
type remoteAscFetcher struct {
// F is a function that actually does the fetching
F func(*url.URL, *os.File) error
// S is a store - used for getting a temporary file
S *imagestore.Store
}
func (f *remoteAscFetcher) Get(location string) (readSeekCloser, error) {
var roc *removeOnClose // closed on error
var errClose error // error signaling to close roc
roc, err := getTmpROC(f.S, location)
if err != nil {
return nil, err
}
defer func() {
if errClose != nil {
roc.Close()
}
}()
u, errClose := url.Parse(location)
if errClose != nil {
return nil, errwrap.Wrap(errors.New("invalid signature location"), errClose)
}
errClose = f.F(u, roc.File)
if errClose != nil {
return nil, errClose
}
return roc, nil
}
// asc is an abstraction for getting signature files.
type asc struct {
// Location is a string passed to the Fetcher.
Location string
// Fetcher (if available) does the actual fetching of a
// signature key.
Fetcher ascFetcher
}
// Get fetches a signature file. It returns nil and no error if there
// was no fetcher set.
func (a *asc) Get() (readSeekCloser, error) {
if a.Fetcher != nil {
return a.Fetcher.Get(a.Location)
}
return NopReadSeekCloser(nil), nil
}