keys.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, 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. dir := config.BuildDir + pkg.PackageBase + "/"
  47. pkgbuild, err := gopkg.ParseSRCINFO(dir + ".SRCINFO")
  48. if err != nil {
  49. return fmt.Errorf("%s: %s", pkg.Name, err)
  50. }
  51. for _, key := range pkgbuild.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. cmd := exec.Command(config.GpgBin, append(args, key)...)
  59. err := cmd.Run()
  60. if err != nil {
  61. problematic.set(key, pkg)
  62. }
  63. }
  64. }
  65. // No key issues!
  66. if len(problematic) == 0 {
  67. return nil
  68. }
  69. question, err := formatKeysToImport(problematic, bases)
  70. if err != nil {
  71. return err
  72. }
  73. if continueTask(question, "nN") {
  74. return importKeys(gpgExtraArgs, problematic.toSlice())
  75. }
  76. return nil
  77. }
  78. // importKeys tries to import the list of keys specified in its argument. As
  79. // in checkGpgKeys, gpgExtraArgs are extra parameters to pass to gpg.
  80. func importKeys(gpgExtraArgs, keys []string) error {
  81. args := append(gpgExtraArgs, "--recv-keys")
  82. cmd := exec.Command(config.GpgBin, append(args, keys...)...)
  83. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  84. fmt.Printf("%s Importing keys with gpg...\n", bold(cyan("::")))
  85. err := cmd.Run()
  86. if err != nil {
  87. return fmt.Errorf("%s Problem importing keys", bold(red(arrow+" Error:")))
  88. }
  89. return nil
  90. }
  91. // formatKeysToImport receives a set of keys and returns a string containing the
  92. // question asking the user wants to import the problematic keys.
  93. func formatKeysToImport(keys pgpKeySet, bases map[string][]*rpc.Pkg) (string, error) {
  94. if len(keys) == 0 {
  95. return "", fmt.Errorf("%s No keys to import", bold(red(arrow+" Error:")))
  96. }
  97. var buffer bytes.Buffer
  98. buffer.WriteString(bold(green(("GPG keys need importing:\n"))))
  99. for key, pkgs := range keys {
  100. pkglist := ""
  101. for _, pkg := range pkgs {
  102. pkglist += formatPkgbase(pkg, bases) + " "
  103. }
  104. pkglist = strings.TrimRight(pkglist, " ")
  105. buffer.WriteString(fmt.Sprintf("\t%s, required by: %s\n", green(key), cyan(pkglist)))
  106. }
  107. buffer.WriteString(bold(green(fmt.Sprintf("%s Import?", arrow))))
  108. return buffer.String(), nil
  109. }