From 13ffd922104c8bb2c103c26222d683deb1db4c9c Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Sun, 4 Sep 2022 15:13:16 +0300 Subject: [PATCH 1/4] update logger version (#773) --- core/cautils/customerloader.go | 4 ++-- core/cautils/datastructures.go | 2 +- core/cautils/getter/datastructures.go | 2 +- core/cautils/getter/kscloudapi.go | 2 +- go.mod | 2 +- go.sum | 4 ++-- httphandler/go.mod | 2 +- httphandler/go.sum | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/cautils/customerloader.go b/core/cautils/customerloader.go index a7bbb0e9..4016cbeb 100644 --- a/core/cautils/customerloader.go +++ b/core/cautils/customerloader.go @@ -182,11 +182,11 @@ TODO - supprot: KS_CACHE // path to cached files */ type ClusterConfig struct { + backendAPI getter.IBackend k8s *k8sinterface.KubernetesApi + configObj *ConfigObj configMapName string configMapNamespace string - backendAPI getter.IBackend - configObj *ConfigObj } func NewClusterConfig(k8s *k8sinterface.KubernetesApi, backendAPI getter.IBackend, credentials *Credentials, clusterName string) *ClusterConfig { diff --git a/core/cautils/datastructures.go b/core/cautils/datastructures.go index a4962bd3..733ca8d7 100644 --- a/core/cautils/datastructures.go +++ b/core/cautils/datastructures.go @@ -98,6 +98,6 @@ type RegoInputData struct { } type Policies struct { - Frameworks []string Controls map[string]reporthandling.Control // map[] + Frameworks []string } diff --git a/core/cautils/getter/datastructures.go b/core/cautils/getter/datastructures.go index 47a06626..d30abb1c 100644 --- a/core/cautils/getter/datastructures.go +++ b/core/cautils/getter/datastructures.go @@ -8,8 +8,8 @@ type FeLoginData struct { type FeLoginResponse struct { Token string `json:"accessToken"` RefreshToken string `json:"refreshToken"` - ExpiresIn int32 `json:"expiresIn"` Expires string `json:"expires"` + ExpiresIn int32 `json:"expiresIn"` } type KSCloudSelectCustomer struct { diff --git a/core/cautils/getter/kscloudapi.go b/core/cautils/getter/kscloudapi.go index 676b5129..437ffa06 100644 --- a/core/cautils/getter/kscloudapi.go +++ b/core/cautils/getter/kscloudapi.go @@ -42,8 +42,8 @@ type KSCloudAPI struct { accountID string clientID string secretKey string - feToken FeLoginResponse authCookie string + feToken FeLoginResponse loggedIn bool } diff --git a/go.mod b/go.mod index c3db7a65..4bc7464a 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/go-git/go-git/v5 v5.4.2 github.com/google/uuid v1.3.0 github.com/johnfercher/maroto v0.37.0 - github.com/kubescape/go-logger v0.0.5 + github.com/kubescape/go-logger v0.0.6 github.com/kubescape/k8s-interface v0.0.83 github.com/kubescape/opa-utils v0.0.180 github.com/kubescape/rbac-utils v0.0.17 diff --git a/go.sum b/go.sum index 4210ffbc..47a367df 100644 --- a/go.sum +++ b/go.sum @@ -835,8 +835,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kubescape/go-logger v0.0.5 h1:lA25XvI4LT4JqwnNkIDUtwdYS4KVmnbWrVB7SRte2Es= -github.com/kubescape/go-logger v0.0.5/go.mod h1:DnVWEvC90LFY1nNMaNo6nBVOcqkLMK3S0qzXP1fzRvI= +github.com/kubescape/go-logger v0.0.6 h1:ynhAmwrz0O7Jtqq1CdmCZUrKveji25hVP+B/FAb3QrA= +github.com/kubescape/go-logger v0.0.6/go.mod h1:DnVWEvC90LFY1nNMaNo6nBVOcqkLMK3S0qzXP1fzRvI= github.com/kubescape/k8s-interface v0.0.83 h1:yQ1kWNZmKfBim/+NmxpPI/j7L9ASDq2h3mCNdmYgzqY= github.com/kubescape/k8s-interface v0.0.83/go.mod h1:ihX96yqar+xogHl45mFE8zT9DLI06iy7XQPAP+j5KJE= github.com/kubescape/opa-utils v0.0.180 h1:QPYgjAE17Zcvz2wihmtXhS1YCMDhTIlwgvwY2Gb0djw= diff --git a/httphandler/go.mod b/httphandler/go.mod index f003d13e..c73a56fd 100644 --- a/httphandler/go.mod +++ b/httphandler/go.mod @@ -10,7 +10,7 @@ require ( github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 github.com/gorilla/schema v1.2.0 - github.com/kubescape/go-logger v0.0.5 + github.com/kubescape/go-logger v0.0.6 github.com/kubescape/kubescape/v2 v2.0.0-00010101000000-000000000000 github.com/kubescape/opa-utils v0.0.180 github.com/stretchr/testify v1.8.0 diff --git a/httphandler/go.sum b/httphandler/go.sum index 58fad021..ad9e7b2c 100644 --- a/httphandler/go.sum +++ b/httphandler/go.sum @@ -891,8 +891,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kubescape/go-logger v0.0.5 h1:lA25XvI4LT4JqwnNkIDUtwdYS4KVmnbWrVB7SRte2Es= -github.com/kubescape/go-logger v0.0.5/go.mod h1:DnVWEvC90LFY1nNMaNo6nBVOcqkLMK3S0qzXP1fzRvI= +github.com/kubescape/go-logger v0.0.6 h1:ynhAmwrz0O7Jtqq1CdmCZUrKveji25hVP+B/FAb3QrA= +github.com/kubescape/go-logger v0.0.6/go.mod h1:DnVWEvC90LFY1nNMaNo6nBVOcqkLMK3S0qzXP1fzRvI= github.com/kubescape/k8s-interface v0.0.83 h1:yQ1kWNZmKfBim/+NmxpPI/j7L9ASDq2h3mCNdmYgzqY= github.com/kubescape/k8s-interface v0.0.83/go.mod h1:ihX96yqar+xogHl45mFE8zT9DLI06iy7XQPAP+j5KJE= github.com/kubescape/opa-utils v0.0.180 h1:QPYgjAE17Zcvz2wihmtXhS1YCMDhTIlwgvwY2Gb0djw= From 137b3d7b5dabb0666ab263c659d7fd23c9fd4cf6 Mon Sep 17 00:00:00 2001 From: Suhas Gumma <43647369+suhasgumma@users.noreply.github.com> Date: Sun, 4 Sep 2022 17:47:15 +0530 Subject: [PATCH 2/4] 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 Co-authored-by: Ben Hirschberg <59160382+slashben@users.noreply.github.com> --- core/pkg/resourcehandler/filesloaderutils.go | 2 +- core/pkg/resourcehandler/remotegitutils.go | 57 ++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/core/pkg/resourcehandler/filesloaderutils.go b/core/pkg/resourcehandler/filesloaderutils.go index 7f9ec407..bfd8eb97 100644 --- a/core/pkg/resourcehandler/filesloaderutils.go +++ b/core/pkg/resourcehandler/filesloaderutils.go @@ -18,7 +18,7 @@ func cloneGitRepo(path *string) (string, error) { var clonedDir string // Clone git repository if needed - gitURL, err := giturl.NewGitURL(*path) + gitURL, err := giturl.NewGitAPI(*path) if err == nil { logger.L().Info("cloning", helpers.String("repository url", gitURL.GetURL().String())) cautils.StartSpinner() diff --git a/core/pkg/resourcehandler/remotegitutils.go b/core/pkg/resourcehandler/remotegitutils.go index 47a45d09..1dc4922b 100644 --- a/core/pkg/resourcehandler/remotegitutils.go +++ b/core/pkg/resourcehandler/remotegitutils.go @@ -1,16 +1,45 @@ package resourcehandler import ( + "errors" "fmt" + nethttp "net/http" "os" giturl "github.com/armosec/go-git-url" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/transport" + "github.com/go-git/go-git/v5/plumbing/transport/http" ) +// To Check if the given repository is Public(No Authentication needed), send a HTTP GET request to the URL +// If response code is 200, the repository is Public. +func isGitRepoPublic(URL string) bool { + resp, err := nethttp.Get(URL) + + if err != nil { + return false + } + // if the status code is 200, our get request is successful. + // It only happens when the repository is public. + if resp.StatusCode == 200 { + return true + } + + return false +} + +// Check if the GITHUB_TOKEN is present +func isGitTokenPresent(gitURL giturl.IGitAPI) bool { + if token := gitURL.GetToken(); token == "" { + return false + } + return true +} + // cloneRepo clones a repository to a local temporary directory and returns the directory -func cloneRepo(gitURL giturl.IGitURL) (string, error) { +func cloneRepo(gitURL giturl.IGitAPI) (string, error) { // Create temp directory tmpDir, err := os.MkdirTemp("", "") @@ -18,9 +47,31 @@ func cloneRepo(gitURL giturl.IGitURL) (string, error) { return "", fmt.Errorf("failed to create temporary directory: %w", err) } - // Clone option + // Get the URL to clone cloneURL := gitURL.GetHttpCloneURL() - cloneOpts := git.CloneOptions{URL: cloneURL} + + isGitRepoPublic := isGitRepoPublic(cloneURL) + + // Declare the authentication variable required for cloneOptions + var auth transport.AuthMethod + + if isGitRepoPublic { + // No authentication needed if repository is public + auth = nil + } else { + + // Return Error if the GITHUB_TOKEN is not present + if isGitTokenPresent := isGitTokenPresent(gitURL); !isGitTokenPresent { + return "", fmt.Errorf("%w", errors.New("GITHUB_TOKEN is not present")) + } + auth = &http.BasicAuth{ + Username: "anything Except Empty String", + Password: gitURL.GetToken(), + } + } + + // Clone option + cloneOpts := git.CloneOptions{URL: cloneURL, Auth: auth} if gitURL.GetBranchName() != "" { cloneOpts.ReferenceName = plumbing.NewBranchReferenceName(gitURL.GetBranchName()) cloneOpts.SingleBranch = true From 23ea7e05114f9aa7cb7a133e0ed59fd1f58a624d Mon Sep 17 00:00:00 2001 From: Amir Malka Date: Sun, 4 Sep 2022 15:32:42 +0300 Subject: [PATCH 3/4] bump opa-utils to 181 --- core/pkg/resourcesprioritization/prioritizationhandler.go | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- httphandler/go.mod | 2 +- httphandler/go.sum | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/pkg/resourcesprioritization/prioritizationhandler.go b/core/pkg/resourcesprioritization/prioritizationhandler.go index ce0d5aac..3441ad7f 100644 --- a/core/pkg/resourcesprioritization/prioritizationhandler.go +++ b/core/pkg/resourcesprioritization/prioritizationhandler.go @@ -21,7 +21,7 @@ func NewResourcesPrioritizationHandler(skipZeroScore bool) *ResourcesPrioritizat func (handler *ResourcesPrioritizationHandler) PrioritizeResources(sessionObj *cautils.OPASessionObj) error { for resourceId, result := range sessionObj.ResourcesResult { - resourcePriorityVector := []prioritization.IPriorityVector{} + resourcePriorityVector := []prioritization.ControlsVector{} resource, exist := sessionObj.AllResources[resourceId] if !exist { return fmt.Errorf("expected to find resource id '%s' in scanned resources map", resourceId) @@ -51,7 +51,7 @@ func (handler *ResourcesPrioritizationHandler) PrioritizeResources(sessionObj *c cVector.SetSeverity(apis.ControlSeverityToInt(controlScoreFactor)) cVector.SetScore(float64(controlScoreFactor) + (replicaCount / 10)) - resourcePriorityVector = append(resourcePriorityVector, cVector) + resourcePriorityVector = append(resourcePriorityVector, *cVector) } } diff --git a/go.mod b/go.mod index 4bc7464a..0d805c75 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/johnfercher/maroto v0.37.0 github.com/kubescape/go-logger v0.0.6 github.com/kubescape/k8s-interface v0.0.83 - github.com/kubescape/opa-utils v0.0.180 + github.com/kubescape/opa-utils v0.0.181 github.com/kubescape/rbac-utils v0.0.17 github.com/libgit2/git2go/v33 v33.0.9 github.com/mattn/go-isatty v0.0.14 diff --git a/go.sum b/go.sum index 47a367df..83f970a4 100644 --- a/go.sum +++ b/go.sum @@ -839,8 +839,8 @@ github.com/kubescape/go-logger v0.0.6 h1:ynhAmwrz0O7Jtqq1CdmCZUrKveji25hVP+B/FAb github.com/kubescape/go-logger v0.0.6/go.mod h1:DnVWEvC90LFY1nNMaNo6nBVOcqkLMK3S0qzXP1fzRvI= github.com/kubescape/k8s-interface v0.0.83 h1:yQ1kWNZmKfBim/+NmxpPI/j7L9ASDq2h3mCNdmYgzqY= github.com/kubescape/k8s-interface v0.0.83/go.mod h1:ihX96yqar+xogHl45mFE8zT9DLI06iy7XQPAP+j5KJE= -github.com/kubescape/opa-utils v0.0.180 h1:QPYgjAE17Zcvz2wihmtXhS1YCMDhTIlwgvwY2Gb0djw= -github.com/kubescape/opa-utils v0.0.180/go.mod h1:jC5QrhS6WiFj/tXP2/YSDBnFlGsYUQokDGbKwMBgMpw= +github.com/kubescape/opa-utils v0.0.181 h1:At3/4+yS7NqIKYXRj4iwdLmrGOR6B6SyYMALme9axAI= +github.com/kubescape/opa-utils v0.0.181/go.mod h1:jC5QrhS6WiFj/tXP2/YSDBnFlGsYUQokDGbKwMBgMpw= github.com/kubescape/rbac-utils v0.0.17 h1:B78kjlTKqjYK/PXwmi4GPysHsFxIwVz1KFb4+IGT29w= github.com/kubescape/rbac-utils v0.0.17/go.mod h1:pBwjpcrVeuH/no+DiCZWvlhYtCDzd3U0o/hEZKi+eM8= github.com/linuxkit/virtsock v0.0.0-20201010232012-f8cee7dfc7a3/go.mod h1:3r6x7q95whyfWQpmGZTu3gk3v2YkMi05HEzl7Tf7YEo= diff --git a/httphandler/go.mod b/httphandler/go.mod index c73a56fd..92ab1559 100644 --- a/httphandler/go.mod +++ b/httphandler/go.mod @@ -12,7 +12,7 @@ require ( github.com/gorilla/schema v1.2.0 github.com/kubescape/go-logger v0.0.6 github.com/kubescape/kubescape/v2 v2.0.0-00010101000000-000000000000 - github.com/kubescape/opa-utils v0.0.180 + github.com/kubescape/opa-utils v0.0.181 github.com/stretchr/testify v1.8.0 k8s.io/utils v0.0.0-20220706174534-f6158b442e7c ) diff --git a/httphandler/go.sum b/httphandler/go.sum index ad9e7b2c..4bb05236 100644 --- a/httphandler/go.sum +++ b/httphandler/go.sum @@ -895,8 +895,8 @@ github.com/kubescape/go-logger v0.0.6 h1:ynhAmwrz0O7Jtqq1CdmCZUrKveji25hVP+B/FAb github.com/kubescape/go-logger v0.0.6/go.mod h1:DnVWEvC90LFY1nNMaNo6nBVOcqkLMK3S0qzXP1fzRvI= github.com/kubescape/k8s-interface v0.0.83 h1:yQ1kWNZmKfBim/+NmxpPI/j7L9ASDq2h3mCNdmYgzqY= github.com/kubescape/k8s-interface v0.0.83/go.mod h1:ihX96yqar+xogHl45mFE8zT9DLI06iy7XQPAP+j5KJE= -github.com/kubescape/opa-utils v0.0.180 h1:QPYgjAE17Zcvz2wihmtXhS1YCMDhTIlwgvwY2Gb0djw= -github.com/kubescape/opa-utils v0.0.180/go.mod h1:jC5QrhS6WiFj/tXP2/YSDBnFlGsYUQokDGbKwMBgMpw= +github.com/kubescape/opa-utils v0.0.181 h1:At3/4+yS7NqIKYXRj4iwdLmrGOR6B6SyYMALme9axAI= +github.com/kubescape/opa-utils v0.0.181/go.mod h1:jC5QrhS6WiFj/tXP2/YSDBnFlGsYUQokDGbKwMBgMpw= github.com/kubescape/rbac-utils v0.0.17 h1:B78kjlTKqjYK/PXwmi4GPysHsFxIwVz1KFb4+IGT29w= github.com/kubescape/rbac-utils v0.0.17/go.mod h1:pBwjpcrVeuH/no+DiCZWvlhYtCDzd3U0o/hEZKi+eM8= github.com/linuxkit/virtsock v0.0.0-20201010232012-f8cee7dfc7a3/go.mod h1:3r6x7q95whyfWQpmGZTu3gk3v2YkMi05HEzl7Tf7YEo= From 5c96f877ed3b32d1576d3ff04e05bb8e3bf60f15 Mon Sep 17 00:00:00 2001 From: Aditya Pratap Singh Date: Mon, 5 Sep 2022 11:33:38 +0530 Subject: [PATCH 4/4] 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 --- cmd/root.go | 1 + cmd/rootutils.go | 1 + core/cautils/rootinfo.go | 1 + 3 files changed, 3 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index a94c5f5e..c4d0da28 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -63,6 +63,7 @@ func getRootCmd(ks meta.IKubescape) *cobra.Command { rootCmd.PersistentFlags().StringVarP(&rootInfo.Logger, "logger", "l", helpers.InfoLevel.String(), fmt.Sprintf("Logger level. Supported: %s [$KS_LOGGER]", strings.Join(helpers.SupportedLevels(), "/"))) rootCmd.PersistentFlags().StringVar(&rootInfo.CacheDir, "cache-dir", getter.DefaultLocalStore, "Cache directory [$KS_CACHE_DIR]") rootCmd.PersistentFlags().BoolVarP(&rootInfo.DisableColor, "disable-color", "", false, "Disable Color output for logging") + rootCmd.PersistentFlags().BoolVarP(&rootInfo.EnableColor, "enable-color", "", false, "Force enable Color output for logging") cobra.OnInitialize(initLogger, initLoggerLevel, initEnvironment, initCacheDir) diff --git a/cmd/rootutils.go b/cmd/rootutils.go index cf6e533f..3b0140df 100644 --- a/cmd/rootutils.go +++ b/cmd/rootutils.go @@ -16,6 +16,7 @@ const envFlagUsage = "Send report results to specific URL. Format: