From b547814dec53e077aa79e6a9aead9e12a894ccb4 Mon Sep 17 00:00:00 2001 From: kooomix Date: Wed, 21 Dec 2022 19:17:29 +0200 Subject: [PATCH] DownloadInfo, PolicyIdentifier add Identity, remove ID and Name --- cmd/download/download.go | 11 +++-------- core/cautils/scaninfo.go | 17 +++++------------ core/core/download.go | 20 ++++++++++---------- core/core/initutils.go | 14 +++++++------- core/core/scan.go | 2 +- core/meta/datastructures/v1/download.go | 3 +-- core/pkg/policyhandler/handlepullpolicies.go | 10 +++++----- 7 files changed, 32 insertions(+), 45 deletions(-) diff --git a/cmd/download/download.go b/cmd/download/download.go index 00ed0b99..3c1519ec 100644 --- a/cmd/download/download.go +++ b/cmd/download/download.go @@ -24,8 +24,8 @@ var ( # Download the NSA framework. Run 'kubescape list frameworks' for all frameworks names kubescape download framework nsa - # Download the "HostPath mount" control. Run 'kubescape list controls' for all controls names - kubescape download control "HostPath mount" + # Download the "C-0001" control. Run 'kubescape list controls --id' for all controls ids + kubescape download control "C-0001" # Download the "C-0001" control. Run 'kubescape list controls --id' for all controls ids kubescape download control C-0001 @@ -71,12 +71,7 @@ func GeDownloadCmd(ks meta.IKubescape) *cobra.Command { downloadInfo.Target = args[0] if len(args) >= 2 { - // downloading a control is supported only by id. - if downloadInfo.Target == core.TargetControl { - downloadInfo.ID = args[1] - } else { - downloadInfo.Name = args[1] - } + downloadInfo.Identifier = args[1] } if err := ks.Download(&downloadInfo); err != nil { diff --git a/core/cautils/scaninfo.go b/core/cautils/scaninfo.go index 067e6469..ea9b2b3e 100644 --- a/core/cautils/scaninfo.go +++ b/core/cautils/scaninfo.go @@ -94,8 +94,7 @@ const ( ) type PolicyIdentifier struct { - ID string // policy ID e.g. c-0012 - relevant only to kind=control - Name string // policy name e.g. nsa,mitre + Identifier string // policy Identifier e.g. c-0012 for control, nsa,mitre for frameworks Kind apisv1.NotificationPolicyKind // policy kind e.g. Framework,Control,Rule Designators armotypes.PortalDesignator } @@ -184,7 +183,7 @@ func (scanInfo *ScanInfo) setUseArtifactsFrom() { func (scanInfo *ScanInfo) setUseFrom() { if scanInfo.UseDefault { for _, policy := range scanInfo.PolicyIdentifier { - scanInfo.UseFrom = append(scanInfo.UseFrom, getter.GetDefaultPath(policy.Name+".json")) + scanInfo.UseFrom = append(scanInfo.UseFrom, getter.GetDefaultPath(policy.Identifier+".json")) } } } @@ -215,13 +214,7 @@ func (scanInfo *ScanInfo) SetPolicyIdentifiers(policies []string, kind apisv1.No if !scanInfo.contains(policy) { newPolicy := PolicyIdentifier{} newPolicy.Kind = kind - // control can be identified only by it's id. - if kind == apisv1.KindControl { - newPolicy.ID = policy - } else { - newPolicy.Name = policy - } - + newPolicy.Identifier = policy scanInfo.PolicyIdentifier = append(scanInfo.PolicyIdentifier, newPolicy) } } @@ -229,7 +222,7 @@ func (scanInfo *ScanInfo) SetPolicyIdentifiers(policies []string, kind apisv1.No func (scanInfo *ScanInfo) contains(policyName string) bool { for _, policy := range scanInfo.PolicyIdentifier { - if policy.Name == policyName { + if policy.Identifier == policyName { return true } } @@ -257,7 +250,7 @@ func scanInfoToScanMetadata(scanInfo *ScanInfo) *reporthandlingv2.Metadata { } // append frameworks for _, policy := range scanInfo.PolicyIdentifier { - metadata.ScanMetadata.TargetNames = append(metadata.ScanMetadata.TargetNames, policy.Name) + metadata.ScanMetadata.TargetNames = append(metadata.ScanMetadata.TargetNames, policy.Identifier) } metadata.ScanMetadata.KubescapeVersion = BuildNumber diff --git a/core/core/download.go b/core/core/download.go index a89af26d..49b355c2 100644 --- a/core/core/download.go +++ b/core/core/download.go @@ -93,7 +93,7 @@ func downloadArtifacts(downloadInfo *metav1.DownloadInfo) error { func downloadConfigInputs(downloadInfo *metav1.DownloadInfo) error { tenant := getTenantConfig(&downloadInfo.Credentials, "", "", getKubernetesApi()) - controlsInputsGetter := getConfigInputsGetter(downloadInfo.Name, tenant.GetAccountID(), nil) + controlsInputsGetter := getConfigInputsGetter(downloadInfo.Identifier, tenant.GetAccountID(), nil) controlInputs, err := controlsInputsGetter.GetControlsInputs(tenant.GetContextName()) if err != nil { return err @@ -167,7 +167,7 @@ func downloadFramework(downloadInfo *metav1.DownloadInfo) error { g := getPolicyGetter(nil, tenant.GetTenantEmail(), true, nil) - if downloadInfo.Name == "" { + if downloadInfo.Identifier == "" { // if framework name not specified - download all frameworks frameworks, err := g.GetFrameworks() if err != nil { @@ -184,9 +184,9 @@ func downloadFramework(downloadInfo *metav1.DownloadInfo) error { // return fmt.Errorf("missing framework name") } else { if downloadInfo.FileName == "" { - downloadInfo.FileName = fmt.Sprintf("%s.json", downloadInfo.Name) + downloadInfo.FileName = fmt.Sprintf("%s.json", downloadInfo.Identifier) } - framework, err := g.GetFramework(downloadInfo.Name) + framework, err := g.GetFramework(downloadInfo.Identifier) if err != nil { return err } @@ -209,25 +209,25 @@ func downloadControl(downloadInfo *metav1.DownloadInfo) error { g := getPolicyGetter(nil, tenant.GetTenantEmail(), false, nil) - if downloadInfo.ID == "" { + if downloadInfo.Identifier == "" { // TODO - support return fmt.Errorf("missing control ID") } if downloadInfo.FileName == "" { - downloadInfo.FileName = fmt.Sprintf("%s.json", downloadInfo.ID) + downloadInfo.FileName = fmt.Sprintf("%s.json", downloadInfo.Identifier) } - controls, err := g.GetControl(downloadInfo.ID) + controls, err := g.GetControl(downloadInfo.Identifier) if err != nil { - return fmt.Errorf("failed to download control id '%s', %s", downloadInfo.ID, err.Error()) + return fmt.Errorf("failed to download control id '%s', %s", downloadInfo.Identifier, err.Error()) } if controls == nil { - return fmt.Errorf("failed to download control id '%s' - received an empty objects", downloadInfo.ID) + return fmt.Errorf("failed to download control id '%s' - received an empty objects", downloadInfo.Identifier) } downloadTo := filepath.Join(downloadInfo.Path, downloadInfo.FileName) err = getter.SaveInFile(controls, downloadTo) if err != nil { return err } - logger.L().Success("Downloaded", helpers.String("artifact", downloadInfo.Target), helpers.String("ID", downloadInfo.ID), helpers.String("path", downloadTo)) + logger.L().Success("Downloaded", helpers.String("artifact", downloadInfo.Target), helpers.String("ID", downloadInfo.Identifier), helpers.String("path", downloadTo)) return nil } diff --git a/core/core/initutils.go b/core/core/initutils.go index 66ae6272..79a36a50 100644 --- a/core/core/initutils.go +++ b/core/core/initutils.go @@ -122,18 +122,18 @@ func getFieldSelector(scanInfo *cautils.ScanInfo) resourcehandler.IFieldSelector return &resourcehandler.EmptySelector{} } -func policyIdentifierNames(pi []cautils.PolicyIdentifier) string { - policiesNames := "" +func policyIdentifierIdentities(pi []cautils.PolicyIdentifier) string { + policiesIdentities := "" for i := range pi { - policiesNames += pi[i].Name + policiesIdentities += pi[i].Identifier if i+1 < len(pi) { - policiesNames += "," + policiesIdentities += "," } } - if policiesNames == "" { - policiesNames = "all" + if policiesIdentities == "" { + policiesIdentities = "all" } - return policiesNames + return policiesIdentities } // setSubmitBehavior - Setup the desired cluster behavior regarding submitting to the Kubescape Cloud BE diff --git a/core/core/scan.go b/core/core/scan.go index f17405c0..61b985c4 100644 --- a/core/core/scan.go +++ b/core/core/scan.go @@ -63,7 +63,7 @@ func getInterfaces(scanInfo *cautils.ScanInfo) componentInterfaces { // ================== version testing ====================================== v := cautils.NewIVersionCheckHandler() - v.CheckLatestVersion(cautils.NewVersionCheckRequest(cautils.BuildNumber, policyIdentifierNames(scanInfo.PolicyIdentifier), "", cautils.ScanningContextToScanningScope(scanInfo.GetScanningContext()))) + v.CheckLatestVersion(cautils.NewVersionCheckRequest(cautils.BuildNumber, policyIdentifierIdentities(scanInfo.PolicyIdentifier), "", cautils.ScanningContextToScanningScope(scanInfo.GetScanningContext()))) // ================== setup host scanner object ====================================== diff --git a/core/meta/datastructures/v1/download.go b/core/meta/datastructures/v1/download.go index 26be2ba6..b51a8342 100644 --- a/core/meta/datastructures/v1/download.go +++ b/core/meta/datastructures/v1/download.go @@ -6,7 +6,6 @@ type DownloadInfo struct { Path string // directory to save artifact. Default is "~/.kubescape/" FileName string // can be empty Target string // type of artifact to download - Name string // name of artifact to download - ID string // ID of artifact to download (relevant only for controls) + Identifier string // identifier of artifact to download Credentials cautils.Credentials } diff --git a/core/pkg/policyhandler/handlepullpolicies.go b/core/pkg/policyhandler/handlepullpolicies.go index 95a4df3b..2d12af99 100644 --- a/core/pkg/policyhandler/handlepullpolicies.go +++ b/core/pkg/policyhandler/handlepullpolicies.go @@ -56,7 +56,7 @@ func (policyHandler *PolicyHandler) getScanPolicies(policyIdentifier []cautils.P switch getScanKind(policyIdentifier) { case apisv1.KindFramework: // Download frameworks for _, rule := range policyIdentifier { - receivedFramework, err := policyHandler.getters.PolicyGetter.GetFramework(rule.Name) + receivedFramework, err := policyHandler.getters.PolicyGetter.GetFramework(rule.Identifier) if err != nil { return frameworks, policyDownloadError(err) } @@ -65,7 +65,7 @@ func (policyHandler *PolicyHandler) getScanPolicies(policyIdentifier []cautils.P } if receivedFramework != nil { frameworks = append(frameworks, *receivedFramework) - cache := getter.GetDefaultPath(rule.Name + ".json") + cache := getter.GetDefaultPath(rule.Identifier + ".json") if err := getter.SaveInFile(receivedFramework, cache); err != nil { logger.L().Warning("failed to cache file", helpers.String("file", cache), helpers.Error(err)) } @@ -76,14 +76,14 @@ func (policyHandler *PolicyHandler) getScanPolicies(policyIdentifier []cautils.P var receivedControl *reporthandling.Control var err error for _, policy := range policyIdentifier { - receivedControl, err = policyHandler.getters.PolicyGetter.GetControl(policy.ID) + receivedControl, err = policyHandler.getters.PolicyGetter.GetControl(policy.Identifier) if err != nil { return frameworks, policyDownloadError(err) } if receivedControl != nil { f.Controls = append(f.Controls, *receivedControl) - cache := getter.GetDefaultPath(policy.ID + ".json") + cache := getter.GetDefaultPath(policy.Identifier + ".json") if err := getter.SaveInFile(receivedControl, cache); err != nil { logger.L().Warning("failed to cache file", helpers.String("file", cache), helpers.Error(err)) } @@ -100,7 +100,7 @@ func (policyHandler *PolicyHandler) getScanPolicies(policyIdentifier []cautils.P func policyIdentifierToSlice(rules []cautils.PolicyIdentifier) []string { s := []string{} for i := range rules { - s = append(s, fmt.Sprintf("%s: %s", rules[i].Kind, rules[i].Name)) + s = append(s, fmt.Sprintf("%s: %s", rules[i].Kind, rules[i].Identifier)) } return s }