|
| 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 enricherlist provides methods to initialize enrichers from attributes like names or capabilities. |
| 16 | +package enricherlist |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/google/osv-scalibr/enricher" |
| 22 | + "github.com/google/osv-scalibr/plugin" |
| 23 | +) |
| 24 | + |
| 25 | +// InitFn is the enricher initializer function. |
| 26 | +type InitFn func() enricher.Enricher |
| 27 | + |
| 28 | +// InitMap is a map of names to enricher initializer functions. |
| 29 | +type InitMap map[string][]InitFn |
| 30 | + |
| 31 | +var ( |
| 32 | + // All enrichers. |
| 33 | + All = InitMap{} |
| 34 | +) |
| 35 | + |
| 36 | +// FromNames returns a list of enrichers from a list of names. |
| 37 | +func FromNames(names []string) ([]enricher.Enricher, error) { |
| 38 | + if len(names) == 0 { |
| 39 | + return nil, nil |
| 40 | + } |
| 41 | + return nil, fmt.Errorf("not implemented") |
| 42 | +} |
| 43 | + |
| 44 | +// FromCapabilities returns all enrichers that can run under the specified |
| 45 | +// capabilities (OS, direct filesystem access, network access, etc.) of the |
| 46 | +// scanning environment. |
| 47 | +func FromCapabilities(capabilities *plugin.Capabilities) []enricher.Enricher { |
| 48 | + var all []enricher.Enricher |
| 49 | + for _, initers := range All { |
| 50 | + for _, initer := range initers { |
| 51 | + all = append(all, initer()) |
| 52 | + } |
| 53 | + } |
| 54 | + return FilterByCapabilities(all, capabilities) |
| 55 | +} |
| 56 | + |
| 57 | +// FilterByCapabilities returns all enrichers from the given list that can run |
| 58 | +// under the specified capabilities (OS, direct filesystem access, network |
| 59 | +// access, etc.) of the scanning environment. |
| 60 | +func FilterByCapabilities(es []enricher.Enricher, capabilities *plugin.Capabilities) []enricher.Enricher { |
| 61 | + var result []enricher.Enricher |
| 62 | + for _, e := range es { |
| 63 | + if err := plugin.ValidateRequirements(e, capabilities); err == nil { |
| 64 | + result = append(result, e) |
| 65 | + } |
| 66 | + } |
| 67 | + return result |
| 68 | +} |
0 commit comments