Skip to content

Commit 1448a88

Browse files
Merge pull request #304 from debricked/update-marshal-json-debricked-config
Add Support for Ignore in YAML Configuration Format
2 parents b6f4cfa + 91585a9 commit 1448a88

File tree

7 files changed

+225
-28
lines changed

7 files changed

+225
-28
lines changed

build/docker/alpine.Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RUN mkdir -p internal/file/embedded && \
99
RUN apk add --no-cache make curl && make install && apk del make curl
1010
CMD [ "debricked" ]
1111

12-
FROM alpine:latest AS cli-base
12+
FROM alpine:3.21 AS cli-base
1313
ENV DEBRICKED_TOKEN=""
1414
RUN apk add --no-cache git
1515
WORKDIR /root/
@@ -60,7 +60,7 @@ RUN apk --no-cache --update add \
6060
curl \
6161
bash
6262

63-
RUN apk --no-cache --update add dotnet8-sdk go~=1.23 --repository=https://dl-cdn.alpinelinux.org/alpine/v3.20/community
63+
RUN apk --no-cache --update add dotnet8-sdk go~=1.23 --repository=https://dl-cdn.alpinelinux.org/alpine/v3.21/community
6464

6565
RUN dotnet --version && npm -v && yarn -v
6666

build/docker/debian.Dockerfile

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.23-bookworm AS dev
1+
FROM golang:1.23.4-bookworm AS dev
22
WORKDIR /cli
33

44
ARG DEBIAN_FRONTEND=noninteractive
@@ -90,18 +90,23 @@ RUN curl -fsSLO https://dot.net/v1/dotnet-install.sh \
9090
&& rm ./dotnet-install.sh \
9191
&& dotnet help
9292

