github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/cmd/defsec/fs.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"path/filepath"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/khulnasoft-lab/defsec/pkg/extrafs"
    11  	"github.com/khulnasoft-lab/defsec/pkg/scanners/options"
    12  	"github.com/khulnasoft-lab/defsec/pkg/scanners/universal"
    13  )
    14  
    15  func init() {
    16  	fsCmd := &cobra.Command{
    17  		Use:   "fs [directory]",
    18  		Short: "Scan a filesystem for misconfigurations of all types",
    19  		Args:  cobra.ExactArgs(1),
    20  		RunE: func(cmd *cobra.Command, args []string) error {
    21  			cmd.SilenceUsage = true
    22  			cmd.SilenceErrors = true
    23  			return scanFS(args[0], cmd.OutOrStdout(), cmd.ErrOrStderr())
    24  		},
    25  	}
    26  	rootCmd.AddCommand(fsCmd)
    27  }
    28  
    29  func scanFS(dir string, stdout, stderr io.Writer) error {
    30  
    31  	abs, err := filepath.Abs(dir)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	filesystem := extrafs.OSDir(abs)
    36  
    37  	opts := []options.ScannerOption{
    38  		options.ScannerWithEmbeddedPolicies(true),
    39  		options.ScannerWithEmbeddedLibraries(true),
    40  	}
    41  
    42  	if flagDebug {
    43  		opts = append(opts, options.ScannerWithDebug(stderr))
    44  	}
    45  
    46  	scanner := universal.New(opts...)
    47  
    48  	// Execute the filesystem based scanners
    49  	results, err := scanner.ScanFS(context.TODO(), filesystem, ".")
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	return outputResults(stdout, abs, results)
    55  }