From 5fc783fd83837094c6a3d21bc1574499e947c35f Mon Sep 17 00:00:00 2001 From: chaosinthecrd Date: Mon, 11 Dec 2023 17:28:04 +0000 Subject: [PATCH] added flag for defining the pom file location --- attestation/maven/maven.go | 14 +++++----- attestation/maven/maven_test.go | 45 +++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/attestation/maven/maven.go b/attestation/maven/maven.go index ae114503..57c93840 100644 --- a/attestation/maven/maven.go +++ b/attestation/maven/maven.go @@ -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 }, ), ) @@ -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 { diff --git a/attestation/maven/maven_test.go b/attestation/maven/maven_test.go index ab1df6b9..0377e782 100644 --- a/attestation/maven/maven_test.go +++ b/attestation/maven/maven_test.go @@ -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 @@ -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 = `