gittuf/pkg/gitinterface/tag.go
Paulo Gomes 9e269f46fb
Introduce gitstore.Storer and make gittuf's core dependency-light
Restructure storage so gittuf verification can run over backends other
than the git binary (e.g. go-git):

- pkg/githash: concrete Git object hash, stdlib-only.
  gitinterface.Hash aliases it.
- pkg/gitstore: the single Storer interface (24 methods) that all
  storage consumers program against, plus the shared
  ErrReferenceNotFound sentinel. *gitinterface.Repository satisfies it
  structurally (compile-time asserted). Also defines ConfigKey, the
  canonical type for the Git config settings gittuf reads.
- pkg/rsl (from internal/rsl): entry model, codec, and readers over
  gitstore.Storer; zero gitinterface/sigstore dependencies. rsl.Hash
  aliases githash.Hash; nil is the unset-Hash sentinel and IsZero
  matches nil and empty as well as both format zeros (no
  object-format-unaware ZeroHash). Entry commits (empty tree on the
  RSL ref) are owned by the package; no storer adapter.
- internal/signerverifier/gitobject: verifies commit/tag signatures
  over (payload, signature) bytes, Rekor URL as an option. The storage
  half is Repository.GetObjectSignature. Removes sigstore, cosign, and
  gitsign from gitinterface's dependency tree.
- internal/propagation: propagation workflow, moved off pkg/rsl's
  public API (its tuf directive types are internal).
- internal/{attestations,cache,policy}: storage via gitstore.Storer;
  tree writing via WriteTree(blobs, subtrees).

Breaking changes to pkg/gitinterface: Repository.VerifySignature and
the verification sentinels are removed (use gitobject.Verify);
ErrReferenceNotFound now aliases gitstore's. Repository.GetGitConfig
(which returned the whole config map) is replaced by
LookupConfig(gitstore.ConfigKey), returning a single setting's value.
Policy resolves the Rekor override from git config once per
verification and extracts signed payloads once per object instead of
per key attempt.

Assisted-by: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Paulo Gomes <paulo@entire.io>
2026-08-03 21:27:49 +01:00

127 lines
3.3 KiB
Go

// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"errors"
"fmt"
"io"
"strings"
"github.com/gittuf/gittuf/pkg/gitstore"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/object"
"github.com/go-git/go-git/v6/storage/memory"
)
var ErrTagAlreadyExists = errors.New("tag already exists")
// TagUsingSpecificKey creates a Git tag signed using the specified, PEM encoded
// SSH or GPG key. It is primarily intended for use with testing. As of now,
// gittuf is not expected to be used to create tags in developer workflows,
// though this may change with command compatibility.
func (r *Repository) TagUsingSpecificKey(target Hash, name, message string, signingKeyPEMBytes []byte) (Hash, error) {
userName, _, err := r.LookupConfig(gitstore.ConfigUserName)
if err != nil {
return ZeroHash, err
}
userEmail, _, err := r.LookupConfig(gitstore.ConfigUserEmail)
if err != nil {
return ZeroHash, err
}
goGitRepo, err := r.GetGoGitRepository()
if err != nil {
return ZeroHash, err
}
targetObj, err := goGitRepo.Object(plumbing.AnyObject, plumbing.NewHash(target.String()))
if err != nil {
return ZeroHash, err
}
if !strings.HasSuffix(message, "\n") {
message += "\n"
}
tag := &object.Tag{
Name: name,
Tagger: object.Signature{
Name: userName,
Email: userEmail,
When: r.clock.Now(),
},
Message: message,
TargetType: targetObj.Type(),
Target: targetObj.ID(),
}
tagContents, err := getTagBytesWithoutSignature(tag)
if err != nil {
return ZeroHash, err
}
signature, err := signGitObjectUsingKey(tagContents, signingKeyPEMBytes)
if err != nil {
return ZeroHash, err
}
// Git appends tag signatures to the tag payload regardless of the object
// format; only commits store the signature under a header named for the
// hash algorithm (`gpgsig` / `gpgsig-sha256`).
tag.Signature = signature
obj := goGitRepo.Storer.NewEncodedObject()
if err := tag.Encode(obj); err != nil {
return ZeroHash, err
}
tagID, err := goGitRepo.Storer.SetEncodedObject(obj)
if err != nil {
return ZeroHash, err
}
tagIDHash, err := NewHash(tagID.String())
if err != nil {
return ZeroHash, err
}
return tagIDHash, r.SetReference(TagReferenceName(name), tagIDHash)
}
// GetTagTarget returns the ID of the Git object a tag points to.
func (r *Repository) GetTagTarget(tagID Hash) (Hash, error) {
targetID, err := r.executor("rev-list", "-n", "1", tagID.String()).executeString()
if err != nil {
return ZeroHash, fmt.Errorf("unable to resolve tag's target ID: %w", err)
}
hash, err := NewHash(targetID)
if err != nil {
return ZeroHash, fmt.Errorf("invalid format for target ID: %w", err)
}
return hash, nil
}
func (r *Repository) ensureIsTag(tagID Hash) error {
objType, err := r.executor("cat-file", "-t", tagID.String()).executeString()
if err != nil {
return fmt.Errorf("unable to inspect if object is tag: %w", err)
} else if objType != "tag" {
return fmt.Errorf("requested Git ID '%s' is not a tag object", tagID.String())
}
return nil
}
func getTagBytesWithoutSignature(tag *object.Tag) ([]byte, error) {
tagEncoded := memory.NewStorage().NewEncodedObject()
if err := tag.EncodeWithoutSignature(tagEncoded); err != nil {
return nil, err
}
r, err := tagEncoded.Reader()
if err != nil {
return nil, err
}
return io.ReadAll(r)
}