forked from 0xPolygon/cdk-validium-node
-
Notifications
You must be signed in to change notification settings - Fork 28
/
policy.go
308 lines (279 loc) · 6.48 KB
/
policy.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
package main
import (
"context"
"encoding/csv"
"errors"
"fmt"
"os"
"strings"
"github.com/0xPolygon/cdk-validium-node/config"
"github.com/0xPolygon/cdk-validium-node/pool"
"github.com/0xPolygon/cdk-validium-node/pool/pgpoolstorage"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2"
)
var (
policyFlag = cli.StringFlag{
Name: "policy",
Aliases: []string{"p"},
Usage: "Name of policy to operate on",
Required: false,
}
csvFlag = cli.StringFlag{
Name: "csv",
Usage: "CSV file with addresses",
Required: false,
}
allowFlag = cli.BoolFlag{
Name: "allow",
Usage: "Update policy to 'allow' addresses on list",
Required: false,
}
denyFlag = cli.BoolFlag{
Name: "deny",
Usage: "Update policy to 'deny' addresses on list",
Required: false,
}
noHeaderFlag = cli.BoolFlag{
Name: "no-header",
Value: false,
Required: false,
}
policyActionFlags = []cli.Flag{&policyFlag}
)
var policyCommands = cli.Command{
Name: "policy",
Usage: "View, update, and apply policies",
Action: describe,
Flags: []cli.Flag{&configFileFlag},
Subcommands: []*cli.Command{
{
Name: "add",
Usage: "Add address(es) to a policy exclusion list",
Action: addAcl,
Flags: append(policyActionFlags, &csvFlag),
}, {
Name: "clear",
Usage: "Clear the addresses listed as exceptions to a policy",
Action: clearAcl,
Flags: policyActionFlags,
}, {
Name: "describe",
Usage: "Describe the default actions for the policies",
Action: describe,
Flags: append(policyActionFlags, &noHeaderFlag),
}, {
Name: "remove",
Usage: "Remove address(es) from a policy exclusion list",
Action: removeAcl,
Flags: append(policyActionFlags, &csvFlag),
}, {
Name: "update",
Usage: "Update the default action for a policy",
Action: updatePolicy,
Flags: append(policyActionFlags, &allowFlag, &denyFlag),
},
},
}
func updatePolicy(cli *cli.Context) error {
_, db, err := configAndStorage(cli)
if err != nil {
return err
}
policy, err := resolvePolicy(cli)
if err != nil {
return err
}
allow := cli.Bool(allowFlag.Name)
deny := cli.Bool(denyFlag.Name)
// exactly one must be set
if (allow && deny) || (!allow && !deny) {
return errors.New("supply one policy action [--allow or --deny]")
}
var setting bool
if allow {
setting = true
} else if deny {
setting = false
}
err = db.UpdatePolicy(context.Background(), policy, setting)
if err != nil {
return err
}
return nil
}
func addAcl(cli *cli.Context) error {
_, db, err := configAndStorage(cli)
if err != nil {
return err
}
policy, addresses, err := requirePolicyAndAddresses(cli)
if err != nil {
return err
}
err = db.AddAddressesToPolicy(context.Background(), policy, addresses)
if err != nil {
return err
}
return nil
}
func removeAcl(cli *cli.Context) error {
_, db, err := configAndStorage(cli)
if err != nil {
return err
}
policy, addresses, err := requirePolicyAndAddresses(cli)
if err != nil {
return err
}
err = db.RemoveAddressesFromPolicy(context.Background(), policy, addresses)
if err != nil {
return err
}
return nil
}
func clearAcl(cli *cli.Context) error {
_, db, err := configAndStorage(cli)
if err != nil {
return err
}
policy, err := resolvePolicy(cli)
if err != nil {
return err
}
err = db.ClearPolicy(context.Background(), policy)
if err != nil {
return err
}
return nil
}
func describe(cli *cli.Context) error {
showHeader := !cli.Bool(noHeaderFlag.Name)
if cli.IsSet(policyFlag.Name) {
return describePolicy(cli, showHeader)
}
return describePolicies(cli, showHeader)
}
func describePolicy(cli *cli.Context, showHeader bool) error {
_, db, err := configAndStorage(cli)
if err != nil {
return err
}
policyName, err := resolvePolicy(cli)
if err != nil {
return err
}
if showHeader {
policy, err := db.DescribePolicy(context.Background(), policyName)
if err != nil {
return err
}
fmt.Printf("%s: %s\n", "Policy", policy.Name)
fmt.Printf("%s: %s\n", "Action", policy.Desc())
}
query, err := resolveAddresses(cli, false)
if err != nil {
return nil
}
list, err := db.ListAcl(context.Background(), policyName, query)
if err != nil {
return err
}
if showHeader {
fmt.Println("Addresses:")
}
for _, address := range list {
fmt.Println(address.Hex())
}
return nil
}
func describePolicies(cli *cli.Context, showHeader bool) error {
_, db, err := configAndStorage(cli)
if err != nil {
return err
}
list, err := db.DescribePolicies(context.Background())
if err != nil {
return err
}
if showHeader {
fmt.Printf("%7s: %s\n", "Policy", "Action")
}
for _, p := range list {
fmt.Printf("%7s: %s\n", p.Name, p.Desc())
}
return nil
}
func configAndStorage(cli *cli.Context) (*config.Config, *pgpoolstorage.PostgresPoolStorage, error) {
c, err := config.Load(cli, false)
if err != nil {
return nil, nil, err
}
setupLog(c.Log)
db, err := pgpoolstorage.NewPostgresPoolStorage(c.Pool.DB)
if err != nil {
return nil, nil, err
}
return c, db, nil
}
func requirePolicyAndAddresses(cli *cli.Context) (pool.PolicyName, []common.Address, error) {
policy, err := resolvePolicy(cli)
if err != nil {
return "", nil, err
}
addresses, err := resolveAddresses(cli, true)
if err != nil {
return "", nil, err
}
return policy, addresses, nil
}
func resolvePolicy(cli *cli.Context) (pool.PolicyName, error) {
policy := cli.String(policyFlag.Name)
if policy == "" {
return "", nil
}
if !pool.IsPolicy(policy) {
return "", fmt.Errorf("invalid policy name: %s", policy)
}
return pool.PolicyName(policy), nil
}
func resolveAddresses(cli *cli.Context, failIfEmpty bool) ([]common.Address, error) {
var set = make(map[common.Address]struct{})
if cli.IsSet("csv") {
file := cli.String(csvFlag.Name)
fd, err := os.Open(file)
if err != nil {
return nil, err
}
defer func(fd *os.File) {
_ = fd.Close()
}(fd)
fileReader := csv.NewReader(fd)
records, err := fileReader.ReadAll()
if err != nil {
return nil, err
}
for _, row := range records {
for _, cell := range row {
hex := strings.TrimSpace(cell)
set[common.HexToAddress(hex)] = struct{}{}
}
}
}
for _, a := range cli.Args().Slice() {
a = strings.TrimSpace(a)
a = strings.Trim(a, ",|")
if !strings.HasPrefix(a, "0x") {
a = "0x" + a
}
set[common.HexToAddress(a)] = struct{}{}
}
var ret []common.Address
for a := range set {
ret = append(ret, a)
}
if failIfEmpty && len(ret) == 0 {
return nil, errors.New("no addresses given")
}
return ret, nil
}