forked from endophage/gotuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpstore.go
112 lines (101 loc) · 2.81 KB
/
httpstore.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
package store
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"path"
"github.com/Sirupsen/logrus"
"github.com/endophage/gotuf/utils"
)
// HTTPStore manages pulling and pushing metadata from and to a remote
// service over HTTP. It assumes the URL structure of the remote service
// maps identically to the structure of the TUF repo:
// <baseURL>/<metaPrefix>/(root|targets|snapshot|timestamp).json
// <baseURL>/<targetsPrefix>/foo.sh
//
// If consistent snapshots are disabled, it is advised that caching is not
// enabled. Simple set a cachePath (and ensure it's writeable) to enable
// caching.
type HTTPStore struct {
baseURL url.URL
metaPrefix string
metaExtension string
targetsPrefix string
}
func NewHTTPStore(baseURL, metaPrefix, metaExtension, targetsPrefix string) (*HTTPStore, error) {
base, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
if !base.IsAbs() {
return nil, errors.New("HTTPStore requires an absolute baseURL")
}
return &HTTPStore{
baseURL: *base,
metaPrefix: metaPrefix,
metaExtension: metaExtension,
targetsPrefix: targetsPrefix,
}, nil
}
// GetMeta downloads the named meta file with the given size. A short body
// is acceptable because in the case of timestamp.json, the size is a cap,
// not an exact length.
func (s HTTPStore) GetMeta(name string, size int64) (json.RawMessage, error) {
url, err := s.buildMetaURL(name)
if err != nil {
return nil, err
}
resp, err := utils.Download(*url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b := io.LimitReader(resp.Body, int64(size))
body, err := ioutil.ReadAll(b)
if err != nil {
return nil, err
}
return json.RawMessage(body), nil
}
func (s HTTPStore) SetMeta(name string, blob json.RawMessage) error {
url, err := s.buildMetaURL(name)
if err != nil {
return err
}
_, err = utils.Upload(url.String(), bytes.NewReader(blob))
return err
}
func (s HTTPStore) buildMetaURL(name string) (*url.URL, error) {
filename := fmt.Sprintf("%s.%s", name, s.metaExtension)
uri := path.Join(s.metaPrefix, filename)
return s.buildURL(uri)
}
func (s HTTPStore) buildTargetsURL(name string) (*url.URL, error) {
uri := path.Join(s.targetsPrefix, name)
return s.buildURL(uri)
}
func (s HTTPStore) buildURL(uri string) (*url.URL, error) {
sub, err := url.Parse(uri)
if err != nil {
return nil, err
}
return s.baseURL.ResolveReference(sub), nil
}
// GetTarget returns a reader for the desired target or an error.
// N.B. The caller is responsible for closing the reader.
func (s HTTPStore) GetTarget(path string) (io.ReadCloser, error) {
url, err := s.buildTargetsURL(path)
if err != nil {
return nil, err
}
logrus.Debug("Attempting to download target: ", url.String())
resp, err := utils.Download(*url)
if err != nil {
return nil, err
}
return resp.Body, nil
}