| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package cato_networks |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "net" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 13 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | defaultUpdateEvery = 60 |
| 18 | defaultEndpoint = "https://api.catonetworks.com/api/v1/graphql2" |
| 19 | defaultDiscoveryEvery = 3600 |
| 20 | defaultDiscoveryLimit = 100 |
| 21 | defaultMetricsTimeFrame = "last.PT5M" |
| 22 | defaultMetricsBuckets = 5 |
| 23 | defaultMetricsParallel = 4 |
| 24 | defaultNoBGPCacheTTL = 3600 |
| 25 | defaultEntitySelector = "*" |
| 26 | maxDiscoveryPages = 1000 |
| 27 | ) |
| 28 | |
| 29 | var defaultTimeout = confopt.Duration(30 * time.Second) |
| 30 | |
| 31 | type Config struct { |
| 32 | Vnode string `yaml:"vnode,omitempty" json:"vnode,omitempty"` |
| 33 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every,omitempty"` |
| 34 | AutoDetectionRetry int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry,omitempty"` |
| 35 | |
| 36 | AccountID string `yaml:"account_id" json:"account_id"` |
| 37 | APIKey string `yaml:"api_key" json:"api_key"` |
| 38 | |
| 39 | web.HTTPConfig `yaml:",inline" json:""` |
| 40 | |
| 41 | SiteSelector string `yaml:"site_selector,omitempty" json:"site_selector,omitempty"` |
| 42 | } |
| 43 | |
| 44 | func (c *Config) applyDefaults() { |
| 45 | c.AccountID = strings.TrimSpace(c.AccountID) |
| 46 | c.APIKey = strings.TrimSpace(c.APIKey) |
| 47 | c.URL = strings.TrimSpace(c.URL) |
| 48 | c.SiteSelector = strings.TrimSpace(c.SiteSelector) |
| 49 | |
| 50 | if c.UpdateEvery <= 0 { |
| 51 | c.UpdateEvery = defaultUpdateEvery |
| 52 | } |
| 53 | if c.URL == "" { |
| 54 | c.URL = defaultEndpoint |
| 55 | } |
| 56 | if c.SiteSelector == "" { |
| 57 | c.SiteSelector = defaultEntitySelector |
| 58 | } |
| 59 | if c.Timeout.Duration() == 0 { |
| 60 | c.Timeout = defaultTimeout |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (c Config) validate() error { |
| 65 | var errs []error |
| 66 | |
| 67 | if strings.TrimSpace(c.AccountID) == "" { |
| 68 | errs = append(errs, errors.New("'account_id' is required")) |
| 69 | } |
| 70 | if strings.TrimSpace(c.APIKey) == "" { |
| 71 | errs = append(errs, errors.New("'api_key' is required")) |
| 72 | } |
| 73 | if strings.TrimSpace(c.URL) == "" { |
| 74 | errs = append(errs, errors.New("'url' is required")) |
| 75 | } else if u, err := url.Parse(strings.TrimSpace(c.URL)); err != nil || u.Scheme == "" || u.Host == "" { |
| 76 | errs = append(errs, errors.New("'url' must be a valid absolute URL")) |
| 77 | } else if u.Scheme != "https" && !(u.Scheme == "http" && isLoopbackHost(u.Hostname())) { |
| 78 | errs = append(errs, errors.New("'url' scheme must be https unless using a loopback HTTP endpoint")) |
| 79 | } |
| 80 | if c.UpdateEvery < 60 { |
| 81 | errs = append(errs, errors.New("'update_every' must be >= 60 seconds")) |
| 82 | } |
| 83 | |
| 84 | return errors.Join(errs...) |
| 85 | } |
| 86 | |
| 87 | func isLoopbackHost(host string) bool { |
| 88 | host = strings.TrimSpace(host) |
| 89 | if strings.EqualFold(host, "localhost") { |
| 90 | return true |
| 91 | } |
| 92 | ip := net.ParseIP(host) |
| 93 | return ip != nil && ip.IsLoopback() |
| 94 | } |