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
|
2019-04-06 13:44:04 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-27 19:59:44 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-04-06 13:44:04 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-11-23 14:36:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-12-13 19:33:07 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParamsToEnv(t *testing.T) {
|
2023-11-12 17:23:48 +00:00
|
|
|
from := map[string]any{
|
2022-03-19 11:34:32 +00:00
|
|
|
"skip": nil,
|
|
|
|
"string": "stringz",
|
|
|
|
"int": 1,
|
|
|
|
"float": 1.2,
|
|
|
|
"bool": true,
|
|
|
|
"slice": []int{1, 2, 3},
|
2023-11-12 17:23:48 +00:00
|
|
|
"map": map[string]any{"hello": "world"},
|
2022-03-19 11:34:32 +00:00
|
|
|
"complex": []struct{ Name string }{{"Jack"}, {"Jill"}},
|
|
|
|
"complex2": struct{ Name string }{"Jack"},
|
|
|
|
"from.address": "noreply@example.com",
|
|
|
|
"tags": stringsToInterface("next", "latest"),
|
|
|
|
"tag": stringsToInterface("next"),
|
2023-11-12 17:23:48 +00:00
|
|
|
"my_secret": map[string]any{"from_secret": "secret_token"},
|
|
|
|
"UPPERCASE_SECRET": map[string]any{"from_secret": "SECRET_TOKEN"},
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
want := map[string]string{
|
2022-03-19 11:34:32 +00:00
|
|
|
"PLUGIN_STRING": "stringz",
|
|
|
|
"PLUGIN_INT": "1",
|
|
|
|
"PLUGIN_FLOAT": "1.2",
|
|
|
|
"PLUGIN_BOOL": "true",
|
|
|
|
"PLUGIN_SLICE": "1,2,3",
|
|
|
|
"PLUGIN_MAP": `{"hello":"world"}`,
|
|
|
|
"PLUGIN_COMPLEX": `[{"name":"Jack"},{"name":"Jill"}]`,
|
|
|
|
"PLUGIN_COMPLEX2": `{"name":"Jack"}`,
|
|
|
|
"PLUGIN_FROM_ADDRESS": "noreply@example.com",
|
|
|
|
"PLUGIN_TAG": "next",
|
|
|
|
"PLUGIN_TAGS": "next,latest",
|
|
|
|
"PLUGIN_MY_SECRET": "FooBar",
|
|
|
|
"PLUGIN_UPPERCASE_SECRET": "FooBar",
|
2021-12-13 19:33:07 +00:00
|
|
|
}
|
2022-10-03 17:25:43 +00:00
|
|
|
secrets := map[string]string{
|
|
|
|
"secret_token": "FooBar",
|
2021-12-13 19:33:07 +00:00
|
|
|
}
|
|
|
|
got := map[string]string{}
|
2024-01-27 19:59:44 +00:00
|
|
|
getSecretValue := func(name string) (string, error) {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
secret, ok := secrets[name]
|
|
|
|
if ok {
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:12:40 +00:00
|
|
|
assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue))
|
2021-12-13 19:33:07 +00:00
|
|
|
assert.EqualValues(t, want, got, "Problem converting plugin parameters to environment variables")
|
2023-03-17 07:54:53 +00:00
|
|
|
|
|
|
|
// handle edge cases (#1609)
|
|
|
|
got = map[string]string{}
|
2024-02-25 09:12:40 +00:00
|
|
|
assert.NoError(t, ParamsToEnv(map[string]any{"a": []any{"a", nil}}, got, "PLUGIN_", nil))
|
2023-03-17 07:54:53 +00:00
|
|
|
assert.EqualValues(t, map[string]string{"PLUGIN_A": "a,"}, got)
|
2021-12-13 19:33:07 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 09:12:40 +00:00
|
|
|
func TestParamsToEnvPrefix(t *testing.T) {
|
|
|
|
from := map[string]any{
|
|
|
|
"string": "stringz",
|
|
|
|
"int": 1,
|
|
|
|
}
|
|
|
|
wantPrefixPlugin := map[string]string{
|
|
|
|
"PLUGIN_STRING": "stringz",
|
|
|
|
"PLUGIN_INT": "1",
|
|
|
|
}
|
|
|
|
got := map[string]string{}
|
|
|
|
getSecretValue := func(name string) (string, error) {
|
|
|
|
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue))
|
|
|
|
assert.EqualValues(t, wantPrefixPlugin, got, "Problem converting plugin parameters to environment variables")
|
|
|
|
|
|
|
|
wantNoPrefix := map[string]string{
|
|
|
|
"STRING": "stringz",
|
|
|
|
"INT": "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle edge cases (#1609)
|
|
|
|
got = map[string]string{}
|
|
|
|
assert.NoError(t, ParamsToEnv(from, got, "", getSecretValue))
|
|
|
|
assert.EqualValues(t, wantNoPrefix, got, "Problem converting plugin parameters to environment variables")
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:25:43 +00:00
|
|
|
func TestSanitizeParamKey(t *testing.T) {
|
2024-02-25 09:12:40 +00:00
|
|
|
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", "dry-run"))
|
|
|
|
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", "dry_Run"))
|
|
|
|
assert.EqualValues(t, "PLUGIN_DRY_RUN", sanitizeParamKey("PLUGIN_", "dry.run"))
|
2022-10-03 17:25:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 19:33:07 +00:00
|
|
|
func TestYAMLToParamsToEnv(t *testing.T) {
|
|
|
|
fromYAML := []byte(`skip: ~
|
|
|
|
string: stringz
|
|
|
|
int: 1
|
|
|
|
float: 1.2
|
|
|
|
bool: true
|
|
|
|
slice: [1, 2, 3]
|
|
|
|
my_secret:
|
|
|
|
from_secret: secret_token
|
2022-10-03 17:25:43 +00:00
|
|
|
map:
|
|
|
|
key: "value"
|
|
|
|
entry2:
|
|
|
|
- "a"
|
|
|
|
- "b"
|
|
|
|
- 3
|
|
|
|
secret:
|
|
|
|
from_secret: secret_token
|
|
|
|
list.map:
|
|
|
|
- registry: https://codeberg.org
|
|
|
|
username: "6543"
|
|
|
|
password:
|
|
|
|
from_secret: cb_password
|
2021-12-13 19:33:07 +00:00
|
|
|
`)
|
2023-11-12 17:23:48 +00:00
|
|
|
var from map[string]any
|
2021-12-13 19:33:07 +00:00
|
|
|
err := yaml.Unmarshal(fromYAML, &from)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
want := map[string]string{
|
|
|
|
"PLUGIN_STRING": "stringz",
|
|
|
|
"PLUGIN_INT": "1",
|
|
|
|
"PLUGIN_FLOAT": "1.2",
|
|
|
|
"PLUGIN_BOOL": "true",
|
|
|
|
"PLUGIN_SLICE": "1,2,3",
|
|
|
|
"PLUGIN_MY_SECRET": "FooBar",
|
2022-10-03 17:25:43 +00:00
|
|
|
"PLUGIN_MAP": `{"entry2":["a","b",3],"key":"value","secret":"FooBar"}`,
|
|
|
|
"PLUGIN_LIST_MAP": `[{"password":"geheim","registry":"https://codeberg.org","username":"6543"}]`,
|
2021-12-13 19:33:07 +00:00
|
|
|
}
|
2022-10-03 17:25:43 +00:00
|
|
|
secrets := map[string]string{
|
|
|
|
"secret_token": "FooBar",
|
|
|
|
"cb_password": "geheim",
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
got := map[string]string{}
|
2024-01-27 19:59:44 +00:00
|
|
|
getSecretValue := func(name string) (string, error) {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
secret, ok := secrets[name]
|
|
|
|
if ok {
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:12:40 +00:00
|
|
|
assert.NoError(t, ParamsToEnv(from, got, "PLUGIN_", getSecretValue))
|
2021-12-04 12:23:33 +00:00
|
|
|
assert.EqualValues(t, want, got, "Problem converting plugin parameters to environment variables")
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
2021-12-08 17:17:52 +00:00
|
|
|
|
2021-12-13 19:33:07 +00:00
|
|
|
func TestYAMLToParamsToEnvError(t *testing.T) {
|
|
|
|
fromYAML := []byte(`my_secret:
|
|
|
|
from_secret: not_a_secret
|
|
|
|
`)
|
2023-11-12 17:23:48 +00:00
|
|
|
var from map[string]any
|
2021-12-13 19:33:07 +00:00
|
|
|
err := yaml.Unmarshal(fromYAML, &from)
|
|
|
|
assert.NoError(t, err)
|
2022-10-03 17:25:43 +00:00
|
|
|
secrets := map[string]string{
|
|
|
|
"secret_token": "FooBar",
|
2021-12-13 19:33:07 +00:00
|
|
|
}
|
2024-01-27 19:59:44 +00:00
|
|
|
getSecretValue := func(name string) (string, error) {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
secret, ok := secrets[name]
|
|
|
|
if ok {
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:12:40 +00:00
|
|
|
assert.Error(t, ParamsToEnv(from, make(map[string]string), "PLUGIN_", getSecretValue))
|
2021-12-13 19:33:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-12 17:23:48 +00:00
|
|
|
func stringsToInterface(val ...string) []any {
|
|
|
|
res := make([]any, len(val))
|
2021-12-08 17:17:52 +00:00
|
|
|
for i := range val {
|
|
|
|
res[i] = val[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2024-01-27 19:59:44 +00:00
|
|
|
|
|
|
|
func TestSecretNotFound(t *testing.T) {
|
|
|
|
from := map[string]any{
|
|
|
|
"map": map[string]any{"secret": map[string]any{"from_secret": "secret_token"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets := map[string]string{
|
|
|
|
"a_different_password": "secret",
|
|
|
|
}
|
|
|
|
getSecretValue := func(name string) (string, error) {
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
secret, ok := secrets[name]
|
|
|
|
if ok {
|
|
|
|
return secret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("secret %q not found or not allowed to be used", name)
|
|
|
|
}
|
|
|
|
got := map[string]string{}
|
|
|
|
|
|
|
|
assert.ErrorContains(t,
|
2024-02-25 09:12:40 +00:00
|
|
|
ParamsToEnv(from, got, "PLUGIN_", getSecretValue),
|
2024-01-27 19:59:44 +00:00
|
|
|
fmt.Sprintf("secret %q not found or not allowed to be used", "secret_token"))
|
|
|
|
}
|