mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
* phase-1 Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> * factory Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> * wip: feat(cli): add an image scanning command Add a CLI command that launches an image scan. Does not scan images yet. Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * wip: feat: add image scanning service Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * chore: include dependencies Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * wip: adjust image scanning service Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * wip: feat: use scanning service in CLI Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * use iface Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> * touches Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> * continue Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> * add cmd Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> * support single workload scan Signed-off-by: Amir Malka <amirm@armosec.io> * fix conflict Signed-off-by: Amir Malka <amirm@armosec.io> * identifiers * go mod * feat(imagescan): add an image scanning command This commit adds a CLI command and an associated package that scan images for vulnerabilities. Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> feat(imagescan): fail on exceeding the severity threshold Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * chore(imagescan): include dependencies This commit adds the dependencies necessary for image scanning. Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * chore(imagescan): add dependencies to httphandler Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * added unit tests Signed-off-by: Amir Malka <amirm@armosec.io> * merge * more * integrate img scan * added unit tests Signed-off-by: Amir Malka <amirm@armosec.io> * more refactoring Signed-off-by: Amir Malka <amirm@armosec.io> * add scanned workload reference to opasessionobj Signed-off-by: Amir Malka <amirm@armosec.io> * fix GetWorkloadParentKind Signed-off-by: Amir Malka <amirm@armosec.io> * remove namespace argument from pullSingleResource, using field selector instead Signed-off-by: Amir Malka <amirm@armosec.io> * removed designators (unused) field from PolicyIdentifier, and designators argument from GetResources function Signed-off-by: Amir Malka <amirm@armosec.io> * changes * changes * fixes * changes * feat(imagescan): add an image scanning command This commit adds a CLI command and an associated package that scan images for vulnerabilities. Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> feat(imagescan): fail on exceeding the severity threshold Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * chore(imagescan): include dependencies This commit adds the dependencies necessary for image scanning. Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * chore(imagescan): add dependencies to httphandler Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * chore(imagescan): create vuln db with dedicated function Remove commented out code, too. Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * docs(imagescan): provide package-level docs Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> * finish merge * image scan tests * continue * fixes * refactor * rm duplicate * start fixes * update gh actions Signed-off-by: David Wertenteil <dwertent@armosec.io> * pr fixes * fix test * improvements --------- Signed-off-by: Daniel Grunberger <danielgrunberger@armosec.io> Signed-off-by: Vlad Klokun <vklokun@protonmail.ch> Signed-off-by: Amir Malka <amirm@armosec.io> Signed-off-by: David Wertenteil <dwertent@armosec.io> Co-authored-by: Daniel Grunberger <danielgrunberger@armosec.io> Co-authored-by: Vlad Klokun <vklokun@protonmail.ch> Co-authored-by: Amir Malka <amirm@armosec.io> Co-authored-by: David Wertenteil <dwertent@armosec.io>
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package scan
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
logger "github.com/kubescape/go-logger"
|
|
"github.com/kubescape/kubescape/v2/core/cautils"
|
|
"github.com/kubescape/kubescape/v2/core/core"
|
|
"github.com/kubescape/kubescape/v2/core/meta"
|
|
"github.com/kubescape/kubescape/v2/core/pkg/resultshandling"
|
|
"github.com/kubescape/kubescape/v2/pkg/imagescan"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type imageScanInfo struct {
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
// TODO(vladklokun): document image scanning on the Kubescape Docs Hub?
|
|
var (
|
|
imageExample = fmt.Sprintf(`
|
|
# Scan the 'nginx' image
|
|
%[1]s scan image "nginx"
|
|
|
|
# Image scan documentation:
|
|
# https://hub.armosec.io/docs/images
|
|
`, cautils.ExecName())
|
|
)
|
|
|
|
// imageCmd represents the image command
|
|
func getImageCmd(ks meta.IKubescape, scanInfo *cautils.ScanInfo, imgScanInfo *imageScanInfo) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "image <IMAGE_NAME>",
|
|
Short: "Scans an image for vulnerabilities",
|
|
Example: imageExample,
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
if len(args) != 1 {
|
|
return fmt.Errorf("The command takes exactly one image.")
|
|
}
|
|
return nil
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if err := validateImageScanInfo(scanInfo); err != nil {
|
|
return err
|
|
}
|
|
failOnSeverity := imagescan.ParseSeverity(scanInfo.FailThresholdSeverity)
|
|
|
|
ctx := context.Background()
|
|
dbCfg, _ := imagescan.NewDefaultDBConfig()
|
|
svc := imagescan.NewScanService(dbCfg)
|
|
|
|
creds := imagescan.RegistryCredentials{
|
|
Username: imgScanInfo.Username,
|
|
Password: imgScanInfo.Password,
|
|
}
|
|
|
|
userInput := args[0]
|
|
|
|
logger.L().Info(fmt.Sprintf("Scanning image: %s", userInput))
|
|
scanResults, err := svc.Scan(ctx, userInput, creds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
logger.L().Success("Image scan completed successfully")
|
|
|
|
scanInfo.SetScanType(cautils.ScanTypeImage)
|
|
|
|
outputPrinters := core.GetOutputPrinters(scanInfo, ctx)
|
|
|
|
uiPrinter := core.GetUIPrinter(ctx, scanInfo)
|
|
|
|
resultsHandler := resultshandling.NewResultsHandler(nil, outputPrinters, uiPrinter)
|
|
|
|
resultsHandler.ImageScanData = []cautils.ImageScanData{
|
|
{
|
|
PresenterConfig: scanResults,
|
|
Image: userInput,
|
|
},
|
|
}
|
|
|
|
resultsHandler.HandleResults(ctx)
|
|
|
|
if imagescan.ExceedsSeverityThreshold(scanResults, failOnSeverity) {
|
|
terminateOnExceedingSeverity(scanInfo, logger.L())
|
|
}
|
|
|
|
return err
|
|
},
|
|
}
|
|
|
|
cmd.PersistentFlags().StringVarP(&imgScanInfo.Username, "username", "u", "", "Username for registry login")
|
|
cmd.PersistentFlags().StringVarP(&imgScanInfo.Password, "password", "p", "", "Password for registry login")
|
|
|
|
return cmd
|
|
}
|
|
|
|
// validateImageScanInfo validates the ScanInfo struct for the `image` command
|
|
func validateImageScanInfo(scanInfo *cautils.ScanInfo) error {
|
|
severity := scanInfo.FailThresholdSeverity
|
|
|
|
if err := validateSeverity(severity); severity != "" && err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|