1// Copyright (C) 2019 Storj Labs, Inc.
2// See LICENSE for copying information.
3
4package trust
5
6import (
7	"strings"
8	"time"
9
10	"github.com/zeebo/errs"
11)
12
13// Config is the trust configuration.
14type Config struct {
15	Sources         Sources       `help:"list of trust sources" devDefault:"" releaseDefault:"https://www.storj.io/dcs-satellites"`
16	Exclusions      Exclusions    `help:"list of trust exclusions" devDefault:"" releaseDefault:""`
17	RefreshInterval time.Duration `help:"how often the trust pool should be refreshed" default:"6h"`
18	CachePath       string        `help:"file path where trust lists should be cached" default:"${CONFDIR}/trust-cache.json"`
19}
20
21// Sources is a list of sources that implements pflag.Value.
22type Sources []Source
23
24// String returns the string representation of the config.
25func (sources Sources) String() string {
26	s := make([]string, 0, len(sources))
27	for _, source := range sources {
28		s = append(s, source.String())
29	}
30	return strings.Join(s, ",")
31}
32
33// Set implements pflag.Value by parsing a comma separated list of sources.
34func (sources *Sources) Set(value string) error {
35	var entries []string
36	if value != "" {
37		entries = strings.Split(value, ",")
38	}
39
40	var toSet []Source
41	for _, entry := range entries {
42		source, err := NewSource(entry)
43		if err != nil {
44			return Error.New("invalid source %q: %w", entry, errs.Unwrap(err))
45		}
46		toSet = append(toSet, source)
47	}
48
49	*sources = toSet
50	return nil
51}
52
53// Type returns the type of the pflag.Value.
54func (sources Sources) Type() string {
55	return "trust-sources"
56}
57
58// Exclusions is a list of excluding rules that implements pflag.Value.
59type Exclusions struct {
60	Rules Rules
61}
62
63// String returns the string representation of the config.
64func (exclusions *Exclusions) String() string {
65	s := make([]string, 0, len(exclusions.Rules))
66	for _, rule := range exclusions.Rules {
67		s = append(s, rule.String())
68	}
69	return strings.Join(s, ",")
70}
71
72// Set implements pflag.Value by parsing a comma separated list of exclusions.
73func (exclusions *Exclusions) Set(value string) error {
74	var entries []string
75	if value != "" {
76		entries = strings.Split(value, ",")
77	}
78
79	var rules Rules
80	for _, entry := range entries {
81		rule, err := NewExcluder(entry)
82		if err != nil {
83			return Error.New("invalid exclusion %q: %w", entry, errs.Unwrap(err))
84		}
85		rules = append(rules, rule)
86	}
87
88	exclusions.Rules = rules
89	return nil
90}
91
92// Type returns the type of the pflag.Value.
93func (exclusions Exclusions) Type() string {
94	return "trust-exclusions"
95}
96