2022-11-16 06:46:14 +00:00
|
|
|
// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-11-16 07:38:04 +00:00
|
|
|
"bytes"
|
2022-12-14 05:41:03 +00:00
|
|
|
"crypto/md5"
|
2022-11-16 06:46:14 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-12-09 06:43:07 +00:00
|
|
|
"io/ioutil"
|
2022-11-22 06:16:54 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2022-11-16 06:46:14 +00:00
|
|
|
|
|
|
|
op "github.com/1Password/connect-sdk-go/onepassword"
|
2022-11-22 06:16:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-11-16 06:46:14 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
const YAMLTypeSecret string = "!!secret"
|
|
|
|
const YAMLTypeMetaConfig string = "!!joao"
|
|
|
|
|
2022-11-16 06:46:14 +00:00
|
|
|
type Config struct {
|
|
|
|
Vault string
|
|
|
|
Name string
|
|
|
|
Tree *Entry
|
|
|
|
}
|
|
|
|
|
|
|
|
var redactOutput = false
|
2022-11-22 06:16:54 +00:00
|
|
|
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-11-16 06:46:14 +00:00
|
|
|
|
2022-11-22 06:16:54 +00:00
|
|
|
func (cfg *Config) ToMap() map[string]any {
|
|
|
|
ret := map[string]any{}
|
|
|
|
for _, child := range cfg.Tree.Content {
|
2022-12-14 05:41:03 +00:00
|
|
|
if child.Name() == "" {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-22 06:16:54 +00:00
|
|
|
ret[child.Name()] = child.AsMap()
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) ToOP() *op.Item {
|
|
|
|
sections := []*op.ItemSection{annotationsSection}
|
2022-11-22 06:16:54 +00:00
|
|
|
fields := append([]*op.ItemField{}, defaultItemFields...)
|
2022-11-16 06:46:14 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
newHash := md5.New()
|
|
|
|
datafields := cfg.Tree.ToOP()
|
|
|
|
for _, field := range datafields {
|
|
|
|
newHash.Write([]byte(field.ID + field.Value))
|
|
|
|
}
|
|
|
|
fields[0].Value = fmt.Sprintf("%x", newHash.Sum(nil))
|
|
|
|
fields = append(fields, datafields...)
|
|
|
|
|
|
|
|
for i := 0; i < len(cfg.Tree.Content); i += 2 {
|
|
|
|
value := cfg.Tree.Content[i+1]
|
|
|
|
if value.Type == YAMLTypeMetaConfig {
|
2022-11-16 06:46:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
if value.Kind == yaml.MappingNode {
|
2022-11-16 06:46:14 +00:00
|
|
|
sections = append(sections, &op.ItemSection{
|
2022-12-14 05:41:03 +00:00
|
|
|
ID: value.Name(),
|
|
|
|
Label: value.Name(),
|
2022-11-16 06:46:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &op.Item{
|
|
|
|
Title: cfg.Name,
|
|
|
|
Sections: sections,
|
2022-12-09 06:43:07 +00:00
|
|
|
Vault: op.ItemVault{ID: cfg.Vault},
|
2022-11-16 06:46:14 +00:00
|
|
|
Category: op.Password,
|
|
|
|
Fields: fields,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 06:43:07 +00:00
|
|
|
type opDetails struct {
|
|
|
|
Vault string `yaml:"vault"`
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
NameTemplate string `yaml:"nameTemplate"`
|
2022-12-14 05:41:03 +00:00
|
|
|
Repo string
|
2022-12-09 06:43:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// type opConfig interface {
|
|
|
|
// Name() string
|
|
|
|
// Vault() string
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// type inFileConfig struct {
|
|
|
|
// *opDetails
|
|
|
|
// *yaml.Node
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// type virtualConfig struct {
|
|
|
|
// *opDetails
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// func (ifc *inFileConfig) MarshalYAML() (any, error) {
|
|
|
|
// return ifc.Node, nil
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// func (vc *virtualConfig) MarshalYAML() (any, error) {
|
|
|
|
// return nil, nil
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// func (ifc *inFileConfig) UnmarshalYAML(node *yaml.Node) error {
|
|
|
|
// ifc.Node = node
|
|
|
|
// d := &opDetails{}
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// if err := node.Decode(&d); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// ifc.opDetails = d
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// return nil
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// func (ifc *inFileConfig) Name() string {
|
|
|
|
// return ifc.opDetails.Name
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// func (ifc *inFileConfig) Vault() string {
|
|
|
|
// return ifc.opDetails.Name
|
|
|
|
// }
|
2022-12-09 06:43:07 +00:00
|
|
|
|
|
|
|
type singleModeConfig struct {
|
|
|
|
Config *opDetails `yaml:"_config,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// FromFile reads a path and returns a config.
|
|
|
|
func FromFile(path string) (*Config, error) {
|
2022-12-09 06:43:07 +00:00
|
|
|
buf, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read file %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) == 0 {
|
|
|
|
buf = []byte("{}")
|
|
|
|
}
|
|
|
|
|
|
|
|
name, vault, err := vaultAndNameFrom(path, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Found name: %s and vault: %s", name, vault)
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
cfg, err := FromYAML(buf)
|
2022-12-09 06:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cfg.Name = name
|
|
|
|
cfg.Vault = vault
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// FromYAML reads yaml bytes and returns a config.
|
|
|
|
func FromYAML(data []byte) (*Config, error) {
|
2022-11-16 06:46:14 +00:00
|
|
|
cfg := &Config{
|
2022-12-09 06:43:07 +00:00
|
|
|
Tree: NewEntry("root", yaml.MappingNode),
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
err := yaml.Unmarshal(data, &cfg.Tree)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-22 06:16:54 +00:00
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scalarsIn(data map[string]yaml.Node, parents []string) ([]string, error) {
|
|
|
|
keys := []string{}
|
|
|
|
for key, leaf := range data {
|
2022-12-14 05:41:03 +00:00
|
|
|
if key == "_config" && len(parents) == 0 {
|
2022-11-22 06:16:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch leaf.Kind {
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
newKey := strings.Join(append(parents, key), ".")
|
|
|
|
keys = append(keys, newKey)
|
|
|
|
case yaml.MappingNode, yaml.DocumentNode, yaml.SequenceNode:
|
|
|
|
sub := map[string]yaml.Node{}
|
|
|
|
if leaf.Kind == yaml.SequenceNode {
|
|
|
|
list := []yaml.Node{}
|
|
|
|
if err := leaf.Decode(&list); err != nil {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, child := range list {
|
|
|
|
sub[fmt.Sprintf("%d", idx)] = child
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := leaf.Decode(&sub); err != nil {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret, err := scalarsIn(sub, append(parents, key))
|
|
|
|
if err != nil {
|
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
keys = append(keys, ret...)
|
|
|
|
default:
|
2022-12-09 06:43:07 +00:00
|
|
|
logrus.Fatalf("found unknown %v at %s", leaf.Kind, key)
|
2022-11-22 06:16:54 +00:00
|
|
|
}
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 06:16:54 +00:00
|
|
|
sort.Strings(keys)
|
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func KeysFromYAML(data []byte) ([]string, error) {
|
|
|
|
cfg := map[string]yaml.Node{}
|
2022-12-14 05:41:03 +00:00
|
|
|
err := yaml.Unmarshal(data, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-22 06:16:54 +00:00
|
|
|
|
|
|
|
return scalarsIn(cfg, []string{})
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 05:41:03 +00:00
|
|
|
// FromOP reads a config from an op item and returns a config.
|
|
|
|
func FromOP(item *op.Item) (*Config, error) {
|
2022-11-16 06:46:14 +00:00
|
|
|
cfg := &Config{
|
|
|
|
Vault: item.Vault.ID,
|
|
|
|
Name: item.Title,
|
2022-11-22 06:16:54 +00:00
|
|
|
Tree: NewEntry("root", yaml.MappingNode),
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := cfg.Tree.FromOP(item.Fields)
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2022-11-22 06:16:54 +00:00
|
|
|
func (cfg *Config) MarshalYAML() (any, error) {
|
2022-11-16 06:46:14 +00:00
|
|
|
return cfg.Tree.MarshalYAML()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) AsYAML(redacted bool) ([]byte, error) {
|
|
|
|
redactOutput = redacted
|
2022-11-16 07:38:04 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
enc := yaml.NewEncoder(&out)
|
|
|
|
enc.SetIndent(2)
|
2022-12-14 05:41:03 +00:00
|
|
|
if err := enc.Encode(cfg); err != nil {
|
2022-11-16 06:46:14 +00:00
|
|
|
return nil, fmt.Errorf("could not serialize config as yaml: %w", err)
|
|
|
|
}
|
2022-11-16 07:38:04 +00:00
|
|
|
return out.Bytes(), nil
|
2022-11-16 06:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) AsJSON(redacted bool, item bool) ([]byte, error) {
|
2022-11-22 06:16:54 +00:00
|
|
|
var repr any
|
2022-11-16 06:46:14 +00:00
|
|
|
if item {
|
|
|
|
repr = cfg.ToOP()
|
|
|
|
} else {
|
|
|
|
redactOutput = redacted
|
|
|
|
repr = cfg.ToMap()
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(repr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not serialize config as json: %w", err)
|
|
|
|
}
|
|
|
|
return bytes, nil
|
|
|
|
}
|
2022-11-22 06:16:54 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueStr = strings.Trim(valueStr, "\n")
|
|
|
|
if isSecret {
|
|
|
|
newEntry.Style = yaml.TaggedStyle
|
2022-12-14 05:41:03 +00:00
|
|
|
newEntry.Tag = YAMLTypeSecret
|
2022-11-22 06:16:54 +00:00
|
|
|
}
|
|
|
|
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 {
|
|
|
|
dst := entry.ChildNamed(key)
|
|
|
|
if dst == nil {
|
|
|
|
if entry.Kind == yaml.MappingNode {
|
|
|
|
key := NewEntry(key, yaml.ScalarNode)
|
|
|
|
entry.Content = append(entry.Content, key, newEntry)
|
|
|
|
} else {
|
|
|
|
entry.Content = append(entry.Content, newEntry)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Infof("setting %v", newEntry.Path)
|
|
|
|
dst.Value = newEntry.Value
|
|
|
|
dst.Tag = newEntry.Tag
|
|
|
|
dst.Style = newEntry.Style
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if child := entry.ChildNamed(key); child != nil {
|
|
|
|
logrus.Infof("found child named %s, with len %v", key, len(child.Content))
|
|
|
|
entry = child
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("no child named %s found in %s", key, entry.Name())
|
|
|
|
kind := yaml.MappingNode
|
|
|
|
if isNumeric(key) {
|
|
|
|
kind = yaml.SequenceNode
|
|
|
|
}
|
|
|
|
sub := NewEntry(key, kind)
|
|
|
|
sub.Path = append(entry.Path, key)
|
|
|
|
entry.Content = append(entry.Content, sub)
|
|
|
|
entry = sub
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|