Skip to content

Commit

Permalink
make sure add mondoo labels works for all mondoo labels
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Oct 4, 2023
1 parent 2195fd0 commit 02aa18a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion providers-sdk/v1/inventory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
fmt "fmt"
"regexp"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -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
}
}
Expand Down
36 changes: 36 additions & 0 deletions providers-sdk/v1/inventory/asset_test.go
Original file line number Diff line number Diff line change
@@ -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",
})
}

0 comments on commit 02aa18a

Please sign in to comment.