forked from sonm-io/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whitelist.go
187 lines (153 loc) · 4.32 KB
/
whitelist.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
package hub
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
"sync"
"time"
"github.com/docker/distribution/reference"
dc "github.com/docker/docker/client"
"github.com/ethereum/go-ethereum/common"
log "github.com/noxiouz/zapctx/ctxlog"
"github.com/pkg/errors"
"github.com/sonm-io/core/util"
"go.uber.org/zap"
)
type Whitelist interface {
Allowed(ctx context.Context, registry string, image string, auth string) (bool, reference.Named, error)
}
func NewWhitelist(ctx context.Context, config *WhitelistConfig) Whitelist {
if config.Enabled != nil && !*config.Enabled {
return &disabledWhitelist{}
}
wl := whitelist{
superusers: make(map[string]struct{}),
}
for _, su := range config.PrivilegedAddresses {
parsed := common.HexToAddress(su)
wl.superusers[parsed.Hex()] = struct{}{}
}
go wl.updateRoutine(ctx, config.Url, config.RefreshPeriod)
return &wl
}
type WhitelistRecord struct {
AllowedHashes []string `json:"allowed_hashes"`
}
type whitelist struct {
superusers map[string]struct{}
Records map[string]WhitelistRecord
RecordsMu sync.RWMutex
}
func (w *whitelist) updateRoutine(ctx context.Context, url string, updatePeriod uint) error {
ticker := util.NewImmediateTicker(time.Duration(time.Second * time.Duration(updatePeriod)))
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
err := w.load(ctx, url)
if err != nil {
log.G(ctx).Error("could not load whitelist", zap.Error(err))
}
}
}
}
func (w *whitelist) load(ctx context.Context, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
log.G(ctx).Info("fetched whitelist")
if resp.StatusCode != 200 {
return fmt.Errorf("failed to download whitelist - got %s", resp.Status)
}
return w.fillFromJsonReader(ctx, resp.Body)
}
func (w *whitelist) fillFromJsonReader(ctx context.Context, jsonReader io.Reader) error {
decoder := json.NewDecoder(jsonReader)
r := make(map[string]WhitelistRecord)
err := decoder.Decode(&r)
if err != nil {
return errors.Wrap(err, "could not decode whitelist data")
}
w.RecordsMu.Lock()
w.Records = r
w.RecordsMu.Unlock()
return nil
}
func (w *whitelist) digestAllowed(name string, digest string) (bool, error) {
ref, err := reference.ParseNormalizedNamed(name)
if err != nil {
return false, err
}
w.RecordsMu.RLock()
defer w.RecordsMu.RUnlock()
record, ok := w.Records[ref.Name()]
if !ok {
return false, nil
}
for _, allowedDigest := range record.AllowedHashes {
if allowedDigest == digest {
return true, nil
}
}
return false, nil
}
func (w *whitelist) Allowed(ctx context.Context, registry string, image string, auth string) (bool, reference.Named, error) {
fullName := filepath.Join(registry, image)
ref, err := reference.ParseNormalizedNamed(fullName)
if err != nil {
return false, nil, err
}
signedWallet, err := extractWalletFromContext(ctx)
if err != nil {
log.G(ctx).Warn("could not extract wallet from context", zap.Error(err))
}
wallet, err := util.VerifySelfSignedWallet(signedWallet)
if err != nil {
return false, nil, err
}
_, ok := w.superusers[wallet]
if ok {
return true, ref, nil
}
digestedRef, isDigested := ref.(reference.Digested)
if isDigested {
if err != nil {
return false, nil, err
}
allowed, err := w.digestAllowed(ref.Name(), (string)(digestedRef.Digest()))
return allowed, ref, err
}
dockerClient, err := dc.NewEnvClient()
if err != nil {
return false, nil, err
}
defer dockerClient.Close()
inspection, err := dockerClient.DistributionInspect(ctx, image, auth)
if err != nil {
return false, nil, errors.Wrap(err, "could not perform DistributionInspect")
}
ref, err = reference.ParseNormalizedNamed(ref.String() + "@" + (string)(inspection.Descriptor.Digest))
if err != nil {
// This should never happen
panic("logical error - can not append digest and parse")
}
allowed, err := w.digestAllowed(ref.Name(), (string)(inspection.Descriptor.Digest))
return allowed, ref, err
}
type disabledWhitelist struct {
}
func (w *disabledWhitelist) Allowed(ctx context.Context, registry string, image string, auth string) (bool, reference.Named, error) {
fullName := filepath.Join(registry, image)
ref, err := reference.ParseNormalizedNamed(fullName)
if err != nil {
return false, nil, err
}
return true, ref, nil
}