Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from 4 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
29 changes: 9 additions & 20 deletions 29 pkg/cmd/attestation/verification/attestation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package verification

import (
"bufio"
"bytes"
"encoding/json"
"errors"
Expand Down Expand Up @@ -76,35 +75,25 @@ func loadBundleFromJSONFile(path string) ([]*api.Attestation, error) {
}

func loadBundlesFromJSONLinesFile(path string) ([]*api.Attestation, error) {
file, err := os.Open(path)
fileContent, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("could not open file: %v", err)
return nil, fmt.Errorf("could not read file: %v", err)
}
defer file.Close()

attestations := []*api.Attestation{}
attestations := []*api.Attestation{}

decoder := json.NewDecoder(bytes.NewReader(fileContent))

reader := bufio.NewReader(file)

var line []byte
line, err = reader.ReadBytes('\n')
for err == nil {
if len(bytes.TrimSpace(line)) == 0 {
line, err = reader.ReadBytes('\n')
continue
}
for decoder.More() {
var bundle bundle.ProtobufBundle
bundle.Bundle = new(protobundle.Bundle)
err = bundle.UnmarshalJSON(line)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal bundle from JSON: %v", err)
if err := decoder.Decode(&bundle); err != nil {
return nil, fmt.Errorf("failed to unmarshal bundle from JSON: %v", err)
}
a := api.Attestation{Bundle: &bundle}
attestations = append(attestations, &a)

line, err = reader.ReadBytes('\n')
}

return attestations, nil
}

Expand Down
31 changes: 27 additions & 4 deletions 31 pkg/cmd/attestation/verification/attestation_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package verification

import (
"os"
"path/filepath"
"testing"

protobundle "github.com/sigstore/protobuf-specs/gen/pb-go/bundle/v1"
Expand All @@ -12,11 +14,32 @@ import (
)

func TestLoadBundlesFromJSONLinesFile(t *testing.T) {
path := "../test/data/sigstore-js-2.1.0_with_2_bundles.jsonl"
attestations, err := loadBundlesFromJSONLinesFile(path)
t.Run("with original file", func(t *testing.T) {
path := "../test/data/sigstore-js-2.1.0_with_2_bundles.jsonl"
attestations, err := loadBundlesFromJSONLinesFile(path)
require.NoError(t, err)
require.Len(t, attestations, 2)
})

require.NoError(t, err)
require.Len(t, attestations, 2)
t.Run("with extra lines", func(t *testing.T) {
// Create a temporary file with extra lines
tempDir := t.TempDir()
tempFile := filepath.Join(tempDir, "test_with_extra_lines.jsonl")

originalContent, err := os.ReadFile("../test/data/sigstore-js-2.1.0_with_2_bundles.jsonl")
require.NoError(t, err)

extraLines := []byte("\n\n{\"invalid\": \"json\"}\n\n")
aryanbhosale marked this conversation as resolved.
Outdated
Show resolved Hide resolved
newContent := append(originalContent, extraLines...)

err = os.WriteFile(tempFile, newContent, 0644)
require.NoError(t, err)

// Test the function with the new file
attestations, err := loadBundlesFromJSONLinesFile(tempFile)
require.NoError(t, err)
require.Len(t, attestations, 2, "Should still load 2 valid attestations")
})
}

func TestLoadBundleFromJSONFile(t *testing.T) {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.