Skip to content

Commit ae28398

Browse files
fix(suse): SUSE - update OSType constants and references for compatility (#8236)
Co-authored-by: thatipelli santhosh <[email protected]>
1 parent 92697c7 commit ae28398

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

pkg/fanal/applier/applier_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,91 @@ func TestApplier_ApplyLayers(t *testing.T) {
961961
},
962962
wantErr: "unknown OS",
963963
},
964+
{
965+
name: "SUSE images - legacy OS name with backward compatibility",
966+
args: args{
967+
imageID: "sha256:fb44d01953611ba18d43d88e158c25579d18eff42db671182245010620a283f3",
968+
layerIDs: []string{
969+
"sha256:2615f175cf3da67c48c6542914744943ee5e9c253547b03e3cfe8aae605c3199",
970+
},
971+
},
972+
getLayerExpectations: []cache.LocalArtifactCacheGetBlobExpectation{
973+
{
974+
Args: cache.LocalArtifactCacheGetBlobArgs{
975+
BlobID: "sha256:2615f175cf3da67c48c6542914744943ee5e9c253547b03e3cfe8aae605c3199",
976+
},
977+
Returns: cache.LocalArtifactCacheGetBlobReturns{
978+
BlobInfo: types.BlobInfo{
979+
SchemaVersion: 1,
980+
Digest: "sha256:fb44d01953611ba18d43d88e158c25579d18eff42db671182245010620a283f3",
981+
DiffID: "sha256:d555e1b0b42f21a1cf198e52bcb12fe66aa015348e4390d2d5acddd327d79073",
982+
OS: types.OS{
983+
Family: "suse linux enterprise server",
984+
Name: "15.4",
985+
},
986+
PackageInfos: []types.PackageInfo{
987+
{
988+
FilePath: "usr/lib/sysimage/rpm/Packages.db",
989+
Packages: types.Packages{
990+
{
991+
Name: "curl",
992+
Version: "7.79.1",
993+
SrcName: "curl",
994+
SrcVersion: "7.79.1",
995+
},
996+
},
997+
},
998+
},
999+
},
1000+
},
1001+
},
1002+
},
1003+
getArtifactExpectations: []cache.LocalArtifactCacheGetArtifactExpectation{
1004+
{
1005+
Args: cache.LocalArtifactCacheGetArtifactArgs{
1006+
ArtifactID: "sha256:fb44d01953611ba18d43d88e158c25579d18eff42db671182245010620a283f3",
1007+
},
1008+
Returns: cache.LocalArtifactCacheGetArtifactReturns{
1009+
ArtifactInfo: types.ArtifactInfo{
1010+
SchemaVersion: 1,
1011+
},
1012+
},
1013+
},
1014+
},
1015+
want: types.ArtifactDetail{
1016+
OS: types.OS{
1017+
Family: "sles",
1018+
Name: "15.4",
1019+
},
1020+
Packages: types.Packages{
1021+
{
1022+
Name: "curl",
1023+
Version: "7.79.1",
1024+
SrcName: "curl",
1025+
SrcVersion: "7.79.1",
1026+
Identifier: types.PkgIdentifier{
1027+
UID: "1e9b3d3a73785651",
1028+
PURL: &packageurl.PackageURL{
1029+
Type: packageurl.TypeRPM,
1030+
Namespace: "suse",
1031+
Name: "curl",
1032+
Version: "7.79.1",
1033+
Qualifiers: packageurl.Qualifiers{
1034+
{
1035+
Key: "distro",
1036+
Value: "sles-15.4",
1037+
},
1038+
},
1039+
},
1040+
},
1041+
Layer: types.Layer{
1042+
Digest: "sha256:fb44d01953611ba18d43d88e158c25579d18eff42db671182245010620a283f3",
1043+
DiffID: "sha256:d555e1b0b42f21a1cf198e52bcb12fe66aa015348e4390d2d5acddd327d79073",
1044+
},
1045+
},
1046+
},
1047+
},
1048+
},
9641049
}
9651050
for _, tt := range tests {
9661051
t.Run(tt.name, func(t *testing.T) {

pkg/fanal/types/artifact.go

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func (o *OS) Detected() bool {
2727
return o.Family != ""
2828
}
2929

30+
// Normalize normalizes OS family names for backward compatibility
31+
func (o *OS) Normalize() {
32+
if alias, ok := OSTypeAliases[o.Family]; ok {
33+
o.Family = alias
34+
}
35+
}
36+
3037
// Merge merges OS version and enhanced security maintenance programs
3138
func (o *OS) Merge(newOS OS) {
3239
if lo.IsEmpty(newOS) {
@@ -53,6 +60,11 @@ func (o *OS) Merge(newOS OS) {
5360
o.Extended = true
5461
}
5562
}
63+
// When merging layers, there are cases when a layer contains an OS with an old name:
64+
// - Cache contains a layer derived from an old version of Trivy.
65+
// - `client` uses an old version of Trivy, but `server` is a new version of Trivy (for `client/server` mode).
66+
// So we need to normalize the OS name for backward compatibility.
67+
o.Normalize()
5668
}
5769

5870
type Repository struct {

pkg/fanal/types/const.go

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ const (
4343
Wolfi OSType = "wolfi"
4444
)
4545

46+
// OSTypeAliases is a map of aliases for operating systems.
47+
// This is used to map the old family names to the new ones for backward compatibility.
48+
var OSTypeAliases = map[OSType]OSType{
49+
"opensuse.leap": OpenSUSELeap,
50+
"opensuse.tumbleweed": OpenSUSETumbleweed,
51+
"suse linux enterprise micro": SLEMicro,
52+
"suse linux enterprise server": SLES,
53+
}
54+
4655
// Programming language dependencies
4756
const (
4857
Bundler LangType = "bundler"

0 commit comments

Comments
 (0)