|
| 1 | +// SPDX-FileCopyrightText: 2026 SUSE LLC |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package rotate |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/rs/zerolog/log" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + adm_podman "github.com/uyuni-project/uyuni-tools/mgradm/shared/podman" |
| 14 | + . "github.com/uyuni-project/uyuni-tools/shared/l10n" |
| 15 | + "github.com/uyuni-project/uyuni-tools/shared/podman" |
| 16 | + "github.com/uyuni-project/uyuni-tools/shared/types" |
| 17 | + "github.com/uyuni-project/uyuni-tools/shared/utils" |
| 18 | +) |
| 19 | + |
| 20 | +func rotateForPodman(_ *types.GlobalFlags, flags *rotateFlags, cmd *cobra.Command, args []string) error { |
| 21 | + fqdn, err := utils.GetFqdn(args) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + |
| 26 | + if flags.Emergency { |
| 27 | + return emergencyRotate(flags, cmd, fqdn) |
| 28 | + } |
| 29 | + return plannedRotate(flags, cmd, fqdn) |
| 30 | +} |
| 31 | + |
| 32 | +func plannedRotate(flags *rotateFlags, cmd *cobra.Command, fqdn string) error { |
| 33 | + checkOnly, _ := cmd.Flags().GetBool("check-only") |
| 34 | + |
| 35 | + fingerprint, err := adm_podman.NewCAFingerprint() |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if err := checkClientReadiness(fingerprint, flags.Force); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + if checkOnly { |
| 43 | + return nil |
| 44 | + } |
| 45 | + |
| 46 | + if err := switchServerCertificate(flags, cmd, fqdn, false); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + return adm_podman.ApplyNewCertificates(fqdn) |
| 50 | +} |
| 51 | + |
| 52 | +func emergencyRotate(flags *rotateFlags, cmd *cobra.Command, fqdn string) error { |
| 53 | + if checkOnly, _ := cmd.Flags().GetBool("check-only"); checkOnly { |
| 54 | + log.Warn().Msg(L("--check-only is ignored with --emergency! Proceeding with the emergency rotation")) |
| 55 | + } |
| 56 | + |
| 57 | + if err := switchServerCertificate(flags, cmd, fqdn, true); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + return adm_podman.ApplyNewCertificates(fqdn) |
| 61 | +} |
| 62 | + |
| 63 | +func switchServerCertificate(flags *rotateFlags, cmd *cobra.Command, fqdn string, regenerateCA bool) error { |
| 64 | + if flags.SSL.Ca.IsThirdParty() { |
| 65 | + if !flags.SSL.UseProvided() { |
| 66 | + return errors.New(L("the server certificate, key and root CA need to be all provided")) |
| 67 | + } |
| 68 | + log.Info().Msg(L("Installing the provided 3rd party server certificate")) |
| 69 | + return adm_podman.SetThirdPartyCertificates(&flags.SSL, fqdn) |
| 70 | + } |
| 71 | + |
| 72 | + utils.AskPasswordIfMissing(&flags.SSL.Password, cmd.Flag("ssl-password").Usage, 0, 0) |
| 73 | + if flags.SSL.Password == "" { |
| 74 | + return errors.New(L("the CA key password is required")) |
| 75 | + } |
| 76 | + |
| 77 | + image := podman.GetServiceImage(podman.ServerService) |
| 78 | + tz := adm_podman.GetContainerTimezone() |
| 79 | + if regenerateCA { |
| 80 | + log.Info().Msg(L("Generating a new CA and server certificate")) |
| 81 | + return adm_podman.RegenerateCAAndCertificate(image, &flags.SSL, tz, fqdn) |
| 82 | + } |
| 83 | + log.Info().Msg(L("Generating a new server certificate signed by the new CA")) |
| 84 | + return adm_podman.RotateServerCertificate(image, &flags.SSL, tz, fqdn) |
| 85 | +} |
| 86 | + |
| 87 | +func checkClientReadiness(fingerprint string, force bool) error { |
| 88 | + if force { |
| 89 | + log.Warn().Msg(L("Skipping the client CA trust check as --force was set")) |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + result, err := adm_podman.CheckClientsCATrust(fingerprint) |
| 94 | + if err != nil { |
| 95 | + return utils.Error(err, L("failed to verify client readiness, rerun with --force to rotate without checking")) |
| 96 | + } |
| 97 | + |
| 98 | + reportClientReadiness(result) |
| 99 | + if !result.AllMigrated() { |
| 100 | + return errors.New( |
| 101 | + L("some clients do not trust the new CA yet! Distribute it and retry, or use --force to skip this check"), |
| 102 | + ) |
| 103 | + } |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func reportClientReadiness(result adm_podman.ClientCheckResult) { |
| 108 | + log.Info().Msgf(L("Clients trusting the new CA: %d"), len(result.Migrated)) |
| 109 | + if len(result.NotMigrated) > 0 { |
| 110 | + log.Warn().Msgf(L("Clients not yet trusting the new CA (%[1]d): %[2]s"), |
| 111 | + len(result.NotMigrated), strings.Join(result.NotMigrated, ", ")) |
| 112 | + } |
| 113 | + if len(result.Unreachable) > 0 { |
| 114 | + log.Warn().Msgf(L("Unreachable minions (%[1]d): %[2]s"), |
| 115 | + len(result.Unreachable), strings.Join(result.Unreachable, ", ")) |
| 116 | + } |
| 117 | + log.Warn().Msg(L("Only Salt-managed clients are checked! Non-managed systems and proxy " + |
| 118 | + "configurations are not verified and must be handled manually.")) |
| 119 | +} |
0 commit comments