Skip to content

Commit

Permalink
feat: Update README and hadnle long images (#30)
Browse files Browse the repository at this point in the history
large number of niggle fixes as well

Signed-off-by: Owen Rumney <[email protected]>

Signed-off-by: Owen Rumney <[email protected]>
  • Loading branch information
owenrumney authored Sep 21, 2022
1 parent d968830 commit f344c27
Show file tree
Hide file tree
Showing 32 changed files with 205 additions and 166 deletions.
Binary file modified .github/images/scan_all.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/images/scan_all_images.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/images/scan_aws_account.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/images/scan_filesystem.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/images/scan_individual_images.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/images/scan_remote_image.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/images/settings.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 60 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ lazydocker, lazynpm, lazygit)
- File System Scanning
- [Scan a filesystem for vulnerabilities and misconfigurations](#scanning-a-filesystem)

## What does it do

lazytrivy will run Trivy in a docker container and display the results in a terminal UI, the intention is that this will make it more stable across all platforms.

When running a Filesystem scan, lazytrivy will mount the target dir in the container and run Trivy against it.

Trivy intermittently downloads the latest DB - while lazytrivy maintains a cache, if you experience a delay in the scanning of an image or filesystem, it is likely trivy is running a download.

If you're interested in seeing what's happening behind the scenes in Docker, I'd thoroughly recommend using [LazyDocker](https://github.com/jesseduffield/lazydocker).


## Installation

Expand All @@ -49,13 +59,24 @@ A config file can be added to `~/.config/lazytrivy/config.yml` to set default op

```yaml
aws:
accountno: "1234567890981"
region: eu-west-1
accountno: "464897523927"
region: us-east-1
vulnerability:
ignoreunfixed: false
cachedirectory: /home/owen/.cache/trivy
debug: false
ignoreunfixed: false
filesystem:
scansecrets: true
scanmisconfiguration: true
scanvulnerabilities: true
cachedirectory: ~/.cache
debug: true
trace: false

```
#### Config via UI
Settings can be adjusted via the UI by pressing the `,` key at any time.

![Settings](./.github/images/settings.gif)

By setting `debug` to true, additional logs will be generated in `/tmp/lazytrivy.log`

Expand All @@ -64,9 +85,41 @@ By setting `debug` to true, additional logs will be generated in `/tmp/lazytrivy
`lazytrivy` is super easy to use, just run it with the following command:

```bash
lazytrivy
lazytrivy --help
Usage:
lazytrivy [command]
Available Commands:
aws Launch lazytrivy in aws scanning mode
filesystem Launch lazytrivy in filesystem scanning mode
help Help about any command
image Launch lazytrivy in image scanning mode
Flags:
--debug Launch with debug logging
--docker-host string Docker host to connect to (default "unix:///var/run/docker.sock")
-h, --help help for lazytrivy
--trace Launch with trace logging
Use "lazytrivy [command] --help" for more information about a command.
```

### Viewing logs

Logs are generated in `$HOME/.lazytrivy/logs/lazytrivy.log` with the default level at `info`. You can change the log level by setting the `--debug` flag.

To get even more information (more than you need), you can set the `--trace` flag. This will generate a lot of logs, so be careful and most of it is for tracking the position of the cursor, Docker events etc.

### Setting the docker host

By default, lazytrivy will connect to the docker daemon on the local machine by looking at the current context.

The default docker host is `unix:///var/run/docker.sock`. If you are running Docker on a remote host, you can set the docker host with the `--docker-host` flag.

```bash
### Starting in a specific mode
You can start `lazytrivy` in a specific mode using `aws`, `images` or `filesystem`:
Expand All @@ -77,7 +130,7 @@ For example, to scan a specific filesystem folder, you could run:
lazytrivy fs --path /home/owen/code/github/owenrumney/example
```

This will start in that mode.
This will start in filesystem mode pointing to the specified path. If no path is provided it will point to the current working directory.


### Scanning all local images
Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func createGeneralFlags() *pflag.FlagSet {

func createFilesystemFlags() *pflag.FlagSet {
filesystemFlags := pflag.NewFlagSet("filesystem", pflag.ExitOnError)

filesystemFlags.StringVar(&scanPath, "path", "", "Path to scan")

return filesystemFlags
}
10 changes: 8 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ var cmdAWS = &cobra.Command{
}

var cmdFS = &cobra.Command{
Use: "filesystem",
Short: "Launch lazytrivy in filesystem scanning mode",
Use: "filesystem",
Aliases: []string{"fs"},
Short: "Launch lazytrivy in filesystem scanning mode",
RunE: func(cmd *cobra.Command, args []string) error {
return startGUI(widgets.FileSystemTab)
},
Expand All @@ -43,9 +44,14 @@ func GetRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "lazytrivy",
}

rootCmd.AddCommand(cmdImage)
rootCmd.AddCommand(cmdAWS)
rootCmd.AddCommand(cmdFS)

rootCmd.Flags().AddFlagSet(generalFlags)

rootCmd.CompletionOptions.HiddenDefaultCmd = true

return rootCmd
}
26 changes: 6 additions & 20 deletions internal/cmd/start.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/owenrumney/lazytrivy/pkg/config"
Expand All @@ -11,9 +10,11 @@ import (
)

func startGUI(tab widgets.Tab) error {
logger.Configure()

workingDir, err := os.Getwd()
if err != nil {
fail(err)
return err
}

cfg, err := config.Load()
Expand All @@ -40,15 +41,14 @@ func startGUI(tab widgets.Tab) error {

control, err := gui.New(tab, cfg)
if err != nil {
fail(err)

return err
}

defer control.Close()

// create the widgets
if err := control.CreateWidgets(); err != nil {
fail(err)
return err

}

Expand All @@ -57,20 +57,6 @@ func startGUI(tab widgets.Tab) error {
return err
}

if control.IsDockerDesktop() {
control.ShowDockerDesktopWarning()
}

// Enter the run loop - it's all in the gui from this point on
if err := control.Run(); err != nil {
fail(err)

}

return nil
}

func fail(err error) {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s", err)
os.Exit(1)
return control.Run()
}
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import (
"os"

"github.com/owenrumney/lazytrivy/internal/cmd"
"github.com/owenrumney/lazytrivy/pkg/logger"
)

func main() {
// configure the logger
logger.Configure()

// if no args are passed, open in image mode
if len(os.Args[1:]) == 0 {
logger.Infof("No arguments passed, opening in image mode")
os.Args = append(os.Args, "image")
}

rootCmd := cmd.GetRootCmd()
_ = rootCmd.Execute()
if err := rootCmd.Execute(); err != nil {
logger.Errorf("Error executing command: %v", err)
os.Exit(1)
}
}
25 changes: 16 additions & 9 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ var defaultConfig *Config

var configPath string

func init() {
func createDefaultConfig() error {
logger.Debugf("Creating default config")
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Errorf("Error getting user home directory: %s", err)
homeDir = os.TempDir()
}
trivyCacheDir := filepath.Join(homeDir, ".cache", "trivy")
Expand All @@ -64,17 +66,26 @@ func init() {
}
configDir, err := os.UserConfigDir()
if err != nil {
return
return err
}

lazyTrivyConfigDir := filepath.Join(configDir, "lazytrivy")

_ = os.MkdirAll(lazyTrivyConfigDir, os.ModePerm)
if err := os.MkdirAll(lazyTrivyConfigDir, os.ModePerm); err != nil {
return err
}

configPath = filepath.Join(lazyTrivyConfigDir, "config.yaml")

return nil
}

func Load() (*Config, error) {

if err := createDefaultConfig(); err != nil {
return nil, err
}

logger.Debugf("Attempting to load config from %s", configPath)
if _, err := os.Stat(configPath); err != nil {
logger.Debugf("No config file found, using defaults")
Expand All @@ -91,12 +102,7 @@ func Load() (*Config, error) {
logger.Errorf("Error parsing config file: %s", err)
return defaultConfig, err
}

cwd, err := os.Getwd()
if err != nil {
return nil, err
}
defaultConfig.Filesystem.WorkingDirectory = cwd
logger.Infof("Loaded config from %s", configPath)

return defaultConfig, nil
}
Expand All @@ -114,6 +120,7 @@ func Save(config *Config) error {
return err
}

logger.Infof("Saved config to %s", configPath)
return nil
}

Expand Down
40 changes: 1 addition & 39 deletions pkg/controllers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,6 @@ func (c *Controller) Initialise() error {
return outerErr
}

func (c *Controller) refreshServices() error {
logger.Debugf("getting caches services")
services, err := c.accountRegionCacheServices(c.Config.AWS.AccountNo, c.Config.AWS.Region)
if err != nil {
return err
}

logger.Debugf("Updating the services view with the identified services")
if v, ok := c.Views[widgets.Services].(*widgets.ServicesWidget); ok {
if err := v.RefreshServices(services, 20); err != nil {
return err
}
}
return nil
}

func (c *Controller) UpdateAccount(account string) error {
logger.Debugf("Updating the AWS account details in the config")
c.Config.AWS.AccountNo = account
c.Config.AWS.Region = "us-east-1"
if err := c.Config.Save(); err != nil {
return err
}

return c.update()
}

func (c *Controller) UpdateRegion(region string) error {
logger.Debugf("Updating the AWS region details in the config")
c.Config.AWS.Region = region
if err := c.Config.Save(); err != nil {
return err
}
return c.update()
}

func (c *Controller) update() error {
if v, ok := c.Views[widgets.Account]; ok {
if a, ok := v.(*widgets.AccountWidget); ok {
Expand Down Expand Up @@ -235,7 +199,7 @@ func (c *Controller) discoverAccount(region string) (string, string, error) {
}

if regionEnv, ok := os.LookupEnv("AWS_REGION"); ok {
logger.Debugf("Using AWS_REGION environment variable")
logger.Infof("Using AWS_REGION environment variable")
cfg.Region = regionEnv
}

Expand All @@ -259,7 +223,6 @@ func (c *Controller) scanAccount(gui *gocui.Gui, _ *gocui.View) error {
if err != nil {
if strings.HasPrefix(err.Error(), "failed to discover AWS caller identity") {
c.UpdateStatus("Failed to discover AWS credentials.")
logger.Errorf("failed to discover AWS credentials: %v", err)
return NewErrNoValidCredentials()
}
return err
Expand All @@ -268,7 +231,6 @@ func (c *Controller) scanAccount(gui *gocui.Gui, _ *gocui.View) error {
c.UpdateStatus("Checking credentials for account...")
if account != c.Config.AWS.AccountNo && c.Config.AWS.AccountNo != "" {
c.UpdateStatus("Account number does not match credentials.")
logger.Errorf("Account number does not match credentials.")
return fmt.Errorf("account number mismatch: %s != %s", account, c.Config.AWS.AccountNo)
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/controllers/aws/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ var helpCommands = []string{
}

func help(gui *gocui.Gui, _ *gocui.View) error {

w, h := gui.Size()

v := widgets.NewAnnouncementWidget("help", "Help", w, h, helpCommands, gui)
v := widgets.NewAnnouncementWidget("help", "Help", helpCommands, gui)

if err := gui.SetKeybinding("help", gocui.KeyEsc, gocui.ModNone, func(gui *gocui.Gui, _ *gocui.View) error {
if _, err := gui.SetCurrentView("services"); err != nil {
Expand Down
Loading

0 comments on commit f344c27

Please sign in to comment.