Skip to content

Commit 0d84ce5

Browse files
feat: Support old and new names in security scanning config (#2340)
1 parent 817a55d commit 0d84ce5

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

pkg/module/scaffold/security_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ func (g *Generator) SecurityConfigFileExists() (bool, error) {
1717
func (g *Generator) GenerateSecurityConfigFile() error {
1818
cfg := module.SecurityScanCfg{
1919
ModuleName: g.ModuleName,
20-
Protecode: []string{"europe-docker.pkg.dev/kyma-project/prod/myimage:1.2.3",
20+
Images: []string{"europe-docker.pkg.dev/kyma-project/prod/myimage:1.2.3",
2121
"europe-docker.pkg.dev/kyma-project/prod/external/ghcr.io/mymodule/anotherimage:4.5.6"},
22-
WhiteSource: module.WhiteSourceSecCfg{
22+
Mend: module.WhiteSourceSecCfg{
2323
Exclude: []string{"**/test/**", "**/*_test.go"},
2424
},
2525
}

pkg/module/security_scan.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"reflect"
78
"strings"
89

910
itociartifact "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/ociartifact"
1011
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/ociartifact"
1112
ocm "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
1213
ocmv1 "github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc/meta/v1"
13-
"sigs.k8s.io/yaml"
14+
"gopkg.in/yaml.v3"
1415
)
1516

1617
var ErrFailedToParseImageURL = errors.New("error parsing protecode image URL")
@@ -30,7 +31,7 @@ func AddSecurityScanningMetadata(descriptor *ocm.ComponentDescriptor, securityCo
3031
if err != nil {
3132
return err
3233
}
33-
excludedWhitesourcePathPatterns := strings.Join(config.WhiteSource.Exclude, ",")
34+
excludedWhitesourcePathPatterns := strings.Join(config.Mend.Exclude, ",")
3435

3536
// add security scan enabled global label
3637
err = appendLabelToAccessor(descriptor, "scan", secScanEnabled, globalLabelTemplate)
@@ -48,7 +49,7 @@ func AddSecurityScanningMetadata(descriptor *ocm.ComponentDescriptor, securityCo
4849
if err != nil {
4950
return err
5051
}
51-
err = appendLabelToAccessor(src, "language", config.WhiteSource.Language, labelTemplate)
52+
err = appendLabelToAccessor(src, "language", config.Mend.Language, labelTemplate)
5253
if err != nil {
5354
return err
5455
}
@@ -57,7 +58,7 @@ func AddSecurityScanningMetadata(descriptor *ocm.ComponentDescriptor, securityCo
5758
return err
5859
}
5960

60-
err = appendLabelToAccessor(src, "subprojects", config.WhiteSource.SubProjects, labelTemplate)
61+
err = appendLabelToAccessor(src, "subprojects", config.Mend.SubProjects, labelTemplate)
6162
if err != nil {
6263
return err
6364
}
@@ -77,7 +78,7 @@ func AddSecurityScanningMetadata(descriptor *ocm.ComponentDescriptor, securityCo
7778
}
7879

7980
func appendProtecodeImagesLayers(descriptor *ocm.ComponentDescriptor, config *SecurityScanCfg) error {
80-
for _, imageURL := range config.Protecode {
81+
for _, imageURL := range config.Images {
8182
imageName, imageTag, err := getImageName(imageURL)
8283
if err != nil {
8384
return err
@@ -125,18 +126,50 @@ func getImageName(imageURL string) (string, string, error) {
125126
}
126127

127128
type SecurityScanCfg struct {
128-
ModuleName string `json:"module-name" yaml:"module-name" comment:"string, name of your module"`
129-
Protecode []string `json:"protecode" yaml:"protecode" comment:"list, includes the images which must be scanned by the Protecode scanner (aka. Black Duck Binary Analysis)"`
130-
WhiteSource WhiteSourceSecCfg `json:"whitesource" yaml:"whitesource" comment:"whitesource (aka. Mend) security scanner specific configuration"`
131-
DevBranch string `json:"dev-branch" yaml:"dev-branch" comment:"string, name of the development branch"`
132-
RcTag string `json:"rc-tag" yaml:"rc-tag" comment:"string, release candidate tag"`
129+
ModuleName string `json:"module-name" yaml:"module-name" comment:"string, name of your module"`
130+
Images []string `json:"bdba" yaml:"bdba" comment:"list, includes the images which must be scanned by the Protecode scanner (aka. Black Duck Binary Analysis)"`
131+
Mend WhiteSourceSecCfg `json:"mend" yaml:"mend" comment:"whitesource (aka. Mend) security scanner specific configuration"`
132+
DevBranch string `json:"dev-branch" yaml:"dev-branch" comment:"string, name of the development branch"`
133+
RcTag string `json:"rc-tag" yaml:"rc-tag" comment:"string, release candidate tag"`
133134
}
134135
type WhiteSourceSecCfg struct {
135136
Language string `json:"language" yaml:"language" comment:"string, indicating the programming language the scanner has to analyze"`
136137
SubProjects string `json:"subprojects" yaml:"subprojects" comment:"string, specifying any subprojects"`
137138
Exclude []string `json:"exclude" yaml:"exclude" comment:"list, directories within the repository which should not be scanned"`
138139
}
139140

141+
func (config *SecurityScanCfg) UnmarshalYAML(value *yaml.Node) error {
142+
// Cannot use inheritance due to infinite loop
143+
var cfg struct {
144+
ModuleName string `json:"module-name" yaml:"module-name" comment:"string, name of your module"`
145+
Protecode []string `json:"protecode" yaml:"protecode" comment:"list, includes the images which must be scanned by the Protecode scanner (aka. Black Duck Binary Analysis)"`
146+
Whitesource WhiteSourceSecCfg `json:"whitesource" yaml:"whitesource" comment:"whitesource (aka. Mend) security scanner specific configuration"`
147+
DevBranch string `json:"dev-branch" yaml:"dev-branch" comment:"string, name of the development branch"`
148+
RcTag string `json:"rc-tag" yaml:"rc-tag" comment:"string, release candidate tag"`
149+
Images []string `json:"bdba" yaml:"bdba" comment:"list, includes the images which must be scanned by the Protecode scanner (aka. Black Duck Binary Analysis)"`
150+
Mend WhiteSourceSecCfg `json:"mend" yaml:"mend" comment:"whitesource (aka. Mend) security scanner specific configuration"`
151+
}
152+
153+
if err := value.Decode(&cfg); err != nil {
154+
return err
155+
}
156+
157+
config.ModuleName = cfg.ModuleName
158+
config.RcTag = cfg.RcTag
159+
config.Images = cfg.Images
160+
config.Mend = cfg.Mend
161+
162+
if len(cfg.Protecode) > 0 {
163+
config.Images = cfg.Protecode
164+
}
165+
166+
if !reflect.DeepEqual(cfg.Whitesource, WhiteSourceSecCfg{}) {
167+
config.Mend = cfg.Whitesource
168+
}
169+
170+
return nil
171+
}
172+
140173
func parseSecurityScanConfig(securityConfigPath string) (*SecurityScanCfg, error) {
141174
fileBytes, err := os.ReadFile(securityConfigPath)
142175
if err != nil {

0 commit comments

Comments
 (0)