joao/pkg/config/config.go

230 lines
5.0 KiB
Go
Raw Normal View History

// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
2022-12-31 06:14:08 +00:00
// SPDX-License-Identifier: Apache-2.0
package config
import (
"fmt"
2022-12-20 05:49:37 +00:00
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
op "github.com/1Password/connect-sdk-go/onepassword"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
var annotationsSection = &op.ItemSection{
ID: "~annotations",
Label: "~annotations",
}
var defaultItemFields = []*op.ItemField{
{
ID: "password",
Type: "CONCEALED",
Purpose: "PASSWORD",
Label: "password",
Value: "hash",
}, {
ID: "notesPlain",
Type: "STRING",
Purpose: "NOTES",
Label: "notesPlain",
Value: "flushed by joao",
},
}
2022-12-17 05:40:43 +00:00
type Config struct {
Vault string
Name string
Tree *Entry
}
// Delete a value at path.
func (cfg *Config) Delete(path []string) error {
parent := cfg.Tree
for idx, key := range path {
if len(path)-1 == idx {
newContents := []*Entry{}
found := false
for idx, child := range parent.Content {
if child.Name() == key {
found = true
logrus.Debugf("Deleting %s", strings.Join(path, "."))
if parent.Kind == yaml.DocumentNode || parent.Kind == yaml.MappingNode {
newContents = newContents[0 : idx-1]
}
continue
}
newContents = append(newContents, child)
}
if !found {
return fmt.Errorf("no value found at %s", key)
}
parent.Content = newContents
break
}
parent = parent.ChildNamed(key)
if parent == nil {
return fmt.Errorf("no value found at %s", key)
}
}
return nil
}
// Set a new value, optionally parsing the supplied bytes as a secret or a JSON-encoded value.
func (cfg *Config) Set(path []string, data []byte, isSecret, parseEntry bool) error {
newEntry := NewEntry(path[len(path)-1], yaml.ScalarNode)
newEntry.Path = path
valueStr := string(data)
newEntry.Value = valueStr
if parseEntry {
if err := yaml.Unmarshal(data, newEntry); err != nil {
return err
}
2023-01-11 04:50:06 +00:00
if newEntry.Kind == yaml.MappingNode || newEntry.Kind == yaml.SequenceNode {
newEntry.Style = yaml.FoldedStyle | yaml.LiteralStyle
for _, v := range newEntry.Content {
v.Style = yaml.FlowStyle
}
}
} else {
valueStr = strings.Trim(valueStr, "\n")
if isSecret {
newEntry.Style = yaml.TaggedStyle
2022-12-14 05:41:03 +00:00
newEntry.Tag = YAMLTypeSecret
}
newEntry.Kind = yaml.ScalarNode
newEntry.Value = valueStr
if !strings.Contains(valueStr, "\n") {
newEntry.Style &= yaml.LiteralStyle
} else {
newEntry.Style &= yaml.FlowStyle
}
}
entry := cfg.Tree
for idx, key := range path {
if len(path)-1 == idx {
2023-01-11 04:50:06 +00:00
if dst := entry.ChildNamed(key); dst == nil {
key := NewEntry(key, yaml.ScalarNode)
if entry.Kind == yaml.MappingNode {
entry.Content = append(entry.Content, key, newEntry)
} else {
2023-01-11 04:50:06 +00:00
entry.Kind = yaml.SequenceNode
entry.Content = append(entry.Content, newEntry)
}
} else {
dst.Value = newEntry.Value
dst.Tag = newEntry.Tag
dst.Style = newEntry.Style
}
break
}
if child := entry.ChildNamed(key); child != nil {
entry = child
continue
}
kind := yaml.MappingNode
2023-01-11 04:50:06 +00:00
if idx+1 == len(path)-1 && isNumeric(path[idx+1]) {
kind = yaml.SequenceNode
}
sub := NewEntry(key, kind)
sub.Path = append([]string{}, entry.Path...)
sub.Path = append(sub.Path, key)
2023-01-11 04:50:06 +00:00
keyEntry := NewEntry(sub.Name(), yaml.ScalarNode)
keyEntry.Value = key
entry.Content = append(entry.Content, keyEntry, sub)
entry = sub
}
return nil
}
2022-12-18 18:16:46 +00:00
func (cfg *Config) Merge(other *Config) error {
return cfg.Tree.Merge(other.Tree)
}
2022-12-20 05:49:37 +00:00
func (cfg *Config) DiffRemote(path string, stdout io.Writer, stderr io.Writer) error {
remote, err := Load(path, true)
if err != nil {
return err
}
localBytes, err := cfg.AsYAML(OutputModeNoComments, OutputModeSorted, OutputModeNoConfig)
if err != nil {
return err
}
file1, cleanupLocalDiff, err := tempfile(localBytes)
if err != nil {
return err
}
defer cleanupLocalDiff()
remoteBytes, err := remote.AsYAML(OutputModeNoComments, OutputModeSorted)
if err != nil {
return err
}
file2, cleanupRemoteDiff, err := tempfile(remoteBytes)
if err != nil {
return err
}
defer cleanupRemoteDiff()
opPath := fmt.Sprintf("op://%s/%s", cfg.Vault, remote.Name)
diff := exec.Command("diff", "-u", "-L", path, file1, "-L", opPath, file2)
diff.Env = os.Environ()
diff.Stdout = stdout
diff.Stderr = stderr
2023-01-10 07:02:10 +00:00
if err := diff.Run(); err != nil {
2023-01-11 06:57:58 +00:00
if _, ok := err.(*exec.ExitError); ok {
if diff.ProcessState.ExitCode() == 1 {
return nil
}
}
2023-01-10 07:02:10 +00:00
return fmt.Errorf("diff could not run: %w", err)
}
2022-12-20 05:49:37 +00:00
if diff.ProcessState.ExitCode() > 2 {
return fmt.Errorf("diff exited with exit code %d", diff.ProcessState.ExitCode())
}
return nil
}
func tempfile(data []byte) (string, func(), error) {
f, err := ioutil.TempFile("", "joao-diff")
if err != nil {
return "", nil, err
}
if _, err := f.Write(data); err != nil {
return "", nil, err
}
if err := f.Close(); err != nil {
return "", nil, err
}
deferFn := func() {
if err := os.Remove(f.Name()); err != nil {
logrus.Error(err)
}
}
return f.Name(), deferFn, nil
}