2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-05-21 19:32:25 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-07-11 14:13:40 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/lib/decode"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-05-21 19:32:25 +00:00
|
|
|
)
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
func loadFromFile(path string) (string, error) {
|
2022-11-10 16:26:01 +00:00
|
|
|
data, err := os.ReadFile(path)
|
2019-04-30 23:27:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read file: %v", err)
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadFromStdin(testStdin io.Reader) (string, error) {
|
2018-05-21 19:32:25 +00:00
|
|
|
var stdin io.Reader = os.Stdin
|
|
|
|
if testStdin != nil {
|
|
|
|
stdin = testStdin
|
|
|
|
}
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
if _, err := io.Copy(&b, stdin); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to read stdin: %v", err)
|
|
|
|
}
|
|
|
|
return b.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadDataSource(data string, testStdin io.Reader) (string, error) {
|
2018-05-21 19:32:25 +00:00
|
|
|
// Handle empty quoted shell parameters
|
|
|
|
if len(data) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch data[0] {
|
|
|
|
case '@':
|
2019-04-30 23:27:16 +00:00
|
|
|
return loadFromFile(data[1:])
|
2018-05-21 19:32:25 +00:00
|
|
|
case '-':
|
|
|
|
if len(data) > 1 {
|
|
|
|
return data, nil
|
|
|
|
}
|
2019-04-30 23:27:16 +00:00
|
|
|
return loadFromStdin(testStdin)
|
2018-05-21 19:32:25 +00:00
|
|
|
default:
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
}
|
2019-04-30 23:27:16 +00:00
|
|
|
|
|
|
|
func LoadDataSourceNoRaw(data string, testStdin io.Reader) (string, error) {
|
|
|
|
if len(data) == 0 {
|
|
|
|
return "", fmt.Errorf("Failed to load data: must specify a file path or '-' for stdin")
|
|
|
|
}
|
|
|
|
|
|
|
|
if data == "-" {
|
|
|
|
return loadFromStdin(testStdin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadFromFile(data)
|
|
|
|
}
|
2022-07-11 14:13:40 +00:00
|
|
|
|
|
|
|
func ParseConfigEntry(data string) (api.ConfigEntry, error) {
|
|
|
|
// parse the data
|
|
|
|
var raw map[string]interface{}
|
|
|
|
if err := hclDecode(&raw, data); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to decode config entry input: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newDecodeConfigEntry(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is a 'structs' variation of this in
|
|
|
|
// agent/structs/config_entry.go:DecodeConfigEntry
|
|
|
|
func newDecodeConfigEntry(raw map[string]interface{}) (api.ConfigEntry, error) {
|
|
|
|
var entry api.ConfigEntry
|
|
|
|
|
|
|
|
kindVal, ok := raw["Kind"]
|
|
|
|
if !ok {
|
|
|
|
kindVal, ok = raw["kind"]
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Payload does not contain a kind/Kind key at the top level")
|
|
|
|
}
|
|
|
|
|
|
|
|
if kindStr, ok := kindVal.(string); ok {
|
|
|
|
newEntry, err := api.MakeConfigEntry(kindStr, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entry = newEntry
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Kind value in payload is not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
|
|
|
decodeConf := &mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
|
|
|
decode.HookWeakDecodeFromSlice,
|
|
|
|
decode.HookTranslateKeys,
|
|
|
|
mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
mapstructure.StringToTimeHookFunc(time.RFC3339),
|
|
|
|
),
|
|
|
|
Metadata: &md,
|
|
|
|
Result: &entry,
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(decodeConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range md.Unused {
|
|
|
|
switch k {
|
|
|
|
case "kind", "Kind":
|
|
|
|
// The kind field is used to determine the target, but doesn't need
|
|
|
|
// to exist on the target.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = multierror.Append(err, fmt.Errorf("invalid config key %q", k))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|