Skip to content

Commit

Permalink
Merge 7245b73 into fb2509b
Browse files Browse the repository at this point in the history
  • Loading branch information
rcohencyberarmor committed Oct 3, 2023
2 parents fb2509b + 7245b73 commit e15303b
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 131 deletions.
22 changes: 11 additions & 11 deletions adapters/v1/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ type BackendAdapter struct {
eventReceiverRestURL string
apiServerRestURL string
clusterConfig pkgcautils.ClusterConfig
getCVEExceptionsFunc func(string, string, *identifiers.PortalDesignator) ([]armotypes.VulnerabilityExceptionPolicy, error)
getCVEExceptionsFunc func(string, string, *identifiers.PortalDesignator, map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error)
httpPostFunc func(httputils.IHttpClient, string, map[string]string, []byte) (*http.Response, error)
sendStatusFunc func(*backendClientV1.BaseReportSender, string, bool, chan<- error)
sendStatusFunc func(*backendClientV1.BaseReportSender, string, bool)
accessToken string
}

var _ ports.Platform = (*BackendAdapter)(nil)

func NewBackendAdapter(accountID, apiServerRestURL, eventReceiverRestURL string) *BackendAdapter {
func NewBackendAdapter(accountID, apiServerRestURL, eventReceiverRestURL, accessToken string) *BackendAdapter {
return &BackendAdapter{
clusterConfig: pkgcautils.ClusterConfig{
AccountID: accountID,
Expand All @@ -45,9 +46,10 @@ func NewBackendAdapter(accountID, apiServerRestURL, eventReceiverRestURL string)
apiServerRestURL: apiServerRestURL,
getCVEExceptionsFunc: backendClientV1.GetCVEExceptionByDesignator,
httpPostFunc: httputils.HttpPost,
sendStatusFunc: func(sender *backendClientV1.BaseReportSender, status string, sendReport bool, errChan chan<- error) {
sender.SendStatus(status, sendReport, errChan) // TODO - update this function to use from kubescape/backend
sendStatusFunc: func(sender *backendClientV1.BaseReportSender, status string, sendReport bool) {
sender.SendStatus(status, sendReport) // TODO - update this function to use from kubescape/backend
},
accessToken: accessToken,
}
}

Expand Down Expand Up @@ -90,7 +92,7 @@ func (a *BackendAdapter) GetCVEExceptions(ctx context.Context) (domain.CVEExcept
},
}

vulnExceptionList, err := a.getCVEExceptionsFunc(a.apiServerRestURL, a.clusterConfig.AccountID, &designator)
vulnExceptionList, err := a.getCVEExceptionsFunc(a.apiServerRestURL, a.clusterConfig.AccountID, &designator, a.getRequestHeaders())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -122,11 +124,9 @@ func (a *BackendAdapter) SendStatus(ctx context.Context, step int) error {
report.ParentAction = workload.ParentJobID
report.Details = details[step]

ReportErrorsChan := make(chan error)
sender := backendClientV1.NewBaseReportSender(a.eventReceiverRestURL, &http.Client{}, report)
a.sendStatusFunc(sender, sysreport.JobSuccess, true, ReportErrorsChan)
err := <-ReportErrorsChan
return err
sender := backendClientV1.NewBaseReportSender(a.eventReceiverRestURL, &http.Client{}, a.getRequestHeaders(), report)
a.sendStatusFunc(sender, sysreport.JobSuccess, true)
return nil
}

// SubmitCVE submits the given CVE to the platform
Expand Down
9 changes: 4 additions & 5 deletions adapters/v1/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
func TestBackendAdapter_GetCVEExceptions(t *testing.T) {
type fields struct {
clusterConfig armometadata.ClusterConfig
getCVEExceptionsFunc func(string, string, *identifiers.PortalDesignator) ([]armotypes.VulnerabilityExceptionPolicy, error)
getCVEExceptionsFunc func(string, string, *identifiers.PortalDesignator, map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error)
}
tests := []struct {
name string
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestBackendAdapter_SubmitCVE(t *testing.T) {
}
a := &BackendAdapter{
clusterConfig: armometadata.ClusterConfig{},
getCVEExceptionsFunc: func(s, a string, designator *identifiers.PortalDesignator) ([]armotypes.VulnerabilityExceptionPolicy, error) {
getCVEExceptionsFunc: func(s, a string, designator *identifiers.PortalDesignator, headers map[string]string) ([]armotypes.VulnerabilityExceptionPolicy, error) {
return tt.exceptions, nil
},
httpPostFunc: httpPostFunc,
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestNewBackendAdapter(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewBackendAdapter(tt.args.accountID, tt.args.apiServerRestURL, tt.args.eventReceiverRestURL)
got := NewBackendAdapter(tt.args.accountID, tt.args.apiServerRestURL, tt.args.eventReceiverRestURL, "")
// need to nil functions to compare
got.httpPostFunc = nil
got.getCVEExceptionsFunc = nil
Expand Down Expand Up @@ -248,10 +248,9 @@ func TestBackendAdapter_SendStatus(t *testing.T) {
for _, tt := range tests { //nolint:govet
t.Run(tt.name, func(t *testing.T) {
a := &BackendAdapter{
sendStatusFunc: func(sender *beClientV1.BaseReportSender, s string, b bool, c chan<- error) {
sendStatusFunc: func(sender *beClientV1.BaseReportSender, s string, b bool) {
report := sender.GetBaseReport()
assert.NotEqual(t, *report, tt.report) //nolint:govet
close(c)
},
}
ctx := context.TODO()
Expand Down
10 changes: 9 additions & 1 deletion adapters/v1/backend_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func (a *BackendAdapter) postResultsAsGoroutine(ctx context.Context, report *v1.
}(report, eventReceiverURL, imagetag, wlid, errorChan, wg)
}

func (a *BackendAdapter) getRequestHeaders() map[string]string {
return map[string]string{
"Content-Type": "application/json",
"Authorization": "Bearer " + a.accessToken,
}
}

func (a *BackendAdapter) postResults(ctx context.Context, report *v1.ScanResultReport, eventReceiverURL, imagetag, wlid string, errorChan chan<- error) {
payload, err := json.Marshal(report)
if err != nil {
Expand All @@ -78,7 +85,8 @@ func (a *BackendAdapter) postResults(ctx context.Context, report *v1.ScanResultR
errorChan <- err
return
}
resp, err := a.httpPostFunc(http.DefaultClient, urlBase.String(), map[string]string{"Content-Type": "application/json"}, payload)

resp, err := a.httpPostFunc(http.DefaultClient, urlBase.String(), a.getRequestHeaders(), payload)
if err != nil {
logger.L().Ctx(ctx).Error("failed posting to event", helpers.Error(err),
helpers.String("image", imagetag),
Expand Down
8 changes: 7 additions & 1 deletion cmd/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/armosec/armoapi-go/apis"
"github.com/gin-gonic/gin"
"github.com/kubescape/backend/pkg/utils"
"github.com/kubescape/go-logger"
"github.com/kubescape/go-logger/helpers"
"github.com/kubescape/kubevuln/adapters"
Expand All @@ -31,6 +32,11 @@ func main() {
logger.L().Ctx(ctx).Fatal("load config error", helpers.Error(err))
}

sd, err := utils.LoadTokenFromSecret("/etc/access-token-secret")
if err != nil {
logger.L().Ctx(ctx).Fatal("load secret data error", helpers.Error(err))
}

// to enable otel, set OTEL_COLLECTOR_SVC=otel-collector:4317
if otelHost, present := os.LookupEnv("OTEL_COLLECTOR_SVC"); present {
ctx = logger.InitOtel("kubevuln",
Expand Down Expand Up @@ -63,7 +69,7 @@ func main() {
logger.L().Ctx(ctx).Fatal("load services error", helpers.Error(err))
}
logger.L().Info("loaded backend services", helpers.String("ApiServerUrl", backendServices.GetApiServerUrl()), helpers.String("ReportReceiverHttpUrl", backendServices.GetReportReceiverHttpUrl()))
platform = v1.NewBackendAdapter(c.AccountID, backendServices.GetApiServerUrl(), backendServices.GetReportReceiverHttpUrl())
platform = v1.NewBackendAdapter(c.AccountID, backendServices.GetApiServerUrl(), backendServices.GetReportReceiverHttpUrl(), sd.Token)
}
service := services.NewScanService(sbomAdapter, storage, cveAdapter, storage, platform, c.Storage)
controller := controllers.NewHTTPController(service, c.ScanConcurrency)
Expand Down
73 changes: 36 additions & 37 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,41 @@ require (
github.com/anchore/stereoscope v0.0.0-20230323161519-d7551b7f46f5
github.com/anchore/syft v0.76.0
github.com/aquilax/truncate v1.0.0
github.com/armosec/armoapi-go v0.0.220
github.com/armosec/utils-go v0.0.20
github.com/armosec/armoapi-go v0.0.254
github.com/armosec/utils-go v0.0.40
github.com/armosec/utils-k8s-go v0.0.18
github.com/distribution/distribution v2.8.2+incompatible
github.com/docker/docker v24.0.5+incompatible
github.com/eapache/go-resiliency v1.3.0
github.com/gammazero/workerpool v1.1.3
github.com/gin-gonic/gin v1.9.1
github.com/google/go-containerregistry v0.14.0
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/hashicorp/go-multierror v1.1.1
github.com/kinbiko/jsonassert v1.1.1
github.com/kubescape/backend v0.0.1
github.com/kubescape/go-logger v0.0.14-0.20230730134225-e59751254525
github.com/kubescape/backend v0.0.8-0.20231003075242-690fc75a1964
github.com/kubescape/go-logger v0.0.21
github.com/kubescape/k8s-interface v0.0.135-0.20230730135750-e6e709507847
github.com/kubescape/storage v0.0.18
github.com/spdx/tools-golang v0.5.0-rc1
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.40.0
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
go.opentelemetry.io/otel v1.18.0
go.opentelemetry.io/otel/trace v1.18.0
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
schneider.vip/problem v1.8.1
)

require (
cloud.google.com/go v0.110.2 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go v0.110.7 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/container v1.24.0 // indirect
cloud.google.com/go/iam v0.13.0 // indirect
cloud.google.com/go/storage v1.29.0 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
Expand Down Expand Up @@ -89,6 +89,7 @@ require (
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bmatcuk/doublestar/v2 v2.0.4 // indirect
github.com/bmatcuk/doublestar/v4 v4.6.0 // indirect
github.com/briandowns/spinner v1.23.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down Expand Up @@ -140,7 +141,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -163,7 +164,7 @@ require (
github.com/knqyf263/go-apk-version v0.0.0-20200609155635-041fdbb8563f // indirect
github.com/knqyf263/go-deb-version v0.0.0-20190517075300-09fca494f03d // indirect
github.com/knqyf263/go-rpmdb v0.0.0-20221030135625-4082a22221ce // indirect
github.com/kubescape/opa-utils v0.0.266 // indirect
github.com/kubescape/opa-utils v0.0.268 // indirect
github.com/kubescape/rbac-utils v0.0.20 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down Expand Up @@ -223,7 +224,7 @@ require (
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.2.2 // indirect
github.com/uptrace/opentelemetry-go-extra/otelzap v0.2.2 // indirect
github.com/uptrace/uptrace-go v1.16.0 // indirect
github.com/uptrace/uptrace-go v1.18.0 // indirect
github.com/vbatts/go-mtree v0.5.3 // indirect
github.com/vbatts/tar-split v0.11.2 // indirect
github.com/vifraa/gopom v0.2.1 // indirect
Expand All @@ -234,39 +235,37 @@ require (
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/yashtewari/glob-intersection v0.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.42.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.39.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.39.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.39.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.opentelemetry.io/contrib/instrumentation/runtime v0.44.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.41.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.18.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.18.0 // indirect
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/sdk v1.18.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.41.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.9.1 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.126.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230913181813-007df8e322eb // indirect
google.golang.org/grpc v1.58.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down

0 comments on commit e15303b

Please sign in to comment.