diff --git a/controllers/export_test.go b/controllers/export_test.go index 2296539c..56f66474 100644 --- a/controllers/export_test.go +++ b/controllers/export_test.go @@ -360,6 +360,7 @@ var ( FindRemovedCRDVersions = findRemovedCRDVersions WithRestConfig = withRestConfig GetHelmUpgradeClient = getHelmUpgradeClient + GetHelmUninstallClient = getHelmUninstallClient ) type ( diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index 7c00d9a4..4b116d63 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -1031,7 +1031,11 @@ func uninstallHelmCharts(ctx context.Context, c client.Client, clusterSummary *c } if currentRelease != nil && currentRelease.Status != string(releasecommon.StatusUninstalled) { - err = doUninstallRelease(ctx, clusterSummary, instantiatedChart, kubeconfig, registryOptions, logger) + // ClusterProfile/Profile deleted or stopped matching this cluster: an admin + // lifecycle decision, not an internal recovery heuristic. Respect the chart's + // own hook settings. + err = doUninstallRelease(ctx, clusterSummary, instantiatedChart, kubeconfig, registryOptions, + false, logger) if err != nil { if !errors.Is(err, driver.ErrReleaseNotFound) { return nil, err @@ -1687,7 +1691,11 @@ func handleInstall(ctx context.Context, dCtx *deploymentContext, if !isPullMode { if fs := getFeatureSummaryForFeatureID(dCtx.clusterSummary, libsveltosv1beta1.FeatureHelm); fs != nil { if fs.ConsecutiveFailures%recoverAfter == 0 && fs.FailureMessage != nil { - err := doUninstallRelease(ctx, dCtx.clusterSummary, currentChart, kubeconfig, registryOptions, logger) + // Internal recovery heuristic, not an admin decision: nothing validated that + // this release's recorded Hooks reflect what Sveltos itself last deployed, so + // they must not run. See getHelmUninstallClient. + err := doUninstallRelease(ctx, dCtx.clusterSummary, currentChart, kubeconfig, registryOptions, + true, logger) if err != nil { // Ignore release not found error if errors.Is(err, driver.ErrReleaseNotFound) || strings.Contains(err.Error(), "release: not found") { @@ -1760,7 +1768,8 @@ func handleUninstall(ctx context.Context, clusterSummary *configv1beta1.ClusterS var report *configv1beta1.ReleaseReport logger.V(logs.LogDebug).Info("uninstall helm release") - err := doUninstallRelease(ctx, clusterSummary, currentChart, kubeconfig, registryOptions, logger) + // Explicit HelmChartActionUninstall: an admin decision. Respect the chart's own hook settings. + err := doUninstallRelease(ctx, clusterSummary, currentChart, kubeconfig, registryOptions, false, logger) if err != nil { return nil, err } @@ -2250,9 +2259,12 @@ func checkDependencies(chartRequested chartbase.Charter, installClient *action.I // uninstallRelease removes helm release from a managed Cluster. // No action in DryRun mode. +// uninstallRelease uninstalls a Helm release. forceDisableHooks is documented on +// getHelmUninstallClient: pass true only when recovering from a release whose stored +// state (and so whose Hooks) cannot be trusted. func uninstallRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSummary, releaseName, releaseNamespace, kubeconfig string, registryOptions *registryClientOptions, - helmChart *configv1beta1.HelmChart, logger logr.Logger) error { + helmChart *configv1beta1.HelmChart, forceDisableHooks bool, logger logr.Logger) error { // No-op in DryRun mode if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun { @@ -2291,7 +2303,7 @@ func uninstallRelease(ctx context.Context, clusterSummary *configv1beta1.Cluster return err } - uninstallClient := getHelmUninstallClient(helmChart, actionConfig) + uninstallClient := getHelmUninstallClient(helmChart, actionConfig, forceDisableHooks) _, err = uninstallClient.Run(releaseName) if err != nil { @@ -3228,8 +3240,10 @@ func recoverRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSu return nil } + // This release's state was just found inconsistent (status), so its stored Hooks + // cannot be trusted: forceDisableHooks=true. See getHelmUninstallClient. return uninstallRelease(ctx, clusterSummary, requestedChart.ReleaseName, - requestedChart.ReleaseNamespace, kubeconfig, registryOptions, requestedChart, logger) + requestedChart.ReleaseNamespace, kubeconfig, registryOptions, requestedChart, true, logger) } // shouldInstall returns true if action is not uninstall and there is no release to manage @@ -3421,9 +3435,15 @@ func doInstallRelease(ctx context.Context, dCtx *deploymentContext, // doUninstallRelease uninstalls helm release from the Cluster. // No action in DryRun mode. +// doUninstallRelease uninstalls a Helm release that an admin decision (not an internal +// recovery heuristic) determined should go away: an explicit HelmChartActionUninstall, or a +// whole ClusterSummary being torn down because its ClusterProfile/Profile was deleted or +// stopped matching. forceDisableHooks is documented on getHelmUninstallClient; it must stay +// false here so the chart's own Options.DisableHooks (the admin's own choice for this chart) +// is respected, same as any other admin-intended uninstall. func doUninstallRelease(ctx context.Context, clusterSummary *configv1beta1.ClusterSummary, requestedChart *configv1beta1.HelmChart, kubeconfig string, registryOptions *registryClientOptions, - logger logr.Logger) error { + forceDisableHooks bool, logger logr.Logger) error { if clusterSummary.Spec.ClusterProfileSpec.SyncMode == configv1beta1.SyncModeDryRun { return nil @@ -3435,7 +3455,7 @@ func doUninstallRelease(ctx context.Context, clusterSummary *configv1beta1.Clust requestedChart.RepositoryName)) return uninstallRelease(ctx, clusterSummary, requestedChart.ReleaseName, requestedChart.ReleaseNamespace, - kubeconfig, registryOptions, requestedChart, logger) + kubeconfig, registryOptions, requestedChart, forceDisableHooks, logger) } // doUpgradeRelease upgrades helm release in the Cluster. @@ -3544,7 +3564,7 @@ func undeployStaleReleases(ctx context.Context, c client.Client, clusterSummary } if err := uninstallRelease(ctx, clusterSummary, staleReleases[i].Name, - staleReleases[i].Namespace, kubeconfig, ®istryClientOptions{}, nil, logger); err != nil { + staleReleases[i].Namespace, kubeconfig, ®istryClientOptions{}, nil, false, logger); err != nil { return nil, err } @@ -4960,8 +4980,18 @@ func getHelmUpgradeClient(requestedChart *configv1beta1.HelmChart, actionConfig return upgradeClient } +// getHelmUninstallClient builds the Helm uninstall client. forceDisableHooks must be true +// when the uninstall is recovering from a release whose state we do not trust (see +// recoverRelease): the release being uninstalled was found pending/uninstalling/unknown, +// meaning its Hooks come from a Secret that is not known to reflect what Sveltos actually +// deployed. Anyone able to write Secrets in the release namespace can set those Hooks, so +// running them would execute attacker-controlled manifests with Sveltos' own, usually far +// more privileged, credentials for the managed cluster. requestedChart.Options' DisableHooks +// setting must not override this: it reflects what the admin wants for a normal deploy or +// uninstall of a release whose content Sveltos itself produced, not for clearing a release +// state that cannot be trusted. func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfig *action.Configuration, -) *action.Uninstall { + forceDisableHooks bool) *action.Uninstall { uninstallClient := action.NewUninstall(actionConfig) uninstallClient.DryRun = false @@ -4976,6 +5006,9 @@ func getHelmUninstallClient(requestedChart *configv1beta1.HelmChart, actionConfi uninstallClient.KeepHistory = getKeepHistoryValue(requestedChart.Options) uninstallClient.DeletionPropagation = getDeletionPropagation(requestedChart.Options) } + if forceDisableHooks { + uninstallClient.DisableHooks = true + } return uninstallClient } diff --git a/controllers/handlers_helm_test.go b/controllers/handlers_helm_test.go index 4a060bb0..7382f2df 100644 --- a/controllers/handlers_helm_test.go +++ b/controllers/handlers_helm_test.go @@ -3333,6 +3333,42 @@ var _ = Describe("getHelmUpgradeClient Force", func() { }) }) +var _ = Describe("getHelmUninstallClient forceDisableHooks", func() { + It("disables hooks when forceDisableHooks is true, even if the chart wants hooks enabled", func() { + requestedChart := &configv1beta1.HelmChart{ + ReleaseName: randomString(), + ReleaseNamespace: randomString(), + Options: &configv1beta1.HelmOptions{ + DisableHooks: false, + }, + } + + uninstallClient := controllers.GetHelmUninstallClient(requestedChart, &action.Configuration{}, true) + + Expect(uninstallClient.DisableHooks).To(BeTrue()) + }) + + It("respects the chart's own DisableHooks setting when forceDisableHooks is false", func() { + requestedChart := &configv1beta1.HelmChart{ + ReleaseName: randomString(), + ReleaseNamespace: randomString(), + Options: &configv1beta1.HelmOptions{ + DisableHooks: false, + }, + } + + uninstallClient := controllers.GetHelmUninstallClient(requestedChart, &action.Configuration{}, false) + + Expect(uninstallClient.DisableHooks).To(BeFalse()) + }) + + It("still disables hooks when forceDisableHooks is true and requestedChart is nil", func() { + uninstallClient := controllers.GetHelmUninstallClient(nil, &action.Configuration{}, true) + + Expect(uninstallClient.DisableHooks).To(BeTrue()) + }) +}) + var _ = Describe("getCRDObjects", func() { newChartWithFiles := func(name string, fileNames ...string) *chart.Chart { files := make([]*common.File, 0, len(fileNames))