1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package commit
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/catfile"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v14/streamio"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var gpgSiganturePrefix = []byte("gpgsig")
func (s *server) GetCommitSignatures(request *gitalypb.GetCommitSignaturesRequest, stream gitalypb.CommitService_GetCommitSignaturesServer) error {
if err := validateGetCommitSignaturesRequest(request); err != nil {
return status.Errorf(codes.InvalidArgument, "GetCommitSignatures: %v", err)
}
return s.getCommitSignatures(request, stream)
}
func (s *server) getCommitSignatures(request *gitalypb.GetCommitSignaturesRequest, stream gitalypb.CommitService_GetCommitSignaturesServer) error {
ctx := stream.Context()
repo := s.localrepo(request.GetRepository())
c, err := s.catfileCache.BatchProcess(ctx, repo)
if err != nil {
return helper.ErrInternal(err)
}
for _, commitID := range request.CommitIds {
commitObj, err := c.Commit(ctx, git.Revision(commitID))
if err != nil {
if catfile.IsNotFound(err) {
continue
}
return helper.ErrInternal(err)
}
signatureKey, commitText, err := extractSignature(commitObj.Reader)
if err != nil {
return helper.ErrInternal(err)
}
if err = sendResponse(commitID, signatureKey, commitText, stream); err != nil {
return helper.ErrInternal(err)
}
}
return nil
}
func extractSignature(reader io.Reader) ([]byte, []byte, error) {
commitText := []byte{}
signatureKey := []byte{}
sawSignature := false
inSignature := false
lineBreak := []byte("\n")
whiteSpace := []byte(" ")
bufferedReader := bufio.NewReader(reader)
for {
line, err := bufferedReader.ReadBytes('\n')
if err == io.EOF {
commitText = append(commitText, line...)
break
}
if err != nil {
return nil, nil, err
}
if !sawSignature && !inSignature && bytes.HasPrefix(line, gpgSiganturePrefix) {
sawSignature, inSignature = true, true
line = bytes.TrimPrefix(line, gpgSiganturePrefix)
}
if inSignature && !bytes.Equal(line, lineBreak) {
line = bytes.TrimPrefix(line, whiteSpace)
signatureKey = append(signatureKey, line...)
} else if inSignature {
inSignature = false
commitText = append(commitText, line...)
} else {
commitText = append(commitText, line...)
}
}
// Remove last line break from signature
signatureKey = bytes.TrimSuffix(signatureKey, lineBreak)
return signatureKey, commitText, nil
}
func sendResponse(commitID string, signatureKey []byte, commitText []byte, stream gitalypb.CommitService_GetCommitSignaturesServer) error {
if len(signatureKey) <= 0 {
return nil
}
err := stream.Send(&gitalypb.GetCommitSignaturesResponse{
CommitId: commitID,
Signature: signatureKey,
})
if err != nil {
return err
}
streamWriter := streamio.NewWriter(func(p []byte) error {
return stream.Send(&gitalypb.GetCommitSignaturesResponse{SignedText: p})
})
msgReader := bytes.NewReader(commitText)
_, err = io.Copy(streamWriter, msgReader)
if err != nil {
return fmt.Errorf("failed to send response: %v", err)
}
return nil
}
func validateGetCommitSignaturesRequest(request *gitalypb.GetCommitSignaturesRequest) error {
if request.GetRepository() == nil {
return errors.New("empty Repository")
}
if len(request.GetCommitIds()) == 0 {
return errors.New("empty CommitIds")
}
// Do not support shorthand or invalid commit SHAs
for _, commitID := range request.CommitIds {
if err := git.ValidateObjectID(commitID); err != nil {
return err
}
}
return nil
}
|