keys.go 3.2 KB

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