|
| 1 | +// SPDX-FileCopyrightText: 2026 SUSE LLC |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package gpg |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/rs/zerolog/log" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/uyuni-project/uyuni-tools/shared/api" |
| 16 | + . "github.com/uyuni-project/uyuni-tools/shared/l10n" |
| 17 | + "github.com/uyuni-project/uyuni-tools/shared/types" |
| 18 | + "github.com/uyuni-project/uyuni-tools/shared/utils" |
| 19 | +) |
| 20 | + |
| 21 | +const armorHeader = "-----BEGIN PGP PUBLIC KEY BLOCK-----" |
| 22 | + |
| 23 | +func gpgKeyUpload(client *api.APIClient, key string) error { |
| 24 | + response, err := api.PostChecked[float64]( |
| 25 | + client, |
| 26 | + "admin/gpg/uploadGpgKey", |
| 27 | + "admin.gpg.upload_gpg_key", |
| 28 | + map[string]interface{}{ |
| 29 | + "gpgKey": key, |
| 30 | + }, |
| 31 | + ) |
| 32 | + |
| 33 | + if err != nil { |
| 34 | + return utils.Errorf(err, L("error uploading GPG key")) |
| 35 | + } |
| 36 | + |
| 37 | + if !response.Success { |
| 38 | + return fmt.Errorf(L("failed to upload GPG key: %s"), response.Message) |
| 39 | + } |
| 40 | + |
| 41 | + if int(response.Result) == 1 { |
| 42 | + fmt.Println(L("GPG key successfully uploaded")) |
| 43 | + } else { |
| 44 | + fmt.Println(L("unable to upload GPG key, server returned an error")) |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func readKey(source string) (string, error) { |
| 51 | + var data []byte |
| 52 | + var err error |
| 53 | + |
| 54 | + if _, err = os.Stat(source); err == nil { |
| 55 | + log.Debug().Msgf("Reading GPG key from file %s", source) |
| 56 | + data, err = os.ReadFile(source) |
| 57 | + if err != nil { |
| 58 | + return "", utils.Errorf(err, L("failed to read key file %s"), source) |
| 59 | + } |
| 60 | + } else { |
| 61 | + log.Debug().Msgf("Downloading GPG key from %s", source) |
| 62 | + data, err = utils.GetURLBody(source) |
| 63 | + if err != nil { |
| 64 | + return "", utils.Errorf(err, L("failed to download key from %s"), source) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + key := string(data) |
| 69 | + // Armored GPG keys start with this header. |
| 70 | + if !strings.Contains(key, armorHeader) { |
| 71 | + return "", errors.New(L("the provided key is not an armored GPG key")) |
| 72 | + } |
| 73 | + |
| 74 | + return key, nil |
| 75 | +} |
| 76 | + |
| 77 | +func runGpgKeyUpload(_ *types.GlobalFlags, flags *apiFlags, _ *cobra.Command, args []string) error { |
| 78 | + source := args[0] |
| 79 | + |
| 80 | + key, err := readKey(source) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + log.Debug().Msgf("Uploading GPG key...") |
| 86 | + client, err := api.Init(&flags.ConnectionDetails) |
| 87 | + if err == nil { |
| 88 | + err = client.Login() |
| 89 | + } |
| 90 | + if err != nil { |
| 91 | + return utils.Errorf(err, L("unable to login to the server")) |
| 92 | + } |
| 93 | + |
| 94 | + return gpgKeyUpload(client, key) |
| 95 | +} |
0 commit comments