Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/cmd/sanitize/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func Run(cmd *cobra.Command, sanitizer sanitize.ISanitizer, options sanitize.Opt
// printGroups uses the output format of `debricked files find`.
func printGroups(out io.Writer, groups []file.Group, asJson bool) error {
if asJson {
if groups == nil {
// The same output as `debricked files find --json` when nothing is found.
groups = []file.Group{}
}
encoded, err := json.Marshal(groups)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/sanitize/sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func TestRunEPrintsSanitizedFilesAsJson(t *testing.T) {
]`, out)
}

func TestRunEPrintsAnEmptyJsonListWhenNothingWasSanitized(t *testing.T) {
out, _, err := execute(&sanitizerMock{}, "x", "--json")

assert.NoError(t, err)
assert.Equal(t, "[]\n", out)
}

func TestRunEPrintsSummary(t *testing.T) {
mock := &sanitizerMock{result: sanitize.Result{
OutputDir: "x/.debricked/sanitized",
Expand Down
6 changes: 6 additions & 0 deletions internal/resolution/pm/swift/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions internal/sanitize/pm/bazel/install_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package bazel

import "github.com/debricked/cli/internal/sanitize/pm/util"

// sanitizeInstallJSON keeps each entry's coord and its edges: directDependencies, or
// dependencies if there are no direct ones, because that is what the backend reads. URLs, mirror
// URLs, file paths and hashes are removed: the URLs can hold private hosts and credentials.
//
// The backend needs dependency_tree and finds no packages in the v2 format, which has "artifacts"
// instead. A v2 file is skipped with ErrUnsupportedVersion, so it is reported and never copied.
func sanitizeInstallJSON(content []byte, _ *util.FileContext) ([]byte, error) {
install, err := util.ParseJSONObject(content)
if err != nil {
return nil, util.ErrInvalidFormat
}
tree, isObject := install.Values["dependency_tree"].(*util.OrderedObject)
if _, isV2 := install.Values["artifacts"].(*util.OrderedObject); !isObject && isV2 {
return nil, util.ErrUnsupportedVersion
}
if !isObject {
return nil, util.ErrInvalidFormat
}
entries, isList := tree.Values["dependencies"].([]any)
if !isList {
return nil, util.ErrInvalidFormat
}

sanitizedEntries := make([]any, 0, len(entries))
for _, item := range entries {
entry, isObject := item.(*util.OrderedObject)
if !isObject {
return nil, util.ErrInvalidFormat
}
sanitizedEntry, err := sanitizeEntry(entry)
if err != nil {
return nil, err
}
sanitizedEntries = append(sanitizedEntries, sanitizedEntry)
}

sanitizedTree := util.NewOrderedObject()
sanitizedTree.Set("dependencies", sanitizedEntries)
sanitized := util.NewOrderedObject()
sanitized.Set("dependency_tree", sanitizedTree)

return util.EncodeJSON(sanitized)
}

func sanitizeEntry(entry *util.OrderedObject) (*util.OrderedObject, error) {
coord, isString := entry.Values["coord"].(string)
if !isString || coord == "" {
return nil, util.ErrInvalidFormat
}
sanitized := util.NewOrderedObject()
sanitized.Set("coord", coord)

for _, key := range []string{"directDependencies", "dependencies"} {
value, present := entry.Values[key]
if !present {
continue
}
coords, err := coordList(value)
if err != nil {
return nil, err
}
sanitized.Set(key, coords)

break
}

return sanitized, nil
}

func coordList(value any) ([]any, error) {
list, isList := value.([]any)
if !isList {
return nil, util.ErrInvalidFormat
}
for _, item := range list {
if _, isString := item.(string); !isString {
return nil, util.ErrInvalidFormat
}
}

return list, nil
}
137 changes: 137 additions & 0 deletions internal/sanitize/pm/bazel/install_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package bazel

import (
"testing"

"github.com/debricked/cli/internal/sanitize/pm/util"
"github.com/stretchr/testify/assert"
)

// #nosec G101 -- fake credentials, the test checks they are removed
const installJSONBefore = `{
"dependency_tree": {
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": 1234,
"__INPUT_ARTIFACTS_HASH": 5678,
"conflict_resolution": {},
"dependencies": [
{
"coord": "com.acme:auth-client:2.1.0",
"dependencies": [
"com.google.guava:guava:32.0.0-jre",
"com.google.code.findbugs:jsr305:3.0.2"
],
"directDependencies": [
"com.google.guava:guava:32.0.0-jre"
],
"file": "v1/https/artifactory.acme.internal/libs/com/acme/auth-client-2.1.0.jar",
"mirror_urls": [
"https://ci-user:s3cret@artifactory.acme.internal/libs/com/acme/auth-client-2.1.0.jar"
],
"sha256": "aaaa",
"url": "https://ci-user:s3cret@artifactory.acme.internal/libs/com/acme/auth-client-2.1.0.jar"
},
{
"coord": "com.google.guava:guava:32.0.0-jre",
"dependencies": [
"com.google.code.findbugs:jsr305:3.0.2"
],
"directDependencies": [
"com.google.code.findbugs:jsr305:3.0.2"
],
"file": "v1/https/repo1.maven.org/guava.jar",
"sha256": "bbbb",
"url": "https://repo1.maven.org/maven2/guava.jar"
},
{
"coord": "com.google.code.findbugs:jsr305:3.0.2",
"dependencies": [],
"directDependencies": [],
"file": "v1/https/repo1.maven.org/jsr305.jar",
"sha256": "cccc",
"url": "https://repo1.maven.org/maven2/jsr305.jar"
}
],
"version": "0.1.0"
}
}`

const installJSONAfter = `{
"dependency_tree": {
"dependencies": [
{
"coord": "com.acme:auth-client:2.1.0",
"directDependencies": [
"com.google.guava:guava:32.0.0-jre"
]
},
{
"coord": "com.google.guava:guava:32.0.0-jre",
"directDependencies": [
"com.google.code.findbugs:jsr305:3.0.2"
]
},
{
"coord": "com.google.code.findbugs:jsr305:3.0.2",
"directDependencies": []
}
]
}
}
`

func TestSanitizeInstallJSON(t *testing.T) {
sanitized, err := sanitizeInstallJSON([]byte(installJSONBefore), nil)

assert.NoError(t, err)
assert.Equal(t, installJSONAfter, string(sanitized))
assert.NotContains(t, string(sanitized), "acme.internal")
}

func TestSanitizeInstallJSONUsesDependenciesWhenThereAreNoDirectOnes(t *testing.T) {
before := `{"dependency_tree": {"dependencies": [
{"coord": "a:b:1", "dependencies": ["c:d:2"], "url": "https://ci:s3cret@repo.acme.internal/b.jar"},
{"coord": "c:d:2"}
]}}`

sanitized, err := sanitizeInstallJSON([]byte(before), nil)

assert.NoError(t, err)
assert.Equal(t, `{
"dependency_tree": {
"dependencies": [
{
"coord": "a:b:1",
"dependencies": [
"c:d:2"
]
},
{
"coord": "c:d:2"
}
]
}
}
`, string(sanitized))
}

func TestSanitizeInstallJSONSkipsVersion2(t *testing.T) {
_, err := sanitizeInstallJSON([]byte(`{"version": "2", "artifacts": {"a:b": {"version": "1"}}}`), nil)

assert.ErrorIs(t, err, util.ErrUnsupportedVersion)
}

func TestSanitizeInstallJSONInvalid(t *testing.T) {
for _, content := range []string{
``,
`[]`,
`{"dependency_tree": []}`,
`{"dependency_tree": {"dependencies": {}}}`,
`{"dependency_tree": {"dependencies": ["a:b:1"]}}`,
`{"dependency_tree": {"dependencies": [{"url": "https://repo/b.jar"}]}}`,
`{"dependency_tree": {"dependencies": [{"coord": "a:b:1", "directDependencies": "c:d:2"}]}}`,
`{"dependency_tree": {"dependencies": [{"coord": "a:b:1", "directDependencies": [{"coord": "c:d:2"}]}]}}`,
} {
_, err := sanitizeInstallJSON([]byte(content), nil)
assert.ErrorIs(t, err, util.ErrInvalidFormat, content)
}
}
30 changes: 30 additions & 0 deletions internal/sanitize/pm/bazel/pm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package bazel

import "github.com/debricked/cli/internal/sanitize/pm/util"

const Name = "bazel"

type Pm struct {
name string
}

func NewPm() Pm {
return Pm{
name: Name,
}
}

func (pm Pm) Name() string {
return pm.name
}

func (Pm) LockFiles() map[string]util.FileSanitizer {
return map[string]util.FileSanitizer{
// rules_jvm_external names the file after the repository, such as maven_install.json.
"*_install.json": sanitizeInstallJSON,
}
}

func (Pm) Manifests() map[string]util.FileSanitizer {
return map[string]util.FileSanitizer{}
}
16 changes: 16 additions & 0 deletions internal/sanitize/pm/bazel/pm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bazel

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewPm(t *testing.T) {
pm := NewPm()

assert.Equal(t, Name, pm.Name())
assert.Len(t, pm.LockFiles(), 1)
assert.Contains(t, pm.LockFiles(), "*_install.json")
assert.Empty(t, pm.Manifests())
}
Loading
Loading