#Container scanning in dagger

1 messages ยท Page 1 of 1 (latest)

scenic sedge
#

Heya @wind rose!

Just for completeness, there's also an option 3: if your container scanner supports scanning unpacked filesystems on disk you could just get the root filesystem of the container and scan that - this is the most efficient for dagger, since no export step needs to take place.

But also, sometimes the layers are important details for the container scnaning (e.g. the layer ids might be used) - if those are important, you probably want to do 2, since then that's the same as your final production image.

You can still run everything inside dagger - you can export the image and tag it, and then scan on it (using snyk inside dagger).

wind rose
#

Sadly snyk doesn't support unpacked filesystems. Or well it does, but isn't as intelligent as actually knowing that it stems from an image.

I guess 2 is the most sane way. I've already gotten snyk to work in dagger, so that is not a problem. Only that it needs an image. We'll take some penalty in that we may or may not have to pull the image after it has been published, but I guess that is fine. Rather that then have to fiddle around with tarballs.

velvet dust
wind rose
#

Shouldve clicked on advanced container scanning... thanks a lot. I will give it a go

wind rose
#

Fyi if anyone else is searching this up. Snak only supports ociv1 images. While dagger with buildkit only seems to support ociv2 format. Might be wrong though but i couldnt get them to play nice with each other. So I just exported the ociv2 tarball. Loaded it into docker proper with docker load. Got the sha put and had snyk use that.

Snyk has some interesting cli conventions so it tool a while to debug. One of them being that it will only report the opposite of what you want. I.e. oci-archive will fail with docker archive invalid. And vice versa.

scenic sedge
#

out of curiosity, what do you mean by ociv2? there is no ociv2

velvet dust
#

I'd assume he might be referring to media types

wind rose
#

Yep I've tried pretty much all the options. Nothing seemed to play ball. As far as I can tell. With the docker option it still keeps the same file structure. The type will just either be docker or oci v2.

Snyk really wants the file structure to be .tar with layers in the root of the tar ball. Where dagger only would allow blob/sha256/<sha>.

@scenic sedge you are correct in that the version is still 1.x, however in the media types in the file structure, there are some differences. The manifest in there will say something like. opencontainers.oci.v2 or opencontainers.docker.v2. I don't have my files on hand atm. So it may be slightly off.

#

I will find the actual example monday. When I am back at work

scenic sedge
#

oh i see

#

right, it's schemav1 images then - yeah, buildkit doesn't produce these, since they've been deprecated for quite a while - i'm really surprised that snyk doesn't support scanning schemav2

wind rose
#

Also I think most of these issues are on snyks hand. As they're reimplementing the oci spec in node.

#

I'd like to contribute upstream to snyk to support these newer manifests. But I don't really want to spend company time implementing the new spec for them. Not when I've actually found a worthwhile workaround.

scenic sedge
#

here's another very hacky workaround you could try - you could create AsTarball image, and then use something like https://github.com/containers/skopeo to upload the image to a registry (which you could run inside dagger using a service) - then snyk can scan the image in your local registry

GitHub

Work with remote images registries - retrieving information, images, signing content - GitHub - containers/skopeo: Work with remote images registries - retrieving information, images, signing content

#

it's super hacky sorry, but it might work? as long as snyk doesn't need the registry to be publicly accessible

#

that's the best i can come up with at the moment sadly

wind rose
#

Ah well. I've just mounted the docker sock, so that it is available. And in the local registry anyways. That works fine for our needs.

scenic sedge
#

ahh glad you found a way to make it work!

wind rose
#

Yeah. It is still a little janky, as you need the docker sock available to run out stuff. But we need it for other things as well, so it isn't a huge deal.

#

I'd be nice though to just extract a tarball, and let snyk scan it, but it doesn't matter too much that it isn't as clean as we'd like.

velvet dust
#

@wind rose this seemed to work for me:

package main

import (
    "context"
    "os"

    "dagger.io/dagger"
)

func main() {
    ctx := context.Background()

    // initialize Dagger client
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
    if err != nil {
        panic(err)
    }
    defer client.Close()

    client.Container().From("snyk/snyk:linux").
        WithEnvVariable("SNYK_TOKEN", "$TOKEN").
        WithMountedFile("/image.tar", client.Container().From("alpine").AsTarball()).
        WithExec([]string{"snyk", "container", "test", "oci-archive:/image.tar"}).Sync(ctx)
}
#
19: exec /usr/local/bin/docker-entrypoint.sh snyk container test oci-archive:/image.tar
19: [3.65s] 
19: [3.65s] Testing oci-archive:/image.tar...
19: [3.65s] 
19: [3.65s] Organization:      marcosnils
19: [3.65s] Package manager:   apk
19: [3.65s] Project name:      docker-image|image.tar
19: [3.65s] Docker image:      oci-archive:/image.tar
19: [3.65s] Platform:          linux/amd64
19: [3.65s] Base image:        alpine:3.19.0
19: [3.65s] Licenses:          enabled
19: [3.65s] 
19: [3.65s] โœ” Tested 15 dependencies for known issues, no vulnerable paths found.
19: [3.65s] 
19: [3.65s] According to our scan, you are currently using the most secure version of the selected base image
19: [3.65s] 
19: [3.65s] 
19: exec /usr/local/bin/docker-entrypoint.sh snyk container test oci-archive:/image.tar DONE

wondering how you're triggering the OCI v2 thing

wind rose
#

I will have a look tomorrow. I don't have the code on hand atm. The only difference I can see right now is that I used AsTarball( /* options */). I even tried to download and save a raw debian image, and that worked. But the image produced by dagger wouldn't work. But anyways. I will send it tomorrow ๐Ÿ˜„

I did use snyk/snyk:docker-19 though.

muted sleet
scenic sedge
#

you can also pass the host docker socket to the function

#

if the function takes a dagger.Socket type

wind rose
#

I believe this was back when i didnt use modules so i just passed it in as a docker type or using the docker API.

#

Modules should be the same however