Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgp: make error the last return value #1310

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pgp/keysource.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (d GnuPGHome) Import(armoredKey []byte) error {
}

args := []string{"--batch", "--import"}
err, _, stderr := gpgExec(d.String(), args, bytes.NewReader(armoredKey))
_, stderr, err := gpgExec(d.String(), args, bytes.NewReader(armoredKey))
if err != nil {
return fmt.Errorf("failed to import armored key data into GnuPG keyring: %s", strings.TrimSpace(stderr.String()))
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func (key *MasterKey) encryptWithGnuPG(dataKey []byte) error {
fingerprint,
"--no-encrypt-to",
}
err, stdout, stderr := gpgExec(key.gnuPGHomeDir, args, bytes.NewReader(dataKey))
stdout, stderr, err := gpgExec(key.gnuPGHomeDir, args, bytes.NewReader(dataKey))
if err != nil {
return fmt.Errorf("failed to encrypt sops data key with pgp: %s", strings.TrimSpace(stderr.String()))
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func (key *MasterKey) decryptWithGnuPG() ([]byte, error) {
args := []string{
"-d",
}
err, stdout, stderr := gpgExec(key.gnuPGHomeDir, args, strings.NewReader(key.EncryptedKey))
stdout, stderr, err := gpgExec(key.gnuPGHomeDir, args, strings.NewReader(key.EncryptedKey))
if err != nil {
return nil, fmt.Errorf("failed to decrypt sops data key with pgp: %s",
strings.TrimSpace(stderr.String()))
Expand Down Expand Up @@ -564,7 +564,7 @@ func fingerprintIndex(ring openpgp.EntityList) map[string]openpgp.Entity {
// gpgExec runs the provided args with the gpgBinary, while restricting it to
// homeDir when provided. Stdout and stderr can be read from the returned
// buffers. When the command fails, an error is returned.
func gpgExec(homeDir string, args []string, stdin io.Reader) (err error, stdout bytes.Buffer, stderr bytes.Buffer) {
func gpgExec(homeDir string, args []string, stdin io.Reader) (stdout bytes.Buffer, stderr bytes.Buffer, err error) {
if homeDir != "" {
args = append([]string{"--homedir", homeDir}, args...)
}
Expand Down
12 changes: 6 additions & 6 deletions pgp/keysource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func TestGnuPGHome_Import(t *testing.T) {
assert.NoError(t, err)
assert.NoError(t, gnuPGHome.Import(b))

err, _, stderr := gpgExec(gnuPGHome.String(), []string{"--list-keys", mockFingerprint}, nil)
_, stderr, err := gpgExec(gnuPGHome.String(), []string{"--list-keys", mockFingerprint}, nil)
assert.NoErrorf(t, err, stderr.String())

b, err = os.ReadFile(mockPrivateKey)
assert.NoError(t, err)
assert.NoError(t, gnuPGHome.Import(b))

err, _, stderr = gpgExec(gnuPGHome.String(), []string{"--list-secret-keys", mockFingerprint}, nil)
_, stderr, err = gpgExec(gnuPGHome.String(), []string{"--list-secret-keys", mockFingerprint}, nil)
assert.NoErrorf(t, err, stderr.String())

assert.Error(t, gnuPGHome.Import([]byte("invalid armored data")))
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestMasterKey_encryptWithGnuPG(t *testing.T) {
args := []string{
"-d",
}
err, stdout, stderr := gpgExec(key.gnuPGHomeDir, args, strings.NewReader(key.EncryptedKey))
stdout, stderr, err := gpgExec(key.gnuPGHomeDir, args, strings.NewReader(key.EncryptedKey))
assert.NoError(t, err, stderr.String())
assert.Equal(t, data, stdout.Bytes())
})
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestMasterKey_Decrypt(t *testing.T) {
fingerprint := shortenFingerprint(mockFingerprint)

data := []byte("this data is absolutely top secret")
err, stdout, stderr := gpgExec(gnuPGHome.String(), []string{
stdout, stderr, err := gpgExec(gnuPGHome.String(), []string{
"--no-default-recipient",
"--yes",
"--encrypt",
Expand Down Expand Up @@ -403,7 +403,7 @@ func TestMasterKey_decryptWithOpenPGP(t *testing.T) {
fingerprint := shortenFingerprint(mockFingerprint)

data := []byte("this data is absolutely top secret")
err, stdout, stderr := gpgExec(gnuPGHome.String(), []string{
stdout, stderr, err := gpgExec(gnuPGHome.String(), []string{
"--no-default-recipient",
"--yes",
"--encrypt",
Expand Down Expand Up @@ -451,7 +451,7 @@ func TestMasterKey_decryptWithGnuPG(t *testing.T) {
fingerprint := shortenFingerprint(mockFingerprint)

data := []byte("this data is absolutely top secret")
err, stdout, stderr := gpgExec(gnuPGHome.String(), []string{
stdout, stderr, err := gpgExec(gnuPGHome.String(), []string{
"--no-default-recipient",
"--yes",
"--encrypt",
Expand Down
Loading