Skip to content
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
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ issues:
- path: "(central|compliance|integration-tests|local|migrator|operator|pkg|sensor|tests|tools|scale)/"
linters:
- wrapcheck
- path: "(central/graphql/schema/print|compliance|integration-tests|local|migrator|operator|pkg|sensor|tests|tools|scale)/"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not exclude sensor. If there are fmt.Print, we should change them to errors.Info/Warn.... Nonetheless, it can come in a follow-up PR.

linters:
- forbidigo
- path: "roxctl/central/generate/interactive.go"
linters:
- forbidigo
- path: _test\.go
linters:
- wrapcheck
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package querymgr

import (
"context"
"fmt"

"github.com/pkg/errors"
deploymentDataStore "github.com/stackrox/rox/central/deployment/datastore"
Expand Down Expand Up @@ -60,7 +59,6 @@ func (m *queryManagerImpl) Images(ctx context.Context, requestID string, query *
if err != nil {
return nil, err
}
fmt.Println(query)
return m.images.SearchRawImages(ctx, query)
}

Expand Down
3 changes: 2 additions & 1 deletion roxctl/central/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/spf13/cobra"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/ioutils"
pkgCommon "github.com/stackrox/rox/pkg/roxctl/common"
"github.com/stackrox/rox/pkg/tlsutils"
"github.com/stackrox/rox/pkg/utils"
Expand Down Expand Up @@ -85,7 +86,7 @@ func (cmd *centralCertCommand) certs() error {
switch cmd.filename {
case "-":
// Default to STDOUT.
handle = os.Stdout
handle = ioutils.NopWriteCloser(cmd.env.InputOutput().Out())
default:
// Open the given filename.
handle, err = os.Create(cmd.filename)
Expand Down
2 changes: 1 addition & 1 deletion roxctl/central/db/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func outputZip(envLogger logger.Logger, config renderer.Config) error {
if err != nil {
return errors.Wrap(err, "error generating zip file")
}
_, err = os.Stdout.Write(bytes)
_, err = os.Stdout.Write(bytes) //nolint:forbidigo // TODO(ROX-13473)
if err != nil {
return errors.Wrap(err, "couldn't write zip file")
}
Expand Down
4 changes: 2 additions & 2 deletions roxctl/central/db/restore/v2_restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ func (r *v2Restorer) Run(ctx context.Context, file *os.File) (*http.Response, er
r.statusLine.SetSpinner(waitingSpinner)
r.statusLine.SetTextStatic("Initiating restore ...")

termWidth, _, err := terminal.GetSize(int(os.Stderr.Fd()))
termWidth, _, err := terminal.GetSize(int(os.Stderr.Fd())) //nolint:forbidigo // TODO(ROX-13473)
if err == nil && termWidth > 40 {
if termWidth > 120 {
termWidth = 120
}

progressBarContainer := mpb.NewWithContext(subCtx, mpb.WithOutput(os.Stderr), mpb.WithWidth(termWidth))
progressBarContainer := mpb.NewWithContext(subCtx, mpb.WithOutput(r.env.InputOutput().ErrOut()), mpb.WithWidth(termWidth))
defer progressBarContainer.Wait()
defer cancel() // canceling twice doesn't hurt, but we need to ensure this gets called before Wait() above.

Expand Down
2 changes: 1 addition & 1 deletion roxctl/central/db/transfer/progress_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (f *unknownTotalSizeFiller) Fill(w io.Writer, width int, stat *decor.Statis
}

func createProgressBars(ctx context.Context, name string, totalSize int64) (*mpb.Bar, func()) {
outFile := os.Stderr
outFile := os.Stderr //nolint:forbidigo // TODO(ROX-13473)

opts := []mpb.ContainerOption{
mpb.WithWidth(progressBarWidth),
Expand Down
2 changes: 1 addition & 1 deletion roxctl/central/debug/authz_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func authzTraceCommand(cliEnvironment environment.Environment) *cobra.Command {
func writeAuthzTraces(cliEnvironment environment.Environment, timeout time.Duration) error {
// Write traces directly to stdout without buffering. Sync iff supported,
// e.g., stdout is redirected to a file and not attached to the console.
traceOutput := os.Stdout
traceOutput := os.Stdout //nolint:forbidigo // TODO(ROX-13473)
toSync := false
if traceOutput.Sync() == nil {
toSync = true
Expand Down
7 changes: 4 additions & 3 deletions roxctl/central/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stackrox/rox/roxctl/common"
"github.com/stackrox/rox/roxctl/common/environment"
"github.com/stackrox/rox/roxctl/common/flags"
io2 "github.com/stackrox/rox/roxctl/common/io"
"github.com/stackrox/rox/roxctl/common/logger"
"github.com/stackrox/rox/roxctl/common/mode"
"github.com/stackrox/rox/roxctl/common/util"
Expand Down Expand Up @@ -228,7 +229,7 @@ func createBundle(logger logger.Logger, config renderer.Config) (*zip.Wrapper, e

// OutputZip renders a deployment bundle. The deployment bundle can either be
// written directly into a directory, or as a zipfile to STDOUT.
func OutputZip(logger logger.Logger, config renderer.Config) error {
func OutputZip(logger logger.Logger, io io2.IO, config renderer.Config) error {
logger.InfofLn("Generating deployment bundle...")

common.LogInfoPsp(logger, config.EnablePodSecurityPolicies)
Expand All @@ -244,7 +245,7 @@ func OutputZip(logger logger.Logger, config renderer.Config) error {
if err != nil {
return errors.Wrap(err, "error generating zip file")
}
_, err = os.Stdout.Write(bytes)
_, err = io.Out().Write(bytes)
if err != nil {
return errors.Wrap(err, "couldn't write zip file")
}
Expand All @@ -262,7 +263,7 @@ func OutputZip(logger logger.Logger, config renderer.Config) error {
logger.InfofLn("Wrote central bundle to %q", outputPath)
}

if err := config.WriteInstructions(os.Stderr); err != nil {
if err := config.WriteInstructions(io.ErrOut()); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion roxctl/central/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestRestoreKeysAndCerts(t *testing.T) {
// Note: This test is not for parallel run.
config.OutputDir = filepath.Join(tmpDir, testCase.testDir)
config.BackupBundle = testCase.backupBundle
require.NoError(t, OutputZip(logger, config))
require.NoError(t, OutputZip(logger, io, config))

// Load values-private.yaml file
values, err := chartutil.ReadValuesFile(filepath.Join(config.OutputDir, "values-private.yaml"))
Expand Down
6 changes: 3 additions & 3 deletions roxctl/central/generate/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func externalVolume(cliEnvironment environment.Environment) *cobra.Command {
if err := validateConfig(&cfg); err != nil {
return err
}
return OutputZip(cliEnvironment.Logger(), cfg)
return OutputZip(cliEnvironment.Logger(), cliEnvironment.InputOutput(), cfg)
}
flagWrap := &flagsWrapper{FlagSet: c.Flags()}
flagWrap.StringVarP(&external.Central.Name, "name", "", "stackrox-db", "external volume name for Central", "central")
Expand All @@ -55,7 +55,7 @@ func noVolume(cliEnvironment environment.Environment) *cobra.Command {
if err := validateConfig(&cfg); err != nil {
return err
}
return OutputZip(cliEnvironment.Logger(), cfg)
return OutputZip(cliEnvironment.Logger(), cliEnvironment.InputOutput(), cfg)
}
c.Hidden = true
return c
Expand All @@ -74,7 +74,7 @@ func hostPathVolume(cliEnvironment environment.Environment) *cobra.Command {
if err := validateConfig(&cfg); err != nil {
return err
}
return OutputZip(cliEnvironment.Logger(), cfg)
return OutputZip(cliEnvironment.Logger(), cliEnvironment.InputOutput(), cfg)
}
c.Flags().StringVarP(&hostpath.Central.HostPath, "hostpath", "", "/var/lib/stackrox", "path on the host")
c.Flags().StringVarP(&hostpath.Central.NodeSelectorKey, "node-selector-key", "", "", "node selector key (e.g. kubernetes.io/hostname)")
Expand Down
42 changes: 23 additions & 19 deletions roxctl/central/initbundles/fetch_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package initbundles

import (
"context"
"io"
"os"

"github.com/pkg/errors"
Expand All @@ -24,20 +25,31 @@ func fetchCAConfig(cliEnvironment environment.Environment, outputFile string) er
defer utils.IgnoreError(conn.Close)
svc := v1.NewClusterInitServiceClient(conn)

bundleOutput := os.Stdout
if outputFile != "" {
bundleOutput, err = os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return errors.Wrap(err, "opening output file for writing CA config")
if outputFile == "" {
return writeCA(ctx, svc, cliEnvironment.InputOutput().Out())
}

bundleOutput, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return errors.Wrap(err, "opening output file for writing CA config")
}
defer func() {
if bundleOutput != nil {
_ = bundleOutput.Close()
utils.Should(os.Remove(outputFile))
}
defer func() {
if bundleOutput != nil {
_ = bundleOutput.Close()
utils.Should(os.Remove(outputFile))
}
}()
}()
if err := writeCA(ctx, svc, bundleOutput); err != nil {
return err
}
cliEnvironment.Logger().InfofLn("The CA configuration has been written to file %q.", outputFile)
if err := bundleOutput.Close(); err != nil {
return errors.Wrap(err, "closing output file for CA config")
}
return nil
}

func writeCA(ctx context.Context, svc v1.ClusterInitServiceClient, bundleOutput io.Writer) error {
resp, err := svc.GetCAConfig(ctx, &v1.Empty{})
if err != nil {
return errors.Wrap(err, "fetching CA config")
Expand All @@ -47,14 +59,6 @@ func fetchCAConfig(cliEnvironment environment.Environment, outputFile string) er
if err != nil {
return errors.Wrap(err, "writing init bundle")
}
if bundleOutput != os.Stdout {
cliEnvironment.Logger().InfofLn("The CA configuration has been written to file %q.", outputFile)
if err := bundleOutput.Close(); err != nil {
return errors.Wrap(err, "closing output file for CA config")
}
bundleOutput = nil
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions roxctl/central/initbundles/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func generateInitBundle(cliEnvironment environment.Environment, name string, out
files := make([]*os.File, 0, len(outputs))
defer func() {
for _, f := range files {
if f != nil && f != os.Stdout {
if f != nil && f != os.Stdout { //nolint:forbidigo // TODO(ROX-13473)
name := f.Name()
_ = f.Close()
utils.Should(os.Remove(name))
Expand All @@ -44,7 +44,7 @@ func generateInitBundle(cliEnvironment environment.Environment, name string, out
// First try to open all files. Since creating a bundle has side effects, let's not attempt to do so
// before we have high confidence that the writing will succeed.
for _, out := range outputs {
outFile := os.Stdout
outFile := os.Stdout //nolint:forbidigo // TODO(ROX-13473)
if out.filename != "" {
outFile, err = os.OpenFile(out.filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
Expand Down Expand Up @@ -77,7 +77,7 @@ func generateInitBundle(cliEnvironment environment.Environment, name string, out
if _, err := outFile.Write(out.format(resp)); err != nil {
return errors.Wrapf(err, "writing init bundle to %s", stringutils.FirstNonEmpty(out.filename, "<stdout>"))
}
if outFile != os.Stdout {
if outFile != os.Stdout { //nolint:forbidigo // TODO(ROX-13473)
cliEnvironment.Logger().InfofLn("The newly generated init bundle has been written to file %q.", outFile.Name())
if err := outFile.Close(); err != nil {
return errors.Wrapf(err, "closing output file %q", outFile.Name())
Expand Down
3 changes: 1 addition & 2 deletions roxctl/central/userpki/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package list

import (
"context"
"os"
"time"

"github.com/cloudflare/cfssl/helpers"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (cmd *centralUserPkiListCommand) listProviders() error {
}
if cmd.json {
m := jsonpb.Marshaler{Indent: " "}
err = m.Marshal(os.Stdout, providers)
err = m.Marshal(cmd.env.InputOutput().Out(), providers)
if err == nil {
cmd.env.Logger().PrintfLn("")
}
Expand Down