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

Update some exec calls to use tablehelpers.Exec #1606

Merged
merged 2 commits into from
Feb 15, 2024
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
32 changes: 1 addition & 31 deletions ee/tables/dataflattentable/exec.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package dataflattentable

import (
"bytes"
"context"

"fmt"
"os"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -64,7 +61,7 @@ func TablePluginExec(logger log.Logger, tableName string, dataSourceType DataSou
func (t *Table) generateExec(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
var results []map[string]string

execBytes, err := t.exec(ctx)
execBytes, err := tablehelpers.Exec(ctx, t.logger, 50, t.cmdGen, t.execArgs, false)
if err != nil {
// exec will error if there's no binary, so we never want to record that
if os.IsNotExist(errors.Cause(err)) {
Expand Down Expand Up @@ -94,30 +91,3 @@ func (t *Table) generateExec(ctx context.Context, queryContext table.QueryContex

return results, nil
}

func (t *Table) exec(ctx context.Context) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, 50*time.Second)
defer cancel()

var stdout bytes.Buffer
var stderr bytes.Buffer

cmd, err := t.cmdGen(ctx, t.execArgs...)
if err != nil {
return nil, fmt.Errorf("creating command: %w", err)
}

cmd.Stdout = &stdout
cmd.Stderr = &stderr

level.Debug(t.logger).Log("msg", "calling %s", "args", cmd.String())

if err := cmd.Run(); os.IsNotExist(err) {
return nil, fmt.Errorf("command %s not found: %w", cmd.Path, err)
} else if err != nil {
return nil, fmt.Errorf("calling %s. Got: %s: %w", cmd.Path, stderr.String(), err)
}

// success!
return stdout.Bytes(), nil
}
20 changes: 2 additions & 18 deletions ee/tables/secedit/secedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
package secedit

import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -96,28 +94,14 @@ func (t *Table) execSecedit(ctx context.Context, mergedPolicy bool) ([]byte, err
defer os.RemoveAll(dir)

dst := filepath.Join(dir, "tmpfile.ini")
var stdout bytes.Buffer
var stderr bytes.Buffer

ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()

args := []string{"/export", "/cfg", dst}
if mergedPolicy {
args = append(args, "/mergedpolicy")
}

cmd, err := allowedcmd.Secedit(ctx, args...)
if err != nil {
return nil, fmt.Errorf("creating secedit command: %w", err)
}
cmd.Stdout = &stdout
cmd.Stderr = &stderr

level.Debug(t.logger).Log("msg", "calling secedit", "args", cmd.Args)

if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("calling secedit. Got: %s: %w", stderr.String(), err)
if out, err := tablehelpers.Exec(ctx, t.logger, 30, allowedcmd.Secedit, args, true); err != nil {
return nil, fmt.Errorf("calling secedit. Got: %s: %w", string(out), err)
}

file, err := os.Open(dst)
Expand Down
Loading