-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathgrpc_asset.go
225 lines (181 loc) · 6.02 KB
/
grpc_asset.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package server
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"io"
"net/http"
"net/url"
"strings"
"google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
grpc_status "google.golang.org/grpc/status"
asset "github.com/buchgr/bazel-remote/v2/genproto/build/bazel/remote/asset/v1"
pb "github.com/buchgr/bazel-remote/v2/genproto/build/bazel/remote/execution/v2"
"github.com/buchgr/bazel-remote/v2/cache"
)
// FetchServer implementation
var errNilFetchBlobRequest = grpc_status.Error(codes.InvalidArgument,
"expected a non-nil *FetchBlobRequest")
func (s *grpcServer) FetchBlob(ctx context.Context, req *asset.FetchBlobRequest) (*asset.FetchBlobResponse, error) {
var sha256Str string
// Q: which combinations of qualifiers to support?
// * simple file, identified by sha256 SRI AND/OR recognisable URL
// * git repository, identified by ???
// * go repository, identified by tag/branch/???
// "strong" identifiers:
// checksum.sri -> direct lookup for sha256 (easy), indirect lookup for
// others (eg sha256 of the SRI hash).
// vcs.commit + .git extension -> indirect lookup? or sha1 lookup?
// But this could waste a lot of space.
//
// "weak" identifiers:
// vcs.branch + .git extension -> indirect lookup, with timeout check
// directory: limit one of the vcs.* returns
// insert to tree into the CAS?
//
// git archive --format=tar --remote=http://foo/bar.git ref dir...
// For TTL items, we need another (persistent) index, eg BadgerDB?
// key -> CAS sha256 + timestamp
// Should we place a limit on the size of the index?
if req == nil {
return nil, errNilFetchBlobRequest
}
headers := http.Header{}
for _, q := range req.GetQualifiers() {
if q == nil {
return &asset.FetchBlobResponse{
Status: &status.Status{
Code: int32(codes.InvalidArgument),
Message: "unexpected nil qualifier in FetchBlobRequest",
},
}, nil
}
const QualifierHTTPHeaderPrefix = "http_header:"
if strings.HasPrefix(q.Name, QualifierHTTPHeaderPrefix) {
key := q.Name[len(QualifierHTTPHeaderPrefix):]
headers[key] = strings.Split(q.Value, ",")
continue
}
if q.Name == "checksum.sri" && strings.HasPrefix(q.Value, "sha256-") {
// Ref: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
b64hash := strings.TrimPrefix(q.Value, "sha256-")
decoded, err := base64.StdEncoding.DecodeString(b64hash)
if err != nil {
s.errorLogger.Printf("failed to base64 decode \"%s\": %v",
b64hash, err)
continue
}
sha256Str = hex.EncodeToString(decoded)
found, size := s.cache.Contains(ctx, cache.CAS, sha256Str, -1)
if !found {
continue
}
if size < 0 {
// We don't know the size yet (bad http backend?).
r, actualSize, err := s.cache.Get(ctx, cache.CAS, sha256Str, -1, 0)
if r != nil {
defer r.Close()
}
if err != nil || actualSize < 0 {
s.errorLogger.Printf("failed to get CAS %s from proxy backend size: %d err: %v",
sha256Str, actualSize, err)
continue
}
size = actualSize
}
return &asset.FetchBlobResponse{
Status: &status.Status{Code: int32(codes.OK)},
BlobDigest: &pb.Digest{
Hash: sha256Str,
SizeBytes: size,
},
}, nil
}
}
// Cache miss.
// See if we can download one of the URIs.
for _, uri := range req.GetUris() {
ok, actualHash, size := s.fetchItem(ctx, uri, headers, sha256Str)
if ok {
return &asset.FetchBlobResponse{
Status: &status.Status{Code: int32(codes.OK)},
BlobDigest: &pb.Digest{
Hash: actualHash,
SizeBytes: size,
},
Uri: uri,
}, nil
}
// Not a simple file. Not yet handled...
}
return &asset.FetchBlobResponse{
Status: &status.Status{Code: int32(codes.NotFound)},
}, nil
}
func (s *grpcServer) fetchItem(ctx context.Context, uri string, headers http.Header, expectedHash string) (bool, string, int64) {
u, err := url.Parse(uri)
if err != nil {
s.errorLogger.Printf("unable to parse URI: %s err: %v", uri, err)
return false, "", int64(-1)
}
if u.Scheme != "http" && u.Scheme != "https" {
s.errorLogger.Printf("unsupported URI: %s", uri)
return false, "", int64(-1)
}
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
s.errorLogger.Printf("failed to create http.Request: %s err: %v", uri, err)
return false, "", int64(-1)
}
req.Header = headers
resp, err := http.DefaultClient.Do(req)
if err != nil {
s.errorLogger.Printf("failed to get URI: %s err: %v", uri, err)
return false, "", int64(-1)
}
defer resp.Body.Close()
rc := resp.Body
s.accessLogger.Printf("GRPC ASSET FETCH %s %s", uri, resp.Status)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return false, "", int64(-1)
}
expectedSize := resp.ContentLength
if expectedHash == "" || expectedSize < 0 {
// We can't call Put until we know the hash and size.
data, err := io.ReadAll(resp.Body)
if err != nil {
s.errorLogger.Printf("failed to read data: %v", uri)
return false, "", int64(-1)
}
expectedSize = int64(len(data))
hashBytes := sha256.Sum256(data)
hashStr := hex.EncodeToString(hashBytes[:])
if expectedHash != "" && hashStr != expectedHash {
s.errorLogger.Printf("URI data has hash %s, expected %s",
hashStr, expectedHash)
return false, "", int64(-1)
}
expectedHash = hashStr
rc = io.NopCloser(bytes.NewReader(data))
}
err = s.cache.Put(ctx, cache.CAS, expectedHash, expectedSize, rc)
if err != nil && err != io.EOF {
s.errorLogger.Printf("failed to Put %s: %v", expectedHash, err)
return false, "", int64(-1)
}
return true, expectedHash, expectedSize
}
func (s *grpcServer) FetchDirectory(context.Context, *asset.FetchDirectoryRequest) (*asset.FetchDirectoryResponse, error) {
return nil, nil
}
/* PushServer implementation
func (s *grpcServer) PushBlob(context.Context, *asset.PushBlobRequest) (*asset.PushBlobResponse, error) {
return nil, nil
}
func (s *grpcServer) PushDirectory(context.Context, *asset.PushDirectoryRequest) (*asset.PushDirectoryResponse, error) {
return nil, nil
}
*/