This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transformer.go
440 lines (378 loc) · 12.9 KB
/
transformer.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package upstream
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
"gophers.dev/pkgs/loggy"
"oss.indeed.com/go/modprox/pkg/coordinates"
)
//go:generate go run github.com/gojuno/minimock/v3/cmd/minimock -g -i Resolver -s _mock.go
// A Resolver is able to turn the globally unique identifier of
// a Go module (which includes a Source and a Version) and applies
// a set of Transform operations until a Request is created
// that can later be used to fetch the module from some source,
// which is typically a VCS host (e.g. github).
type Resolver interface {
// Resolve applies any underlying Transform operations
// and returns the resulting Request, or an error if
// one of the Transform operations does not work.
Resolve(coordinates.Module) (*Request, error)
// UseProxy indicates whether Module can be downloaded from a generic open
// global proxy (e.g. proxy.golang.org) instead of the original upstream
// source (e.g. github / gitlab). This is going to be true for any Module
// which does not get matched by any configured domain type Transform. The
// rational is that any domain type Transform is a flag that the module is
// not going to be present an the open source context, and the original
// upstream must be used since it is likely a private repository.
//
// The transforms that prohibit proxy use are:
// - StaticRedirectTransform
// - DomainTransportTransform
// - DomainHeaderTransform
UseProxy(coordinates.Module) (bool, error)
}
// A Transform is one operation that is applied to a Request,
// which creates a new Request with zero or more parameters
// of the input Request having been modified. A Transform can
// be used to handle things like static domain name redirection,
// indirect domain name redirect (i.e. accommodate go-get meta URIs),
// domain-based path rewriting, etc.
//
// As time goes on, more and more Transform implementations will be
// added, to support additional use cases for enterprise environments
// which tend to have special needs.
type Transform interface {
Modify(*Request) (*Request, error)
}
type resolver struct {
transforms []Transform
}
// NewResolver creates a Resolver which will apply the given set
// of Transform operations in the order in which they appear.
func NewResolver(transforms ...Transform) Resolver {
return &resolver{
transforms: transforms,
}
}
func (r *resolver) UseProxy(mod coordinates.Module) (bool, error) {
original, err := NewRequest(mod)
if err != nil {
return false, err
}
// go through each transform and decide if it applies to this module
for _, transform := range r.transforms {
switch transform.(type) {
// select on the types that trigger an upstream request to be necessary
case *StaticRedirectTransform,
*DomainTransportTransform,
*DomainHeaderTransform:
// Apply the transform, if the request is modified, that means
// the transform is applies to this module and we cannot use the
// global proxy to make the request.
changed, err := transform.Modify(original)
if err != nil {
return false, err
}
// Compare the original request with the modified request. If they
// do not match, we cannot use a global proxy to make request the
// archive for this module.
if !original.Equals(changed) {
return false, nil
}
}
}
return true, nil
}
func (r *resolver) Resolve(mod coordinates.Module) (*Request, error) {
request, err := NewRequest(mod)
if err != nil {
return nil, err
}
for _, transform := range r.transforms {
request, err = transform.Modify(request)
if err != nil {
return nil, err
}
}
return request, nil
}
// NewRequest creates a default Request from the given module. This
// initial Request is likely useless, as it only becomes useful after
// a set of Transform operations are applied to it, which then compute
// correct URI for the module it represents.
func NewRequest(mod coordinates.Module) (*Request, error) {
domain, namespace, err := splitSource(mod.Source)
if err != nil {
return nil, err
}
return &Request{
Transport: "https",
Domain: domain,
Namespace: namespace,
Version: mod.Version,
}, nil
}
func splitSource(s string) (string, Namespace, error) {
s = strings.TrimSpace(s)
if s == "" {
return "", nil, errors.New("source is empty string")
}
split := strings.Split(s, "/")
if len(split) == 1 {
// e.g. go.opencensus.io is a whole domain used
// to represent one package using go-get meta
return s, nil, nil
}
// we have a domain and a path
domain := split[0]
namespace := Namespace(split[1:])
return domain, namespace, nil
}
// A StaticRedirectTransform is used to directly convert one domain
// to another. For example, if your organization internally keeps packages
// organized like
// ${GOPATH}/company/...
// but the internal VCS is only addressable in a way like
// code.internal.company.net/...
// then the StaticRedirectTransform can be used to automatically acquire
// modules prefixed with name "company/" from the internal VCS of the
// different domain name.
type StaticRedirectTransform struct {
original string
substitution string
log loggy.Logger
}
// NewStaticRedirectTransform creates a Transform which will convert
// domains of the original name to become the substitution name.
//
// Currently only exact matches on the domain are supported.
func NewStaticRedirectTransform(original, substitution string) Transform {
return &StaticRedirectTransform{
original: original,
substitution: substitution,
log: loggy.New("redirect-transform"),
}
}
func (t *StaticRedirectTransform) Modify(r *Request) (*Request, error) {
newDomain := r.Domain
if newDomain == t.original {
newDomain = t.substitution
}
modified := &Request{
Transport: r.Transport,
Domain: newDomain,
Namespace: r.Namespace,
Version: r.Version,
}
t.log.Tracef("original: %s", r)
t.log.Tracef("modified: %s", modified)
return modified, nil
}
// The GoGetTransform is no longer configurable, it will always automatically
// do go-get=1 requests, following the redirect as the Go documentation specifies.
type GoGetTransform struct {
httpClient *http.Client
log loggy.Logger
}
// NewAutomaticGoGetTransform creates a GoGetTransform where any module URI
// will be redirected to wherever the go-get meta HTML tag in the domain
// indicates.
func NewAutomaticGoGetTransform() Transform {
return &GoGetTransform{
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
log: loggy.New("go-get-transform"),
}
}
func (t *GoGetTransform) Modify(r *Request) (*Request, error) {
t.log.Infof("doing go-get redirect lookup for domain %s", r.Domain)
meta, err := t.doGoGetRequest(r)
if err != nil {
// in theory everything should respond well to go-get=1, but in practice, nah
t.log.Warnf("unable to go-get domain: %s, leaving request unmodified: %v", r.Domain, err)
return r, nil
}
t.log.Infof("go-get redirect to: %s", meta)
modified := &Request{
Transport: meta.transport,
Domain: meta.domain,
Namespace: strings.Split(meta.path, "/"),
Version: r.Version,
// Path: only set by the domain re-writer
}
t.log.Tracef("original: %s", r)
t.log.Tracef("modified: %s", modified)
return modified, nil
}
// A DomainPathTransform is used to generate or rewrite the URL path
// of the module archive that is to be fetched per the domain of desired
// module of the Request. Default path rewriting rules are provided for
// repositories ultimately hosted in github or gitlab. Additional path
// transformations should be defined for internally hosed VCSs.
//
// e.g. github:
// https://github.com/ELEM1/ELEM2/archive/VERSION.zip
// e.g. gitlab:
// https://gitlab.com/ELEM1/ELEM2/-/archive/VERSOIN/ELEM2-v2.0.1.zip
type DomainPathTransform struct {
pathFmt string
}
func (t *DomainPathTransform) Modify(r *Request) (*Request, error) {
version := addressableVersion(r.Version) // this seems a little conflated
newPath := formatPath(t.pathFmt, version, r.Namespace)
return &Request{
Transport: r.Transport,
Domain: r.Domain,
Namespace: r.Namespace,
Version: r.Version,
Path: newPath,
}, nil
}
// e.g. v2.0.0 => v2.0.0
// e.g. v0.0.0-20180111040409-fbec762f837d => fbec762f837d
// e.g. v2.3.3+incompatible => v2.3.3
func addressableVersion(version string) string {
// dashes indicate <version>-<timestamp>-<hash> format,
// where the hash is what is addressable in vcs
splitOnDash := strings.Split(version, "-")
if len(splitOnDash) == 3 {
return splitOnDash[2] // return the hash if it exists
}
// plus indicates <version>+<comment> where the version
// is what is addressable in vcs
splitOnPlus := strings.Split(version, "+")
if len(splitOnPlus) > 1 {
return splitOnPlus[0]
}
// the version is just the version
return version
}
// e.g. ELEM1/ELEM2/archive/VERSION.zip => shoenig/petrify/archive/v4.0.1.zip
// e.g. ELEM1/ELEM2/-/archive/VERSION/ELEM2-VERSION.zip => crypo/cryptsetup/-/archive/v2.0.1/cryptsetup-v2.0.1.zip
func formatPath(pathFmt, version string, namespace Namespace) string {
var path = pathFmt
for i, elem := range namespace {
elemIdx := fmt.Sprintf("ELEM%d", i+1)
path = strings.Replace(path, elemIdx, elem, -1)
}
path = strings.Replace(path, "VERSION", version, -1)
return path
}
func NewDomainPathTransform(pathFmt string) Transform {
return &DomainPathTransform{
pathFmt: pathFmt,
}
}
// DefaultPathTransforms provides a set of default Transform types which
// create the Request.Path for a known set of VCSs systems in the open source
// world (i.e. github and gitlab).
// Additional Transforms should be specified via NewSetPathTransform, which
// accepts a map of domain to Transform, for internally hosed code.
var DefaultPathTransforms = map[string]Transform{
"github.com": NewDomainPathTransform("ELEM1/ELEM2/archive/VERSION.zip"),
"gitlab.com": NewDomainPathTransform("ELEM1/ELEM2/-/archive/VERSION/ELEM2-VERSION.zip"),
"": NewDomainPathTransform(""), // unknown
}
// A SetPathTransform is a collection of transforms which set the Path of
// a Request given a domain. Think of it as a map from a domain to a
// DomainPathTransform, which can be used in the general case rather than
// specifying an explicit list of DomainPathTransform.
type SetPathTransform struct {
domainPathTransforms map[string]Transform
log loggy.Logger
}
func NewSetPathTransform(customDomainPathTransforms map[string]Transform) Transform {
combined := combinedDomainPathTransforms(customDomainPathTransforms)
return &SetPathTransform{
domainPathTransforms: combined,
log: loggy.New("set-path-transform"),
}
}
func combinedDomainPathTransforms(
customDomainPathTransforms map[string]Transform,
) map[string]Transform {
m := make(map[string]Transform, len(DefaultPathTransforms)+len(customDomainPathTransforms))
for domain, transform := range DefaultPathTransforms {
m[domain] = transform
}
for domain, transform := range customDomainPathTransforms {
m[domain] = transform
}
return m
}
func (t *SetPathTransform) Modify(r *Request) (*Request, error) {
domainPathTransform, exists := t.domainPathTransforms[r.Domain]
if !exists {
return nil, errors.Errorf("no path transformation exists for domain %s", r.Domain)
}
modified, err := domainPathTransform.Modify(r)
t.log.Tracef("original: %s", r)
t.log.Tracef("modified: %s", modified)
return modified, err
}
func NewDomainHeaderTransform(domain string, headers map[string]string) Transform {
return &DomainHeaderTransform{
domain: domain,
headers: headers,
log: loggy.New("domain-header-transform"),
}
}
// A DomainHeaderTransform is used to set the header for a request.
// Typically one of these will be used to set the authentication key
// for https requests to an internal VCS system.
type DomainHeaderTransform struct {
domain string
headers map[string]string
log loggy.Logger
}
func (t *DomainHeaderTransform) Modify(r *Request) (*Request, error) {
if r.Domain != t.domain {
return r, nil
}
newHeaders := make(map[string]string, len(r.Headers))
for k, v := range r.Headers {
newHeaders[k] = v
}
for key, value := range t.headers {
t.log.Tracef("setting a value for request header %q", key)
newHeaders[key] = value
}
return &Request{
Transport: r.Transport,
Domain: r.Domain,
Namespace: r.Namespace,
Version: r.Version,
Path: r.Path,
Headers: newHeaders,
}, nil
}
func NewDomainTransportTransform(domain, transport string) Transform {
return &DomainTransportTransform{
domain: domain,
transport: transport,
log: loggy.New("domain-transport-transform"),
}
}
type DomainTransportTransform struct {
domain string
transport string // e.g. https/http
log loggy.Logger
}
func (t *DomainTransportTransform) Modify(r *Request) (*Request, error) {
if r.Domain != t.domain {
return r, nil
}
newTransport := t.transport
t.log.Tracef("setting transport of request to %q", newTransport)
return &Request{
Transport: newTransport,
Domain: r.Domain,
Namespace: r.Namespace,
Version: r.Version,
Path: r.Path,
Headers: r.Headers,
}, nil
}