From 8f976c2031108651738ebd4db69fb09758754a28 Mon Sep 17 00:00:00 2001 From: Artem Chernyshev Date: Tue, 16 Feb 2021 19:56:04 +0300 Subject: [PATCH] feat: replace exec.Command with go-cmd module This should fix reaper problem in Talos. Signed-off-by: Artem Chernyshev --- blockdevice/encryption/luks/luks.go | 31 +++++++++++------------------ go.mod | 8 ++------ go.sum | 8 ++++++-- 3 files changed, 20 insertions(+), 27 deletions(-) diff --git a/blockdevice/encryption/luks/luks.go b/blockdevice/encryption/luks/luks.go index 159ecd4..c0fb9d4 100644 --- a/blockdevice/encryption/luks/luks.go +++ b/blockdevice/encryption/luks/luks.go @@ -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" @@ -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: @@ -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 diff --git a/go.mod b/go.mod index 590b4ff..f66f398 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 676862e..a3922ad 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=