2022-10-03 17:25:43 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// 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 settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"codeberg.org/6543/go-yaml2json"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParamsToEnv uses reflection to convert a map[string]interface to a list
|
|
|
|
// of environment variables.
|
2024-03-20 14:53:33 +00:00
|
|
|
func ParamsToEnv(from map[string]any, to map[string]string, prefix string, upper bool, getSecretValue func(name string) (string, error)) (err error) {
|
2022-10-03 17:25:43 +00:00
|
|
|
if to == nil {
|
|
|
|
return fmt.Errorf("no map to write to")
|
|
|
|
}
|
|
|
|
for k, v := range from {
|
|
|
|
if v == nil || len(k) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-20 14:53:33 +00:00
|
|
|
to[sanitizeParamKey(prefix, upper, k)], err = sanitizeParamValue(v, getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// sanitizeParamKey formats the environment variable key.
|
2024-03-20 14:53:33 +00:00
|
|
|
func sanitizeParamKey(prefix string, upper bool, k string) string {
|
2024-10-28 16:23:49 +00:00
|
|
|
r := k
|
2024-03-20 14:53:33 +00:00
|
|
|
if upper {
|
2024-10-28 16:23:49 +00:00
|
|
|
r = strings.ReplaceAll(strings.ReplaceAll(k, ".", "_"), "-", "_")
|
2024-03-20 14:53:33 +00:00
|
|
|
r = strings.ToUpper(r)
|
|
|
|
}
|
|
|
|
return prefix + r
|
2022-10-03 17:25:43 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// isComplex indicate if a data type can be turned into string without encoding as json.
|
2022-10-03 17:25:43 +00:00
|
|
|
func isComplex(t reflect.Kind) bool {
|
|
|
|
switch t {
|
|
|
|
case reflect.Bool,
|
|
|
|
reflect.String,
|
|
|
|
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
|
|
reflect.Float32, reflect.Float64:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// sanitizeParamValue returns the value of a setting as string prepared to be injected as environment variable.
|
2024-01-27 19:59:44 +00:00
|
|
|
func sanitizeParamValue(v any, getSecretValue func(name string) (string, error)) (string, error) {
|
2022-10-03 17:25:43 +00:00
|
|
|
t := reflect.TypeOf(v)
|
|
|
|
vv := reflect.ValueOf(v)
|
|
|
|
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
return strconv.FormatBool(vv.Bool()), nil
|
|
|
|
|
|
|
|
case reflect.String:
|
|
|
|
return vv.String(), nil
|
|
|
|
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
return fmt.Sprintf("%v", vv.Int()), nil
|
|
|
|
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
return fmt.Sprintf("%v", vv.Float()), nil
|
|
|
|
|
|
|
|
case reflect.Map:
|
|
|
|
switch v := v.(type) {
|
|
|
|
// gopkg.in/yaml.v3 only emits this map interface
|
2023-11-12 17:23:48 +00:00
|
|
|
case map[string]any:
|
2022-10-03 17:25:43 +00:00
|
|
|
// check if it's a secret and return value if it's the case
|
2024-01-27 19:59:44 +00:00
|
|
|
value, isSecret, err := injectSecret(v, getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if isSecret {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("could not handle: %#v", v)
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:59:44 +00:00
|
|
|
return handleComplex(vv.Interface(), getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if vv.Len() == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's an interface unwrap and element check happen for each iteration later
|
|
|
|
if t.Elem().Kind() == reflect.Interface ||
|
|
|
|
// else check directly if element is not complex
|
|
|
|
!isComplex(t.Elem().Kind()) {
|
|
|
|
containsComplex := false
|
|
|
|
in := make([]string, vv.Len())
|
|
|
|
|
|
|
|
for i := 0; i < vv.Len(); i++ {
|
|
|
|
v := vv.Index(i).Interface()
|
|
|
|
|
2023-03-17 07:54:53 +00:00
|
|
|
// if we handle a list with a nil entry we just return a empty list
|
|
|
|
if v == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:25:43 +00:00
|
|
|
// ensure each element is not complex
|
|
|
|
if isComplex(reflect.TypeOf(v).Kind()) {
|
|
|
|
containsComplex = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-01-27 19:59:44 +00:00
|
|
|
if in[i], err = sanitizeParamValue(v, getSecretValue); err != nil {
|
2022-10-03 17:25:43 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !containsComplex {
|
|
|
|
return strings.Join(in, ","), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle all elements which are not primitives, string-maps containing secrets or arrays
|
2024-01-27 19:59:44 +00:00
|
|
|
return handleComplex(vv.Interface(), getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// handleComplex uses yaml2json to get json strings as values for environment variables.
|
2024-01-27 19:59:44 +00:00
|
|
|
func handleComplex(v any, getSecretValue func(name string) (string, error)) (string, error) {
|
|
|
|
v, err := injectSecretRecursive(v, getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := yaml.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
out, err = yaml2json.Convert(out)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// injectSecret probes if a map is a from_secret request.
|
|
|
|
// If it's a from_secret request it either returns the secret value or an error if the secret was not found
|
2024-05-13 20:58:21 +00:00
|
|
|
// else it just indicates to progress normally using the provided map as is.
|
2024-01-27 19:59:44 +00:00
|
|
|
func injectSecret(v map[string]any, getSecretValue func(name string) (string, error)) (string, bool, error) {
|
2022-10-03 17:25:43 +00:00
|
|
|
if secretNameI, ok := v["from_secret"]; ok {
|
|
|
|
if secretName, ok := secretNameI.(string); ok {
|
2024-01-27 19:59:44 +00:00
|
|
|
secret, err := getSecretValue(secretName)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
2022-10-03 17:25:43 +00:00
|
|
|
}
|
2024-01-27 19:59:44 +00:00
|
|
|
|
|
|
|
return secret, true, nil
|
2022-10-03 17:25:43 +00:00
|
|
|
}
|
|
|
|
return "", false, fmt.Errorf("from_secret has to be a string")
|
|
|
|
}
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// injectSecretRecursive iterates over all types and if they contain elements
|
2024-05-13 20:58:21 +00:00
|
|
|
// it iterates recursively over them too, using injectSecret internally.
|
2024-01-27 19:59:44 +00:00
|
|
|
func injectSecretRecursive(v any, getSecretValue func(name string) (string, error)) (any, error) {
|
2022-10-03 17:25:43 +00:00
|
|
|
t := reflect.TypeOf(v)
|
|
|
|
|
|
|
|
if !isComplex(t.Kind()) {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
switch v := v.(type) {
|
|
|
|
// gopkg.in/yaml.v3 only emits this map interface
|
2023-11-12 17:23:48 +00:00
|
|
|
case map[string]any:
|
2022-10-03 17:25:43 +00:00
|
|
|
// handle secrets
|
2024-01-27 19:59:44 +00:00
|
|
|
value, isSecret, err := injectSecret(v, getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if isSecret {
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range v {
|
2024-01-27 19:59:44 +00:00
|
|
|
v[key], err = injectSecretRecursive(val, getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return v, fmt.Errorf("could not handle: %#v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
vv := reflect.ValueOf(v)
|
2023-11-12 17:23:48 +00:00
|
|
|
vl := make([]any, vv.Len())
|
2022-10-03 17:25:43 +00:00
|
|
|
|
|
|
|
for i := 0; i < vv.Len(); i++ {
|
2024-01-27 19:59:44 +00:00
|
|
|
v, err := injectSecretRecursive(vv.Index(i).Interface(), getSecretValue)
|
2022-10-03 17:25:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
vl[i] = v
|
|
|
|
}
|
|
|
|
return vl, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|