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

refactor(k8s): add v prefix for Go packages #7839

Merged
merged 4 commits into from
Nov 25, 2024
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
31 changes: 19 additions & 12 deletions pkg/k8s/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ func (s *Scanner) scanK8sVulns(ctx context.Context, artifactsData []*artifacts.A
if err != nil {
return nil, err
}
cpcVersion := unifiedVersion(comp.Version)

lang := k8sNamespace(comp.Version, nodeName)
lang := k8sNamespace(cpcVersion, nodeName)
results, _, err := k8sScanner.Scan(ctx, types.ScanTarget{
Applications: []ftypes.Application{
{
Expand All @@ -252,7 +253,7 @@ func (s *Scanner) scanK8sVulns(ctx context.Context, artifactsData []*artifacts.A
Packages: []ftypes.Package{
{
Name: comp.Name,
Version: comp.Version,
Version: cpcVersion,
},
},
},
Expand All @@ -277,7 +278,7 @@ func (s *Scanner) scanK8sVulns(ctx context.Context, artifactsData []*artifacts.A
if err != nil {
return nil, err
}
kubeletVersion := sanitizedVersion(nf.KubeletVersion)
kubeletVersion := unifiedVersion(nf.KubeletVersion)
lang := k8sNamespace(kubeletVersion, nodeName)
runtimeName, runtimeVersion := runtimeNameVersion(nf.ContainerRuntimeVersion)
results, _, err := k8sScanner.Scan(ctx, types.ScanTarget{
Expand Down Expand Up @@ -387,14 +388,15 @@ func (s *Scanner) clusterInfoToReportResources(allArtifact []*artifacts.Artifact
if err := ms.Decode(artifact.RawResource, &comp); err != nil {
return nil, err
}
cVersion := unifiedVersion(comp.Version)

controlPlane := &core.Component{
Name: comp.Name,
Version: comp.Version,
Version: cVersion,
Type: core.TypeApplication,
Properties: toProperties(comp.Properties, k8sCoreComponentNamespace),
PkgIdentifier: ftypes.PkgIdentifier{
PURL: generatePURL(comp.Name, comp.Version, nodeName),
PURL: generatePURL(comp.Name, cVersion, nodeName),
},
}
coreComponents = append(coreComponents, controlPlane)
Expand All @@ -405,7 +407,7 @@ func (s *Scanner) clusterInfoToReportResources(allArtifact []*artifacts.Artifact
if !strings.Contains(c.Digest, string(digest.SHA256)) {
cDigest = fmt.Sprintf("%s:%s", string(digest.SHA256), cDigest)
}
ver := sanitizedVersion(c.Version)
ver := unifiedVersion(c.Version)

imagePURL, err := purl.New(purl.TypeOCI, types.Metadata{
RepoDigests: []string{
Expand Down Expand Up @@ -448,13 +450,15 @@ func (s *Scanner) clusterInfoToReportResources(allArtifact []*artifacts.Artifact
if err := ms.Decode(artifact.RawResource, &cf); err != nil {
return nil, err
}
cVersion := unifiedVersion(cf.Version)

rootComponent = &core.Component{
Type: core.TypePlatform,
Name: cf.Name,
Version: cf.Version,
Version: cVersion,
Properties: toProperties(cf.Properties, k8sCoreComponentNamespace),
PkgIdentifier: ftypes.PkgIdentifier{
PURL: generatePURL(cf.Name, cf.Version, nodeName),
PURL: generatePURL(cf.Name, cVersion, nodeName),
},
Root: true,
}
Expand All @@ -474,7 +478,7 @@ func (s *Scanner) clusterInfoToReportResources(allArtifact []*artifacts.Artifact
func (s *Scanner) nodeComponent(b *core.BOM, nf bom.NodeInfo) *core.Component {
osName, osVersion := osNameVersion(nf.OsImage)
runtimeName, runtimeVersion := runtimeNameVersion(nf.ContainerRuntimeVersion)
kubeletVersion := sanitizedVersion(nf.KubeletVersion)
kubeletVersion := unifiedVersion(nf.KubeletVersion)
properties := toProperties(nf.Properties, "")
properties = append(properties, toProperties(map[string]string{
k8sComponentType: k8sComponentNode,
Expand Down Expand Up @@ -557,8 +561,11 @@ func (s *Scanner) nodeComponent(b *core.BOM, nf bom.NodeInfo) *core.Component {
return nodeComponent
}

func sanitizedVersion(ver string) string {
return strings.TrimPrefix(ver, "v")
func unifiedVersion(ver string) string {
if strings.HasPrefix(ver, "v") || ver == "" {
return ver
}
return "v" + ver
}

func osNameVersion(name string) (string, string) {
Expand Down Expand Up @@ -592,7 +599,7 @@ func runtimeNameVersion(name string) (string, string) {
case "cri-dockerd":
name = "github.com/Mirantis/cri-dockerd"
}
return name, ver
return name, unifiedVersion(ver)
}

func toProperties(props map[string]string, namespace string) []core.Property {
Expand Down
32 changes: 16 additions & 16 deletions pkg/k8s/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestScanner_Scan(t *testing.T) {
{
Type: core.TypeApplication,
Name: "github.com/containerd/containerd",
Version: "1.5.2",
Version: "v1.5.2",
Properties: []core.Property{
{
Name: k8sComponentName,
Expand All @@ -104,29 +104,29 @@ func TestScanner_Scan(t *testing.T) {
PURL: &packageurl.PackageURL{
Type: "golang",
Name: "github.com/containerd/containerd",
Version: "1.5.2",
Version: "v1.5.2",
Qualifiers: packageurl.Qualifiers{},
},
BOMRef: "pkg:golang/github.com%2Fcontainerd%2Fcontainerd@1.5.2",
BOMRef: "pkg:golang/github.com%2Fcontainerd%2Fcontainerd@v1.5.2",
},
},
{
Type: core.TypeApplication,
Name: "k8s.io/apiserver",
Version: "1.21.1",
Version: "v1.21.1",
PkgIdentifier: ftypes.PkgIdentifier{
PURL: &packageurl.PackageURL{
Type: purl.TypeK8s,
Name: "k8s.io/apiserver",
Version: "1.21.1",
Version: "v1.21.1",
},
BOMRef: "pkg:k8s/k8s.io%2Fapiserver@1.21.1",
BOMRef: "pkg:k8s/k8s.io%2Fapiserver@v1.21.1",
},
},
{
Type: core.TypeApplication,
Name: "k8s.io/kubelet",
Version: "1.21.1",
Version: "v1.21.1",
Properties: []core.Property{
{
Name: k8sComponentName,
Expand All @@ -143,9 +143,9 @@ func TestScanner_Scan(t *testing.T) {
PURL: &packageurl.PackageURL{
Type: "k8s",
Name: "k8s.io/kubelet",
Version: "1.21.1",
Version: "v1.21.1",
},
BOMRef: "pkg:k8s/k8s.io%2Fkubelet@1.21.1",
BOMRef: "pkg:k8s/k8s.io%2Fkubelet@v1.21.1",
},
},
{
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestScanner_Scan(t *testing.T) {
Properties: []core.Property{
{
Name: core.PropertyPkgID,
Value: "k8s.gcr.io/kube-apiserver:1.21.1",
Value: "k8s.gcr.io/kube-apiserver:v1.21.1",
},
{
Name: core.PropertyPkgType,
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestScanner_Scan(t *testing.T) {
Type: core.TypePlatform,
Root: true,
Name: "k8s.io/kubernetes",
Version: "1.21.1",
Version: "v1.21.1",
Properties: []core.Property{
{
Name: "Name",
Expand All @@ -225,9 +225,9 @@ func TestScanner_Scan(t *testing.T) {
PURL: &packageurl.PackageURL{
Type: purl.TypeK8s,
Name: "k8s.io/kubernetes",
Version: "1.21.1",
Version: "v1.21.1",
},
BOMRef: "pkg:k8s/k8s.io%2Fkubernetes@1.21.1",
BOMRef: "pkg:k8s/k8s.io%2Fkubernetes@v1.21.1",
},
},
{
Expand Down Expand Up @@ -464,19 +464,19 @@ func TestRuntimeVersion(t *testing.T) {
name: "containerd",
runtimeVersion: "containerd://1.5.2",
wantName: "github.com/containerd/containerd",
wantVersion: "1.5.2",
wantVersion: "v1.5.2",
},
{
name: "cri-o",
runtimeVersion: "cri-o://1.5.2",
wantName: "github.com/cri-o/cri-o",
wantVersion: "1.5.2",
wantVersion: "v1.5.2",
},
{
name: "cri-dockerd",
runtimeVersion: "cri-dockerd://1.5.2",
wantName: "github.com/Mirantis/cri-dockerd",
wantVersion: "1.5.2",
wantVersion: "v1.5.2",
},
{
name: "na runtime",
Expand Down