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

feat(filesystem): scan in client/server mode #1829

Merged
merged 36 commits into from
Mar 21, 2022
Merged

feat(filesystem): scan in client/server mode #1829

merged 36 commits into from
Mar 21, 2022

Conversation

afdesk
Copy link
Contributor

@afdesk afdesk commented Mar 14, 2022

Description

Added a new option --server to filesystem command for support local filesystem scan in client/server mode.

Also there was moved client package to artifact/client.go.

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@@ -313,6 +312,19 @@ var (
EnvVars: []string{"TRIVY_INSECURE"},
}

remoteServer = cli.StringFlag{
Name: "remote",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I want to rename it with --server, but --remote should be also used as an alias.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -18,9 +21,19 @@ type Option struct {
option.CacheOption
option.ConfigOption

// For policy downloading
NoProgress bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have it from DBOption.

NoProgress bool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. the comment got confused me.

Comment on lines 31 to 36
RemoteAddr string
token string
tokenHeader string
customHeaders []string
// this field is populated in Init()
CustomHeaders http.Header
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should define a new struct for these options.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've created a new struct: RemoteOption

@@ -46,6 +46,12 @@ var StandaloneFilesystemSet = wire.NewSet(
StandaloneSuperSet,
)

// StandaloneFilesystemRemoteSet binds filesystem dependencies for client/server mode
var StandaloneFilesystemRemoteSet = wire.NewSet(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not standalone, but client/server.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return remoteCache.NewRemoteCache(c.RemoteAddr, c.CustomHeaders, c.Insecure), nil, nil
}

func initFSCache(c Option) (cache.ArtifactCache, cache.LocalArtifactCache, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We may want to rename it with initCache and define NopCache like NopCloser.
https://pkg.go.dev/io#NopCloser

The signature could be:

func NopCache(ArtifactCache) Cache

so that RemoteCache can implement cache.Cache. Then, update operation.go as below.

	// client/server mode
	if c.ServerAddr != "" {
		remoteCache, err = cache. NewRemoteCache(c.ServerAddr, c.CustomHeaders, c.Insecure)
		if err != nil {...}

		// Wrap RemoteCache so that it can implement cache.Cache
		return Cache{Cache: cache.NopCache(remoteCache)}, nil
	}

	// standalone mode with local fs cache
	fsCache, err := cache.NewFSCache(utils.CacheDir())
	if err != nil {...}
	return Cache{Cache: fsCache}, nil

fsCache, err := cache.NewFSCache(utils.CacheDir())
if err != nil {
return Cache{}, xerrors.Errorf("unable to initialize fs cache: %w", err)
}
return Cache{Cache: fsCache}, nil

What do you think of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's an interesting idea. I've done it

@afdesk afdesk requested a review from knqyf263 March 16, 2022 15:54
@afdesk afdesk marked this pull request as ready for review March 20, 2022 14:18
@@ -177,11 +253,17 @@ func TestOption_Init(t *testing.T) {
set.Bool("reset", false, "")
set.Bool("skip-db-update", false, "")
set.Bool("download-db-only", false, "")
set.Bool("clear-cache", false, "")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, removed.

}

// Init initialize the options for client/server mode
func (c *RemoteOption) Init(logger *zap.SugaredLogger) (err error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like we don't need to return an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch! removed.

RemoteAddrOption: "--server",
Target: "testdata/fixtures/fs/pom/",
},
golden: "testdata/fs-pom.json.golder",
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have to use the same golden file with standalone mode. Maybe we have some differences now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea! done.

@knqyf263 knqyf263 merged commit d6418cf into main Mar 21, 2022
@knqyf263 knqyf263 deleted the fs_remote branch March 21, 2022 13:51
@knqyf263
Copy link
Collaborator

Thanks!

ankk13 pushed a commit to ankk13/trivy that referenced this pull request Mar 28, 2022
Co-authored-by: knqyf263 <knqyf263@gmail.com>
liamg pushed a commit that referenced this pull request Jun 7, 2022
Co-authored-by: knqyf263 <knqyf263@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

trivy scan local filesystem not supported in client mode
2 participants