keys.go 3.6 KB

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