Back-end: * update alerting module * update GetSecretKeysForContactPointType to extract secret fields from nested options * Update RemoveSecretsForContactPoint to support complex settings * update PostableGrafanaReceiverToEmbeddedContactPoint to support nested secrets * update Integration to support nested settings in models.Integration * make sigv4 fields optional Front-end: * add UI support for encrypted subform fields * allow emptying nested secure fields * Omit non touched secure fields in POST payload when saving a contact point * Use SecretInput from grafana-ui instead of the new EncryptedInput * use produce from immer * rename mapClone * rename sliceClone * Don't use produce from immer as we need to delete the fileds afterwards --------- Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com> Co-authored-by: Sonia Aguilar <soniaaguilarpeiron@gmail.com> Co-authored-by: Matt Jacobson <matthew.jacobson@grafana.com>
126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
package provisioning
|
|
|
|
import (
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/exp/maps"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
)
|
|
|
|
func TestPostableGrafanaReceiverToEmbeddedContactPoint(t *testing.T) {
|
|
expectedProvenance := models.KnownProvenances[rand.Intn(len(models.KnownProvenances))]
|
|
tests := []struct {
|
|
name string
|
|
input definitions.PostableGrafanaReceiver
|
|
expected definitions.EmbeddedContactPoint
|
|
}{
|
|
{
|
|
name: "should create expected object",
|
|
input: definitions.PostableGrafanaReceiver{
|
|
UID: "test-uid",
|
|
Name: "test-name",
|
|
Type: "test-type",
|
|
DisableResolveMessage: true,
|
|
Settings: definitions.RawMessage(`{ "name": "test" }`),
|
|
},
|
|
expected: definitions.EmbeddedContactPoint{
|
|
UID: "test-uid",
|
|
Name: "test-name",
|
|
Type: "test-type",
|
|
Settings: simplejson.NewFromAny(map[string]any{
|
|
"name": "test",
|
|
}),
|
|
DisableResolveMessage: true,
|
|
Provenance: string(expectedProvenance),
|
|
},
|
|
},
|
|
{
|
|
name: "should merge decrypted secrets into settings",
|
|
input: definitions.PostableGrafanaReceiver{
|
|
Settings: definitions.RawMessage(`{ "name": "test" }`),
|
|
SecureSettings: map[string]string{
|
|
"secret": "data",
|
|
},
|
|
},
|
|
expected: definitions.EmbeddedContactPoint{
|
|
Settings: simplejson.NewFromAny(map[string]any{
|
|
"name": "test",
|
|
"secret": "data",
|
|
}),
|
|
Provenance: string(expectedProvenance),
|
|
},
|
|
},
|
|
{
|
|
name: "should override existing settings with decrypted secrets into settings",
|
|
input: definitions.PostableGrafanaReceiver{
|
|
Settings: definitions.RawMessage(`{ "name": "test" }`),
|
|
SecureSettings: map[string]string{
|
|
"name": "secret-data",
|
|
},
|
|
},
|
|
expected: definitions.EmbeddedContactPoint{
|
|
Settings: simplejson.NewFromAny(map[string]any{
|
|
"name": "secret-data",
|
|
}),
|
|
Provenance: string(expectedProvenance),
|
|
},
|
|
},
|
|
{
|
|
name: "should support nested secrets",
|
|
input: definitions.PostableGrafanaReceiver{
|
|
Settings: definitions.RawMessage(`{ "name": "test" }`),
|
|
SecureSettings: map[string]string{
|
|
"secret.sub-secret": "data",
|
|
},
|
|
},
|
|
expected: definitions.EmbeddedContactPoint{
|
|
Settings: simplejson.NewFromAny(map[string]any{
|
|
"name": "test",
|
|
"secret": map[string]any{
|
|
"sub-secret": "data",
|
|
},
|
|
}),
|
|
Provenance: string(expectedProvenance),
|
|
},
|
|
},
|
|
{
|
|
name: "should amend to nested structs",
|
|
input: definitions.PostableGrafanaReceiver{
|
|
Settings: definitions.RawMessage(`{ "name": "test", "secret": { "data": "test"} }`),
|
|
SecureSettings: map[string]string{
|
|
"secret.sub-secret": "secret-data",
|
|
},
|
|
},
|
|
expected: definitions.EmbeddedContactPoint{
|
|
Settings: simplejson.NewFromAny(map[string]any{
|
|
"name": "test",
|
|
"secret": map[string]any{
|
|
"data": "test",
|
|
"sub-secret": "secret-data",
|
|
},
|
|
}),
|
|
Provenance: string(expectedProvenance),
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var decrypted []string
|
|
decrypt := func(s string) string {
|
|
decrypted = append(decrypted, s)
|
|
return s
|
|
}
|
|
embeddedContactPoint, err := PostableGrafanaReceiverToEmbeddedContactPoint(&tt.input, expectedProvenance, decrypt)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.expected, embeddedContactPoint)
|
|
assert.ElementsMatch(t, maps.Values(tt.input.SecureSettings), decrypted)
|
|
})
|
|
}
|
|
}
|