keys.go 3.1 KB

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