Skip to content

Commit

Permalink
added flag for defining the pom file location
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaosInTheCRD authored and jkjell committed Dec 11, 2023
1 parent 801d811 commit f50bf3e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
14 changes: 7 additions & 7 deletions attestation/maven/maven.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ func init() {
return New()
},
registry.StringConfigOption(
"include-glob",
"pom-path",
fmt.Sprintf("The path to the Project Object Model (POM) XML file used for task being attested (default \"%s\").", defaultPomPath),
defaultPomPath,
func(a attestation.Attestor, includeGlob string) (attestation.Attestor, error) {
prodAttestor, ok := a.(*Attestor)
func(a attestation.Attestor, pomPath string) (attestation.Attestor, error) {
mavAttestor, ok := a.(*Attestor)
if !ok {
return a, fmt.Errorf("unexpected attestor type: %T is not a product attestor", a)
return a, fmt.Errorf("unexpected attestor type: %T is not a maven attestor", a)
}

WithIncludeGlob(includeGlob)(prodAttestor)
return prodAttestor, nil
WithPom(pomPath)(mavAttestor)
return mavAttestor, nil
},
),
)
Expand Down Expand Up @@ -90,7 +90,7 @@ func WithPom(path string) Option {

func New(opts ...Option) *Attestor {
attestor := &Attestor{
pomPath: "pom.xml",
pomPath: defaultPomPath,
}

for _, opt := range opts {
Expand Down
45 changes: 35 additions & 10 deletions attestation/maven/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testifysec/go-witness/attestation"
)

func writeTempPomXml(t *testing.T) (string, error) {
func writeTempPomXml(t *testing.T, path string) (string, error) {
tmpDir := t.TempDir()
pomPath := filepath.Join(tmpDir, "pom.xml")
pomPath := filepath.Join(tmpDir, path)
file, err := os.Create(pomPath)
if err != nil {
return "", err
Expand All @@ -41,13 +40,39 @@ func writeTempPomXml(t *testing.T) (string, error) {
}

func TestMaven(t *testing.T) {
pomPath, err := writeTempPomXml(t)
require.NoError(t, err)
attestor := New(WithPom(pomPath))
ctx, err := attestation.NewContext([]attestation.Attestor{attestor})
require.NoError(t, err)
err = attestor.Attest(ctx)
assert.NoError(t, err)
workingDir := t.TempDir()

tests := []struct {
name string
pomPath string
}{
{"no pom specified", ""},
{"regular pom with custom name", "custom-pom.xml"},
{"effective pom", "effective-pom.xml"},
}

for _, test := range tests {
var p string
var err error
if test.pomPath != "" {
p, err = writeTempPomXml(t, test.pomPath)
if err != nil {
t.Fatal(err)
}
} else {
p, err = writeTempPomXml(t, "pom.xml")
if err != nil {
t.Fatal(err)
}
}

t.Run(test.name, func(t *testing.T) {
ctx, err := attestation.NewContext([]attestation.Attestor{}, attestation.WithWorkingDir(workingDir))
require.NoError(t, err)
a := New(WithPom(p))
require.NoError(t, a.Attest(ctx))
})
}
}

const testPomXml = `<?xml version="1.0" encoding="UTF-8"?>
Expand Down

0 comments on commit f50bf3e

Please sign in to comment.