Skip to content

Commit

Permalink
feat: replace exec.Command with go-cmd module
Browse files Browse the repository at this point in the history
This should fix reaper problem in Talos.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
  • Loading branch information
Unix4ever authored and talos-bot committed Feb 16, 2021
1 parent 1cf7f25 commit 8f976c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
31 changes: 12 additions & 19 deletions blockdevice/encryption/luks/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ package luks

import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"

"github.com/talos-systems/go-cmd/pkg/cmd"
"golang.org/x/sys/unix"

"github.com/talos-systems/go-blockdevice/blockdevice/encryption"
Expand Down Expand Up @@ -225,23 +227,16 @@ func (l *LUKS) ReadKeyslots(deviceName string) (*encryption.Keyslots, error) {

// runCommand executes cryptsetup with arguments.
func (l *LUKS) runCommand(args []string, stdin []byte) error {
cmd := exec.Command("cryptsetup", args...)

var out bytes.Buffer

if stdin != nil {
cmd.Stdin = bytes.NewBuffer(stdin)
}

cmd.Stdout = &out
cmd.Stderr = &out

err := cmd.Run()
_, err := cmd.RunContext(cmd.WithStdin(
context.Background(),
bytes.NewBuffer(stdin)), "cryptsetup", args...)
if err != nil {
if e, ok := err.(*exec.ExitError); ok { //nolint:errorlint
switch e.ExitCode() {
var exitError *cmd.ExitError

if errors.As(err, &exitError) {
switch exitError.ExitCode {
case 1:
if strings.Contains(out.String(), "Keyslot open failed.\nNo usable keyslot is available.") {
if strings.Contains(string(exitError.Output), "Keyslot open failed.\nNo usable keyslot is available.") {
return encryption.ErrEncryptionKeyRejected
}
case 2:
Expand All @@ -251,9 +246,7 @@ func (l *LUKS) runCommand(args []string, stdin []byte) error {
}
}

if !strings.Contains(err.Error(), "no child processes") {
return fmt.Errorf("failed to call cryptsetup: %w, output: %s", err, out.String())
}
return fmt.Errorf("failed to call cryptsetup: %w", err)
}

return nil
Expand Down
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ module github.com/talos-systems/go-blockdevice
go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.1.2
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
github.com/talos-systems/go-cmd v0.0.0-20210216164758-68eb0067e0f0
github.com/talos-systems/go-retry v0.1.1-0.20201113203059-8c63d290a688
golang.org/x/sys v0.0.0-20201130171929-760e229fe7c5
golang.org/x/text v0.3.4
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 h1:7Ip0wMmLHLRJdrloDxZfhMm0xrLXZS8+COSu2bXmEQs=
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -14,8 +16,10 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/talos-systems/go-cmd v0.0.0-20210216164758-68eb0067e0f0 h1:DI+BjK+fcrLBc70Fi50dZocQcaHosqsuWHrGHKp2NzE=
github.com/talos-systems/go-cmd v0.0.0-20210216164758-68eb0067e0f0/go.mod h1:kf+rZzTEmlDiYQ6ulslvRONnKLQH8x83TowltGMhO+k=
github.com/talos-systems/go-retry v0.1.1-0.20201113203059-8c63d290a688 h1:U5wFGj5LXt/r+qfy1nGftQxJvEbg/lVJuasHKtk3K7s=
github.com/talos-systems/go-retry v0.1.1-0.20201113203059-8c63d290a688/go.mod h1:HiXQqyVStZ35uSY/MTLWVvQVmC3lIW2MS5VdDaMtoKM=
golang.org/x/sys v0.0.0-20201130171929-760e229fe7c5 h1:dMDtAap8F/+vsyXblqK90iTzYJjNix5MsXDicSYol6w=
Expand Down

0 comments on commit 8f976c2

Please sign in to comment.