fixed cmd init

This commit is contained in:
dwertent
2022-03-16 15:26:59 +02:00
parent 8f9b46cdbe
commit eae6458b42
10 changed files with 226 additions and 32 deletions

View File

@@ -35,10 +35,10 @@ RUN addgroup -S ks && adduser -S ks -G ks
USER ks USER ks
WORKDIR /home/ks/ WORKDIR /home/ks/
COPY --from=builder /work/httphandler/build/ubuntu-latest/kubescape /usr/bin/kubescape COPY --from=builder /work/httphandler/build/ubuntu-latest/kubescape /usr/bin/ksserver
COPY --from=builder /work/build/ubuntu-latest/kubescape /usr/bin/kscli COPY --from=builder /work/build/ubuntu-latest/kubescape /usr/bin/kubescape
RUN mkdir /home/ks/.kubescape && chmod 777 -R /home/ks/.kubescape RUN mkdir /home/ks/.kubescape && chmod 777 -R /home/ks/.kubescape
COPY --from=builder /work/artifacts/ /home/ks/.kubescape COPY --from=builder /work/artifacts/ /home/ks/.kubescape
ENTRYPOINT ["kubescape"] ENTRYPOINT ["ksserver"]

View File

@@ -22,6 +22,8 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var rootInfo cautils.RootInfo
var ksExamples = ` var ksExamples = `
# Scan command # Scan command
kubescape scan --submit kubescape scan --submit
@@ -43,7 +45,6 @@ func NewDefaultKubescapeCommand() *cobra.Command {
} }
func getRootCmd(ks meta.IKubescape) *cobra.Command { func getRootCmd(ks meta.IKubescape) *cobra.Command {
var rootInfo cautils.RootInfo
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: "kubescape", Use: "kubescape",
@@ -53,8 +54,8 @@ func getRootCmd(ks meta.IKubescape) *cobra.Command {
Example: ksExamples, Example: ksExamples,
} }
rootCmd.PersistentFlags().StringVar(&armoBEURLsDep, "environment", "", envFlagUsage) rootCmd.PersistentFlags().StringVar(&rootInfo.ArmoBEURLsDep, "environment", "", envFlagUsage)
rootCmd.PersistentFlags().StringVar(&armoBEURLs, "env", "", envFlagUsage) rootCmd.PersistentFlags().StringVar(&rootInfo.ArmoBEURLs, "env", "", envFlagUsage)
rootCmd.PersistentFlags().MarkDeprecated("environment", "use 'env' instead") rootCmd.PersistentFlags().MarkDeprecated("environment", "use 'env' instead")
rootCmd.PersistentFlags().MarkHidden("environment") rootCmd.PersistentFlags().MarkHidden("environment")
rootCmd.PersistentFlags().MarkHidden("env") rootCmd.PersistentFlags().MarkHidden("env")
@@ -66,11 +67,7 @@ func getRootCmd(ks meta.IKubescape) *cobra.Command {
rootCmd.PersistentFlags().StringVar(&rootInfo.CacheDir, "cache-dir", getter.DefaultLocalStore, "Cache directory [$KS_CACHE_DIR]") 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.DisableColor, "disable-color", "", false, "Disable Color output for logging")
// Initialize cobra.OnInitialize(initLogger, initLoggerLevel, initEnvironment, initCacheDir)
initLogger(&rootInfo)
initLoggerLevel(&rootInfo)
initEnvironment(&rootInfo)
initCacheDir(&rootInfo)
// Supported commands // Supported commands
rootCmd.AddCommand(scan.GetScanCommand(ks)) rootCmd.AddCommand(scan.GetScanCommand(ks))

View File

@@ -5,7 +5,6 @@ import (
"os" "os"
"strings" "strings"
"github.com/armosec/kubescape/core/cautils"
"github.com/armosec/kubescape/core/cautils/getter" "github.com/armosec/kubescape/core/cautils/getter"
"github.com/armosec/kubescape/core/cautils/logger" "github.com/armosec/kubescape/core/cautils/logger"
"github.com/armosec/kubescape/core/cautils/logger/helpers" "github.com/armosec/kubescape/core/cautils/logger/helpers"
@@ -13,12 +12,9 @@ import (
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
) )
var armoBEURLs = ""
var armoBEURLsDep = ""
const envFlagUsage = "Send report results to specific URL. Format:<ReportReceiver>,<Backend>,<Frontend>.\n\t\tExample:report.armo.cloud,api.armo.cloud,portal.armo.cloud" const envFlagUsage = "Send report results to specific URL. Format:<ReportReceiver>,<Backend>,<Frontend>.\n\t\tExample:report.armo.cloud,api.armo.cloud,portal.armo.cloud"
func initLogger(rootInfo *cautils.RootInfo) { func initLogger() {
logger.DisableColor(rootInfo.DisableColor) logger.DisableColor(rootInfo.DisableColor)
if rootInfo.LoggerName == "" { if rootInfo.LoggerName == "" {
@@ -36,8 +32,8 @@ func initLogger(rootInfo *cautils.RootInfo) {
logger.InitLogger(rootInfo.LoggerName) logger.InitLogger(rootInfo.LoggerName)
} }
func initLoggerLevel(rootInfo *cautils.RootInfo) { func initLoggerLevel() {
if rootInfo.Logger != helpers.InfoLevel.String() { if rootInfo.Logger == helpers.InfoLevel.String() {
} else if l := os.Getenv("KS_LOGGER"); l != "" { } else if l := os.Getenv("KS_LOGGER"); l != "" {
rootInfo.Logger = l rootInfo.Logger = l
} }
@@ -47,7 +43,7 @@ func initLoggerLevel(rootInfo *cautils.RootInfo) {
} }
} }
func initCacheDir(rootInfo *cautils.RootInfo) { func initCacheDir() {
if rootInfo.CacheDir == getter.DefaultLocalStore { if rootInfo.CacheDir == getter.DefaultLocalStore {
getter.DefaultLocalStore = rootInfo.CacheDir getter.DefaultLocalStore = rootInfo.CacheDir
} else if cacheDir := os.Getenv("KS_CACHE_DIR"); cacheDir != "" { } else if cacheDir := os.Getenv("KS_CACHE_DIR"); cacheDir != "" {
@@ -58,11 +54,11 @@ func initCacheDir(rootInfo *cautils.RootInfo) {
logger.L().Debug("cache dir updated", helpers.String("path", getter.DefaultLocalStore)) logger.L().Debug("cache dir updated", helpers.String("path", getter.DefaultLocalStore))
} }
func initEnvironment(rootInfo *cautils.RootInfo) { func initEnvironment() {
if armoBEURLsDep != "" { if rootInfo.ArmoBEURLs == "" {
armoBEURLs = armoBEURLsDep rootInfo.ArmoBEURLs = rootInfo.ArmoBEURLsDep
} }
urlSlices := strings.Split(armoBEURLs, ",") urlSlices := strings.Split(rootInfo.ArmoBEURLs, ",")
if len(urlSlices) != 1 && len(urlSlices) < 3 { if len(urlSlices) != 1 && len(urlSlices) < 3 {
logger.L().Fatal("expected at least 3 URLs (report, api, frontend, auth)") logger.L().Fatal("expected at least 3 URLs (report, api, frontend, auth)")
} }

89
core/cautils/rootinfo.go Normal file
View File

@@ -0,0 +1,89 @@
package cautils
type RootInfo struct {
Logger string // logger level
LoggerName string // logger name ("pretty"/"zap"/"none")
CacheDir string // cached dir
DisableColor bool // Disable Color
ArmoBEURLs string // armo url
ArmoBEURLsDep string // armo url
}
// func (rootInfo *RootInfo) InitLogger() {
// logger.DisableColor(rootInfo.DisableColor)
// if rootInfo.LoggerName == "" {
// if l := os.Getenv("KS_LOGGER_NAME"); l != "" {
// rootInfo.LoggerName = l
// } else {
// if isatty.IsTerminal(os.Stdout.Fd()) {
// rootInfo.LoggerName = "pretty"
// } else {
// rootInfo.LoggerName = "zap"
// }
// }
// }
// logger.InitLogger(rootInfo.LoggerName)
// }
// func (rootInfo *RootInfo) InitLoggerLevel() error {
// if rootInfo.Logger == helpers.InfoLevel.String() {
// } else if l := os.Getenv("KS_LOGGER"); l != "" {
// rootInfo.Logger = l
// }
// if err := logger.L().SetLevel(rootInfo.Logger); err != nil {
// return fmt.Errorf("supported levels: %s", strings.Join(helpers.SupportedLevels(), "/"))
// }
// return nil
// }
// func (rootInfo *RootInfo) InitCacheDir() error {
// if rootInfo.CacheDir == getter.DefaultLocalStore {
// getter.DefaultLocalStore = rootInfo.CacheDir
// } else if cacheDir := os.Getenv("KS_CACHE_DIR"); cacheDir != "" {
// getter.DefaultLocalStore = cacheDir
// } else {
// return nil // using default cache dir location
// }
// // TODO create dir if not found exist
// // logger.L().Debug("cache dir updated", helpers.String("path", getter.DefaultLocalStore))
// return nil
// }
// func (rootInfo *RootInfo) InitEnvironment() error {
// urlSlices := strings.Split(rootInfo.ArmoBEURLs, ",")
// if len(urlSlices) != 1 && len(urlSlices) < 3 {
// return fmt.Errorf("expected at least 2 URLs (report,api,frontend,auth)")
// }
// switch len(urlSlices) {
// case 1:
// switch urlSlices[0] {
// case "dev", "development":
// getter.SetARMOAPIConnector(getter.NewARMOAPIDev())
// case "stage", "staging":
// getter.SetARMOAPIConnector(getter.NewARMOAPIStaging())
// case "":
// getter.SetARMOAPIConnector(getter.NewARMOAPIProd())
// default:
// return fmt.Errorf("unknown environment")
// }
// case 2:
// armoERURL := urlSlices[0] // mandatory
// armoBEURL := urlSlices[1] // mandatory
// getter.SetARMOAPIConnector(getter.NewARMOAPICustomized(armoERURL, armoBEURL, "", ""))
// case 3, 4:
// var armoAUTHURL string
// armoERURL := urlSlices[0] // mandatory
// armoBEURL := urlSlices[1] // mandatory
// armoFEURL := urlSlices[2] // mandatory
// if len(urlSlices) <= 4 {
// armoAUTHURL = urlSlices[3]
// }
// getter.SetARMOAPIConnector(getter.NewARMOAPICustomized(armoERURL, armoBEURL, armoFEURL, armoAUTHURL))
// }
// return nil
// }

View File

@@ -53,13 +53,6 @@ func (bpf *BoolPtrFlag) Set(val string) error {
return nil return nil
} }
type RootInfo struct {
Logger string // logger level
LoggerName string // logger name ("pretty"/"zap"/"none")
CacheDir string // cached dir
DisableColor bool // Disable Color
}
// TODO - UPDATE // TODO - UPDATE
type ScanInfo struct { type ScanInfo struct {
Getters // TODO - remove from object Getters // TODO - remove from object

View File

@@ -60,6 +60,9 @@ func responseObjectToVulnerabilities(vulnerabilitiesList containerscan.Vulnerabi
vulnerabilities[i].Relevancy = vulnerabilityEntry.Relevancy vulnerabilities[i].Relevancy = vulnerabilityEntry.Relevancy
vulnerabilities[i].Severity = vulnerabilityEntry.Severity vulnerabilities[i].Severity = vulnerabilityEntry.Severity
vulnerabilities[i].UrgentCount = vulnerabilityEntry.UrgentCount vulnerabilities[i].UrgentCount = vulnerabilityEntry.UrgentCount
vulnerabilities[i].Categories = registryvulnerabilities.Categories{
IsRCE: vulnerabilityEntry.Categories.IsRCE,
}
} }
return vulnerabilities return vulnerabilities
} }

View File

@@ -23,6 +23,10 @@ type FixedIn struct {
ImgTag string `json:"imageTag"` ImgTag string `json:"imageTag"`
Version string `json:"version"` Version string `json:"version"`
} }
type Categories struct {
IsRCE bool `json:"isRce"`
}
type Vulnerability struct { type Vulnerability struct {
Name string `json:"name"` Name string `json:"name"`
RelatedPackageName string `json:"packageName"` RelatedPackageName string `json:"packageName"`
@@ -36,6 +40,7 @@ type Vulnerability struct {
UrgentCount int `json:"urgent"` UrgentCount int `json:"urgent"`
NeglectedCount int `json:"neglected"` NeglectedCount int `json:"neglected"`
HealthStatus string `json:"healthStatus"` HealthStatus string `json:"healthStatus"`
Categories Categories `json:"categories"`
} }
type ContainerImageVulnerabilityReport struct { type ContainerImageVulnerabilityReport struct {

View File

@@ -2,7 +2,7 @@
1. Deploy kubescape microservice 1. Deploy kubescape microservice
```bash ```bash
kubectl apply -f https://raw.githubusercontent.com/armosec/kubescape/master/httphandler/examples/prometheus/kubescape.yaml kubectl apply -f ks-deployment.yaml
``` ```
> **NOTE** Make sure the configurations suit your cluster (e.g. `serviceType`, namespace, etc.) > **NOTE** Make sure the configurations suit your cluster (e.g. `serviceType`, namespace, etc.)

View File

@@ -0,0 +1,111 @@
---
apiVersion: v1
kind: Namespace
metadata:
labels:
app: kubescape
name: ks-scanner
---
# ------------------- Kubescape Service Account ------------------- #
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app: kubescape
name: kubescape-discovery
namespace: ks-scanner
---
# ------------------- Kubescape Cluster Role & Cluster Role Binding ------------------- #
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: kubescape-discovery-clusterroles
# "namespace" omitted since ClusterRoles are not namespaced
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["get", "list", "describe"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: kubescape-discovery-role-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubescape-discovery-clusterroles
subjects:
- kind: ServiceAccount
name: kubescape-discovery
namespace: ks-scanner
---
apiVersion: v1
kind: Service
metadata:
name: kubescape-service
namespace: ks-scanner
labels:
app: kubescape-service
spec:
type: NodePort
ports:
- port: 8080
name: http
targetPort: 8080
protocol: TCP
selector:
app: kubescape
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kubescape
namespace: ks-scanner
labels:
app: kubescape
spec:
replicas: 1
selector:
matchLabels:
app: kubescape
template:
metadata:
labels:
app: kubescape
spec:
serviceAccountName: kubescape-discovery
containers:
- name: kubescape
livenessProbe:
httpGet:
path: /livez
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
readinessProbe:
httpGet:
path: /readyz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
image: quay.io/armosec/kubescape:prometheus.v2
env:
- name: KS_DEFAULT_CONFIGMAP_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
ports:
- containerPort: 8080
name: http
protocol: TCP
command:
- ksserver
resources:
requests:
cpu: 10m
memory: 100Mi
limits:
cpu: 500m
memory: 500Mi

View File

@@ -107,7 +107,7 @@ spec:
name: http name: http
protocol: TCP protocol: TCP
command: command:
- kubescape - ksserver
resources: resources:
requests: requests:
cpu: 10m cpu: 10m