2020-02-15 22:13:50 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"dehub/accessctl"
|
|
|
|
"dehub/fs"
|
|
|
|
"dehub/sigcred"
|
|
|
|
"dehub/yamlutil"
|
2020-02-16 17:28:59 +00:00
|
|
|
"encoding"
|
2020-02-15 22:13:50 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/src-d/go-git.v4"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
|
|
|
"gopkg.in/src-d/go-git.v4/plumbing/object"
|
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
// ChangeCommit describes the structure of a change commit message.
|
|
|
|
type ChangeCommit struct {
|
2020-02-15 22:13:50 +00:00
|
|
|
Message string `yaml:"message"`
|
|
|
|
ChangeHash yamlutil.Blob `yaml:"change_hash"`
|
|
|
|
Credentials []sigcred.Credential `yaml:"credentials"`
|
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
type ccYAML struct {
|
|
|
|
Val ChangeCommit `yaml:",inline"`
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func msgHead(msg string) string {
|
|
|
|
i := strings.Index(msg, "\n")
|
|
|
|
if i > 0 {
|
|
|
|
return msg[:i]
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalText implements the encoding.TextMarshaler interface by returning the
|
2020-02-21 05:06:13 +00:00
|
|
|
// form the ChangeCommit object takes in the git commit message.
|
|
|
|
func (cc ChangeCommit) MarshalText() ([]byte, error) {
|
|
|
|
changeCommitEncoded, err := yaml.Marshal(ccYAML{cc})
|
2020-02-15 22:13:50 +00:00
|
|
|
if err != nil {
|
2020-02-21 05:06:13 +00:00
|
|
|
return nil, fmt.Errorf("failed to encode ChangeCommit message: %w", err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
fullMsg := msgHead(cc.Message) + "\n\n" + string(changeCommitEncoded)
|
2020-02-15 22:13:50 +00:00
|
|
|
return []byte(fullMsg), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements the encoding.TextUnmarshaler interface by decoding a
|
2020-02-21 05:06:13 +00:00
|
|
|
// ChangeCommit object which has been encoded into a git commit message.
|
|
|
|
func (cc *ChangeCommit) UnmarshalText(msg []byte) error {
|
2020-02-15 22:13:50 +00:00
|
|
|
i := bytes.Index(msg, []byte("\n"))
|
|
|
|
if i < 0 {
|
|
|
|
return fmt.Errorf("commit message %q is malformed", msg)
|
|
|
|
}
|
|
|
|
msgHead, msg := msg[:i], msg[i:]
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
var ccy ccYAML
|
|
|
|
if err := yaml.Unmarshal(msg, &ccy); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal ChangeCommit message: %w", err)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
*cc = ccy.Val
|
|
|
|
if !strings.HasPrefix(cc.Message, string(msgHead)) {
|
|
|
|
return errors.New("encoded ChangeCommit is malformed, it might not be an encoded ChangeCommit")
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-16 17:28:59 +00:00
|
|
|
// Commit uses the given TextMarshaler to create a git commit object (with the
|
|
|
|
// specified accountID as the author) and commits it to the current HEAD,
|
|
|
|
// returning the hash of the commit.
|
|
|
|
func (r *Repo) Commit(m encoding.TextMarshaler, accountID string) (plumbing.Hash, error) {
|
|
|
|
msgB, err := m.MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
return plumbing.ZeroHash, fmt.Errorf("error marshaling %T to string: %v", m, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := r.GitRepo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return plumbing.ZeroHash, fmt.Errorf("could not get git worktree: %w", err)
|
|
|
|
}
|
|
|
|
return w.Commit(string(msgB), &git.CommitOptions{
|
|
|
|
Author: &object.Signature{
|
|
|
|
Name: accountID,
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-22 00:37:19 +00:00
|
|
|
// HasStagedChanges returns true if there are file changes which have been
|
|
|
|
// staged (e.g. via "git add").
|
|
|
|
func (r *Repo) HasStagedChanges() (bool, error) {
|
|
|
|
w, err := r.GitRepo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving worktree: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
status, err := w.Status()
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error retrieving worktree status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var any bool
|
|
|
|
for _, fileStatus := range status {
|
|
|
|
if fileStatus.Staging != git.Unmodified {
|
|
|
|
any = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return any, nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
// NewChangeCommit constructs a ChangeCommit. If sig is given then it is used to
|
|
|
|
// create a Credential for the ChangeCommit.
|
2020-02-21 05:06:13 +00:00
|
|
|
func (r *Repo) NewChangeCommit(msg, accountID string, sig sigcred.SignifierInterface) (ChangeCommit, error) {
|
2020-02-15 22:13:50 +00:00
|
|
|
_, headTree, err := r.head()
|
|
|
|
if errors.Is(err, plumbing.ErrReferenceNotFound) {
|
|
|
|
headTree = &object.Tree{}
|
|
|
|
} else if err != nil {
|
2020-02-21 05:06:13 +00:00
|
|
|
return ChangeCommit{}, err
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, stagedTree, err := fs.FromStagedChangesTree(r.GitRepo)
|
|
|
|
if err != nil {
|
2020-02-21 05:06:13 +00:00
|
|
|
return ChangeCommit{}, err
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
changeHash := genChangeHash(nil, msg, headTree, stagedTree)
|
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
var creds []sigcred.Credential
|
|
|
|
if sig != nil {
|
|
|
|
// this is necessarily different than headTree for the case of there
|
|
|
|
// being no HEAD (ie it's the first commit). In that case we want
|
|
|
|
// headTree to be empty (because it's being used to generate the change
|
|
|
|
// hash), but we want the signifier to use the raw fs (because that's
|
|
|
|
// where the signifier's data might be).
|
|
|
|
sigFS, err := r.headOrRawFS()
|
|
|
|
if err != nil {
|
|
|
|
return ChangeCommit{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, err := sig.Sign(sigFS, changeHash)
|
|
|
|
if err != nil {
|
|
|
|
return ChangeCommit{}, fmt.Errorf("failed to sign commit hash: %w", err)
|
|
|
|
}
|
|
|
|
cred.AccountID = accountID
|
|
|
|
creds = append(creds, cred)
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
return ChangeCommit{
|
2020-02-15 22:13:50 +00:00
|
|
|
Message: msg,
|
|
|
|
ChangeHash: changeHash,
|
2020-02-29 20:02:25 +00:00
|
|
|
Credentials: creds,
|
2020-02-16 17:28:59 +00:00
|
|
|
}, nil
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repo) assertAccessControls(
|
2020-02-29 20:02:25 +00:00
|
|
|
accessCtls []accessctl.BranchAccessControl, creds []sigcred.Credential,
|
|
|
|
branch plumbing.ReferenceName, from, to *object.Tree,
|
2020-02-15 22:13:50 +00:00
|
|
|
) error {
|
|
|
|
filesChanged, err := calcDiff(from, to)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pathsChanged := make([]string, len(filesChanged))
|
|
|
|
for i := range filesChanged {
|
|
|
|
pathsChanged[i] = filesChanged[i].path
|
|
|
|
}
|
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
matchRes, err := accessctl.Match(accessCtls, accessctl.MatchInteractions{
|
|
|
|
Branch: branch.Short(),
|
|
|
|
FilePathsChanged: pathsChanged,
|
|
|
|
})
|
2020-02-15 22:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not determine applicable access controls: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-29 20:02:25 +00:00
|
|
|
for _, matchedAC := range matchRes.ChangeAccessControls {
|
|
|
|
ac := matchedAC.ChangeAccessControl
|
|
|
|
condInt, err := ac.Condition.Interface()
|
2020-02-15 22:13:50 +00:00
|
|
|
if err != nil {
|
2020-02-29 20:02:25 +00:00
|
|
|
return fmt.Errorf("could not cast Condition of file path pattern %q to interface: %w",
|
|
|
|
ac.FilePathPattern, err)
|
2020-02-15 22:13:50 +00:00
|
|
|
} else if err := condInt.Satisfied(creds); err != nil {
|
2020-02-29 20:02:25 +00:00
|
|
|
return fmt.Errorf("access control of file path pattern %q not satisfied: %w\nFiles matched:\n%s",
|
|
|
|
ac.FilePathPattern, err, strings.Join(matchedAC.FilePaths, "\n"))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
// VerifyChangeCommit verifies that the change commit at the given hash, which
|
2020-02-29 20:02:25 +00:00
|
|
|
// is presumably on the given branch, is gucci.
|
|
|
|
func (r *Repo) VerifyChangeCommit(branch plumbing.ReferenceName, h plumbing.Hash) error {
|
2020-02-15 22:13:50 +00:00
|
|
|
commit, err := r.GitRepo.CommitObject(h)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not retrieve commit object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
commitTree, err := r.GitRepo.TreeObject(commit.TreeHash)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not retrieve tree object: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
var changeCommit ChangeCommit
|
|
|
|
if err := changeCommit.UnmarshalText([]byte(commit.Message)); err != nil {
|
2020-02-15 22:13:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sigTree := commitTree // only for root commit
|
|
|
|
parentTree := &object.Tree{}
|
|
|
|
if commit.NumParents() > 0 {
|
|
|
|
parent, err := commit.Parent(0)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not retrieve parent of commit: %w", err)
|
|
|
|
} else if parentTree, err = r.GitRepo.TreeObject(parent.TreeHash); err != nil {
|
|
|
|
return fmt.Errorf("could not retrieve tree object of parent %q: %w", parent.Hash, err)
|
|
|
|
}
|
|
|
|
sigTree = parentTree
|
|
|
|
}
|
|
|
|
sigFS := fs.FromTree(sigTree)
|
|
|
|
|
|
|
|
cfg, err := r.loadConfig(sigFS)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error loading config: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.assertAccessControls(
|
2020-02-21 05:06:13 +00:00
|
|
|
cfg.AccessControls, changeCommit.Credentials,
|
2020-02-29 20:02:25 +00:00
|
|
|
branch, parentTree, commitTree,
|
2020-02-15 22:13:50 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to satisfy all access controls: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
expectedChangeHash := genChangeHash(nil, changeCommit.Message, parentTree, commitTree)
|
|
|
|
if !bytes.Equal(changeCommit.ChangeHash, expectedChangeHash) {
|
2020-02-15 22:13:50 +00:00
|
|
|
return fmt.Errorf("malformed change_hash in commit body, is %s but should be %s",
|
|
|
|
base64.StdEncoding.EncodeToString(expectedChangeHash),
|
2020-02-21 05:06:13 +00:00
|
|
|
base64.StdEncoding.EncodeToString(changeCommit.ChangeHash))
|
2020-02-15 22:13:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 05:06:13 +00:00
|
|
|
for _, cred := range changeCommit.Credentials {
|
2020-02-15 22:13:50 +00:00
|
|
|
sig, err := r.signifierForCredential(sigFS, cred)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error finding signifier for credential %+v: %w", cred, err)
|
|
|
|
} else if err := sig.Verify(sigFS, expectedChangeHash, cred); err != nil {
|
|
|
|
return fmt.Errorf("error verifying credential %+v: %w", cred, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|