github.com/google/osv-scalibr@v0.4.1/annotator/list/list.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package list provides a list of annotation plugins. 16 package list 17 18 import ( 19 "fmt" 20 "maps" 21 "slices" 22 23 "github.com/google/osv-scalibr/annotator" 24 "github.com/google/osv-scalibr/annotator/cachedir" 25 "github.com/google/osv-scalibr/annotator/ffa/unknownbinariesanno" 26 "github.com/google/osv-scalibr/annotator/misc/dpkgsource" 27 "github.com/google/osv-scalibr/annotator/misc/npmsource" 28 noexecutabledpkg "github.com/google/osv-scalibr/annotator/noexecutable/dpkg" 29 "github.com/google/osv-scalibr/annotator/osduplicate/apk" 30 "github.com/google/osv-scalibr/annotator/osduplicate/cos" 31 "github.com/google/osv-scalibr/annotator/osduplicate/dpkg" 32 "github.com/google/osv-scalibr/annotator/osduplicate/rpm" 33 34 cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto" 35 ) 36 37 // InitFn is the annotator initializer function. 38 type InitFn func(cfg *cpb.PluginConfig) annotator.Annotator 39 40 // InitMap is a map of annotator names to their initers. 41 type InitMap map[string][]InitFn 42 43 // VEX generation related annotators. 44 var VEX = InitMap{ 45 apk.Name: {noCFG(apk.New)}, 46 cachedir.Name: {noCFG(cachedir.New)}, 47 cos.Name: {noCFG(cos.New)}, 48 dpkg.Name: {noCFG(dpkg.New)}, 49 rpm.Name: {noCFG(rpm.NewDefault)}, 50 noexecutabledpkg.Name: {noCFG(noexecutabledpkg.New)}, 51 } 52 53 // Misc annotators. 54 var Misc = InitMap{ 55 npmsource.Name: {noCFG(npmsource.New)}, 56 dpkgsource.Name: {noCFG(dpkgsource.New)}, 57 } 58 59 // FFA (Full Filesystem Accountability) related annotators. 60 var FFA = InitMap{unknownbinariesanno.Name: {noCFG(unknownbinariesanno.New)}} 61 62 // Default detectors that are recommended to be enabled. 63 var Default = InitMap{cachedir.Name: {noCFG(cachedir.New)}} 64 65 // All annotators. 66 var All = concat( 67 VEX, 68 Misc, 69 FFA, 70 ) 71 72 var annotatorNames = concat(All, InitMap{ 73 "vex": vals(VEX), 74 "misc": vals(Misc), 75 "ffa": vals(FFA), 76 "annotators/default": vals(Default), 77 "default": vals(Default), 78 "annotators/all": vals(All), 79 "all": vals(All), 80 }) 81 82 func concat(initMaps ...InitMap) InitMap { 83 result := InitMap{} 84 for _, m := range initMaps { 85 maps.Copy(result, m) 86 } 87 return result 88 } 89 90 func vals(initMap InitMap) []InitFn { 91 return slices.Concat(slices.Collect(maps.Values(initMap))...) 92 } 93 94 // Wraps initer functions that don't take any config value to initer functions that do. 95 // TODO(b/400910349): Remove once all plugins take config values. 96 func noCFG(f func() annotator.Annotator) InitFn { 97 return func(_ *cpb.PluginConfig) annotator.Annotator { return f() } 98 } 99 100 // AnnotatorsFromName returns a list of annotators from a name. 101 func AnnotatorsFromName(name string, cfg *cpb.PluginConfig) ([]annotator.Annotator, error) { 102 if initers, ok := annotatorNames[name]; ok { 103 result := []annotator.Annotator{} 104 for _, initer := range initers { 105 result = append(result, initer(cfg)) 106 } 107 return result, nil 108 } 109 return nil, fmt.Errorf("unknown annotator %q", name) 110 }