252 lines
7.2 KiB
Go
252 lines
7.2 KiB
Go
package connectors
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
cpb "github.com/samcharles93/archie-core/internal/contracts/connector/v1"
|
|
gpb "github.com/samcharles93/archie-core/internal/contracts/guard/v1"
|
|
sepb "github.com/samcharles93/archie-core/internal/contracts/secretengine/v1"
|
|
)
|
|
|
|
// Setting is one value an operator enters for an extension to run. The
|
|
// extension declares it, and the dashboard renders it.
|
|
type Setting struct {
|
|
Key string `json:"key"`
|
|
Label string `json:"label"`
|
|
Description string `json:"description,omitempty"`
|
|
Secret bool `json:"secret,omitempty"`
|
|
Required bool `json:"required,omitempty"`
|
|
Type string `json:"type,omitempty"`
|
|
Options []string `json:"options,omitempty"`
|
|
}
|
|
|
|
// Setting types. The empty type is a string.
|
|
const (
|
|
SettingString = "string"
|
|
SettingNumber = "number"
|
|
SettingBoolean = "boolean"
|
|
SettingDuration = "duration"
|
|
SettingList = "list"
|
|
SettingSelect = "select"
|
|
SettingText = "text"
|
|
)
|
|
|
|
// kind returns the setting's type with the default applied.
|
|
func (s Setting) kind() string {
|
|
if s.Type == "" {
|
|
return SettingString
|
|
}
|
|
return s.Type
|
|
}
|
|
|
|
var settingKey = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
|
|
|
|
// validateSettingsSchema checks what an extension declared, so a broken schema
|
|
// is reported when it starts rather than when a value is edited.
|
|
func validateSettingsSchema(settings []Setting) error {
|
|
seen := make(map[string]bool, len(settings))
|
|
for _, s := range settings {
|
|
switch {
|
|
case !settingKey.MatchString(s.Key):
|
|
return fmt.Errorf("setting %q must be lower-case letters, digits and underscores", s.Key)
|
|
case seen[s.Key]:
|
|
return fmt.Errorf("setting %q is declared twice", s.Key)
|
|
case strings.TrimSpace(s.Label) == "":
|
|
return fmt.Errorf("setting %q needs a label", s.Key)
|
|
case s.Secret != strings.HasSuffix(s.Key, "_ref"):
|
|
return fmt.Errorf("setting %q: a secret setting's key ends in _ref, and only a secret's does", s.Key)
|
|
}
|
|
if err := s.validateType(); err != nil {
|
|
return err
|
|
}
|
|
seen[s.Key] = true
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s Setting) validateType() error {
|
|
switch s.kind() {
|
|
case SettingString, SettingNumber, SettingBoolean, SettingDuration, SettingList, SettingSelect, SettingText:
|
|
default:
|
|
return fmt.Errorf("setting %q: unknown type %q", s.Key, s.Type)
|
|
}
|
|
if s.Secret && s.kind() != SettingString {
|
|
return fmt.Errorf("setting %q: a secret setting is a string", s.Key)
|
|
}
|
|
if s.Required && s.kind() == SettingBoolean {
|
|
return fmt.Errorf("setting %q: a boolean cannot be required, false counts as filled", s.Key)
|
|
}
|
|
if s.kind() != SettingSelect {
|
|
if len(s.Options) > 0 {
|
|
return fmt.Errorf("setting %q: options belong to a select setting", s.Key)
|
|
}
|
|
return nil
|
|
}
|
|
if len(s.Options) == 0 {
|
|
return fmt.Errorf("setting %q: a select setting needs options", s.Key)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkSettings checks values against the schema the extension declared and
|
|
// returns them trimmed, with empty values dropped.
|
|
func checkSettings(schema []Setting, values map[string]string) (map[string]string, error) {
|
|
declared := make(map[string]Setting, len(schema))
|
|
for _, s := range schema {
|
|
declared[s.Key] = s
|
|
}
|
|
out := make(map[string]string, len(values))
|
|
for key, value := range values {
|
|
s, ok := declared[key]
|
|
if !ok {
|
|
return nil, fmt.Errorf("setting %q is not declared by the extension", key)
|
|
}
|
|
value = strings.TrimSpace(value)
|
|
if value == "" {
|
|
continue
|
|
}
|
|
checked, err := typedValue(s, value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out[key] = checked
|
|
}
|
|
for _, s := range schema {
|
|
if s.Required && out[s.Key] == "" {
|
|
return nil, fmt.Errorf("%s is required", s.Label)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// typedValue checks a non-empty value against the setting's type and returns
|
|
// its normalised form.
|
|
func typedValue(s Setting, value string) (string, error) {
|
|
if s.Secret {
|
|
if err := checkSecretRef(s.Label, value); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
switch s.kind() {
|
|
case SettingNumber:
|
|
return checkNumber(s.Label, value)
|
|
case SettingBoolean:
|
|
return checkBoolean(s.Label, value)
|
|
case SettingDuration:
|
|
return checkDuration(s.Label, value)
|
|
case SettingList:
|
|
return checkList(s.Label, value)
|
|
case SettingSelect:
|
|
return checkSelect(s, value)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func checkSecretRef(label, value string) error {
|
|
engine, ref, found := strings.Cut(value, ":")
|
|
if !found || engine == "" || ref == "" {
|
|
return fmt.Errorf("%s must be a secret reference (engine:key)", label)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func checkNumber(label, value string) (string, error) {
|
|
if _, err := strconv.ParseFloat(value, 64); err != nil {
|
|
return "", fmt.Errorf("%s must be a number", label)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func checkBoolean(label, value string) (string, error) {
|
|
if value != "true" && value != "false" {
|
|
return "", fmt.Errorf("%s must be true or false", label)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func checkDuration(label, value string) (string, error) {
|
|
if d, err := time.ParseDuration(value); err != nil || d <= 0 {
|
|
return "", fmt.Errorf("%s must be a duration such as 30s", label)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func checkList(label, value string) (string, error) {
|
|
items := strings.Split(value, ",")
|
|
for i, item := range items {
|
|
items[i] = strings.TrimSpace(item)
|
|
if items[i] == "" {
|
|
return "", fmt.Errorf("%s must be a comma-separated list without empty items", label)
|
|
}
|
|
}
|
|
return strings.Join(items, ","), nil
|
|
}
|
|
|
|
func checkSelect(s Setting, value string) (string, error) {
|
|
if !slices.Contains(s.Options, value) {
|
|
return "", fmt.Errorf("%s must be one of %s", s.Label, strings.Join(s.Options, ", "))
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
// settingTypeName maps a declared type, wire-compatible across the extension
|
|
// contracts, to its API name.
|
|
func settingTypeName(t int32) string {
|
|
switch cpb.SettingType(t) {
|
|
case cpb.SettingType_SETTING_TYPE_NUMBER:
|
|
return SettingNumber
|
|
case cpb.SettingType_SETTING_TYPE_BOOLEAN:
|
|
return SettingBoolean
|
|
case cpb.SettingType_SETTING_TYPE_DURATION:
|
|
return SettingDuration
|
|
case cpb.SettingType_SETTING_TYPE_LIST:
|
|
return SettingList
|
|
case cpb.SettingType_SETTING_TYPE_SELECT:
|
|
return SettingSelect
|
|
case cpb.SettingType_SETTING_TYPE_TEXT:
|
|
return SettingText
|
|
}
|
|
return SettingString
|
|
}
|
|
|
|
func connectorSettings(in []*cpb.Setting) []Setting {
|
|
out := make([]Setting, 0, len(in))
|
|
for _, s := range in {
|
|
out = append(out, Setting{
|
|
Key: s.GetKey(), Label: s.GetLabel(), Description: s.GetDescription(),
|
|
Secret: s.GetSecret(), Required: s.GetRequired(), Type: settingTypeName(int32(s.GetType())),
|
|
Options: s.GetOptions(),
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func guardSettings(in []*gpb.Setting) []Setting {
|
|
out := make([]Setting, 0, len(in))
|
|
for _, s := range in {
|
|
out = append(out, Setting{
|
|
Key: s.GetKey(), Label: s.GetLabel(), Description: s.GetDescription(),
|
|
Secret: s.GetSecret(), Required: s.GetRequired(), Type: settingTypeName(int32(s.GetType())),
|
|
Options: s.GetOptions(),
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func engineSettings(in []*sepb.Setting) []Setting {
|
|
out := make([]Setting, 0, len(in))
|
|
for _, s := range in {
|
|
out = append(out, Setting{
|
|
Key: s.GetKey(), Label: s.GetLabel(), Description: s.GetDescription(),
|
|
Secret: s.GetSecret(), Required: s.GetRequired(), Type: settingTypeName(int32(s.GetType())),
|
|
Options: s.GetOptions(),
|
|
})
|
|
}
|
|
return out
|
|
}
|