|
| 1 | +// Copyright 2022 The Falco Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package index |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "gopkg.in/yaml.v3" |
| 23 | +) |
| 24 | + |
| 25 | +// ConfigEntry contains information about one of the index that were cached locally. |
| 26 | +// TODO: add support for all the other fields. |
| 27 | +type ConfigEntry struct { |
| 28 | + AddedTimestamp string `yaml:"added_timestamp"` |
| 29 | + // CaFile string `yaml:"caFile"` |
| 30 | + // CertFile string `yaml:"certFile"` |
| 31 | + // InsecureSkipTLSVerify string `yaml:"insecure_skip_tls_verify"` |
| 32 | + // KeyFile string `yaml:"keyFile"` |
| 33 | + Name string `yaml:"name"` |
| 34 | + // PassCredentialsAll string `yaml:"pass_credentials_all"` |
| 35 | + // Password string `yaml:"password"` |
| 36 | + UpdatedTimestamp string `yaml:"updated_timestamp"` |
| 37 | + URL string `yaml:"url"` |
| 38 | + // Username string `yaml:"username"` |
| 39 | +} |
| 40 | + |
| 41 | +// Config aggregates the info about ConfigEntries. |
| 42 | +type Config struct { |
| 43 | + Configs []ConfigEntry `yaml:"configs"` |
| 44 | +} |
| 45 | + |
| 46 | +// NewConfig loads an index config from a file. |
| 47 | +func NewConfig(path string) (*Config, error) { |
| 48 | + var config Config |
| 49 | + file, err := os.ReadFile(filepath.Clean(path)) |
| 50 | + if os.IsNotExist(err) { |
| 51 | + return &config, nil |
| 52 | + } else if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + err = yaml.Unmarshal(file, &config) |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + return &config, nil |
| 62 | +} |
| 63 | + |
| 64 | +// Add adds a new config to Config. |
| 65 | +func (c *Config) Add(entry ConfigEntry) { |
| 66 | + c.Configs = append(c.Configs, entry) |
| 67 | +} |
| 68 | + |
| 69 | +// Remove removes a config by name from an Config. |
| 70 | +func (c *Config) Remove(name string) error { |
| 71 | + for k, conf := range c.Configs { |
| 72 | + if conf.Name == name { |
| 73 | + c.Configs = append(c.Configs[:k], c.Configs[k+1:]...) |
| 74 | + return nil |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return fmt.Errorf("cannot remove index %s: not found", name) |
| 79 | +} |
| 80 | + |
| 81 | +// Get returns a pointer to an entry in a Config. |
| 82 | +func (c *Config) Get(name string) (*ConfigEntry, error) { |
| 83 | + for k, conf := range c.Configs { |
| 84 | + if conf.Name == name { |
| 85 | + return &c.Configs[k], nil |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return nil, fmt.Errorf("not found") |
| 90 | +} |
| 91 | + |
| 92 | +// Write writes an Config to disk. |
| 93 | +func (c *Config) Write(path string) error { |
| 94 | + data, err := yaml.Marshal(c) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + |
| 99 | + err = os.WriteFile(path, data, writePermissions) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
0 commit comments