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: adding git remotes to subjects and normalising url #361

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
35 changes: 34 additions & 1 deletion attestation/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import (
"crypto"
"fmt"
"path"
"strings"
"time"

Expand All @@ -25,7 +26,9 @@
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/in-toto/go-witness/attestation"
"github.com/in-toto/go-witness/cryptoutil"
"github.com/in-toto/go-witness/log"
"github.com/invopop/jsonschema"
giturl "github.com/whilp/git-urls"
)

const (
Expand Down Expand Up @@ -156,7 +159,14 @@
}

for _, remote := range remotes {
a.Remotes = append(a.Remotes, remote.Config().URLs...)
for _, u := range remote.Config().URLs {
nu, err := normaliseRemoteURL(u)
if err != nil {
log.Debug(err.Error())
}

a.Remotes = append(a.Remotes, nu)
}
}

refs, err := repo.References()
Expand Down Expand Up @@ -326,6 +336,16 @@
subjects[subjectName] = ds
}

// add remotes
for _, remote := range a.Remotes {
subjectName = fmt.Sprintf("remote:%v", remote)
ds, err = cryptoutil.CalculateDigestSetFromBytes([]byte(remote), hashes)
if err != nil {
return nil
}
subjects[subjectName] = ds
}

// add refname short
subjectName = fmt.Sprintf("refnameshort:%v", a.RefNameShort)
ds, err = cryptoutil.CalculateDigestSetFromBytes([]byte(a.RefNameShort), hashes)
Expand Down Expand Up @@ -371,3 +391,16 @@
return string(statusCode)
}
}

func normaliseRemoteURL(url string) (string, error) {
rurl, err := giturl.Parse(url)
if err != nil {
return "", fmt.Errorf("failed to parse remote url: %w", err)
}

if strings.HasSuffix(rurl.Path, ".git") {

Check failure on line 401 in attestation/git/git.go

View workflow job for this annotation

GitHub Actions / lint

S1017: should replace this if statement with an unconditional strings.TrimSuffix (gosimple)
rurl.Path = strings.TrimSuffix(rurl.Path, ".git")
}

return path.Join(rurl.Host, rurl.Path), nil
}
19 changes: 19 additions & 0 deletions attestation/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ func TestNameTypeRunType(t *testing.T) {
require.Equal(t, RunType, attestor.RunType(), "Expected the attestor's run type")
}

func TestNormaliseRemotes(t *testing.T) {
remotes := []string{
"github.com/in-toto/go-witness",
"github.com/in-toto/go-witness.git",
"https://github.com/in-toto/go-witness",
"https://github.com/in-toto/go-witness.git",
Copy link
Member

Choose a reason for hiding this comment

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

I think it may be worth testing with git:// and git+ssh etc, even though some of those are deprecated. I think some SCPs (possibly sourceforge?) sets git:// to be default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

a-ha - I will!

"[email protected]:in-toto/go-witness",
"[email protected]:in-toto/go-witness.git",
}

for _, r := range remotes {
nr, err := normaliseRemoteURL(r)
require.NoError(t, err, "Expected no error from normaliseRemoteURL")

require.Equal(t, "github.com/in-toto/go-witness", nr, "normalising failed for %s. Expected 'github.com/in-toto/go-witness', got %s", r, nr)
}

}

func TestRunWorksWithCommits(t *testing.T) {
attestor := New()

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ require (
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect
github.com/whilp/git-urls v1.0.0 // indirect
Copy link
Member

Choose a reason for hiding this comment

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

weird this is marked indirect? 🤔 go mod tidy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah I'll run and see

github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
go.opencensus.io v0.24.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 h1:e/5i7d4oYZ+C
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399/go.mod h1:LdwHTNJT99C5fTAzDz0ud328OgXz+gierycbcIx2fRs=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
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/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
Expand Down
Loading