From f9796f05fe81b470bf2af14bf3ce8521f7a249c8 Mon Sep 17 00:00:00 2001 From: Tom Ratcliffe Date: Fri, 13 Sep 2024 13:01:05 +0100 Subject: [PATCH] Alerting: Fix sending secure settings when using k8s API (#93320) --- .../contact-points/useContactPoints.tsx | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/public/app/features/alerting/unified/components/contact-points/useContactPoints.tsx b/public/app/features/alerting/unified/components/contact-points/useContactPoints.tsx index ef7bb63bbff..3584be0bf62 100644 --- a/public/app/features/alerting/unified/components/contact-points/useContactPoints.tsx +++ b/public/app/features/alerting/unified/components/contact-points/useContactPoints.tsx @@ -340,10 +340,31 @@ export function useDeleteContactPoint({ alertmanager }: BaseAlertmanagerArgs) { }; } -const mapIntegrationSettings = (integration: GrafanaManagedReceiverConfig): GrafanaManagedReceiverConfig => { +/** + * Turns a Grafana Managed receiver config into a format that can be sent to the k8s API + * + * When updating secure settings, we need to send a value of `true` for any secure setting that we want to keep the same. + * + * Any other setting that has a value in `secureSettings` will correspond to a new value for that setting - + * so we should not tell the API that we want to preserve it. Those values will instead be sent within `settings` + */ +const mapIntegrationSettingsForK8s = (integration: GrafanaManagedReceiverConfig): GrafanaManagedReceiverConfig => { + const { secureSettings, ...restOfIntegration } = integration; + + const secureFields = Object.entries(secureSettings || {}).reduce((acc, [key, value]) => { + if (value === undefined) { + return { + ...acc, + [key]: true, + }; + } + return acc; + }, {}); + return { - ...integration, - settings: { ...integration.settings, ...integration.secureSettings }, + ...restOfIntegration, + secureFields, + settings: { ...restOfIntegration.settings, ...secureSettings }, }; }; const grafanaContactPointToK8sReceiver = ( @@ -358,7 +379,7 @@ const grafanaContactPointToK8sReceiver = ( }, spec: { title: contactPoint.name, - integrations: (contactPoint.grafana_managed_receiver_configs || []).map(mapIntegrationSettings), + integrations: (contactPoint.grafana_managed_receiver_configs || []).map(mapIntegrationSettingsForK8s), }, }; };