keys.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package pgp
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "strings"
  10. gosrc "github.com/Morganamilo/go-srcinfo"
  11. "github.com/leonelquinteros/gotext"
  12. "github.com/Jguer/yay/v12/pkg/settings/exe"
  13. "github.com/Jguer/yay/v12/pkg/text"
  14. )
  15. // pgpKeySet maps a PGP key with a list of PKGBUILDs that require it.
  16. // This is similar to stringSet, used throughout the code.
  17. type pgpKeySet map[string][]string
  18. func (set pgpKeySet) toSlice() []string {
  19. slice := make([]string, 0, len(set))
  20. for v := range set {
  21. slice = append(slice, v)
  22. }
  23. return slice
  24. }
  25. func (set pgpKeySet) set(key, p string) {
  26. // Using ToUpper to make sure keys with a different case will be
  27. // considered the same.
  28. upperKey := strings.ToUpper(key)
  29. set[upperKey] = append(set[upperKey], p)
  30. }
  31. func (set pgpKeySet) get(key string) bool {
  32. upperKey := strings.ToUpper(key)
  33. _, exists := set[upperKey]
  34. return exists
  35. }
  36. type GPGCmdBuilder interface {
  37. exe.Runner
  38. BuildGPGCmd(ctx context.Context, extraArgs ...string) *exec.Cmd
  39. }
  40. // CheckPgpKeys iterates through the keys listed in the PKGBUILDs and if needed,
  41. // asks the user whether yay should try to import them.
  42. func CheckPgpKeys(ctx context.Context, pkgbuildDirsByBase map[string]string, srcinfos map[string]*gosrc.Srcinfo,
  43. cmdBuilder GPGCmdBuilder, noConfirm bool,
  44. ) ([]string, error) {
  45. // Let's check the keys individually, and then we can offer to import
  46. // the problematic ones.
  47. problematic := make(pgpKeySet)
  48. // Mapping all the keys.
  49. for pkg := range pkgbuildDirsByBase {
  50. srcinfo := srcinfos[pkg]
  51. for _, key := range srcinfo.ValidPGPKeys {
  52. // If key already marked as problematic, indicate the current
  53. // PKGBUILD requires it.
  54. if problematic.get(key) {
  55. problematic.set(key, pkg)
  56. continue
  57. }
  58. if err := cmdBuilder.Show(cmdBuilder.BuildGPGCmd(ctx, "--list-keys", key)); err != nil {
  59. problematic.set(key, pkg)
  60. }
  61. }
  62. }
  63. // No key issues!
  64. if len(problematic) == 0 {
  65. return []string{}, nil
  66. }
  67. str, err := formatKeysToImport(problematic)
  68. if err != nil {
  69. return nil, err
  70. }
  71. fmt.Println()
  72. fmt.Println(str)
  73. if text.ContinueTask(os.Stdin, gotext.Get("Import?"), true, noConfirm) {
  74. return problematic.toSlice(), importKeys(ctx, cmdBuilder, problematic.toSlice())
  75. }
  76. return problematic.toSlice(), nil
  77. }
  78. // importKeys tries to import the list of keys specified in its argument.
  79. func importKeys(ctx context.Context, cmdBuilder GPGCmdBuilder, keys []string) error {
  80. text.OperationInfoln(gotext.Get("Importing keys with gpg..."))
  81. if err := cmdBuilder.Show(cmdBuilder.BuildGPGCmd(ctx, append([]string{"--recv-keys"}, keys...)...)); err != nil {
  82. return errors.New(gotext.Get("problem importing keys"))
  83. }
  84. return nil
  85. }
  86. // formatKeysToImport receives a set of keys and returns a string containing the
  87. // question asking the user wants to import the problematic keys.
  88. func formatKeysToImport(keys pgpKeySet) (string, error) {
  89. if len(keys) == 0 {
  90. return "", errors.New(gotext.Get("no keys to import"))
  91. }
  92. var buffer bytes.Buffer
  93. buffer.WriteString(text.SprintOperationInfo(gotext.Get("PGP keys need importing:")))
  94. for key, bases := range keys {
  95. pkglist := ""
  96. for _, base := range bases {
  97. pkglist += base + " "
  98. }
  99. pkglist = strings.TrimRight(pkglist, " ")
  100. buffer.WriteString("\n" + text.SprintWarn(gotext.Get("%s, required by: %s", text.Cyan(key), text.Cyan(pkglist))))
  101. }
  102. return buffer.String(), nil
  103. }