|
| 1 | +package vault |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type ProviderType string |
| 12 | + |
| 13 | +const ( |
| 14 | + ProviderTypeLocal ProviderType = "local" |
| 15 | + ProviderTypeExternal ProviderType = "external" |
| 16 | +) |
| 17 | + |
| 18 | +type Config struct { |
| 19 | + ID string `json:"id"` |
| 20 | + Type ProviderType `json:"type"` |
| 21 | + Local *LocalConfig `json:"local,omitempty"` |
| 22 | + External *ExternalConfig `json:"external,omitempty"` |
| 23 | +} |
| 24 | + |
| 25 | +func (c *Config) Validate() error { |
| 26 | + if c.ID == "" { |
| 27 | + return fmt.Errorf("vault ID is required") |
| 28 | + } |
| 29 | + |
| 30 | + switch c.Type { |
| 31 | + case ProviderTypeLocal: |
| 32 | + if c.Local == nil { |
| 33 | + return fmt.Errorf("local configuration required for local vault") |
| 34 | + } |
| 35 | + return c.Local.Validate() |
| 36 | + case ProviderTypeExternal: |
| 37 | + if c.External == nil { |
| 38 | + return fmt.Errorf("external configuration required for external vault") |
| 39 | + } |
| 40 | + return c.External.Validate() |
| 41 | + default: |
| 42 | + return fmt.Errorf("unsupported vault type: %s", c.Type) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// SaveConfigJSON saves the vault configuration to a file in JSON format |
| 47 | +func SaveConfigJSON(config Config, path string) error { |
| 48 | + data, err := json.MarshalIndent(config, "", " ") |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("failed to marshal config: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 54 | + return fmt.Errorf("failed to create config directory: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + if err := os.WriteFile(path, data, 0600); err != nil { |
| 58 | + return fmt.Errorf("failed to write config file: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// LoadConfigJSON loads the vault configuration from a file in JSON format |
| 65 | +func LoadConfigJSON(path string) (Config, error) { |
| 66 | + data, err := os.ReadFile(path) |
| 67 | + if err != nil { |
| 68 | + return Config{}, fmt.Errorf("failed to read config file: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + var config Config |
| 72 | + if err := json.Unmarshal(data, &config); err != nil { |
| 73 | + return Config{}, fmt.Errorf("failed to unmarshal config: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return config, nil |
| 77 | +} |
| 78 | + |
| 79 | +// IdentitySource represents a source for the local vault identity keys |
| 80 | +type IdentitySource struct { |
| 81 | + // Type of identity source |
| 82 | + // Must be one of: "env", "file", "ssh-agent" |
| 83 | + Type string `json:"type"` |
| 84 | + // Path to the identity file (for "file" type) |
| 85 | + Path string `json:"fullPath,omitempty"` |
| 86 | + // Environment variable name (for "env" type) |
| 87 | + Name string `json:"name,omitempty"` |
| 88 | +} |
| 89 | + |
| 90 | +// LocalConfig contains local (age-based) vault configuration |
| 91 | +type LocalConfig struct { |
| 92 | + // Storage location for the vault file |
| 93 | + StoragePath string `json:"storage_path"` |
| 94 | + |
| 95 | + // Identity sources for decryption (in order of preference) |
| 96 | + IdentitySources []IdentitySource `json:"identity_sources,omitempty"` |
| 97 | + |
| 98 | + // Recipients who can decrypt secrets |
| 99 | + Recipients []string `json:"recipients,omitempty"` |
| 100 | +} |
| 101 | + |
| 102 | +func (c *LocalConfig) Validate() error { |
| 103 | + if c.StoragePath == "" { |
| 104 | + return fmt.Errorf("storage fullPath is required for local vault") |
| 105 | + } |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// CommandSet defines the command templates for external vault operations |
| 110 | +type CommandSet struct { |
| 111 | + Get string `json:"get"` |
| 112 | + Set string `json:"set"` |
| 113 | + Delete string `json:"delete"` |
| 114 | + List string `json:"list"` |
| 115 | + Exists string `json:"exists,omitempty"` |
| 116 | +} |
| 117 | + |
| 118 | +// ExternalConfig contains external (cli command-based) vault configuration |
| 119 | +type ExternalConfig struct { |
| 120 | + // Command templates for operations |
| 121 | + Commands CommandSet `json:"commands"` |
| 122 | + |
| 123 | + // Environment variables for commands |
| 124 | + Environment map[string]string `json:"environment,omitempty"` |
| 125 | + |
| 126 | + // Timeout for command execution |
| 127 | + Timeout time.Duration `json:"timeout,omitempty"` |
| 128 | + |
| 129 | + // WorkingDir for command execution |
| 130 | + WorkingDir string `json:"working_dir,omitempty"` |
| 131 | +} |
| 132 | + |
| 133 | +func (c *ExternalConfig) Validate() error { |
| 134 | + if c.Commands.Get == "" || c.Commands.Set == "" { |
| 135 | + return fmt.Errorf("get and set commands are required for external vault") |
| 136 | + } |
| 137 | + return nil |
| 138 | +} |
0 commit comments