|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// API holds configuration for the Clair API services. |
| 9 | +type API struct { |
| 10 | + // V1 is the configuration for the HTTP v1 API. |
| 11 | + V1 APIv1 `yaml:"v1,omitempty" json:"v1,omitempty"` |
| 12 | +} |
| 13 | + |
| 14 | +func (a *API) validate(_ Mode) ([]Warning, error) { |
| 15 | + // TODO(hank) When there's an "UpdaterMode," don't bother with validating |
| 16 | + // the API configurations. |
| 17 | + |
| 18 | + enabled := slices.ContainsFunc([]*bool{}, func(e *bool) bool { |
| 19 | + return e != nil && *e |
| 20 | + }) |
| 21 | + // With multiple versions, the highest one should be the default, probably. |
| 22 | + if !enabled { |
| 23 | + a.V1.Enabled = &[]bool{true}[0] // TODO(go1.26) Use the "new(true)" syntax. |
| 24 | + } |
| 25 | + |
| 26 | + return nil, nil |
| 27 | +} |
| 28 | + |
| 29 | +// APIv1 holds configuration values for the HTTP v1 API. |
| 30 | +type APIv1 struct { |
| 31 | + // Enabled configures enabling the API server at all. |
| 32 | + // The set of API endpoints served by any one process depends on the mode |
| 33 | + // the process is started in. |
| 34 | + // |
| 35 | + // If unset, defaults to "true". |
| 36 | + Enabled *bool `yaml:"enabled" json:"enabled"` |
| 37 | + |
| 38 | + // Network configures the network type to be used for serving API requests. |
| 39 | + // |
| 40 | + // If unset, [DefaultAPIv1Network] will be used. |
| 41 | + // See also: [net.Dial]. |
| 42 | + Network string `yaml:"network" json:"network"` |
| 43 | + |
| 44 | + // Address configures the address to listen on for serving API requests. |
| 45 | + // The format depends on the "network" member. |
| 46 | + // |
| 47 | + // If unset, [DefaultAPIv1Address] will be used. |
| 48 | + // See also: [net.Dial]. |
| 49 | + Address string `yaml:"address" json:"address"` |
| 50 | + |
| 51 | + // IdleTimeout configures whether the Clair process should exit after not |
| 52 | + // handling any requests for a specified non-zero duration. |
| 53 | + IdleTimeout Duration `yaml:"idle_timeout" json:"idle_timeout"` |
| 54 | + |
| 55 | + // TLS configures HTTPS support. |
| 56 | + // |
| 57 | + // Note that any non-trivial deployment means the certificate provided here |
| 58 | + // will need to be for the name the load balancer used to connect to a given |
| 59 | + // Clair instance. |
| 60 | + TLS *TLS `yaml:"tls,omitempty" json:"tls,omitempty"` |
| 61 | +} |
| 62 | + |
| 63 | +func (a *APIv1) validate(_ Mode) ([]Warning, error) { |
| 64 | + if a.Enabled == nil || !*a.Enabled { |
| 65 | + return nil, nil |
| 66 | + } |
| 67 | + if a.Network == "" { |
| 68 | + a.Network = DefaultAPIv1Network |
| 69 | + } |
| 70 | + if a.Address == "" { |
| 71 | + a.Address = DefaultAPIv1Address |
| 72 | + } |
| 73 | + |
| 74 | + return a.lint() |
| 75 | +} |
| 76 | + |
| 77 | +func (a *APIv1) lint() (ws []Warning, err error) { |
| 78 | + if a.Network == "" { |
| 79 | + ws = append(ws, Warning{ |
| 80 | + path: ".network", |
| 81 | + msg: `listen network not provided, default will be used`, |
| 82 | + }) |
| 83 | + } |
| 84 | + if a.Address == "" { |
| 85 | + ws = append(ws, Warning{ |
| 86 | + path: ".address", |
| 87 | + msg: `listen address not provided, default will be used`, |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + switch dur := time.Duration(a.IdleTimeout); { |
| 92 | + case dur == 0: // OK, disabled. |
| 93 | + case dur < (2 * time.Minute): |
| 94 | + ws = append(ws, Warning{ |
| 95 | + path: ".idle_timeout", |
| 96 | + msg: `idle timeout seems short, may cause frequent startups`, |
| 97 | + }) |
| 98 | + default: // OK, reasonably long. |
| 99 | + } |
| 100 | + |
| 101 | + return ws, nil |
| 102 | +} |
0 commit comments