Compare commits
11 Commits
e27f8a6c3b
...
386b22efb8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
386b22efb8 | ||
|
|
77de489063 | ||
|
|
211f7e6f61 | ||
|
|
f68281d1ce | ||
|
|
655dfee585 | ||
|
|
da23fb774b | ||
|
|
2424be9bc6 | ||
|
|
a368d431f2 | ||
|
|
333dc8059c | ||
|
|
41c1c69f52 | ||
|
|
c8381792cf |
2
.gitignore
vendored
2
.gitignore
vendored
@ -3,8 +3,6 @@
|
|||||||
*.tmp
|
*.tmp
|
||||||
*.dockerimage
|
*.dockerimage
|
||||||
/vendor
|
/vendor
|
||||||
vendor.tzst
|
|
||||||
modcache.tzst
|
|
||||||
|
|
||||||
# Stale files
|
# Stale files
|
||||||
.drone.yml
|
.drone.yml
|
||||||
|
|||||||
@ -26,12 +26,6 @@ func (mfa *CLIApp) generateManifestOperation(ctx *cli.Context) error {
|
|||||||
Fs: mfa.Fs,
|
Fs: mfa.Fs,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set seed for deterministic UUID if provided
|
|
||||||
if seed := ctx.String("seed"); seed != "" {
|
|
||||||
opts.Seed = seed
|
|
||||||
log.Infof("using deterministic seed for manifest UUID")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up signing options if sign-key is provided
|
// Set up signing options if sign-key is provided
|
||||||
if signKey := ctx.String("sign-key"); signKey != "" {
|
if signKey := ctx.String("sign-key"); signKey != "" {
|
||||||
opts.SigningOptions = &mfer.SigningOptions{
|
opts.SigningOptions = &mfer.SigningOptions{
|
||||||
|
|||||||
@ -154,11 +154,6 @@ func (mfa *CLIApp) run(args []string) {
|
|||||||
Usage: "GPG key ID to sign the manifest with",
|
Usage: "GPG key ID to sign the manifest with",
|
||||||
EnvVars: []string{"MFER_SIGN_KEY"},
|
EnvVars: []string{"MFER_SIGN_KEY"},
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
|
||||||
Name: "seed",
|
|
||||||
Usage: "Seed value for deterministic manifest UUID",
|
|
||||||
EnvVars: []string{"MFER_SEED"},
|
|
||||||
},
|
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "include-timestamps",
|
Name: "include-timestamps",
|
||||||
Usage: "Include createdAt timestamp in manifest (omitted by default for determinism)",
|
Usage: "Include createdAt timestamp in manifest (omitted by default for determinism)",
|
||||||
|
|||||||
@ -90,15 +90,6 @@ type Builder struct {
|
|||||||
createdAt time.Time
|
createdAt time.Time
|
||||||
includeTimestamps bool
|
includeTimestamps bool
|
||||||
signingOptions *SigningOptions
|
signingOptions *SigningOptions
|
||||||
fixedUUID []byte // if set, use this UUID instead of generating one
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetSeed derives a deterministic UUID from the given seed string.
|
|
||||||
// The seed is hashed once with SHA-256 and the first 16 bytes are used
|
|
||||||
// as a fixed UUID for the manifest.
|
|
||||||
func (b *Builder) SetSeed(seed string) {
|
|
||||||
hash := sha256.Sum256([]byte(seed))
|
|
||||||
b.fixedUUID = hash[:16]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuilder creates a new Builder.
|
// NewBuilder creates a new Builder.
|
||||||
@ -241,7 +232,7 @@ func (b *Builder) Build(w io.Writer) error {
|
|||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
defer b.mu.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
// Sort files by path for deterministic output
|
// Sort files by path for deterministic output (#23)
|
||||||
sort.Slice(b.files, func(i, j int) bool {
|
sort.Slice(b.files, func(i, j int) bool {
|
||||||
return b.files[i].Path < b.files[j].Path
|
return b.files[i].Path < b.files[j].Path
|
||||||
})
|
})
|
||||||
@ -259,7 +250,6 @@ func (b *Builder) Build(w io.Writer) error {
|
|||||||
m := &manifest{
|
m := &manifest{
|
||||||
pbInner: inner,
|
pbInner: inner,
|
||||||
signingOptions: b.signingOptions,
|
signingOptions: b.signingOptions,
|
||||||
fixedUUID: b.fixedUUID,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate outer wrapper
|
// Generate outer wrapper
|
||||||
|
|||||||
@ -115,52 +115,51 @@ func TestNewTimestampFromTimeExtremeDate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuilderDeterministicOutput(t *testing.T) {
|
func TestBuilderBuildDeterministicOrder(t *testing.T) {
|
||||||
buildManifest := func() []byte {
|
// Regression test for #23: files should be sorted by path in the manifest
|
||||||
|
// to ensure deterministic output regardless of insertion order.
|
||||||
|
buildManifest := func(paths []string) []byte {
|
||||||
b := NewBuilder()
|
b := NewBuilder()
|
||||||
// Use a fixed createdAt and UUID so output is reproducible
|
for _, p := range paths {
|
||||||
b.createdAt = time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
|
content := []byte("content of " + p)
|
||||||
b.fixedUUID = make([]byte, 16) // all zeros
|
reader := bytes.NewReader(content)
|
||||||
|
_, err := b.AddFile(RelFilePath(p), FileSize(len(content)), ModTime(time.Now()), reader, nil)
|
||||||
mtime := ModTime(time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC))
|
|
||||||
|
|
||||||
// Add files in reverse order to test sorting
|
|
||||||
files := []struct {
|
|
||||||
path string
|
|
||||||
content string
|
|
||||||
}{
|
|
||||||
{"c/file.txt", "content c"},
|
|
||||||
{"a/file.txt", "content a"},
|
|
||||||
{"b/file.txt", "content b"},
|
|
||||||
}
|
|
||||||
for _, f := range files {
|
|
||||||
r := bytes.NewReader([]byte(f.content))
|
|
||||||
_, err := b.AddFile(RelFilePath(f.path), FileSize(len(f.content)), mtime, r, nil)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := b.Build(&buf)
|
require.NoError(t, b.Build(&buf))
|
||||||
require.NoError(t, err)
|
|
||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
out1 := buildManifest()
|
// Build with files in two different orders
|
||||||
out2 := buildManifest()
|
order1 := []string{"z.txt", "a.txt", "m/b.txt", "m/a.txt", "b.txt"}
|
||||||
assert.Equal(t, out1, out2, "two builds with same input should produce byte-identical output")
|
order2 := []string{"b.txt", "m/a.txt", "a.txt", "z.txt", "m/b.txt"}
|
||||||
|
|
||||||
|
manifest1 := buildManifest(order1)
|
||||||
|
manifest2 := buildManifest(order2)
|
||||||
|
|
||||||
|
// Parse both and verify file order is sorted
|
||||||
|
m1, err := NewManifestFromReader(bytes.NewReader(manifest1))
|
||||||
|
require.NoError(t, err)
|
||||||
|
m2, err := NewManifestFromReader(bytes.NewReader(manifest2))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
files1 := m1.Files()
|
||||||
|
files2 := m2.Files()
|
||||||
|
require.Len(t, files1, 5)
|
||||||
|
require.Len(t, files2, 5)
|
||||||
|
|
||||||
|
// Both should have same order
|
||||||
|
for i := range files1 {
|
||||||
|
assert.Equal(t, files1[i].Path, files2[i].Path, "file %d path mismatch", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSetSeedDeterministic(t *testing.T) {
|
// Verify the order is lexicographic
|
||||||
b1 := NewBuilder()
|
assert.Equal(t, "a.txt", files1[0].Path)
|
||||||
b1.SetSeed("test-seed-value")
|
assert.Equal(t, "b.txt", files1[1].Path)
|
||||||
b2 := NewBuilder()
|
assert.Equal(t, "m/a.txt", files1[2].Path)
|
||||||
b2.SetSeed("test-seed-value")
|
assert.Equal(t, "m/b.txt", files1[3].Path)
|
||||||
assert.Equal(t, b1.fixedUUID, b2.fixedUUID, "same seed should produce same UUID")
|
assert.Equal(t, "z.txt", files1[4].Path)
|
||||||
assert.Len(t, b1.fixedUUID, 16, "UUID should be 16 bytes")
|
|
||||||
|
|
||||||
b3 := NewBuilder()
|
|
||||||
b3.SetSeed("different-seed")
|
|
||||||
assert.NotEqual(t, b1.fixedUUID, b3.fixedUUID, "different seeds should produce different UUIDs")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatePath(t *testing.T) {
|
func TestValidatePath(t *testing.T) {
|
||||||
|
|||||||
@ -17,7 +17,6 @@ type manifest struct {
|
|||||||
pbOuter *MFFileOuter
|
pbOuter *MFFileOuter
|
||||||
output *bytes.Buffer
|
output *bytes.Buffer
|
||||||
signingOptions *SigningOptions
|
signingOptions *SigningOptions
|
||||||
fixedUUID []byte // if set, use this UUID instead of generating one
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *manifest) String() string {
|
func (m *manifest) String() string {
|
||||||
|
|||||||
@ -48,7 +48,6 @@ type ScannerOptions struct {
|
|||||||
IncludeTimestamps bool // Include createdAt timestamp in manifest (default: omit for determinism)
|
IncludeTimestamps bool // Include createdAt timestamp in manifest (default: omit for determinism)
|
||||||
Fs afero.Fs // Filesystem to use, defaults to OsFs if nil
|
Fs afero.Fs // Filesystem to use, defaults to OsFs if nil
|
||||||
SigningOptions *SigningOptions // GPG signing options (nil = no signing)
|
SigningOptions *SigningOptions // GPG signing options (nil = no signing)
|
||||||
Seed string // If set, derive a deterministic UUID from this seed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileEntry represents a file that has been enumerated.
|
// FileEntry represents a file that has been enumerated.
|
||||||
@ -281,9 +280,6 @@ func (s *Scanner) ToManifest(ctx context.Context, w io.Writer, progress chan<- S
|
|||||||
if s.options.SigningOptions != nil {
|
if s.options.SigningOptions != nil {
|
||||||
builder.SetSigningOptions(s.options.SigningOptions)
|
builder.SetSigningOptions(s.options.SigningOptions)
|
||||||
}
|
}
|
||||||
if s.options.Seed != "" {
|
|
||||||
builder.SetSeed(s.options.Seed)
|
|
||||||
}
|
|
||||||
|
|
||||||
var scannedFiles FileCount
|
var scannedFiles FileCount
|
||||||
var scannedBytes FileSize
|
var scannedBytes FileSize
|
||||||
|
|||||||
@ -49,13 +49,8 @@ func (m *manifest) generateOuter() error {
|
|||||||
return errors.New("internal error")
|
return errors.New("internal error")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use fixed UUID if provided, otherwise generate a new one
|
// Generate UUID and set on inner message
|
||||||
var manifestUUID uuid.UUID
|
manifestUUID := uuid.New()
|
||||||
if len(m.fixedUUID) == 16 {
|
|
||||||
copy(manifestUUID[:], m.fixedUUID)
|
|
||||||
} else {
|
|
||||||
manifestUUID = uuid.New()
|
|
||||||
}
|
|
||||||
m.pbInner.Uuid = manifestUUID[:]
|
m.pbInner.Uuid = manifestUUID[:]
|
||||||
|
|
||||||
innerData, err := proto.MarshalOptions{Deterministic: true}.Marshal(m.pbInner)
|
innerData, err := proto.MarshalOptions{Deterministic: true}.Marshal(m.pbInner)
|
||||||
|
|||||||
BIN
modcache.tzst
Normal file
BIN
modcache.tzst
Normal file
Binary file not shown.
BIN
vendor.tzst
Normal file
BIN
vendor.tzst
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user