Skip to content

Commit

Permalink
sources: Add context to recvGPGKeys
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
  • Loading branch information
monstermunchkin committed Nov 17, 2021
1 parent 7d6ea37 commit d8f59b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion sources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (s *common) CreateGPGKeyring() (string, error) {
var ok bool

for i := 0; i < 3; i++ {
ok, err = recvGPGKeys(gpgDir, s.definition.Source.Keyserver, s.definition.Source.Keys)
ok, err = recvGPGKeys(s.ctx, gpgDir, s.definition.Source.Keyserver, s.definition.Source.Keys)
if ok {
break
}
Expand Down
3 changes: 3 additions & 0 deletions sources/common_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sources

import (
"context"
"log"
"os"
"path"
Expand Down Expand Up @@ -85,6 +86,7 @@ func TestVerifyFile(t *testing.T) {
definition: shared.Definition{
Source: shared.DefinitionSource{},
},
ctx: context.TODO(),
}

for i, tt := range tests {
Expand Down Expand Up @@ -117,6 +119,7 @@ func TestCreateGPGKeyring(t *testing.T) {
Keys: []string{"0x5DE8949A899C8D99"},
},
},
ctx: context.TODO(),
}

keyring, err := c.CreateGPGKeyring()
Expand Down
17 changes: 12 additions & 5 deletions sources/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sources
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"hash"
Expand Down Expand Up @@ -129,7 +130,7 @@ func getChecksum(fname string, hashLen int, r io.Reader) []string {
return nil
}

func recvGPGKeys(gpgDir string, keyserver string, keys []string) (bool, error) {
func recvGPGKeys(ctx context.Context, gpgDir string, keyserver string, keys []string) (bool, error) {
args := []string{"--homedir", gpgDir}

var fingerprints []string
Expand All @@ -146,7 +147,7 @@ func recvGPGKeys(gpgDir string, keyserver string, keys []string) (bool, error) {
for _, f := range publicKeys {
args := append(args, "--import")

cmd := exec.Command("gpg", args...)
cmd := exec.CommandContext(ctx, "gpg", args...)
cmd.Stdin = strings.NewReader(f)
cmd.Env = append(os.Environ(), "LANG=C.UTF-8")

Expand All @@ -165,15 +166,21 @@ func recvGPGKeys(gpgDir string, keyserver string, keys []string) (bool, error) {

args = append(args, append([]string{"--recv-keys"}, fingerprints...)...)

_, out, err := lxd.RunCommandSplit(append(os.Environ(), "LANG=C.UTF-8"), nil, "gpg", args...)
cmd := exec.CommandContext(ctx, "gpg", args...)
cmd.Env = append(os.Environ(), "LANG=C.UTF-8")

var buffer bytes.Buffer
cmd.Stderr = &buffer

err := cmd.Run()
if err != nil {
return false, err
return false, fmt.Errorf("Failed to run: %s: %s", strings.Join(cmd.Args, " "), strings.TrimSpace(buffer.String()))
}

// Verify output
var importedKeys []string
var missingKeys []string
lines := strings.Split(out, "\n")
lines := strings.Split(buffer.String(), "\n")

for _, l := range lines {
if strings.HasPrefix(l, "gpg: key ") && (strings.HasSuffix(l, " imported") || strings.HasSuffix(l, " not changed")) {
Expand Down

0 comments on commit d8f59b1

Please sign in to comment.