-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdrivers.go
More file actions
293 lines (243 loc) · 6.47 KB
/
drivers.go
File metadata and controls
293 lines (243 loc) · 6.47 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) 2025 Columnar Technologies. All rights reserved.
package dbc
import (
_ "embed"
"fmt"
"io"
"iter"
"net/http"
"net/url"
"os"
"path"
"runtime"
"runtime/debug"
"slices"
"sort"
"sync"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/goccy/go-yaml"
)
const defaultURL = "https://dbc-cdn.columnar.tech"
var (
baseURL = defaultURL
Version = "unknown"
)
func init() {
info, ok := debug.ReadBuildInfo()
if ok {
Version = info.Main.Version
}
if val := os.Getenv("DBC_BASE_URL"); val != "" {
baseURL = val
}
}
func makereq(u string) (resp *http.Response, err error) {
uri, err := url.Parse(u)
if err != nil {
return nil, fmt.Errorf("failed to parse URL %s: %w", uri, err)
}
req := http.Request{
Method: http.MethodGet,
URL: uri,
Header: http.Header{
"User-Agent": []string{fmt.Sprintf("dbc-cli/%s (%s; %s)",
Version, runtime.GOOS, runtime.GOARCH)},
},
}
return http.DefaultClient.Do(&req)
}
var getDrivers = sync.OnceValues(func() ([]Driver, error) {
resp, err := makereq(baseURL + "/index.yaml")
if err != nil {
return nil, fmt.Errorf("failed to fetch drivers: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch drivers: %s", resp.Status)
}
defer resp.Body.Close()
drivers := struct {
Drivers []Driver `yaml:"drivers"`
}{}
err = yaml.NewDecoder(resp.Body).Decode(&drivers)
if err != nil {
return nil, fmt.Errorf("failed to parse driver index: %s", err)
}
// Sort by path (short name)
result := drivers.Drivers
sort.Slice(result, func(i, j int) bool {
return result[i].Path < result[j].Path
})
return result, nil
})
//go:embed columnar.pubkey
var armoredPubKey string
var getVerifier = sync.OnceValues(func() (crypto.PGPVerify, error) {
key, err := crypto.NewKeyFromArmored(armoredPubKey)
if err != nil {
return nil, err
}
return crypto.PGP().Verify().VerificationKey(key).New()
})
type PkgInfo struct {
Driver Driver
Version *semver.Version
PlatformTuple string
Path *url.URL
}
func (p PkgInfo) DownloadPackage() (*os.File, error) {
if p.Path == nil {
return nil, fmt.Errorf("cannot download package for %s: no url set", p.Driver.Title)
}
location := p.Path.String()
rsp, err := makereq(location)
if err != nil {
return nil, fmt.Errorf("failed to download driver: %w", err)
}
if rsp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download driver %s: %s", location, rsp.Status)
}
defer rsp.Body.Close()
fname := path.Base(location)
tmpdir, err := os.MkdirTemp(os.TempDir(), "adbc-drivers-*")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %w", err)
}
output, err := os.Create(path.Join(tmpdir, fname))
if err != nil {
return nil, fmt.Errorf("failed to create temp file to download to: %w", err)
}
_, err = io.Copy(output, rsp.Body)
if err != nil {
output.Close()
}
return output, err
}
type pkginfo struct {
Version *semver.Version `yaml:"version"`
Packages []struct {
PlatformTuple string `yaml:"platform"`
URL string `yaml:"url"`
} `yaml:"packages"`
}
func (p pkginfo) GetPackage(d Driver, platformTuple string) (PkgInfo, error) {
if len(p.Packages) == 0 {
return PkgInfo{}, fmt.Errorf("no packages available for version %s", p.Version)
}
base, _ := url.Parse(baseURL)
for _, pkg := range p.Packages {
if pkg.PlatformTuple == platformTuple {
var uri *url.URL
if pkg.URL != "" {
uri, _ = url.Parse(pkg.URL)
if !uri.IsAbs() {
uri = base.JoinPath(pkg.URL)
}
} else {
uri = base.JoinPath(d.Path, p.Version.String(),
d.Path+"_"+platformTuple+"-"+p.Version.String()+".tar.gz")
}
return PkgInfo{
Driver: d,
Version: p.Version,
PlatformTuple: platformTuple,
Path: uri,
}, nil
}
}
return PkgInfo{}, fmt.Errorf("no package found for platform '%s'", platformTuple)
}
func filter[T any](items iter.Seq[T], predicate func(T) bool) iter.Seq[T] {
return func(yield func(T) bool) {
for item := range items {
if predicate(item) && !yield(item) {
return
}
}
}
}
type Driver struct {
Title string `yaml:"name"`
Desc string `yaml:"description"`
License string `yaml:"license"`
Path string `yaml:"path"`
URLs []string `yaml:"urls"`
PkgInfo []pkginfo `yaml:"pkginfo"`
}
func (d Driver) GetWithConstraint(c *semver.Constraints, platformTuple string) (PkgInfo, error) {
if len(d.PkgInfo) == 0 {
return PkgInfo{}, fmt.Errorf("no package info available for driver %s", d.Path)
}
itr := filter(slices.Values(d.PkgInfo), func(p pkginfo) bool {
if !c.Check(p.Version) {
return false
}
return slices.ContainsFunc(p.Packages, func(p struct {
PlatformTuple string `yaml:"platform"`
URL string `yaml:"url"`
}) bool {
return p.PlatformTuple == platformTuple
})
})
var result *pkginfo
for pkg := range itr {
if result == nil || pkg.Version.GreaterThan(result.Version) {
result = &pkg
}
}
if result == nil {
return PkgInfo{}, fmt.Errorf("no package found for driver %s that satisfies constraints %s", d.Path, c)
}
return result.GetPackage(d, platformTuple)
}
func (d Driver) Versions(platformTuple string) semver.Collection {
versions := make(semver.Collection, 0, len(d.PkgInfo))
for _, pkg := range d.PkgInfo {
for _, p := range pkg.Packages {
if p.PlatformTuple == platformTuple {
versions = append(versions, pkg.Version)
}
}
}
sort.Sort(versions)
return versions
}
func (d Driver) GetPackage(version *semver.Version, platformTuple string) (PkgInfo, error) {
var pkg pkginfo
if version == nil {
pkg = slices.MaxFunc(d.PkgInfo, func(a, b pkginfo) int {
return a.Version.Compare(b.Version)
})
version = pkg.Version
} else {
idx := slices.IndexFunc(d.PkgInfo, func(p pkginfo) bool {
return p.Version.Equal(version)
})
if idx == -1 {
return PkgInfo{}, fmt.Errorf("version %s not found", version)
}
pkg = d.PkgInfo[idx]
}
return pkg.GetPackage(d, platformTuple)
}
func GetDriverList() ([]Driver, error) {
return getDrivers()
}
// SignedByColumnar returns nil if the library was signed by
// the columnar public key (embedded in the CLI) or an error
// otherwise.
func SignedByColumnar(lib, sig io.Reader) error {
verifier, err := getVerifier()
if err != nil {
return err
}
reader, err := verifier.VerifyingReader(lib, sig, crypto.Auto)
if err != nil {
return err
}
result, err := reader.DiscardAllAndVerifySignature()
if err != nil {
return err
}
return result.SignatureError()
}