93-
ENV GOLANG_VERSION="1.23"
93+
ENV GOLANG_VERSION="1.23.4"
94+
ENV GOPATH="/usr/lib/go"
95+
ENV PATH="$GOPATH/bin:$PATH"
9496
RUN apt -y update && apt -y upgrade && apt -y install \
95-
ca-certificates && \
97+
ca-certificates \
98+
wget && \
9699
apt -y install -t unstable \
97-
python3.12\
98-
python3.12-venv \
99-
golang-$GOLANG_VERSION \
100+
python3.13 \
101+
python3.13-venv \
100102
openjdk-21-jdk && \
101103
apt -y clean && rm -rf /var/lib/apt/lists/* && \
102-
# Symlink go binary to bin directory which is in path
103-
ln -s /usr/lib/go-$GOLANG_VERSION/bin/go /usr/bin/go && \
104-
ln -s /usr/bin/python3.12 /usr/bin/python
104+
# Install Go manually from official source
105+
wget https://go.dev/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz && \
106+
tar -C /usr/local -xzf go${GOLANG_VERSION}.linux-amd64.tar.gz && \
107+
rm go${GOLANG_VERSION}.linux-amd64.tar.gz && \
108+
ln -s /usr/local/go/bin/go /usr/bin/go && \
109+
ln -s /usr/bin/python3.13 /usr/bin/python
105110

106111
RUN dotnet --version
107112

@@ -132,7 +137,7 @@ RUN apt -y update && apt -y install \
132137

133138
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
134139

135-
RUN ln -sf /usr/bin/python3.12 /usr/bin/python3 && php -v && composer --version && python3 --version
140+
RUN ln -sf /usr/bin/python3.13 /usr/bin/python3 && php -v && composer --version && python3 --version
136141

137142
CMD [ "debricked", "scan" ]
138143

internal/upload/batch.go

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,18 @@ type purlConfig struct {
322322
}
323323

324324
type DebrickedConfig struct {
325-
Overrides []purlConfig `json:"overrides" yaml:"overrides"`
325+
Overrides []purlConfig `json:"override,omitempty" yaml:"overrides"`
326+
Ignore *IgnoreConfig `json:"ignore,omitempty" yaml:"ignore,omitempty"`
327+
}
328+
329+
// IgnoreConfig matches the structure of the 'ignore' section in YAML
330+
type IgnoreConfig struct {
331+
Packages []IgnorePackage `json:"packages" yaml:"packages"`
332+
}
333+
334+
type IgnorePackage struct {
335+
PURL string `json:"pURL" yaml:"pURL"`
336+
Version string `json:"version,omitempty" yaml:"version,omitempty"`
326337
}
327338

328339
type uploadFinish struct {
@@ -361,8 +372,43 @@ type DebrickedConfigYAML struct {
361372
Overrides []pURLConfigYAML `yaml:"overrides"`
362373
}
363374

364-
func GetDebrickedConfig(path string) *DebrickedConfig {
375+
// extractIgnore unmarshals the ignore section from raw config
376+
func extractIgnore(raw map[string]interface{}) *IgnoreConfig {
377+
if rawIgnore, ok := raw["ignore"]; ok {
378+
ignoreYaml, err := yaml.Marshal(rawIgnore)
379+
if err == nil {
380+
var ignoreObj IgnoreConfig
381+
if yaml.Unmarshal(ignoreYaml, &ignoreObj) == nil {
382+
return &ignoreObj
383+
}
384+
}
385+
}
386+
387+
return nil
388+
}
389+
390+
// convertOverrides converts YAML overrides to purlConfig slice
391+
func convertOverrides(yamlOverrides []pURLConfigYAML) []purlConfig {
365392
var overrides []purlConfig
393+
for _, entry := range yamlOverrides {
394+
var version string
395+
var exist bool
396+
pURL := entry.PackageURL
397+
fileRegexes := entry.FileRegexes
398+
if entry.Version == nil {
399+
version = ""
400+
exist = false
401+
} else {
402+
version = *entry.Version
403+
exist = true
404+
}
405+
overrides = append(overrides, purlConfig{PackageURL: pURL, Version: boolOrString{Version: version, HasVersion: exist}, FileRegexes: fileRegexes})
406+
}
407+
408+
return overrides
409+
}
410+
411+
func GetDebrickedConfig(path string) *DebrickedConfig {
366412
var yamlConfig DebrickedConfigYAML
367413
yamlFile, err := os.ReadFile(path)
368414
if err != nil {
@@ -374,7 +420,10 @@ func GetDebrickedConfig(path string) *DebrickedConfig {
374420

375421
return nil
376422
}
377-
err = yaml.Unmarshal(yamlFile, &yamlConfig)
423+
424+
// Unmarshal into map to support any key for overrides and ignore
425+
var raw map[string]interface{}
426+
err = yaml.Unmarshal(yamlFile, &raw)
378427
if err != nil {
379428
fmt.Printf("%s Failed to unmarshal debricked config: \"%s\"\n",
380429
color.YellowString("⚠️"),
@@ -383,22 +432,41 @@ func GetDebrickedConfig(path string) *DebrickedConfig {
383432

384433
return nil
385434
}
386-
for _, entry := range yamlConfig.Overrides {
387-
var version string
388-
var exist bool
389-
pURL := entry.PackageURL
390-
fileRegexes := entry.FileRegexes
391-
if entry.Version == nil {
392-
version = ""
393-
exist = false
394-
} else {
395-
version = *entry.Version
396-
exist = true
435+
436+
// Accept any key for overrides, normalize to 'overrides'
437+
for k, v := range raw {
438+
lower := strings.ToLower(k)
439+
if lower == "overrides" || lower == "override" {
440+
raw["overrides"] = v
397441
}
398-
overrides = append(overrides, purlConfig{PackageURL: pURL, Version: boolOrString{Version: version, HasVersion: exist}, FileRegexes: fileRegexes})
399442
}
400443

444+
// Marshal back to YAML and unmarshal into struct
445+
fixedYaml, err := yaml.Marshal(raw)
446+
if err != nil {
447+
fmt.Printf("%s Failed to re-marshal config: \"%s\"\n",
448+
color.YellowString("⚠️"),
449+
color.RedString(err.Error()),
450+
)
451+
452+
return nil
453+
}
454+
455+
err = yaml.Unmarshal(fixedYaml, &yamlConfig)
456+
if err != nil {
457+
fmt.Printf("%s Failed to unmarshal debricked config: \"%s\"\n",
458+
color.YellowString("⚠️"),
459+
color.RedString(err.Error()),
460+
)
461+
462+
return nil
463+
}
464+
465+
ignore := extractIgnore(raw)
466+
overrides := convertOverrides(yamlConfig.Overrides)
467+
401468
return &DebrickedConfig{
402469
Overrides: overrides,
470+
Ignore: ignore,
403471
}
404472
}

internal/upload/batch_test.go

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,52 @@ func TestGetDebrickedConfig(t *testing.T) {
176176
assert.JSONEq(t, string(configJSON), string(expectedJSON))
177177
}
178178

179+
func TestGetDebrickedConfigIgnore(t *testing.T) {
180+
config := GetDebrickedConfig(filepath.Join("testdata", "debricked-config-ignore.yaml"))
181+
configJSON, err := json.Marshal(config)
182+
assert.Nil(t, err)
183+
expectedJSON, err := json.Marshal(DebrickedConfig{
184+
Ignore: &IgnoreConfig{
185+
Packages: []IgnorePackage{
186+
{PURL: "pkg:npm/verdaccio", Version: "3.7.0"},
187+
{PURL: "pkg:npm/chart.js"},
188+
{PURL: "pkg:nuget/simpleinjector", Version: "4.7.1"},
189+
},
190+
},
191+
})
192+
assert.Nil(t, err)
193+
assert.JSONEq(t, string(configJSON), string(expectedJSON))
194+
}
195+
196+
func TestGetDebrickedConfigOverridesIgnore(t *testing.T) {
197+
config := GetDebrickedConfig(filepath.Join("testdata", "debricked-config-override-ignore.yaml"))
198+
configJSON, err := json.Marshal(config)
199+
assert.Nil(t, err)
200+
expectedJSON, err := json.Marshal(DebrickedConfig{
201+
Overrides: []purlConfig{
202+
{
203+
PackageURL: "pkg:npm/lodash",
204+
Version: boolOrString{Version: "1.0.0", HasVersion: true},
205+
FileRegexes: []string{".*/lodash/.*"},
206+
},
207+
{
208+
PackageURL: "pkg:maven/org.openjfx/javafx-base",
209+
Version: boolOrString{Version: "", HasVersion: false},
210+
FileRegexes: []string{"subpath/org.openjfx/.*"},
211+
},
212+
},
213+
Ignore: &IgnoreConfig{
214+
Packages: []IgnorePackage{
215+
{PURL: "pkg:npm/verdaccio", Version: "3.7.0"},
216+
{PURL: "pkg:npm/chart.js"},
217+
{PURL: "pkg:nuget/simpleinjector", Version: "4.7.1"},
218+
},
219+
},
220+
})
221+
assert.Nil(t, err)
222+
assert.JSONEq(t, string(configJSON), string(expectedJSON))
223+
}
224+
179225
func TestGetDebrickedConfigUnmarshalError(t *testing.T) {
180226
config := GetDebrickedConfig(filepath.Join("testdata", "debricked-config-error.yaml"))
181227
configJSON, err := json.Marshal(config)
@@ -209,7 +255,58 @@ func TestMarshalJSONDebrickedConfig(t *testing.T) {
209255
},
210256
},
211257
})
212-
expectedJSON := "{\"overrides\":[{\"pURL\":\"pkg:npm/lodash\",\"version\":\"1.0.0\",\"fileRegexes\":[\".*/lodash/.*\"]},{\"pURL\":\"pkg:maven/org.openjfx/javafx-base\",\"version\":false,\"fileRegexes\":[\"subpath/org.openjfx/.*\"]}]}"
258+
expectedJSON := "{\"override\":[{\"pURL\":\"pkg:npm/lodash\",\"version\":\"1.0.0\",\"fileRegexes\":[\".*/lodash/.*\"]},{\"pURL\":\"pkg:maven/org.openjfx/javafx-base\",\"version\":false,\"fileRegexes\":[\"subpath/org.openjfx/.*\"]}]}"
259+
assert.Nil(t, err)
260+
assert.Equal(t, []byte(expectedJSON), config)
261+
}
262+
263+
func TestMarshalJSONDebrickedConfigIgnoreOnly(t *testing.T) {
264+
config, err := json.Marshal(DebrickedConfig{
265+
Ignore: &IgnoreConfig{
266+
Packages: []IgnorePackage{
267+
{PURL: "pkg:npm/verdaccio", Version: "3.7.0"},
268+
{PURL: "pkg:npm/chart.js"},
269+
},
270+
},
271+
})
272+
expectedJSON := "{\"ignore\":{\"packages\":[{\"pURL\":\"pkg:npm/verdaccio\",\"version\":\"3.7.0\"},{\"pURL\":\"pkg:npm/chart.js\"}]}}"
213273
assert.Nil(t, err)
214274
assert.Equal(t, []byte(expectedJSON), config)
215275
}
276+
277+
func TestMarshalJSONDebrickedConfigBoth(t *testing.T) {
278+
config, err := json.Marshal(DebrickedConfig{
279+
Overrides: []purlConfig{
280+
{
281+
PackageURL: "pkg:npm/lodash",
282+
Version: boolOrString{Version: "1.0.0", HasVersion: true},
283+
FileRegexes: []string{".*/lodash/.*"},
284+
},
285+
},
286+
Ignore: &IgnoreConfig{
287+
Packages: []IgnorePackage{
288+
{PURL: "pkg:npm/chart.js"},
289+
},
290+
},
291+
})
292+
expectedJSON := "{\"override\":[{\"pURL\":\"pkg:npm/lodash\",\"version\":\"1.0.0\",\"fileRegexes\":[\".*/lodash/.*\"]}],\"ignore\":{\"packages\":[{\"pURL\":\"pkg:npm/chart.js\"}]}}"
293+
assert.Nil(t, err)
294+
assert.Equal(t, []byte(expectedJSON), config)
295+
}
296+
297+
func TestGetDebrickedConfigSingularOverride(t *testing.T) {
298+
config := GetDebrickedConfig(filepath.Join("testdata", "debricked-config-singular-override.yaml"))
299+
configJSON, err := json.Marshal(config)
300+
assert.Nil(t, err)
301+
expectedJSON, err := json.Marshal(DebrickedConfig{
302+
Overrides: []purlConfig{
303+
{
304+
PackageURL: "pkg:npm/lodash",
305+
Version: boolOrString{Version: "1.0.0", HasVersion: true},
306+
FileRegexes: []string{".*/lodash/.*"},
307+
},
308+
},
309+
})
310+
assert.Nil(t, err)
311+
assert.JSONEq(t, string(configJSON), string(expectedJSON))
312+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ignore:
2+
packages:
3+
- pURL: "pkg:npm/verdaccio"
4+
version: "3.7.0"
5+
- pURL: "pkg:npm/chart.js"
6+
- pURL: "pkg:nuget/simpleinjector"
7+
version: "4.7.1"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
overrides:
2+
- pURL: "pkg:npm/lodash"
3+
version: "1.0.0" # optional: if left out, we will decide version
4+
fileRegexes:
5+
- ".*/lodash/.*" # PCRE2
6+
- pURL: "pkg:maven/org.openjfx/javafx-base"
7+
fileRegexes:
8+
- "subpath/org.openjfx/.*"
9+
ignore:
10+
packages:
11+
- pURL: "pkg:npm/verdaccio"
12+
version: "3.7.0"
13+
- pURL: "pkg:npm/chart.js"
14+
- pURL: "pkg:nuget/simpleinjector"
15+
version: "4.7.1"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
override:
2+
- pURL: "pkg:npm/lodash"
3+
version: "1.0.0"
4+
fileRegexes:
5+
- ".*/lodash/.*"

0 commit comments

Comments
 (0)