Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add kas parser #143

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/liamg/jfather v0.0.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/whilp/git-urls v1.0.0 // indirect
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how you'll feel about adding this little package. But still seemed better than manually parsing, especially if more git-based parsers are added later.

go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/text v0.3.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/whilp/git-urls v1.0.0 h1:95f6UMWN5FKW71ECsXRUd3FVYiXdrE7aX4NZKcPmIjU=
github.com/whilp/git-urls v1.0.0/go.mod h1:J16SAmobsqc3Qcy98brfl5f5+e0clUvg1krgwk/qCfE=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down
78 changes: 78 additions & 0 deletions pkg/git/kas/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package kas

import (
"fmt"
"strings"

"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

dio "github.com/aquasecurity/go-dep-parser/pkg/io"
giturls "github.com/whilp/git-urls"

"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/aquasecurity/go-dep-parser/pkg/utils"
)

type Repo struct {
Url string `yaml:"url,omitempty"`
RefSpec string `yaml:"refspec,omitempty"`
}

type KasFile struct {
Repos map[string]Repo `yaml:"repos,omitempty"`
}

type Parser struct{}

func NewParser() types.Parser {
return &Parser{}
}

func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var kasFile KasFile
decoder := yaml.NewDecoder(r)
err := decoder.Decode(&kasFile)
if err != nil {
return nil, nil, xerrors.Errorf("decode error: %w", err)
}

libs, deps := p.parse(&kasFile)

return libs, deps, nil
}

func (p *Parser) parse(kasFile *KasFile) ([]types.Library, []types.Dependency) {
var libs []types.Library

for _, repo := range kasFile.Repos {
name := getRepoNamefromUri(repo.Url)
if name == "" || repo.Url == "" {
continue
}

version := repo.RefSpec
if version == "" {
version = "latest"
}
Comment on lines +54 to +57
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add the latest here because without a refspec, it will clone HEAD (like implicit latest in docker), so just wanted to ensure this still gets picked up for BOMs etc.


libs = append(libs, types.Library{
ID: utils.PackageID(name, version),
Name: name,
Version: version,
})
}

return libs, nil
}

func getRepoNamefromUri(rawUri string) string {
uri, err := giturls.Parse(rawUri)
if err != nil {
return ""
}

name := strings.TrimSuffix(uri.Path, ".git")
name = strings.TrimLeft(name, "/")
return fmt.Sprintf("%s/%s", uri.Host, name)
}
52 changes: 52 additions & 0 deletions pkg/git/kas/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package kas

import (
"os"
"path"
"testing"

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

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

func TestParse(t *testing.T) {
vectors := []struct {
file string
want []types.Library
}{
{
file: "testdata/kas.yml",
want: kasRepo,
},
{
file: "testdata/kas-no-refspec.yml",
want: kasRepoLatest,
},
{
file: "testdata/kas-git-url.yml",
want: kasRepoGitUrl,
},
{
file: "testdata/kas-git-url.yml",
want: kasRepoSshUrl,
},
{
file: "testdata/kas-multiple-repos.yml",
want: kasRepos,
},
}

for _, v := range vectors {
t.Run(path.Base(v.file), func(t *testing.T) {
f, err := os.Open(v.file)
require.NoError(t, err)

got, _, err := NewParser().Parse(f)
require.NoError(t, err)

assert.Equal(t, v.want, got)
})
}
}
50 changes: 50 additions & 0 deletions pkg/git/kas/parse_testcase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kas

import "github.com/aquasecurity/go-dep-parser/pkg/types"

var (
kasRepo = []types.Library{
{
ID: "github.com/org/[email protected]",
Name: "github.com/org/kas",
Version: "1.0.0",
},
}

kasRepoLatest = []types.Library{
{
ID: "github.com/org/kas@latest",
Name: "github.com/org/kas",
Version: "latest",
},
}

kasRepoGitUrl = []types.Library{
{
ID: "github.com/org/[email protected]",
Name: "github.com/org/kas",
Version: "1.0.0",
},
}

kasRepoSshUrl = []types.Library{
{
ID: "github.com/org/[email protected]",
Name: "github.com/org/kas",
Version: "1.0.0",
},
}

kasRepos = []types.Library{
{
ID: "github.com/org/[email protected]",
Name: "github.com/org/kas",
Version: "1.0.0",
},
{
ID: "github.com/user/[email protected]",
Name: "github.com/user/repo",
Version: "2.0.0",
},
}
)
12 changes: 12 additions & 0 deletions pkg/git/kas/testdata/kas-git-url.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
header:
version: 11
includes:
- repo: externalrepo
file: tests/test_layers/test.yml

repos:
this:

externalrepo:
url: [email protected]:org/kas.git
refspec: 1.0.0
16 changes: 16 additions & 0 deletions pkg/git/kas/testdata/kas-multiple-repos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
header:
version: 11
includes:
- repo: externalrepo
file: tests/test_layers/test.yml

repos:
this:

externalrepo:
url: https://github.com/org/kas.git
refspec: 1.0.0

userrepo:
url: https://github.com/user/repo.git
refspec: 2.0.0
11 changes: 11 additions & 0 deletions pkg/git/kas/testdata/kas-no-refspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
header:
version: 11
includes:
- repo: externalrepo
file: tests/test_layers/test.yml

repos:
this:

externalrepo:
url: https://github.com/org/kas.git
12 changes: 12 additions & 0 deletions pkg/git/kas/testdata/kas-ssh-url.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
header:
version: 11
includes:
- repo: externalrepo
file: tests/test_layers/test.yml

repos:
this:

externalrepo:
url: ssh://[email protected]/org/kas.git
refspec: 1.0.0
12 changes: 12 additions & 0 deletions pkg/git/kas/testdata/kas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
header:
version: 11
includes:
- repo: externalrepo
file: tests/test_layers/test.yml

repos:
this:

externalrepo:
url: https://github.com/org/kas.git
refspec: 1.0.0