Skip to content

Commit 3ad843a

Browse files
authored
🧹 add label to k8s manifest assets when discovered via git (#5780)
1 parent f6a4d42 commit 3ad843a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

providers-sdk/v1/plugin/git.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"go.mondoo.com/cnquery/v11/providers-sdk/v1/vault"
1515
)
1616

17+
const GitUrlOptionKey = "git-http-url" // used for tracking the link to the git repo for later reference
18+
1719
func NewGitClone(asset *inventory.Asset) (string, func(), error) {
1820
cc := asset.Connections[0]
1921

@@ -40,6 +42,7 @@ func NewGitClone(asset *inventory.Asset) (string, func(), error) {
4042
// gitlab: git clone https://oauth2:ACCESS_TOKEN@somegitlab.com/vendor/package.git
4143
// if sshUrl := cc.Options["ssh-url"]; sshUrl != "" { ... not doing ssh url right now
4244
if httpUrl := cc.Options["http-url"]; httpUrl != "" {
45+
cc.Options[GitUrlOptionKey] = httpUrl // stick this on the asset connection options so we can reference it later
4346
u, err := url.Parse(httpUrl)
4447
if err != nil {
4548
return "", nil, errors.New("failed to parse url for git repo: " + httpUrl)

providers/k8s/connection/manifest/connection.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/hex"
99
"os"
1010
"path/filepath"
11+
"strings"
1112

1213
"github.com/pkg/errors"
1314
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
@@ -106,7 +107,14 @@ func NewConnection(id uint32, asset *inventory.Asset, opts ...Option) (shared.Co
106107
if asset.Name == "" {
107108
asset.Name = clusterName
108109
}
109-
110+
if gitPath := asset.Connections[0].Options[plugin.GitUrlOptionKey]; gitPath != "" {
111+
// if the GitUrlOptionKey is present, we want to sanitize the url and add it to the
112+
// asset labels so that we can later reference the git url where this object was found
113+
if asset.Labels == nil {
114+
asset.Labels = make(map[string]string)
115+
}
116+
asset.Labels[plugin.GitUrlOptionKey] = trimGitPath(gitPath)
117+
}
110118
c.ManifestParser, err = shared.NewManifestParser(manifest, c.namespace, "")
111119
if err != nil {
112120
return nil, err
@@ -115,6 +123,10 @@ func NewConnection(id uint32, asset *inventory.Asset, opts ...Option) (shared.Co
115123
return c, nil
116124
}
117125

126+
func trimGitPath(gitPath string) string {
127+
return strings.TrimSuffix(gitPath, ".git")
128+
}
129+
118130
func (c *Connection) Close() {
119131
if c.closer != nil {
120132
c.closer()

0 commit comments

Comments
 (0)