-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
resolver.go
477 lines (403 loc) · 13.9 KB
/
resolver.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package git
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
gitcfg "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/factory"
resolverconfig "github.com/tektoncd/pipeline/pkg/apis/config/resolver"
pipelinev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resolutioncommon "github.com/tektoncd/pipeline/pkg/resolution/common"
"github.com/tektoncd/pipeline/pkg/resolution/resolver/framework"
"go.uber.org/zap"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/cache"
"k8s.io/client-go/kubernetes"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/logging"
)
const (
disabledError = "cannot handle resolution request, enable-git-resolver feature flag not true"
// labelValueGitResolverType is the value to use for the
// resolution.tekton.dev/type label on resource requests
labelValueGitResolverType string = "git"
// gitResolverName is the name that the git resolver should be
// associated with
gitResolverName string = "Git"
// yamlContentType is the content type to use when returning yaml
yamlContentType string = "application/x-yaml"
// ConfigMapName is the git resolver's config map
ConfigMapName = "git-resolver-config"
// cacheSize is the size of the LRU secrets cache
cacheSize = 1024
// ttl is the time to live for a cache entry
ttl = 5 * time.Minute
)
var _ framework.Resolver = &Resolver{}
// Resolver implements a framework.Resolver that can fetch files from git.
type Resolver struct {
kubeClient kubernetes.Interface
logger *zap.SugaredLogger
cache *cache.LRUExpireCache
ttl time.Duration
// Used in testing
clientFunc func(string, string, string, ...factory.ClientOptionFunc) (*scm.Client, error)
}
// Initialize performs any setup required by the gitresolver.
func (r *Resolver) Initialize(ctx context.Context) error {
r.kubeClient = kubeclient.Get(ctx)
r.logger = logging.FromContext(ctx)
r.cache = cache.NewLRUExpireCache(cacheSize)
r.ttl = ttl
if r.clientFunc == nil {
r.clientFunc = factory.NewClient
}
return nil
}
// GetName returns the string name that the gitresolver should be
// associated with.
func (r *Resolver) GetName(_ context.Context) string {
return gitResolverName
}
// GetSelector returns the labels that resource requests are required to have for
// the gitresolver to process them.
func (r *Resolver) GetSelector(_ context.Context) map[string]string {
return map[string]string{
resolutioncommon.LabelKeyResolverType: labelValueGitResolverType,
}
}
// ValidateParams returns an error if the given parameter map is not
// valid for a resource request targeting the gitresolver.
func (r *Resolver) ValidateParams(ctx context.Context, params []pipelinev1beta1.Param) error {
if r.isDisabled(ctx) {
return errors.New(disabledError)
}
_, err := populateDefaultParams(ctx, params)
if err != nil {
return err
}
return nil
}
// Resolve performs the work of fetching a file from git given a map of
// parameters.
func (r *Resolver) Resolve(ctx context.Context, origParams []pipelinev1beta1.Param) (framework.ResolvedResource, error) {
if r.isDisabled(ctx) {
return nil, errors.New(disabledError)
}
params, err := populateDefaultParams(ctx, origParams)
if err != nil {
return nil, err
}
if params[urlParam] != "" {
return r.resolveAnonymousGit(ctx, params)
}
return r.resolveAPIGit(ctx, params)
}
func (r *Resolver) resolveAPIGit(ctx context.Context, params map[string]string) (framework.ResolvedResource, error) {
// If we got here, the "repo" param was specified, so use the API approach
scmType, serverURL, err := r.getSCMTypeAndServerURL(ctx)
if err != nil {
return nil, err
}
apiToken, err := r.getAPIToken(ctx)
if err != nil {
return nil, err
}
scmClient, err := r.clientFunc(scmType, serverURL, string(apiToken))
if err != nil {
return nil, fmt.Errorf("failed to create SCM client: %w", err)
}
orgRepo := fmt.Sprintf("%s/%s", params[orgParam], params[repoParam])
path := params[pathParam]
ref := params[revisionParam]
// fetch the actual content from a file in the repo
content, _, err := scmClient.Contents.Find(ctx, orgRepo, path, ref)
if err != nil {
return nil, fmt.Errorf("couldn't fetch resource content: %w", err)
}
if content == nil || len(content.Data) == 0 {
return nil, fmt.Errorf("no content for resource in %s %s", orgRepo, path)
}
// find the actual git commit sha by the ref
commit, _, err := scmClient.Git.FindCommit(ctx, orgRepo, ref)
if err != nil || commit == nil {
return nil, fmt.Errorf("couldn't fetch the commit sha for the ref %s in the repo: %w", ref, err)
}
// fetch the repository URL
repo, _, err := scmClient.Repositories.Find(ctx, orgRepo)
if err != nil {
return nil, fmt.Errorf("couldn't fetch repository: %w", err)
}
return &resolvedGitResource{
Content: content.Data,
Revision: commit.Sha,
Org: params[orgParam],
Repo: params[repoParam],
Path: content.Path,
URL: repo.Clone,
}, nil
}
func (r *Resolver) resolveAnonymousGit(ctx context.Context, params map[string]string) (framework.ResolvedResource, error) {
conf := framework.GetResolverConfigFromContext(ctx)
repo := params[urlParam]
if repo == "" {
if urlString, ok := conf[defaultURLKey]; ok {
repo = urlString
} else {
return nil, fmt.Errorf("default Git Repo Url was not set during installation of the git resolver")
}
}
revision := params[revisionParam]
if revision == "" {
if revisionString, ok := conf[defaultRevisionKey]; ok {
revision = revisionString
} else {
return nil, fmt.Errorf("default Git Revision was not set during installation of the git resolver")
}
}
cloneOpts := &git.CloneOptions{
URL: repo,
}
filesystem := memfs.New()
repository, err := git.Clone(memory.NewStorage(), filesystem, cloneOpts)
if err != nil {
return nil, fmt.Errorf("clone error: %w", err)
}
// try fetch the branch when the given revision refers to a branch name
refSpec := gitcfg.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/remotes/%s", revision, revision))
err = repository.Fetch(&git.FetchOptions{
RefSpecs: []gitcfg.RefSpec{refSpec},
})
if err != nil {
var fetchErr git.NoMatchingRefSpecError
if !errors.As(err, &fetchErr) {
return nil, fmt.Errorf("unexpected fetch error: %v", err)
}
}
w, err := repository.Worktree()
if err != nil {
return nil, fmt.Errorf("worktree error: %v", err)
}
h, err := repository.ResolveRevision(plumbing.Revision(revision))
if err != nil {
return nil, fmt.Errorf("revision error: %v", err)
}
err = w.Checkout(&git.CheckoutOptions{
Hash: *h,
})
if err != nil {
return nil, fmt.Errorf("checkout error: %v", err)
}
path := params[pathParam]
f, err := filesystem.Open(path)
if err != nil {
return nil, fmt.Errorf("error opening file %q: %v", path, err)
}
buf := &bytes.Buffer{}
_, err = io.Copy(buf, f)
if err != nil {
return nil, fmt.Errorf("error reading file %q: %v", path, err)
}
return &resolvedGitResource{
Revision: h.String(),
Content: buf.Bytes(),
URL: params[urlParam],
Path: params[pathParam],
}, nil
}
var _ framework.ConfigWatcher = &Resolver{}
// GetConfigName returns the name of the git resolver's configmap.
func (r *Resolver) GetConfigName(context.Context) string {
return ConfigMapName
}
var _ framework.TimedResolution = &Resolver{}
// GetResolutionTimeout returns a time.Duration for the amount of time a
// single git fetch may take. This can be configured with the
// fetch-timeout field in the git-resolver-config configmap.
func (r *Resolver) GetResolutionTimeout(ctx context.Context, defaultTimeout time.Duration) time.Duration {
conf := framework.GetResolverConfigFromContext(ctx)
if timeoutString, ok := conf[defaultTimeoutKey]; ok {
timeout, err := time.ParseDuration(timeoutString)
if err == nil {
return timeout
}
}
return defaultTimeout
}
func (r *Resolver) isDisabled(ctx context.Context) bool {
cfg := resolverconfig.FromContextOrDefaults(ctx)
if cfg.FeatureFlags.EnableGitResolver {
return false
}
return true
}
// resolvedGitResource implements framework.ResolvedResource and returns
// the resolved file []byte data and an annotation map for any metadata.
type resolvedGitResource struct {
Revision string
Content []byte
Org string
Repo string
Path string
URL string
}
var _ framework.ResolvedResource = &resolvedGitResource{}
// Data returns the bytes of the file resolved from git.
func (r *resolvedGitResource) Data() []byte {
return r.Content
}
// Annotations returns the metadata that accompanies the file fetched
// from git.
func (r *resolvedGitResource) Annotations() map[string]string {
m := map[string]string{
AnnotationKeyRevision: r.Revision,
AnnotationKeyPath: r.Path,
AnnotationKeyURL: r.URL,
resolutioncommon.AnnotationKeyContentType: yamlContentType,
}
if r.Org != "" {
m[AnnotationKeyOrg] = r.Org
}
if r.Repo != "" {
m[AnnotationKeyRepo] = r.Repo
}
return m
}
// Source is the source reference of the remote data that records where the remote
// file came from including the url, digest and the entrypoint.
func (r *resolvedGitResource) Source() *pipelinev1beta1.ConfigSource {
return &pipelinev1beta1.ConfigSource{
URI: spdxGit(r.URL),
Digest: map[string]string{
"sha1": r.Revision,
},
EntryPoint: r.Path,
}
}
type secretCacheKey struct {
ns string
name string
key string
}
func (r *Resolver) getSCMTypeAndServerURL(ctx context.Context) (string, string, error) {
conf := framework.GetResolverConfigFromContext(ctx)
scmType, ok := conf[SCMTypeKey]
if !ok || scmType == "" {
return "", "", fmt.Errorf("missing or empty %s value in configmap", SCMTypeKey)
}
serverURL := conf[ServerURLKey]
return scmType, serverURL, nil
}
func (r *Resolver) getAPIToken(ctx context.Context) ([]byte, error) {
conf := framework.GetResolverConfigFromContext(ctx)
cacheKey := secretCacheKey{}
ok := false
if cacheKey.name, ok = conf[APISecretNameKey]; !ok || cacheKey.name == "" {
err := fmt.Errorf("cannot get API token, required when specifying '%s' param, '%s' not specified in config", repoParam, APISecretNameKey)
r.logger.Info(err)
return nil, err
}
if cacheKey.key, ok = conf[APISecretKeyKey]; !ok || cacheKey.key == "" {
err := fmt.Errorf("cannot get API token, required when specifying '%s' param, '%s' not specified in config", repoParam, APISecretKeyKey)
r.logger.Info(err)
return nil, err
}
if cacheKey.ns, ok = conf[APISecretNamespaceKey]; !ok {
cacheKey.ns = os.Getenv("SYSTEM_NAMESPACE")
}
val, ok := r.cache.Get(cacheKey)
if ok {
return val.([]byte), nil
}
secret, err := r.kubeClient.CoreV1().Secrets(cacheKey.ns).Get(ctx, cacheKey.name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
notFoundErr := fmt.Errorf("cannot get API token, secret %s not found in namespace %s", cacheKey.name, cacheKey.ns)
r.logger.Info(notFoundErr)
return nil, notFoundErr
}
wrappedErr := fmt.Errorf("error reading API token from secret %s in namespace %s: %w", cacheKey.name, cacheKey.ns, err)
r.logger.Info(wrappedErr)
return nil, wrappedErr
}
secretVal, ok := secret.Data[cacheKey.key]
if !ok {
err := fmt.Errorf("cannot get API token, key %s not found in secret %s in namespace %s", cacheKey.key, cacheKey.name, cacheKey.ns)
r.logger.Info(err)
return nil, err
}
r.cache.Add(cacheKey, secretVal, r.ttl)
return secretVal, nil
}
func populateDefaultParams(ctx context.Context, params []pipelinev1beta1.Param) (map[string]string, error) {
conf := framework.GetResolverConfigFromContext(ctx)
paramsMap := make(map[string]string)
for _, p := range params {
paramsMap[p.Name] = p.Value.StringVal
}
var missingParams []string
if _, ok := paramsMap[revisionParam]; !ok {
if defaultRevision, ok := conf[defaultRevisionKey]; ok {
paramsMap[revisionParam] = defaultRevision
} else {
missingParams = append(missingParams, revisionParam)
}
}
if _, ok := paramsMap[pathParam]; !ok {
missingParams = append(missingParams, pathParam)
}
if paramsMap[urlParam] != "" && paramsMap[repoParam] != "" {
return nil, fmt.Errorf("cannot specify both '%s' and '%s'", urlParam, repoParam)
}
if paramsMap[urlParam] == "" && paramsMap[repoParam] == "" {
if urlString, ok := conf[defaultURLKey]; ok {
paramsMap[urlParam] = urlString
} else {
return nil, fmt.Errorf("must specify one of '%s' or '%s'", urlParam, repoParam)
}
}
if paramsMap[repoParam] != "" {
if _, ok := paramsMap[orgParam]; !ok {
if defaultOrg, ok := conf[defaultOrgKey]; ok {
paramsMap[orgParam] = defaultOrg
} else {
return nil, fmt.Errorf("'%s' is required when '%s' is specified", orgParam, repoParam)
}
}
}
if len(missingParams) > 0 {
return nil, fmt.Errorf("missing required git resolver params: %s", strings.Join(missingParams, ", "))
}
// TODO(sbwsg): validate repo url is well-formed, git:// or https://
// TODO(sbwsg): validate pathInRepo is valid relative pathInRepo
return paramsMap, nil
}
// supports the SPDX format which is recommended by in-toto
// ref: https://spdx.dev/spdx-specification-21-web-version/#h.49x2ik5
// ref: https://github.com/in-toto/attestation/blob/main/spec/field_types.md
func spdxGit(url string) string {
return "git+" + url
}