Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attack-chain generic tests + test suite adjustment #84

Merged
merged 5 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions db/mongo/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,50 @@ var collectionIndexes = map[string][]mongo.IndexModel{
Options: options.Index().SetExpireAfterSeconds(0),
},
},
consts.AttackChainsCollection: {
{
Keys: bson.D{
avrahams marked this conversation as resolved.
Show resolved Hide resolved
{Key: "guid", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
{Key: "name", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
{Key: "customers", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
{Key: "attackChainID", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
{Key: "clusterName", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
{Key: "latestReportGUID", Value: 1},
},
Options: options.Index().SetBackground(true),
},
{
Keys: bson.D{
{Key: "uiStatus.processingStatus", Value: 1},
},
Options: options.Index().SetBackground(true),
},
},
}

// defaultIndex is the default index for all collections unless overridden in collectionIndexes
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module config-service
go 1.18

require (
github.com/armosec/armoapi-go v0.0.208
github.com/armosec/armoapi-go v0.0.210
github.com/aws/smithy-go v1.13.5
github.com/chidiwilliams/flatbson v0.3.0
github.com/dchest/uniuri v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armosec/armoapi-go v0.0.208 h1:ugeaXfbBu+rFnvo9YwrK3aRPnz3BLEGwly3zRhfYeTA=
github.com/armosec/armoapi-go v0.0.208/go.mod h1:MSaFIxFu2ucjrY2RAzgasZbBlop+S7bNgRz5Q7iOmOc=
github.com/armosec/armoapi-go v0.0.210 h1:ytwEkpRCg/J4pJ8c1WCV1uJvuGM3P4RZejTOWNYVEn0=
github.com/armosec/armoapi-go v0.0.210/go.mod h1:MSaFIxFu2ucjrY2RAzgasZbBlop+S7bNgRz5Q7iOmOc=
github.com/armosec/gojay v1.2.15 h1:sSB2vnAvacUNkw9nzUYZKcPzhJOyk6/5LK2JCNdmoZY=
github.com/armosec/gojay v1.2.15/go.mod h1:vzVAaay2TWJAngOpxu8aqLbye9jMgoKleuAOK+xsOts=
github.com/armosec/utils-go v0.0.12 h1:NXkG/BhbSVAmTVXr0qqsK02CmxEiXuJyPmdTRcZ4jAo=
Expand Down
12 changes: 12 additions & 0 deletions handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type routerOptions[T types.DocContent] struct {
serveBulkDelete bool //default true, serve DELETE /<path>/bulk with list of GUIDs in body or query to delete documents by GUIDs
serveDeleteByName bool //default false, when true, DELETE will check for name param and will delete the document by name
validatePostUniqueName bool //default true, POST will validate that the name is unique
validatePostMandatoryName bool //default false, POST will validate that the name exists
validatePutGUID bool //default true, PUT will validate GUID existence in body or path
nameQueryParam string //default empty, the param name that indicates query by name (e.g. clusterName) when set GET will check for this param and will return the document by name
QueryConfig *QueryParamsConfig //default nil, when set, GET will check for the specified query params and will return the documents by the query params
Expand Down Expand Up @@ -67,6 +68,7 @@ func newRouterOptions[T types.DocContent]() *routerOptions[T] {
serveGetNamesList: true,
serveGetIncludeGlobalDocs: false,
serveDeleteByName: false,
validatePostMandatoryName: false,
}
}

Expand Down Expand Up @@ -105,6 +107,9 @@ func AddRoutes[T types.DocContent](g *gin.Engine, options ...RouterOption[T]) *g
if opts.validatePostUniqueName {
postValidators = append(postValidators, ValidateUniqueValues(NameKeyGetter[T]))
}
if opts.validatePostMandatoryName {
postValidators = append(postValidators, ValidateNameExistence[T])
}
if opts.uniqueShortName != nil {
postValidators = append(postValidators, ValidatePostAttributeShortName(opts.uniqueShortName))
}
Expand Down Expand Up @@ -345,6 +350,13 @@ func (b *RouterOptionsBuilder[T]) WithValidatePostUniqueName(validatePostUniqueN
return b
}

func (b *RouterOptionsBuilder[T]) WithValidatePostMandatoryName(validatePostMandatoryName bool) *RouterOptionsBuilder[T] {
b.options = append(b.options, func(opts *routerOptions[T]) {
opts.validatePostMandatoryName = validatePostMandatoryName
})
return b
}

func (b *RouterOptionsBuilder[T]) WithValidatePutGUID(validatePutGUID bool) *RouterOptionsBuilder[T] {
b.options = append(b.options, func(opts *routerOptions[T]) {
opts.validatePutGUID = validatePutGUID
Expand Down
11 changes: 11 additions & 0 deletions handlers/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,14 @@ func ValidatePutAttributerShortName[T types.DocContent](c *gin.Context, docs []T
}
return docs, true
}

func ValidateNameExistence[T types.DocContent](c *gin.Context, docs []T) ([]T, bool) {
defer log.LogNTraceEnterExit("ValidateNameExistence", c)()
for i := range docs {
if docs[i].GetName() == "" {
ResponseMissingName(c)
return nil, false
}
}
return docs, true
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"config-service/routes/login"
"config-service/routes/prob"
"config-service/routes/v1/admin"
"config-service/routes/v1/attack_chains"
"config-service/routes/v1/cluster"
"config-service/routes/v1/collaboration_config"
"config-service/routes/v1/customer"
Expand Down Expand Up @@ -79,6 +80,7 @@ func setupRouter() *gin.Engine {
registry_cron_job.AddRoutes(router)
collaboration_config.AddRoutes(router)
users_notifications_cache.AddRoutes(router)
attack_chains.AddRoutes(router)
return router
}

Expand Down
21 changes: 21 additions & 0 deletions routes/v1/attack_chains/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package attack_chains

import (
"config-service/handlers"
"config-service/types"
"config-service/utils/consts"

"github.com/gin-gonic/gin"
)

func AddRoutes(g *gin.Engine) {
handlers.AddRoutes(g, handlers.NewRouterOptionsBuilder[*types.AttackChain]().
WithPath(consts.AttackChainsPath).
WithDBCollection(consts.AttackChainsCollection).
WithV2ListSearch(true).
WithGetNamesList(false).
WithValidatePostUniqueName(false).
WithValidatePostMandatoryName(true).
WithPostValidators(validateAttackChainId).
Get()...)
}
18 changes: 18 additions & 0 deletions routes/v1/attack_chains/validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package attack_chains

import (
"config-service/handlers"
"config-service/types"

"github.com/gin-gonic/gin"
)

func validateAttackChainId(c *gin.Context, docs []*types.AttackChain) ([]*types.AttackChain, bool) {
for i := range docs {
if docs[i].AttackChainID == "" {
handlers.ResponseBadRequest(c, "Attack Chain must contain AttackChainID")
return nil, false
}
}
return docs, true
}
Loading
Loading