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

bug(k8s): Image scanned from "metadata:annotations:kubectl.kubernetes.io/last-applied-configuration" instead of "spec" #7573

Open
afdesk opened this issue Sep 23, 2024 Discussed in #7551 · 7 comments
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug.

Comments

@afdesk
Copy link
Contributor

afdesk commented Sep 23, 2024

Description

since #4786 (aquasecurity/trivy-kubernetes#189) Trivy scan last applied configuration instead of actual Resource state.

Reason

For scans Trivy prefers info from an annotation. so if a customer mixes imperative and declarative styles, the result is a bit confusing.

https://github.com/aquasecurity/trivy-kubernetes/blob/b070991579cacd7634052dee2e250350d6e493e8/pkg/trivyk8s/trivyk8s.go#L208-L214

Note: the issue will be happened only if a resource is created from kubectl apply.

Solution

We can try to receive a version info more flexible way

Reproduction Steps

  1. Create a deployment:
$ kubectl apply -f nginx-deployment.yaml
/nginx-deployment ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx web: my-app spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.1 ports: - containerPort: 80 ```
  1. update an image manually:
$ kubectl set image deployment.apps/nginx-deployment nginx=nginx:1.15.0
  1. Result:
$ kubectl get deployment.apps/nginx-deployment -o yaml
nginx-deployment details apiVersion: apps/v1 kind: Deployment metadata: annotations: deployment.kubernetes.io/revision: "3" kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"nginx","web":"my-app"},"name":"nginx-deployment","namespace":"7551"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"image":"nginx:1.14.1","name":"nginx","ports":[{"containerPort":80}]}]}}}} labels: app: nginx web: my-app pec: replicas: 1 template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.15.0 name: nginx ports: - containerPort: 80 ... ```
  1. Trivy will scan nginx:1.14.1 instead of `nginx:1.15.0", that actually runs in a cluster.

Discussed in #7551

@afdesk afdesk added the kind/bug Categorizes issue or PR as related to a bug. label Sep 23, 2024
@afdesk afdesk self-assigned this Sep 23, 2024
@michaeljsaenz
Copy link

I'd like to grab this one

@afdesk
Copy link
Contributor Author

afdesk commented Oct 7, 2024

@michaeljsaenz thanks! it'd be really nice!

@michaeljsaenz
Copy link

please assign 👍🏽

@afdesk
Copy link
Contributor Author

afdesk commented Oct 29, 2024

hi @michaeljsaenz!
how is it going? do you need any help?
thanks!

@michaeljsaenz
Copy link

hi @afdesk !
thanks for checking in, its going well now, had a little trouble with the tests due to this error, but I applied the workaround and all is well now (I wanted to confirm all tests pass before adding any changes 👍🏽)

  • I am currently working to add logic here in this package, please let me know if you have a recommendation or preference where to add this in?

Thank you!

@afdesk
Copy link
Contributor Author

afdesk commented Oct 30, 2024

@michaeljsaenz
I'd like a detailed reason for aquasecurity/trivy-kubernetes#189 at first
why it required for outdated-api when k8s convert resources
maybe there is any documentation.
honestly, I didn't deep in it yet.
thanks!

@afdesk
Copy link
Contributor Author

afdesk commented Oct 30, 2024

maybe it'll help you #4784

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

No branches or pull requests

2 participants