Skip to content

Commit

Permalink
add otel with uptrace client
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Jan 31, 2023
1 parent 7ec4fb7 commit 160ac0d
Show file tree
Hide file tree
Showing 86 changed files with 845 additions and 613 deletions.
3 changes: 2 additions & 1 deletion cmd/completion/completion.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package completion

import (
"context"
"fmt"
"os"
"strings"
Expand All @@ -19,7 +20,7 @@ var completionCmdExamples = fmt.Sprintf(`
$ echo 'source <(kubectl completion zsh)' >> "${fpath[1]}/_kubectl"
`, cautils.ExecName())

func GetCompletionCmd() *cobra.Command {
func GetCompletionCmd(_ context.Context) *cobra.Command {
completionCmd := &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate autocompletion script",
Expand Down
10 changes: 6 additions & 4 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"context"

"fmt"

"github.com/kubescape/kubescape/v2/core/cautils"
Expand Down Expand Up @@ -34,7 +36,7 @@ var (
`, cautils.ExecName())
)

func GetConfigCmd(ks meta.IKubescape) *cobra.Command {
func GetConfigCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {

// configCmd represents the config command
configCmd := &cobra.Command{
Expand All @@ -43,9 +45,9 @@ func GetConfigCmd(ks meta.IKubescape) *cobra.Command {
Example: configExample,
}

configCmd.AddCommand(getDeleteCmd(ks))
configCmd.AddCommand(getSetCmd(ks))
configCmd.AddCommand(getViewCmd(ks))
configCmd.AddCommand(getDeleteCmd(ctx, ks))
configCmd.AddCommand(getSetCmd(ctx, ks))
configCmd.AddCommand(getViewCmd(ctx, ks))

return configCmd
}
8 changes: 5 additions & 3 deletions cmd/config/delete.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package config

import (
"context"

logger "github.com/kubescape/go-logger"
"github.com/kubescape/kubescape/v2/core/meta"
v1 "github.com/kubescape/kubescape/v2/core/meta/datastructures/v1"
"github.com/spf13/cobra"
)

func getDeleteCmd(ks meta.IKubescape) *cobra.Command {
func getDeleteCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {
return &cobra.Command{
Use: "delete",
Short: "Delete cached configurations",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if err := ks.DeleteCachedConfig(&v1.DeleteConfig{}); err != nil {
logger.L().Fatal(err.Error())
if err := ks.DeleteCachedConfig(ctx, &v1.DeleteConfig{}); err != nil {
logger.L().Ctx(ctx).Fatal(err.Error())
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/set.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"fmt"
"strings"

Expand All @@ -10,7 +11,7 @@ import (
"github.com/spf13/cobra"
)

func getSetCmd(ks meta.IKubescape) *cobra.Command {
func getSetCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {

// configCmd represents the config command
configSetCmd := &cobra.Command{
Expand All @@ -24,7 +25,7 @@ func getSetCmd(ks meta.IKubescape) *cobra.Command {
return err
}
if err := ks.SetCachedConfig(setConfig); err != nil {
logger.L().Fatal(err.Error())
logger.L().Ctx(ctx).Fatal(err.Error())
}
return nil
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/view.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"os"

logger "github.com/kubescape/go-logger"
Expand All @@ -9,7 +10,7 @@ import (
"github.com/spf13/cobra"
)

func getViewCmd(ks meta.IKubescape) *cobra.Command {
func getViewCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {

// configCmd represents the config command
return &cobra.Command{
Expand All @@ -18,7 +19,7 @@ func getViewCmd(ks meta.IKubescape) *cobra.Command {
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if err := ks.ViewCachedConfig(&v1.ViewConfig{Writer: os.Stdout}); err != nil {
logger.L().Fatal(err.Error())
logger.L().Ctx(ctx).Fatal(err.Error())
}
},
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package delete

import (
"context"

"fmt"

"github.com/kubescape/kubescape/v2/core/cautils"
Expand All @@ -17,7 +19,7 @@ var deleteExceptionsExamples = fmt.Sprintf(`
%[1]s delete exceptions "first exception;second exception;third exception"
`, cautils.ExecName())

func GetDeleteCmd(ks meta.IKubescape) *cobra.Command {
func GetDeleteCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {
var deleteInfo v1.Delete

var deleteCmd = &cobra.Command{
Expand All @@ -31,7 +33,7 @@ func GetDeleteCmd(ks meta.IKubescape) *cobra.Command {
deleteCmd.PersistentFlags().StringVarP(&deleteInfo.Credentials.ClientID, "client-id", "", "", "Kubescape SaaS client ID. Default will load client ID from cache, read more - https://hub.armosec.io/docs/authentication")
deleteCmd.PersistentFlags().StringVarP(&deleteInfo.Credentials.SecretKey, "secret-key", "", "", "Kubescape SaaS secret key. Default will load secret key from cache, read more - https://hub.armosec.io/docs/authentication")

deleteCmd.AddCommand(getExceptionsCmd(ks, &deleteInfo))
deleteCmd.AddCommand(getExceptionsCmd(ctx, ks, &deleteInfo))

return deleteCmd
}
9 changes: 5 additions & 4 deletions cmd/delete/exceptions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package delete

import (
"context"
"fmt"
"strings"

Expand All @@ -11,7 +12,7 @@ import (
"github.com/spf13/cobra"
)

func getExceptionsCmd(ks meta.IKubescape, deleteInfo *v1.Delete) *cobra.Command {
func getExceptionsCmd(ctx context.Context, ks meta.IKubescape, deleteInfo *v1.Delete) *cobra.Command {
return &cobra.Command{
Use: "exceptions <exception name>",
Short: fmt.Sprintf("Delete exceptions from Kubescape SaaS version. Run '%[1]s list exceptions' for all exceptions names", cautils.ExecName()),
Expand All @@ -25,15 +26,15 @@ func getExceptionsCmd(ks meta.IKubescape, deleteInfo *v1.Delete) *cobra.Command
Run: func(cmd *cobra.Command, args []string) {

if err := flagValidationDelete(deleteInfo); err != nil {
logger.L().Fatal(err.Error())
logger.L().Ctx(ctx).Fatal(err.Error())
}

exceptionsNames := strings.Split(args[0], ";")
if len(exceptionsNames) == 0 {
logger.L().Fatal("missing exceptions names")
logger.L().Ctx(ctx).Fatal("missing exceptions names")
}
if err := ks.DeleteExceptions(&v1.DeleteExceptions{Credentials: deleteInfo.Credentials, Exceptions: exceptionsNames}); err != nil {
logger.L().Fatal(err.Error())
logger.L().Ctx(ctx).Fatal(err.Error())
}
},
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/download/download.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package download

import (
"context"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -41,7 +42,7 @@ var (
`, cautils.ExecName())
)

func GeDownloadCmd(ks meta.IKubescape) *cobra.Command {
func GetDownloadCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {
var downloadInfo = v1.DownloadInfo{}

downloadCmd := &cobra.Command{
Expand Down Expand Up @@ -74,8 +75,8 @@ func GeDownloadCmd(ks meta.IKubescape) *cobra.Command {
downloadInfo.Identifier = args[1]

}
if err := ks.Download(&downloadInfo); err != nil {
logger.L().Fatal(err.Error())
if err := ks.Download(ctx, &downloadInfo); err != nil {
logger.L().Ctx(ctx).Fatal(err.Error())
}
return nil
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/fix/fix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fix

import (
"context"
"errors"
"fmt"

Expand All @@ -21,7 +22,7 @@ var fixCmdExamples = fmt.Sprintf(`
`, cautils.ExecName())

func GetFixCmd(ks meta.IKubescape) *cobra.Command {
func GetFixCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {
var fixInfo metav1.FixInfo

fixCmd := &cobra.Command{
Expand All @@ -35,7 +36,7 @@ func GetFixCmd(ks meta.IKubescape) *cobra.Command {
}
fixInfo.ReportFile = args[0]

return ks.Fix(&fixInfo)
return ks.Fix(ctx, &fixInfo)
},
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/list/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package list

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -28,7 +29,7 @@ var (
`, cautils.ExecName())
)

func GetListCmd(ks meta.IKubescape) *cobra.Command {
func GetListCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {
var listPolicies = v1.ListPolicies{}

listCmd := &cobra.Command{
Expand All @@ -55,8 +56,8 @@ func GetListCmd(ks meta.IKubescape) *cobra.Command {

listPolicies.Target = args[0]

if err := ks.List(&listPolicies); err != nil {
logger.L().Fatal(err.Error())
if err := ks.List(ctx, &listPolicies); err != nil {
logger.L().Ctx(ctx).Fatal(err.Error())
}
return nil
},
Expand Down
31 changes: 16 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -40,12 +41,12 @@ var ksExamples = fmt.Sprintf(`
%[1]s config view
`, cautils.ExecName())

func NewDefaultKubescapeCommand() *cobra.Command {
func NewDefaultKubescapeCommand(ctx context.Context) *cobra.Command {
ks := core.NewKubescape()
return getRootCmd(ks)
return getRootCmd(ctx, ks)
}

func getRootCmd(ks meta.IKubescape) *cobra.Command {
func getRootCmd(ctx context.Context, ks meta.IKubescape) *cobra.Command {

rootCmd := &cobra.Command{
Use: "kubescape",
Expand Down Expand Up @@ -80,21 +81,21 @@ func getRootCmd(ks meta.IKubescape) *cobra.Command {
cobra.OnInitialize(initLogger, initLoggerLevel, initEnvironment, initCacheDir)

// Supported commands
rootCmd.AddCommand(scan.GetScanCommand(ks))
rootCmd.AddCommand(download.GeDownloadCmd(ks))
rootCmd.AddCommand(delete.GetDeleteCmd(ks))
rootCmd.AddCommand(list.GetListCmd(ks))
rootCmd.AddCommand(submit.GetSubmitCmd(ks))
rootCmd.AddCommand(completion.GetCompletionCmd())
rootCmd.AddCommand(version.GetVersionCmd())
rootCmd.AddCommand(config.GetConfigCmd(ks))
rootCmd.AddCommand(update.GetUpdateCmd())
rootCmd.AddCommand(fix.GetFixCmd(ks))
rootCmd.AddCommand(scan.GetScanCommand(ctx, ks))
rootCmd.AddCommand(download.GetDownloadCmd(ctx, ks))
rootCmd.AddCommand(delete.GetDeleteCmd(ctx, ks))
rootCmd.AddCommand(list.GetListCmd(ctx, ks))
rootCmd.AddCommand(submit.GetSubmitCmd(ctx, ks))
rootCmd.AddCommand(completion.GetCompletionCmd(ctx))
rootCmd.AddCommand(version.GetVersionCmd(ctx))
rootCmd.AddCommand(config.GetConfigCmd(ctx, ks))
rootCmd.AddCommand(update.GetUpdateCmd(ctx))
rootCmd.AddCommand(fix.GetFixCmd(ctx, ks))

return rootCmd
}

func Execute() error {
ks := NewDefaultKubescapeCommand()
func Execute(ctx context.Context) error {
ks := NewDefaultKubescapeCommand(ctx)
return ks.Execute()
}
15 changes: 8 additions & 7 deletions cmd/scan/control.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scan

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -36,7 +37,7 @@ var (
)

// controlCmd represents the control command
func getControlCmd(ks meta.IKubescape, scanInfo *cautils.ScanInfo) *cobra.Command {
func getControlCmd(ctx context.Context, ks meta.IKubescape, scanInfo *cautils.ScanInfo) *cobra.Command {
return &cobra.Command{
Use: "control <control names list>/<control ids list>",
Short: fmt.Sprintf("The controls you wish to use. Run '%[1]s list controls' for the list of supported controls", cautils.ExecName()),
Expand Down Expand Up @@ -96,20 +97,20 @@ func getControlCmd(ks meta.IKubescape, scanInfo *cautils.ScanInfo) *cobra.Comman
return err
}

results, err := ks.Scan(scanInfo)
results, err := ks.Scan(ctx, scanInfo)
if err != nil {
logger.L().Fatal(err.Error())
logger.L().Ctx(ctx).Fatal(err.Error())
}
if err := results.HandleResults(); err != nil {
logger.L().Fatal(err.Error())
if err := results.HandleResults(ctx); err != nil {
logger.L().Ctx(ctx).Fatal(err.Error())
}
if !scanInfo.VerboseMode {
cautils.SimpleDisplay(os.Stderr, "%s Run with '--verbose'/'-v' flag for detailed resources view\n\n", emoji.Detective)
}
if results.GetRiskScore() > float32(scanInfo.FailThreshold) {
logger.L().Fatal("scan risk-score is above permitted threshold", helpers.String("risk-score", fmt.Sprintf("%.2f", results.GetRiskScore())), helpers.String("fail-threshold", fmt.Sprintf("%.2f", scanInfo.FailThreshold)))
logger.L().Ctx(ctx).Fatal("scan risk-score is above permitted threshold", helpers.String("risk-score", fmt.Sprintf("%.2f", results.GetRiskScore())), helpers.String("fail-threshold", fmt.Sprintf("%.2f", scanInfo.FailThreshold)))
}
enforceSeverityThresholds(results.GetResults().SummaryDetails.GetResourcesSeverityCounters(), scanInfo, terminateOnExceedingSeverity)
enforceSeverityThresholds(ctx, results.GetResults().SummaryDetails.GetResourcesSeverityCounters(), scanInfo, terminateOnExceedingSeverity)

return nil
},
Expand Down

0 comments on commit 160ac0d

Please sign in to comment.