2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2018-05-21 19:32:25 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2023-08-10 19:49:51 +00:00
|
|
|
"strings"
|
2022-07-11 14:13:40 +00:00
|
|
|
"time"
|
|
|
|
|
2023-08-10 19:49:51 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
2022-07-11 14:13:40 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/lib/decode"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2018-05-21 19:32:25 +00:00
|
|
|
)
|
|
|
|
|
2023-09-08 12:45:24 +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 '@':
|
2023-09-08 12:45:24 +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)
|
|
|
|
}
|
|
|
|
|
2023-09-08 12:45:24 +00:00
|
|
|
return LoadFromFile(data)
|
2019-04-30 23:27:16 +00:00
|
|
|
}
|
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 {
|
2023-08-10 19:49:51 +00:00
|
|
|
switch {
|
|
|
|
case strings.ToLower(k) == "kind":
|
2022-07-11 14:13:40 +00:00
|
|
|
// The kind field is used to determine the target, but doesn't need
|
|
|
|
// to exist on the target.
|
|
|
|
continue
|
2023-08-10 19:49:51 +00:00
|
|
|
|
|
|
|
case strings.HasSuffix(strings.ToLower(k), "namespace"):
|
|
|
|
err = multierror.Append(err, fmt.Errorf("invalid config key %q, namespaces are a consul enterprise feature", k))
|
|
|
|
case strings.Contains(strings.ToLower(k), "jwt"):
|
|
|
|
err = multierror.Append(err, fmt.Errorf("invalid config key %q, api-gateway jwt validation is a consul enterprise feature", k))
|
|
|
|
default:
|
|
|
|
err = multierror.Append(err, fmt.Errorf("invalid config key %q", k))
|
2022-07-11 14:13:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry, nil
|
|
|
|
}
|