This repository was archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathclient.go
More file actions
96 lines (82 loc) · 2.69 KB
/
client.go
File metadata and controls
96 lines (82 loc) · 2.69 KB
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
package tuf
import (
"io"
"net/http"
"net/url"
"path"
"github.com/pkg/errors"
)
// Client is a TUF client.
type Client struct {
// Client wraps the private repoMan type which contains the actual
// methods for working with TUF repositories. In the future it might
// be worthwile to export the repoMan type as Client instead, but
// wrapping it reduces the amount of present refactoring work.
manager *repoMan
}
func NewClient(settings *Settings) (*Client, error) {
if settings.MaxResponseSize == 0 {
settings.MaxResponseSize = defaultMaxResponseSize
}
// check to see if Notary server is available
notary, err := newNotaryRepo(settings)
if err != nil {
return nil, errors.Wrap(err, "creating notary client")
}
err = notary.ping()
if err != nil {
return nil, errors.Wrap(err, "pinging notary server failed")
}
localRepo, err := newLocalRepo(settings.LocalRepoPath)
if err != nil {
return nil, errors.New("creating local tuf role repo")
}
// store intermediate state until all validation succeeds, then write
// changed roles to non-volitile storage
manager := newRepoMan(localRepo, notary, settings, notary.client)
return &Client{manager: manager}, nil
}
func (c *Client) Update() (files map[targetNameType]FileIntegrityMeta, latest bool, err error) {
latest, err = c.manager.refresSafe()
if err != nil {
return nil, latest, errors.Wrap(err, "refreshing state")
}
if err := c.manager.save(getTag()); err != nil {
return nil, latest, errors.Wrap(err, "unable to save tuf repo state")
}
files = c.manager.getLocalTargets()
return files, latest, nil
}
func (c *Client) Download(targetName string, destination io.Writer) error {
files := c.manager.getLocalTargets()
currentMeta, ok := files[targetNameType(targetName)]
if !ok {
return errors.Errorf("targetName %s not found", targetName)
}
mirrorURL, err := url.Parse(c.manager.settings.MirrorURL)
if err != nil {
return errors.Wrap(err, "parse mirror url for download")
}
mirrorURL.Path = path.Join(mirrorURL.Path, c.manager.settings.GUN, targetName)
request, err := http.NewRequest(http.MethodGet, mirrorURL.String(), nil)
if err != nil {
return errors.Wrapf(err, "creating request to %s", mirrorURL.String())
}
request.Header.Add(cacheControl, cachePolicyNoStore)
resp, err := c.manager.client.Do(request)
if err != nil {
return errors.Wrap(err, "fetching target from mirror")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("get target returned %q", resp.Status)
}
stream := io.LimitReader(resp.Body, int64(currentMeta.Length))
if err := currentMeta.verify(io.TeeReader(stream, destination)); err != nil {
return err
}
return nil
}
func (c *Client) Stop() {
c.manager.Stop()
}