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(image): custom docker host option #3599

Merged
merged 14 commits into from
Apr 20, 2023
Merged

Conversation

aswath-s-tw
Copy link
Contributor

Description

This is a draft. Added custom docker socket option --docker-host. Please review and suggest changes.

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).

@CLAassistant
Copy link

CLAassistant commented Feb 11, 2023

CLA assistant check
All committers have signed the CLA.

@aswath-s-tw aswath-s-tw changed the title feat(mode:image container:docker cli:flag) custom docker host option feat(image) custom docker host option (#2997) Feb 12, 2023
@aswath-s-tw aswath-s-tw changed the title feat(image) custom docker host option (#2997) feat(image): custom docker host option (#2997) Feb 12, 2023
@aswath-s-tw aswath-s-tw marked this pull request as ready for review February 12, 2023 06:28
Copy link
Contributor

@AndreyLevchenko AndreyLevchenko left a comment

Choose a reason for hiding this comment

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

as for testing: there is probably nothing that can be tested in docker_test. However in image_test you can set custom DockerHost for test docker engine and then check if you can successfully reach it

pkg/commands/artifact/scanner.go Outdated Show resolved Hide resolved
pkg/fanal/image/daemon.go Outdated Show resolved Hide resolved
pkg/fanal/image/daemon/docker.go Outdated Show resolved Hide resolved
@aswath-s-tw
Copy link
Contributor Author

Hey @AndreyLevchenko , Thank you so much for suggesting changes. Have tried my bit in improving the code as per the mentioned suggestions. Please take a look at it and let me know ! Thanks again.

@aswath-s-tw
Copy link
Contributor Author

Not sure why the windows section in pipeline failed. Any ideas on this ?

@aswath-s-tw
Copy link
Contributor Author

aswath-s-tw commented Feb 14, 2023

I think i got it, for windows, the domain socket url starts with npipe instead of unix. not sure how we handle this though ! Any suggestions @AndreyLevchenko

@AndreyLevchenko
Copy link
Contributor

I think i got it, for windows, the domain socket url starts with npipe instead of unix. not sure how we handle this though ! Any suggestions @AndreyLevchenko

There is lot of examples in trivy code. For instance https://github.com/aquasecurity/trivy/blob/main/pkg/misconf/scanner_test.go#L128

@aswath-s-tw
Copy link
Contributor Author

@AndreyLevchenko, can you trigger the pipeline to see if the current change works ?

@pmengelbert
Copy link
Contributor

pmengelbert commented Feb 14, 2023

There is already a way to set the socket address for containerd, and it's done by environment variable.

This PR splits the configuration of the socket: docker uses a cli arg and containerd uses an env var. What's more, the socket for the podman daemon can only be set indirectly by the XDG_RUNTIME_DIR env var.

It's a little awkward to have three different ways to configure the socket address for the three different supported runtimes. Would it be possible to expand the scope of this PR to include support for the other runtimes as well? You would probably also want to rename the flag from --docker-host to something like --runtime-host, since it would apply across runtime implementations.

@aswath-s-tw
Copy link
Contributor Author

sure @pmengelbert , that makes total sense. I can maybe get into implementing your suggestion right after we figure out how to resolve the test for windows environment.

@knqyf263
Copy link
Collaborator

I feel like --runtime-host could be confusing. I think we want to configure the specific runtime explicitly. I prefer --docker-host for the reason. Or, we may need something like --runtime-host docker:/path/to/socket.

@aswath-s-tw
Copy link
Contributor Author

aswath-s-tw commented Feb 16, 2023

@knqyf263 @pmengelbert I think we can limit the scope of this issue / PR to support --docker-host and discuss more about --runtime-host in a different issue ? Let me know your thoughts.

@AndreyLevchenko Also the pipeline is green RN. Please review and accept the PR or suggest changes if any.

Thanks !

@pmengelbert
Copy link
Contributor

pmengelbert commented Feb 16, 2023

I feel like --runtime-host could be confusing. I think we want to configure the specific runtime explicitly. I prefer --docker-host for the reason. Or, we may need something like --runtime-host docker:/path/to/socket.

@knqyf263 Since the code looks for the image by first attempting the docker socket, then the containerd socket, then podman, then remote registry, I think you are right that there should be either a) separate flags or, b) --runtime-host can be supplied multiple times with the runtime as the scheme of the url. Separate flags is probably easier. Thoughts?

The other issue is, what happens if users supply both the flag and the env var? I assume the CLI arg takes precedence.

@knqyf263 @pmengelbert I think we can limit the scope of this issue / PR to support --docker-host and discuss more about --runtime-host in a different issue ? Let me know your thoughts.

I'll defer to @knqyf263 but IMO this PR should at least allow setting the socket address by environment variable, since users can do so already with containerd and podman. Thoughts?

@aswath-s-tw
Copy link
Contributor Author

I guess all 3 container runtimes can be configured by ENV vars already. The scope of this issue was to add a flag to control the host for local docker engine scan.

@knqyf263
Copy link
Collaborator

Separate flags is probably easier. Thoughts?

trivy image --docker-host /path/to/docker.sock --containerd-host /path/to/containerd.sock

or

trivy image --runtime-hosts docker:/path/to/docker.sock --runtime-hosts containerd:/path/to/containerd.sock

The first one may be more intuitive.

The other issue is, what happens if users supply both the flag and the env var? I assume the CLI arg takes precedence.

Yes, CLI flags should take precedence. I believe WithHost takes care of that.

opts = append(opts, client.WithHost(host))

I guess all 3 container runtimes can be configured by ENV vars already. The scope of this issue was to add a flag to control the host for local docker engine scan.

Right.

@aswath-s-tw
Copy link
Contributor Author

aswath-s-tw commented Feb 25, 2023

So shall I start working on these changes ? I have one doubt though. Let's assume this scenario. If the user provides --containerd-host as an arg, should we limit the scan to just containerd and ignore the other 3 scanners ? Same question applies for other host options as well !

@pmengelbert
Copy link
Contributor

Related #3049

@aswath-s-tw
Copy link
Contributor Author

#3599 (comment)

Any updates on this comment ? I can work accordingly if we can make a decision !

Copy link
Contributor

@pmengelbert pmengelbert left a comment

Choose a reason for hiding this comment

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

LGTM. I'm not a maintainer, but since I commented earlier requesting changes, I should note I am fine with this being merged as is (after a rebase).

@knqyf263 can the remaining work on the flags be accomplished in a separate PR? It's already being tracked by #3049

@knqyf263 knqyf263 changed the title feat(image): custom docker host option (#2997) feat(image): custom docker host option Apr 20, 2023
@knqyf263
Copy link
Collaborator

We recently refactored options, and it causes conflicts. It is just bothering you, so I've resolved them.

should we limit the scan to just containerd and ignore the other 3 scanners ?

No, I don't think so. --docker-host just configures the socket path.

@knqyf263 knqyf263 merged commit be47b68 into aquasecurity:main Apr 20, 2023
@knqyf263
Copy link
Collaborator

Thanks for your patience!

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.

custom docker socket for image scan option
5 participants