This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
forked from grafeas/voucher
-
Notifications
You must be signed in to change notification settings - Fork 9
/
check.go
77 lines (62 loc) · 1.58 KB
/
check.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
package diy
import (
"context"
"errors"
"strings"
"time"
"github.com/Shopify/voucher"
"github.com/Shopify/voucher/docker"
)
// ErrNotFromRepo is returned when an image does not match one of the valid
// repo paths.
var ErrNotFromRepo = errors.New("image is not from a valid repo")
// check is a check that verifies if the passed image was built
// by us.
type check struct {
auth voucher.Auth
validRepos []string
}
// SetValidRepos sets the repos that images must be in to get signed by the
// DIY check.
func (d *check) SetValidRepos(repos []string) {
d.validRepos = repos
}
// SetAuth sets the authentication system that this check will use
// for its run.
func (d *check) SetAuth(auth voucher.Auth) {
d.auth = auth
}
// isFromValidrepo returns true if the passed image is from a valid repo.
func (d *check) isFromValidRepo(i voucher.ImageData) bool {
for _, repo := range d.validRepos {
if strings.HasPrefix(i.Name(), repo) {
return true
}
}
return false
}
// check checks if an image was built by a trusted source
func (d *check) Check(i voucher.ImageData) (bool, error) {
if !d.isFromValidRepo(i) {
return false, ErrNotFromRepo
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if nil == d.auth {
return false, voucher.ErrNoAuth
}
client, err := d.auth.ToClient(ctx, i)
if nil != err {
return false, err
}
_, err = docker.RequestImageConfig(client, i)
if nil != err {
return false, err
}
return true, nil
}
func init() {
voucher.RegisterCheckFactory("diy", func() voucher.Check {
return new(check)
})
}