Files
grafana/pkg/services/ngalert/provisioning/compat.go
Yuri Tseretyan cb372d3fa8 Alerting: Support secrets in contact points nested fields (#92035)
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>
2024-09-10 22:26:23 -04:00

70 lines
2.5 KiB
Go

package provisioning
import (
"strings"
alertingNotify "github.com/grafana/alerting/notify"
"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 EmbeddedContactPointToGrafanaIntegrationConfig(e definitions.EmbeddedContactPoint) (alertingNotify.GrafanaIntegrationConfig, error) {
data, err := e.Settings.MarshalJSON()
if err != nil {
return alertingNotify.GrafanaIntegrationConfig{}, err
}
return alertingNotify.GrafanaIntegrationConfig{
UID: e.UID,
Name: e.Name,
Type: e.Type,
DisableResolveMessage: e.DisableResolveMessage,
Settings: data,
SecureSettings: nil,
}, nil
}
func PostableGrafanaReceiverToEmbeddedContactPoint(contactPoint *definitions.PostableGrafanaReceiver, provenance models.Provenance, decryptValue func(string) string) (definitions.EmbeddedContactPoint, error) {
simpleJson, err := simplejson.NewJson(contactPoint.Settings)
if err != nil {
return definitions.EmbeddedContactPoint{}, err
}
embeddedContactPoint := definitions.EmbeddedContactPoint{
UID: contactPoint.UID,
Type: contactPoint.Type,
Name: contactPoint.Name,
DisableResolveMessage: contactPoint.DisableResolveMessage,
Settings: simpleJson,
Provenance: string(provenance),
}
for k, v := range contactPoint.SecureSettings {
decryptedValue := decryptValue(v)
if decryptedValue == "" {
continue
}
embeddedContactPoint.Settings.SetPath(strings.Split(k, "."), decryptedValue)
}
return embeddedContactPoint, nil
}
func GrafanaIntegrationConfigToEmbeddedContactPoint(r *models.Integration, provenance models.Provenance) definitions.EmbeddedContactPoint {
settingJson := simplejson.New()
if r.Settings != nil {
settingJson = simplejson.NewFromAny(r.Settings)
}
// We explicitly do not copy the secure settings to the settings field. This is because the provisioning API
// never returns decrypted or encrypted values, only redacted values. Redacted values should already exist in the
// settings field.
return definitions.EmbeddedContactPoint{
UID: r.UID,
Name: r.Name,
Type: r.Config.Type,
DisableResolveMessage: r.DisableResolveMessage,
Settings: settingJson,
Provenance: string(provenance),
}
}