Skip to content

Commit

Permalink
fix: perform deep clone of config properties
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jan 11, 2024
1 parent 248c7ee commit 0dc84a7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
3 changes: 2 additions & 1 deletion api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ type BaseScraper struct {
// Tags allow you to set custom tags on the scraped config items.
Tags JSONStringMap `json:"tags,omitempty"`

// Properties are custom templatable properties for the scraped config items.
// Properties are custom templatable properties for the scraped config items
// grouped by the config type.
Properties map[string]types.Properties `json:"properties,omitempty" template:"true"`
}

Expand Down
18 changes: 9 additions & 9 deletions chart/crds/configs.flanksource.com_scrapeconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
region:
items:
Expand Down Expand Up @@ -550,7 +550,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
subscriptionID:
type: string
Expand Down Expand Up @@ -788,7 +788,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
tags:
additionalProperties:
Expand Down Expand Up @@ -980,7 +980,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
tags:
additionalProperties:
Expand Down Expand Up @@ -1210,7 +1210,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
repository:
type: string
Expand Down Expand Up @@ -1476,7 +1476,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
relationships:
description: Relationships specify the fields to use to relate
Expand Down Expand Up @@ -1716,7 +1716,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
selector:
properties:
Expand Down Expand Up @@ -2023,7 +2023,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
query:
type: string
Expand Down Expand Up @@ -2230,7 +2230,7 @@ spec:
type: object
type: array
description: Properties are custom templatable properties for
the scraped config items.
the scraped config items grouped by the config type.
type: object
scanners:
items:
Expand Down
7 changes: 7 additions & 0 deletions fixtures/aws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ spec:
- af-south-1
- ap-south-1
- eu-central-1
properties:
'AWS::IAM::Role':
- name: AWS Link
icon: aws-iam
links:
- text: AWS Link
url: https://us-east-1.console.aws.amazon.com/iamv2/home#/roles/details/{{.name}}?section=permissions
compliance: true
patch_states: false
trusted_advisor_check: false
Expand Down
11 changes: 8 additions & 3 deletions scrapers/processors/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/flanksource/commons/logger"
v1 "github.com/flanksource/config-db/api/v1"
"github.com/flanksource/duty/types"
"github.com/flanksource/config-db/utils"
"github.com/flanksource/gomplate/v3"
"github.com/magiconair/properties"
"github.com/ohler55/ojg/jp"
Expand Down Expand Up @@ -183,8 +183,13 @@ func (e Extract) Extract(inputs ...v1.ScrapeResult) ([]v1.ScrapeResult, error) {
},
}

input.Properties = make([]*types.Property, len(properties))
copy(input.Properties, properties)
// Need to perform a deep clone otherwise this will affect the base scraper
// and hence all the inputs that come after.
input.Properties, err = utils.CloneWithJSON(properties)
if err != nil {
return results, fmt.Errorf("failed to clone config properties: %w", err)
}

if err := templater.Walk(input.Properties); err != nil {
return results, fmt.Errorf("failed to template scraper properties: %w", err)
}
Expand Down
13 changes: 13 additions & 0 deletions utils/struct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import "encoding/json"

func CloneWithJSON[T any](v T) (T, error) {
b, err := json.Marshal(&v)
if err != nil {
return v, err
}

var v2 T
return v2, json.Unmarshal(b, &v2)
}

0 comments on commit 0dc84a7

Please sign in to comment.