-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
release.go
151 lines (121 loc) · 3.56 KB
/
release.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
package keygen
import (
"bytes"
"context"
"crypto"
"encoding/base64"
"encoding/hex"
"errors"
"net/http"
"runtime"
"text/template"
"time"
"github.com/keygen-sh/go-update"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
)
// Release represents a Keygen release object.
type Release struct {
ID string `json:"-"`
Type string `json:"-"`
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Channel string `json:"channel"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Metadata map[string]interface{} `json:"metadata"`
opts UpgradeOptions `json:"-"`
}
// SetID implements the jsonapi.UnmarshalResourceIdentifier interface.
func (r *Release) SetID(id string) error {
r.ID = id
return nil
}
// SetType implements the jsonapi.UnmarshalResourceIdentifier interface.
func (r *Release) SetType(t string) error {
r.Type = t
return nil
}
// SetData implements the jsonapi.UnmarshalData interface.
func (r *Release) SetData(to func(target interface{}) error) error {
return to(r)
}
// Install performs an update of the current executable to the new Release.
func (r *Release) Install(ctx context.Context) error {
artifact, err := r.artifact(ctx)
if err != nil {
return err
}
res, err := http.Get(artifact.URL)
if err != nil {
return err
}
defer res.Body.Close()
opts := update.Options{}
if s := artifact.Signature; s != "" {
if k := r.opts.PublicKey; k != "" {
opts.Signature, err = base64.RawStdEncoding.DecodeString(s)
if err != nil {
return err
}
opts.Verifier = ed25519phVerifier{}
opts.PublicKey = k
}
}
if c := artifact.Checksum; c != "" {
opts.Checksum, err = base64.RawStdEncoding.DecodeString(c)
if err != nil {
return err
}
opts.Hash = crypto.SHA512
}
err = update.Apply(res.Body, opts)
if err != nil {
return err
}
return nil
}
func (r *Release) artifact(ctx context.Context) (*Artifact, error) {
client := NewClient()
artifact := &Artifact{}
filename, err := r.filename()
if err != nil {
return nil, err
}
res, err := client.Get(ctx, "releases/"+r.ID+"/artifacts/"+filename, nil, artifact)
if err != nil {
return nil, err
}
// Add download URL to artifact
artifact.URL = res.Headers.Get("Location")
return artifact, nil
}
func (r *Release) filename() (string, error) {
tmpl, err := template.New("").Parse(r.opts.Filename)
if err != nil {
return "", err
}
in := map[string]string{"program": Program, "ext": Ext, "platform": runtime.GOOS, "arch": runtime.GOARCH, "channel": r.Channel, "version": r.Version}
var out bytes.Buffer
if err := tmpl.Execute(&out, in); err != nil {
return "", err
}
return out.String(), nil
}
// ed25519phVerifier handles verifying the upgrade's signature.
type ed25519phVerifier struct{}
// VerifySignature verifies the upgrade's signature with Ed25519ph.
func (v ed25519phVerifier) VerifySignature(checksum []byte, signature []byte, _ crypto.Hash, publicKey crypto.PublicKey) error {
opts := &ed25519.Options{Hash: crypto.SHA512, Context: Product}
key, err := hex.DecodeString(publicKey.(string))
if err != nil {
return errors.New("failed to decode ed25519ph public key")
}
if l := len(key); l != ed25519.PublicKeySize {
return errors.New("invalid ed25519ph public key")
}
if ok := ed25519.VerifyWithOptions(key, checksum, signature, opts); !ok {
return errors.New("failed to verify ed25519ph signature")
}
return nil
}