mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
* Fix issue for scanning list obj * Fix go mod in httphandler pkg * Broken links fix in roadmap.md Planning, backlog, and wishlist links were not taking to the required section. * override infoMap only if it's not nil * improved icon of kubescape in readme * Support scanning several files * gramatical improvements * docs(readme): Star → star * Fix issues according to review * Handle with issues caused by updating opa-utils * Fix scanning ListObj following reviews * Update core/pkg/resourcehandler/filesloader.go Co-authored-by: Vlad Klokun <vladklokun@users.noreply.github.com> * Update completion.go * Added fixed control input * update go.mod * Print chart name log when fail to generate * Change formatting to %s * Added resource prioritization information, raw resource will be sent on the result object * Merging typo fixes from master (#772) * greetings * Update aws.sh simplified the comment * typo: In the title and h1 element Their was a typo in index.html file. * punctuation changes * docs : added gitpod badge in readme.md * fixed typos * some grammar mistake is corrected inPULL_REQUEST_TEMPLATE.md file * Updated README.md file Added link to CONTRIBUTING.md file in a line in README. * Added link to code of conduct file I have added link to the code of conduct file and fixed some problems in the Readme file. * Fixed readme * Added alpine tag Adding alpine tag instead of latest and removing repeating commands * roadmap.md file is modified * Automatically Close "Typo" labelled Issue * build.py is modified * modified PR template * Fixed some typos in feature_request.md "." at the end of the headings were missing and all the text were in same line. Now this gives a clear and concise view of the texts. * fixed the typo in docs/index.html Found and fixed typo in the 'alt' attribute of img tag * Update PULL_REQUEST_TEMPLATE.md Co-authored-by: Krishna Agarwal <dmkrishna.agarwal@gmail.com> Co-authored-by: Saswata Senapati <74651639+saswat16@users.noreply.github.com> Co-authored-by: Rahul Singh <110548934+rahuldhirendersingh@users.noreply.github.com> Co-authored-by: deepuyadav004 <deepuyadavze@gmail.com> Co-authored-by: kartik <97971066+kartikgajjar7@users.noreply.github.com> Co-authored-by: Rounak-28 <95576871+Rounak-28@users.noreply.github.com> Co-authored-by: pwnb0y <vickykr07@yahoo.com> Co-authored-by: Ben Hirschberg <59160382+slashben@users.noreply.github.com> Co-authored-by: Saptarshi Sarkar <saptarshi.programmer@gmail.com> Co-authored-by: Rahul Surwade <93492791+RahulSurwade08@users.noreply.github.com> Co-authored-by: Suhas Gumma <43647369+suhasgumma@users.noreply.github.com> Co-authored-by: Kamal Nayan <95926324+legendarykamal@users.noreply.github.com> Co-authored-by: TarangVerma <90996971+TarangVerma@users.noreply.github.com> Co-authored-by: avikittu <65793296+avikittu@users.noreply.github.com> * update logger version (#773) * Fixed: Kubescape fails to authenticate remote private Github repo (#721) * grammar error fixer in CONTRIBUTING.md * scanning private git repository is available * giturl to gitapi * NO TOKEN error functionality added * Used GetToken method of giturl.IGitAPPI for auth Co-authored-by: satyam kale <satyamkale271@gmail.com> Co-authored-by: Ben Hirschberg <59160382+slashben@users.noreply.github.com> * bump opa-utils to 181 * Option to force enable color output (closes #560) (#767) * Option to force enable color output (closes #560) (cherry picked from commit 4f951781ee8dd6bb451ac7d159787f47e4b07379) * Update go.mod * Update host scanner image (#774) * update logger version * update scanner image Co-authored-by: Moshe-Rappaport-CA <moshep@armosec.io> Co-authored-by: Moshe Rappaport <89577611+Moshe-Rappaport-CA@users.noreply.github.com> Co-authored-by: Om Raut <33827410+om2137@users.noreply.github.com> Co-authored-by: Kamal Nayan <95926324+legendarykamal@users.noreply.github.com> Co-authored-by: Vlad Klokun <vladklokun@users.noreply.github.com> Co-authored-by: Chirag Arora <84070677+Chirag8023@users.noreply.github.com> Co-authored-by: shm12 <shmuelb@armosec.io> Co-authored-by: Amir Malka <amirm@armosec.io> Co-authored-by: Krishna Agarwal <dmkrishna.agarwal@gmail.com> Co-authored-by: Saswata Senapati <74651639+saswat16@users.noreply.github.com> Co-authored-by: Rahul Singh <110548934+rahuldhirendersingh@users.noreply.github.com> Co-authored-by: deepuyadav004 <deepuyadavze@gmail.com> Co-authored-by: kartik <97971066+kartikgajjar7@users.noreply.github.com> Co-authored-by: Rounak-28 <95576871+Rounak-28@users.noreply.github.com> Co-authored-by: pwnb0y <vickykr07@yahoo.com> Co-authored-by: Ben Hirschberg <59160382+slashben@users.noreply.github.com> Co-authored-by: Saptarshi Sarkar <saptarshi.programmer@gmail.com> Co-authored-by: Rahul Surwade <93492791+RahulSurwade08@users.noreply.github.com> Co-authored-by: Suhas Gumma <43647369+suhasgumma@users.noreply.github.com> Co-authored-by: TarangVerma <90996971+TarangVerma@users.noreply.github.com> Co-authored-by: avikittu <65793296+avikittu@users.noreply.github.com> Co-authored-by: satyam kale <satyamkale271@gmail.com> Co-authored-by: Aditya Pratap Singh <adityapratapsingh51@gmail.com>
328 lines
7.8 KiB
Go
328 lines
7.8 KiB
Go
package cautils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/kubescape/go-logger/helpers"
|
|
"github.com/kubescape/k8s-interface/workloadinterface"
|
|
|
|
logger "github.com/kubescape/go-logger"
|
|
"github.com/kubescape/opa-utils/objectsenvelopes"
|
|
"github.com/kubescape/opa-utils/objectsenvelopes/localworkload"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
var (
|
|
YAML_PREFIX = []string{"yaml", "yml"}
|
|
JSON_PREFIX = []string{"json"}
|
|
)
|
|
|
|
type FileFormat string
|
|
|
|
const (
|
|
YAML_FILE_FORMAT FileFormat = "yaml"
|
|
JSON_FILE_FORMAT FileFormat = "json"
|
|
)
|
|
|
|
// LoadResourcesFromHelmCharts scans a given path (recuresively) for helm charts, renders the templates and returns a map of workloads and a map of chart names
|
|
func LoadResourcesFromHelmCharts(basePath string) (map[string][]workloadinterface.IMetadata, map[string]string) {
|
|
directories, _ := listDirs(basePath)
|
|
helmDirectories := make([]string, 0)
|
|
for _, dir := range directories {
|
|
if ok, _ := IsHelmDirectory(dir); ok {
|
|
helmDirectories = append(helmDirectories, dir)
|
|
}
|
|
}
|
|
|
|
sourceToWorkloads := map[string][]workloadinterface.IMetadata{}
|
|
sourceToChartName := map[string]string{}
|
|
for _, helmDir := range helmDirectories {
|
|
chart, err := NewHelmChart(helmDir)
|
|
if err == nil {
|
|
wls, errs := chart.GetWorkloadsWithDefaultValues()
|
|
if len(errs) > 0 {
|
|
logger.L().Error(fmt.Sprintf("Rendering of Helm chart template '%s', failed: %v", chart.GetName(), errs))
|
|
continue
|
|
}
|
|
|
|
chartName := chart.GetName()
|
|
for k, v := range wls {
|
|
sourceToWorkloads[k] = v
|
|
sourceToChartName[k] = chartName
|
|
}
|
|
}
|
|
}
|
|
return sourceToWorkloads, sourceToChartName
|
|
}
|
|
|
|
func LoadResourcesFromFiles(input, rootPath string) map[string][]workloadinterface.IMetadata {
|
|
files, errs := listFiles(input)
|
|
if len(errs) > 0 {
|
|
logger.L().Error(fmt.Sprintf("%v", errs))
|
|
}
|
|
if len(files) == 0 {
|
|
return nil
|
|
}
|
|
|
|
workloads, errs := loadFiles(rootPath, files)
|
|
if len(errs) > 0 {
|
|
logger.L().Error(fmt.Sprintf("%v", errs))
|
|
}
|
|
|
|
return workloads
|
|
}
|
|
|
|
func loadFiles(rootPath string, filePaths []string) (map[string][]workloadinterface.IMetadata, []error) {
|
|
workloads := make(map[string][]workloadinterface.IMetadata, 0)
|
|
errs := []error{}
|
|
for i := range filePaths {
|
|
f, err := loadFile(filePaths[i])
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
if len(f) == 0 {
|
|
continue // empty file
|
|
}
|
|
|
|
w, e := ReadFile(f, GetFileFormat(filePaths[i]))
|
|
if e != nil {
|
|
logger.L().Debug("failed to read file", helpers.String("file", filePaths[i]), helpers.Error(e))
|
|
}
|
|
if len(w) != 0 {
|
|
path := filePaths[i]
|
|
if _, ok := workloads[path]; !ok {
|
|
workloads[path] = []workloadinterface.IMetadata{}
|
|
}
|
|
wSlice := workloads[path]
|
|
for j := range w {
|
|
lw := localworkload.NewLocalWorkload(w[j].GetObject())
|
|
if relPath, err := filepath.Rel(rootPath, path); err == nil {
|
|
lw.SetPath(relPath)
|
|
} else {
|
|
lw.SetPath(path)
|
|
}
|
|
wSlice = append(wSlice, lw)
|
|
}
|
|
workloads[path] = wSlice
|
|
}
|
|
}
|
|
return workloads, errs
|
|
}
|
|
|
|
func loadFile(filePath string) ([]byte, error) {
|
|
return os.ReadFile(filePath)
|
|
}
|
|
func ReadFile(fileContent []byte, fileFormat FileFormat) ([]workloadinterface.IMetadata, error) {
|
|
|
|
switch fileFormat {
|
|
case YAML_FILE_FORMAT:
|
|
return readYamlFile(fileContent)
|
|
case JSON_FILE_FORMAT:
|
|
return readJsonFile(fileContent)
|
|
default:
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// listFiles returns the list of absolute paths, full file path and list of errors. The list of abs paths and full path have the same length
|
|
func listFiles(pattern string) ([]string, []error) {
|
|
return listFilesOrDirectories(pattern, false)
|
|
}
|
|
|
|
// listDirs returns the list of absolute paths, full directories path and list of errors. The list of abs paths and full path have the same length
|
|
func listDirs(pattern string) ([]string, []error) {
|
|
return listFilesOrDirectories(pattern, true)
|
|
}
|
|
|
|
func listFilesOrDirectories(pattern string, onlyDirectories bool) ([]string, []error) {
|
|
var paths []string
|
|
errs := []error{}
|
|
|
|
if !filepath.IsAbs(pattern) {
|
|
o, _ := os.Getwd()
|
|
pattern = filepath.Join(o, pattern)
|
|
}
|
|
|
|
if !onlyDirectories && IsFile(pattern) {
|
|
paths = append(paths, pattern)
|
|
return paths, errs
|
|
}
|
|
|
|
root, shouldMatch := filepath.Split(pattern)
|
|
|
|
if IsDir(pattern) {
|
|
root = pattern
|
|
shouldMatch = "*"
|
|
}
|
|
if shouldMatch == "" {
|
|
shouldMatch = "*"
|
|
}
|
|
|
|
f, err := glob(root, shouldMatch, onlyDirectories)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
} else {
|
|
paths = append(paths, f...)
|
|
}
|
|
|
|
return paths, errs
|
|
}
|
|
|
|
func readYamlFile(yamlFile []byte) ([]workloadinterface.IMetadata, error) {
|
|
defer recover()
|
|
|
|
r := bytes.NewReader(yamlFile)
|
|
dec := yaml.NewDecoder(r)
|
|
yamlObjs := []workloadinterface.IMetadata{}
|
|
|
|
var t interface{}
|
|
for dec.Decode(&t) == nil {
|
|
j := convertYamlToJson(t)
|
|
if j == nil {
|
|
continue
|
|
}
|
|
if obj, ok := j.(map[string]interface{}); ok {
|
|
if o := objectsenvelopes.NewObject(obj); o != nil {
|
|
if o.GetObjectType() == workloadinterface.TypeListWorkloads {
|
|
if list := workloadinterface.NewListWorkloadsObj(o.GetObject()); list != nil {
|
|
yamlObjs = append(yamlObjs, list.GetItems()...)
|
|
}
|
|
} else {
|
|
yamlObjs = append(yamlObjs, o)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return yamlObjs, nil
|
|
}
|
|
|
|
func readJsonFile(jsonFile []byte) ([]workloadinterface.IMetadata, error) {
|
|
workloads := []workloadinterface.IMetadata{}
|
|
var jsonObj interface{}
|
|
if err := json.Unmarshal(jsonFile, &jsonObj); err != nil {
|
|
return workloads, err
|
|
}
|
|
|
|
convertJsonToWorkload(jsonObj, &workloads)
|
|
|
|
return workloads, nil
|
|
}
|
|
func convertJsonToWorkload(jsonObj interface{}, workloads *[]workloadinterface.IMetadata) {
|
|
|
|
switch x := jsonObj.(type) {
|
|
case map[string]interface{}:
|
|
if o := objectsenvelopes.NewObject(x); o != nil {
|
|
(*workloads) = append(*workloads, o)
|
|
}
|
|
case []interface{}:
|
|
for i := range x {
|
|
convertJsonToWorkload(x[i], workloads)
|
|
}
|
|
}
|
|
}
|
|
func convertYamlToJson(i interface{}) interface{} {
|
|
switch x := i.(type) {
|
|
case map[interface{}]interface{}:
|
|
m2 := map[string]interface{}{}
|
|
for k, v := range x {
|
|
if s, ok := k.(string); ok {
|
|
m2[s] = convertYamlToJson(v)
|
|
}
|
|
}
|
|
return m2
|
|
case []interface{}:
|
|
for i, v := range x {
|
|
x[i] = convertYamlToJson(v)
|
|
}
|
|
}
|
|
return i
|
|
}
|
|
|
|
func IsYaml(filePath string) bool {
|
|
return StringInSlice(YAML_PREFIX, strings.ReplaceAll(filepath.Ext(filePath), ".", "")) != ValueNotFound
|
|
}
|
|
|
|
func IsJson(filePath string) bool {
|
|
return StringInSlice(JSON_PREFIX, strings.ReplaceAll(filepath.Ext(filePath), ".", "")) != ValueNotFound
|
|
}
|
|
|
|
func glob(root, pattern string, onlyDirectories bool) ([]string, error) {
|
|
var matches []string
|
|
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// listing only directotries
|
|
if onlyDirectories {
|
|
if info.IsDir() {
|
|
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
|
|
return err
|
|
} else if matched {
|
|
matches = append(matches, path)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// listing only files
|
|
if info.IsDir() {
|
|
return nil
|
|
}
|
|
fileFormat := GetFileFormat(path)
|
|
if !(fileFormat == JSON_FILE_FORMAT || fileFormat == YAML_FILE_FORMAT) {
|
|
return nil
|
|
}
|
|
if matched, err := filepath.Match(pattern, filepath.Base(path)); err != nil {
|
|
return err
|
|
} else if matched {
|
|
matches = append(matches, path)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return matches, nil
|
|
}
|
|
|
|
// IsFile checks if a given path is a file
|
|
func IsFile(name string) bool {
|
|
if fi, err := os.Stat(name); err == nil {
|
|
if fi.Mode().IsRegular() {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// IsDir checks if a given path is a directory
|
|
func IsDir(name string) bool {
|
|
if info, err := os.Stat(name); err == nil {
|
|
if info.IsDir() {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func GetFileFormat(filePath string) FileFormat {
|
|
if IsYaml(filePath) {
|
|
return YAML_FILE_FORMAT
|
|
} else if IsJson(filePath) {
|
|
return JSON_FILE_FORMAT
|
|
} else {
|
|
return FileFormat(filePath)
|
|
}
|
|
}
|