diff --git a/providers-sdk/v1/inventory/asset.go b/providers-sdk/v1/inventory/asset.go index 174b3f8989..791a3ceb99 100644 --- a/providers-sdk/v1/inventory/asset.go +++ b/providers-sdk/v1/inventory/asset.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" fmt "fmt" + "regexp" "strings" "github.com/rs/zerolog/log" @@ -139,13 +140,15 @@ func (s *AssetCategory) UnmarshalJSON(data []byte) error { return nil } +var mondooLabelRegex = regexp.MustCompile(`^[a-z0-9]*\.?(mondoo.com\/)[a-z0-9\-]*`) + // Merges the mondoo-specific labels from the provided root into the provided asset func (a *Asset) AddMondooLabels(root *Asset) { if a.Labels == nil { a.Labels = map[string]string{} } for k, v := range root.Labels { - if strings.HasPrefix(k, "mondoo.com/") { + if mondooLabelRegex.MatchString(k) { a.Labels[k] = v } } diff --git a/providers-sdk/v1/inventory/asset_test.go b/providers-sdk/v1/inventory/asset_test.go new file mode 100644 index 0000000000..6406993969 --- /dev/null +++ b/providers-sdk/v1/inventory/asset_test.go @@ -0,0 +1,36 @@ +// Copyright (c) Mondoo, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package inventory + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAddMondooLabels(t *testing.T) { + asset := &Asset{ + Labels: map[string]string{ + "foo": "bar", + }, + } + + rootAsset := &Asset{ + Labels: map[string]string{ + "k8s.mondoo.com/test": "val", + "mondoo.com/sample": "example", + "random": "random-val", + }, + } + + asset.AddMondooLabels(rootAsset) + assert.Equal( + t, + asset.Labels, + map[string]string{ + "foo": "bar", + "k8s.mondoo.com/test": "val", + "mondoo.com/sample": "example", + }) +}