|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "gopkg.in/yaml.v3" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + // DefaultOptions are the default verification options |
| 12 | + DefaultOptions = &CheckOptions{ |
| 13 | + RekorURL: "https://rekor.sigstore.dev", |
| 14 | + } |
| 15 | + |
| 16 | + // DefaultConfig is the configuration used when none is provided |
| 17 | + DefaultConfig = &Config{ |
| 18 | + Verifiers: []Verifier{ |
| 19 | + { |
| 20 | + Options: DefaultOptions, |
| 21 | + }, |
| 22 | + }, |
| 23 | + } |
| 24 | +) |
| 25 | + |
| 26 | +// LoadConfig loads configuration from a file. If a file isn't provided then it |
| 27 | +// returns the default configuration. |
| 28 | +func LoadConfig(confFile string) (*Config, error) { |
| 29 | + if confFile == "" { |
| 30 | + return DefaultConfig, nil |
| 31 | + } |
| 32 | + |
| 33 | + var c *Config |
| 34 | + |
| 35 | + yamlReader, err := os.Open(confFile) |
| 36 | + if err != nil { |
| 37 | + return c, fmt.Errorf("error reading config file: %s", err) |
| 38 | + } |
| 39 | + defer yamlReader.Close() |
| 40 | + decoder := yaml.NewDecoder(yamlReader) |
| 41 | + decoder.KnownFields(true) |
| 42 | + |
| 43 | + if err = decoder.Decode(&c); err != nil { |
| 44 | + return c, fmt.Errorf("error parsing config file: %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + return c, nil |
| 48 | +} |
| 49 | + |
| 50 | +// Config configures the provider |
| 51 | +type Config struct { |
| 52 | + // Verifiers is a list of verifiers. The validator will iterate over |
| 53 | + // this list until it finds a verifier that matches the image its |
| 54 | + // validating. |
| 55 | + Verifiers []Verifier `yaml:"verifiers"` |
| 56 | +} |
| 57 | + |
| 58 | +// Verifier verifies an image |
| 59 | +type Verifier struct { |
| 60 | + // Image is an image reference, either to a specific image or a pattern. |
| 61 | + // Supports '*' and '?' in the pattern string. |
| 62 | + Image string `yaml:"image,omitempty"` |
| 63 | + |
| 64 | + // Options defines verification options |
| 65 | + Options *CheckOptions `yaml:"options,omitempty"` |
| 66 | +} |
| 67 | + |
| 68 | +// UnmarshalYAML sets default options for the verifier config when they aren't |
| 69 | +// provided |
| 70 | +func (v *Verifier) UnmarshalYAML(unmarshal func(interface{}) error) error { |
| 71 | + type rawVerifier Verifier |
| 72 | + var raw rawVerifier |
| 73 | + if err := unmarshal(&raw); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if raw.Options == nil { |
| 78 | + raw.Options = DefaultOptions |
| 79 | + } |
| 80 | + |
| 81 | + *v = Verifier(raw) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// CheckOptions are the options used to verify the signature of an image |
| 87 | +type CheckOptions struct { |
| 88 | + // Key is a path to a public key file, KMS URI or Kubernetes Secret |
| 89 | + Key string `yaml:"key,omitempty"` |
| 90 | + |
| 91 | + // RekorURL is the address of a rekor STL server |
| 92 | + RekorURL string `yaml:"rekorURL,omitempty"` |
| 93 | +} |
0 commit comments