keys.go 3.4 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. gpgExtraArgs are extra
  37. // parameters to pass to gpg, in order to facilitate testing, such as using a
  38. // different keyring. It can be nil.
  39. func checkPgpKeys(pkgs []*rpc.Pkg, srcinfos map[string]*gopkg.PKGBUILD, bases map[string][]*rpc.Pkg, gpgExtraArgs []string) error {
  40. // Let's check the keys individually, and then we can offer to import
  41. // the problematic ones.
  42. problematic := make(pgpKeySet)
  43. args := append(gpgExtraArgs, "--list-keys")
  44. // Mapping all the keys.
  45. for _, pkg := range pkgs {
  46. for _, key := range srcinfos[pkg.PackageBase].Validpgpkeys {
  47. // If key already marked as problematic, indicate the current
  48. // PKGBUILD requires it.
  49. if problematic.get(key) {
  50. problematic.set(key, pkg)
  51. continue
  52. }
  53. cmd := exec.Command(config.GpgBin, append(args, key)...)
  54. err := cmd.Run()
  55. if err != nil {
  56. problematic.set(key, pkg)
  57. }
  58. }
  59. }
  60. // No key issues!
  61. if len(problematic) == 0 {
  62. return nil
  63. }
  64. question, err := formatKeysToImport(problematic, bases)
  65. if err != nil {
  66. return err
  67. }
  68. if continueTask(question, "nN") {
  69. return importKeys(gpgExtraArgs, problematic.toSlice())
  70. }
  71. return nil
  72. }
  73. // importKeys tries to import the list of keys specified in its argument. As
  74. // in checkGpgKeys, gpgExtraArgs are extra parameters to pass to gpg.
  75. func importKeys(gpgExtraArgs, keys []string) error {
  76. args := append(gpgExtraArgs, "--recv-keys")
  77. cmd := exec.Command(config.GpgBin, append(args, keys...)...)
  78. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  79. fmt.Printf("%s Importing keys with gpg...\n", bold(cyan("::")))
  80. err := cmd.Run()
  81. if err != nil {
  82. return fmt.Errorf("%s Problem importing keys", bold(red(arrow+" Error:")))
  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, bases map[string][]*rpc.Pkg) (string, error) {
  89. if len(keys) == 0 {
  90. return "", fmt.Errorf("%s No keys to import", bold(red(arrow+" Error:")))
  91. }
  92. var buffer bytes.Buffer
  93. buffer.WriteString(bold(green(("GPG keys need importing:\n"))))
  94. for key, pkgs := range keys {
  95. pkglist := ""
  96. for _, pkg := range pkgs {
  97. pkglist += formatPkgbase(pkg, bases) + " "
  98. }
  99. pkglist = strings.TrimRight(pkglist, " ")
  100. buffer.WriteString(fmt.Sprintf("\t%s, required by: %s\n", green(key), cyan(pkglist)))
  101. }
  102. buffer.WriteString(bold(green(fmt.Sprintf("%s Import?", arrow))))
  103. return buffer.String(), nil
  104. }