2022-12-17 05:40:43 +00:00
|
|
|
// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
|
2022-12-31 06:14:08 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-12-17 05:40:43 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-05 04:48:29 +00:00
|
|
|
"os"
|
2023-01-15 07:17:34 +00:00
|
|
|
"path/filepath"
|
2022-12-20 05:49:37 +00:00
|
|
|
"strings"
|
2022-12-17 05:40:43 +00:00
|
|
|
|
2022-12-20 05:49:37 +00:00
|
|
|
opClient "git.rob.mx/nidito/joao/internal/op-client"
|
2022-12-17 05:40:43 +00:00
|
|
|
op "github.com/1Password/connect-sdk-go/onepassword"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-01-15 08:19:05 +00:00
|
|
|
const warnChecksumMismatch = `1Password item %s changed and checksum was not updated.
|
2022-12-20 05:49:37 +00:00
|
|
|
Expected: %s
|
|
|
|
found : %s`
|
|
|
|
|
|
|
|
func Load(ref string, preferRemote bool) (*Config, error) {
|
|
|
|
if preferRemote {
|
|
|
|
name := ref
|
|
|
|
vault := ""
|
|
|
|
|
|
|
|
if argIsYAMLFile(ref) {
|
2023-01-17 22:25:18 +00:00
|
|
|
path, err := filepath.Abs(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find asbolute path to file %s: %w", ref, err)
|
|
|
|
}
|
|
|
|
name, vault, err = VaultAndNameFrom(path, nil)
|
2022-12-20 05:49:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parts := strings.SplitN(ref, "/", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
vault = parts[0]
|
|
|
|
name = parts[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := opClient.Get(vault, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromOP(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !argIsYAMLFile(ref) {
|
|
|
|
return nil, fmt.Errorf("could not load %s from local as it's not a path", ref)
|
|
|
|
}
|
|
|
|
|
2023-01-15 07:17:34 +00:00
|
|
|
path, err := filepath.Abs(ref)
|
2023-01-11 04:50:06 +00:00
|
|
|
if err != nil {
|
2023-01-15 07:17:34 +00:00
|
|
|
return nil, fmt.Errorf("could not find asbolute path to file %s: %w", ref, err)
|
|
|
|
}
|
|
|
|
cfg, err := FromFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not load file %s: %w", path, err)
|
2023-01-11 04:50:06 +00:00
|
|
|
}
|
|
|
|
return cfg, nil
|
2022-12-20 05:49:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:43 +00:00
|
|
|
// FromFile reads a path and returns a config.
|
|
|
|
func FromFile(path string) (*Config, error) {
|
2023-08-05 04:48:29 +00:00
|
|
|
buf, err := os.ReadFile(path)
|
2022-12-17 05:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read file %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) == 0 {
|
|
|
|
buf = []byte("{}")
|
|
|
|
}
|
|
|
|
|
2023-01-11 06:57:58 +00:00
|
|
|
name, vault, err := VaultAndNameFrom(path, buf)
|
2022-12-17 05:40:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Found name: %s and vault: %s", name, vault)
|
|
|
|
|
|
|
|
cfg, err := FromYAML(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg.Name = name
|
|
|
|
cfg.Vault = vault
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromYAML reads yaml bytes and returns a config.
|
|
|
|
func FromYAML(data []byte) (*Config, error) {
|
|
|
|
cfg := &Config{
|
|
|
|
Tree: NewEntry("root", yaml.MappingNode),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := yaml.Unmarshal(data, &cfg.Tree)
|
|
|
|
if err != nil {
|
2023-01-11 04:50:06 +00:00
|
|
|
return nil, fmt.Errorf("could not parse %w", err)
|
2022-12-17 05:40:43 +00:00
|
|
|
}
|
2023-01-15 07:17:34 +00:00
|
|
|
cfg.Tree.SetPath([]string{}, ".")
|
2022-12-17 05:40:43 +00:00
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromOP reads a config from an op item and returns a config.
|
|
|
|
func FromOP(item *op.Item) (*Config, error) {
|
|
|
|
cfg := &Config{
|
|
|
|
Vault: item.Vault.ID,
|
|
|
|
Name: item.Title,
|
|
|
|
Tree: NewEntry("root", yaml.MappingNode),
|
|
|
|
}
|
|
|
|
|
2023-08-05 04:48:29 +00:00
|
|
|
if cs := opClient.Checksum(item.Fields); cs != item.GetValue("password") {
|
2023-01-15 08:19:05 +00:00
|
|
|
logrus.Warnf(warnChecksumMismatch, fmt.Sprintf("%s/%s", item.Vault.ID, item.Title), cs, item.GetValue("password"))
|
2022-12-18 18:16:46 +00:00
|
|
|
}
|
2022-12-17 05:40:43 +00:00
|
|
|
err := cfg.Tree.FromOP(item.Fields)
|
|
|
|
return cfg, err
|
|
|
|
}
|