gittuf/pkg/gitinterface/common.go
Paulo Gomes 5f5f5508e3
feat(sha256): support SHA-256 object format
Make gittuf hash-algorithm aware so it operates on SHA-256 repositories:

- gitinterface.Repository detects its object format (via
  `git rev-parse --show-object-format`) and exposes GetObjectFormat and a
  per-repo ZeroHash. GetReference returns the format-appropriate zero hash
  so ref updates to new refs succeed in SHA-256 repositories.
- Commit and tag signing/verification use the `gpgsig-sha256` header for
  SHA-256 objects (go-git's SignatureSHA256), matching Git's behavior. The
  verification field is chosen by the object's OID length. Fixes verify-ref
  failing on Git-signed SHA-256 RSL entries.
- git-remote-gittuf negotiates object-format from the repository instead of
  hardcoding sha1, and uses the repo-appropriate zero hash for ref tips.
- Test helpers can create SHA-256 repositories; gitinterface, rsl, policy,
  and experimental/gittuf suites now run under both formats, plus an
  end-to-end Git-signed record+verify test.

Assisted-by: Claude Opus 4.7 <noreply@anthropic.com>
Signed-off-by: Paulo Gomes <paulo@entire.io>
2026-07-16 23:03:18 +01:00

160 lines
4 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"os"
"os/exec"
"path/filepath"
"testing"
"time"
artifacts "github.com/gittuf/gittuf/internal/testartifacts"
"github.com/jonboulle/clockwork"
)
// ObjectFormat identifies the hash algorithm a Git repository uses for its
// object IDs.
type ObjectFormat string
const (
ObjectFormatSHA1 ObjectFormat = "sha1"
ObjectFormatSHA256 ObjectFormat = "sha256"
)
const (
testName = "Jane Doe"
testEmail = "jane.doe@example.com"
)
var (
testClock = clockwork.NewFakeClockAt(time.Date(1995, time.October, 26, 9, 0, 0, 0, time.UTC))
)
type testRepositoryOptions struct {
objectFormat ObjectFormat
}
// TestRepositoryOption configures the repository created by
// CreateTestGitRepository.
type TestRepositoryOption func(o *testRepositoryOptions)
// WithObjectFormat creates the test repository using the specified object
// format (hash algorithm).
func WithObjectFormat(objectFormat ObjectFormat) TestRepositoryOption {
return func(o *testRepositoryOptions) {
o.objectFormat = objectFormat
}
}
// WithSHA256Format creates the test repository using the SHA-256 object
// format.
func WithSHA256Format() TestRepositoryOption {
return WithObjectFormat(ObjectFormatSHA256)
}
// CreateTestGitRepository creates a Git repository in the specified directory,
// using the SHA-1 object format unless overridden via options. This is meant
// to be used by tests across gittuf packages. This helper also sets up an
// SSH RSA signing key that can be used to create reproducible commits.
func CreateTestGitRepository(t *testing.T, dir string, bare bool, opts ...TestRepositoryOption) *Repository {
t.Helper()
repo, err := createTestGitRepository(dir, t.TempDir(), bare, opts...)
if err != nil {
t.Fatal(err)
}
return repo
}
func createTestGitRepository(dir, signingKeysDir string, bare bool, opts ...TestRepositoryOption) (*Repository, error) {
options := &testRepositoryOptions{objectFormat: ObjectFormatSHA1}
for _, fn := range opts {
fn(options)
}
repo, err := newTestRepository(dir, bare, options.objectFormat)
if err != nil {
return nil, err
}
// Set up author / committer identity
if err := repo.SetGitConfig("user.name", testName); err != nil {
return nil, err
}
if err := repo.SetGitConfig("user.email", testEmail); err != nil {
return nil, err
}
// Set up signing via SSH key
if err := writeSigningKeys(signingKeysDir); err != nil {
return nil, err
}
if err := repo.SetGitConfig("user.signingkey", filepath.Join(signingKeysDir, "key.pub")); err != nil {
return nil, err
}
if err := repo.SetGitConfig("gpg.format", "ssh"); err != nil {
return nil, err
}
return repo, nil
}
func setupRepository(t *testing.T, dir string, bare bool, objectFormat ObjectFormat) *Repository {
t.Helper()
repo, err := newTestRepository(dir, bare, objectFormat)
if err != nil {
t.Fatal(err)
}
return repo
}
func newTestRepository(dir string, bare bool, objectFormat ObjectFormat) (*Repository, error) {
var gitDirPath string
args := []string{"init"}
if bare {
args = append(args, "--bare")
gitDirPath = dir
} else {
gitDirPath = filepath.Join(dir, ".git")
}
args = append(args, "--object-format", string(objectFormat))
args = append(args, "-b", "main")
args = append(args, dir)
cmd := exec.Command(binary, args...)
if err := cmd.Run(); err != nil {
return nil, err
}
repo := &Repository{gitDirPath: gitDirPath, clock: testClock}
format, err := repo.readObjectFormat()
if err != nil {
return nil, err
}
repo.objectFormat = format
return repo, nil
}
func writeSigningKeys(dir string) error {
sshPrivateKey := artifacts.SSHRSAPrivate
sshPublicKey := artifacts.SSHRSAPublicSSH
privateKeyPath := filepath.Join(dir, "key")
publicKeyPath := filepath.Join(dir, "key.pub")
if err := os.WriteFile(privateKeyPath, sshPrivateKey, 0o600); err != nil {
return err
}
if err := os.WriteFile(publicKeyPath, sshPublicKey, 0o600); err != nil {
return err
}
return nil
}