-
Notifications
You must be signed in to change notification settings - Fork 81
/
handleUpload.go
216 lines (185 loc) · 6.55 KB
/
handleUpload.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package api
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/elastic/fleet-server/v7/internal/pkg/apikey"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/file"
"github.com/elastic/fleet-server/v7/internal/pkg/file/cbor"
"github.com/elastic/fleet-server/v7/internal/pkg/file/uploader"
"github.com/elastic/fleet-server/v7/internal/pkg/model"
"github.com/elastic/go-elasticsearch/v8"
"github.com/rs/zerolog"
"go.elastic.co/apm/v2"
)
const (
// TODO: move to a config
maxFileSize = 104857600 // 100 MiB
maxUploadTimer = 24 * time.Hour
)
var (
ErrTransitHashRequired = errors.New("transit hash required")
ErrAgentIDMissing = errors.New("required field agent_id is missing")
ErrFileInfoBodyRequired = fmt.Errorf("file info body is required")
)
// FIXME Should we use the structs in openapi.gen.go instead of the generic ones? Will need to rework the uploader if we do
type UploadT struct {
bulker bulk.Bulk
chunkClient *elasticsearch.Client
cache cache.Cache
uploader *uploader.Uploader
authAgent func(*http.Request, *string, bulk.Bulk, cache.Cache) (*model.Agent, error) // injectable for testing purposes
authAPIKey func(*http.Request, bulk.Bulk, cache.Cache) (*apikey.APIKey, error) // as above
}
func NewUploadT(cfg *config.Server, bulker bulk.Bulk, chunkClient *elasticsearch.Client, cache cache.Cache) *UploadT {
zerolog.Ctx(context.TODO()).Info().
Interface("limits", cfg.Limits.ArtifactLimit).
Int64("maxFileSize", maxFileSize).
Msg("upload limits")
return &UploadT{
chunkClient: chunkClient,
bulker: bulker,
cache: cache,
uploader: uploader.New(chunkClient, bulker, cache, maxFileSize, maxUploadTimer),
authAgent: authAgent,
authAPIKey: authAPIKey,
}
}
func (ut *UploadT) validateUploadBeginRequest(ctx context.Context, reader io.Reader) (uploader.JSDict, string, error) {
span, _ := apm.StartSpan(ctx, "validateRequest", "validate")
defer span.End()
payload, err := uploader.ReadDict(reader)
if err != nil {
if errors.Is(err, io.EOF) {
return nil, "", fmt.Errorf("%w: %w", ErrFileInfoBodyRequired, err)
}
return nil, "", &BadRequestErr{msg: "unable to decode upload begin request", nextErr: err}
}
// check API key matches payload agent ID
agentID, ok := payload.Str("agent_id")
if !ok || agentID == "" {
return nil, "", ErrAgentIDMissing
}
return payload, agentID, nil
}
func (ut *UploadT) handleUploadBegin(_ zerolog.Logger, w http.ResponseWriter, r *http.Request) error {
// decode early to match agentID in the payload
payload, agentID, err := ut.validateUploadBeginRequest(r.Context(), r.Body)
if err != nil {
return err
}
agent, err := ut.authAgent(r, &agentID, ut.bulker, ut.cache)
if err != nil {
return err
}
// validate payload, enrich with additional fields, and write metadata doc to ES
info, err := ut.uploader.Begin(r.Context(), agent.Namespaces, payload)
if err != nil {
return err
}
span, _ := apm.StartSpan(r.Context(), "response", "write")
defer span.End()
// prepare and write response
resp := UploadBeginAPIResponse{
ChunkSize: info.ChunkSize,
UploadId: info.ID,
}
out, err := json.Marshal(resp)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(out)
if err != nil {
return err
}
return nil
}
func (ut *UploadT) handleUploadChunk(zlog zerolog.Logger, w http.ResponseWriter, r *http.Request, uplID string, chunkID int, chunkHash string) error {
// chunkHash is checked by router
upinfo, chunkInfo, err := ut.uploader.Chunk(r.Context(), uplID, chunkID, chunkHash)
if err != nil {
return err
}
// prevent over-sized chunks
data := http.MaxBytesReader(w, r.Body, file.MaxChunkSize)
// compute hash as we stream it
hash := sha256.New()
copier := io.TeeReader(data, hash)
ce := cbor.NewChunkWriter(copier, chunkInfo.Last, chunkInfo.BID, chunkInfo.SHA2, upinfo.ChunkSize)
if err := uploader.IndexChunk(r.Context(), ut.chunkClient, ce, upinfo.Source, chunkInfo.BID, chunkInfo.Pos); err != nil {
return err
}
span, ctx := apm.StartSpan(r.Context(), "validateIndexChunk", "validate")
hashsum := hex.EncodeToString(hash.Sum(nil))
if !strings.EqualFold(chunkHash, hashsum) {
// delete document, since we wrote it, but the hash was invalid
// context scoped to allow this operation to finish even if client disconnects
if err := uploader.DeleteChunk(ctx, ut.bulker, upinfo.Source, chunkInfo.BID, chunkInfo.Pos); err != nil {
zlog.Warn().Err(err).
Str("source", upinfo.Source).
Str("fileID", chunkInfo.BID).
Int("chunkNum", chunkInfo.Pos).
Msg("a chunk hash mismatch occurred, and fleet server was unable to remove the invalid chunk")
}
span.End()
return uploader.ErrHashMismatch
}
span.End()
span, _ = apm.StartSpan(r.Context(), "response", "write")
defer span.End()
w.WriteHeader(http.StatusOK)
return nil
}
func (ut *UploadT) validateUploadCompleteRequest(r *http.Request, id string) (string, error) {
span, ctx := apm.StartSpan(r.Context(), "validateRequest", "validate")
defer span.End()
info, err := ut.uploader.GetUploadInfo(ctx, id)
if err != nil {
return "", err
}
// need to auth that it matches the ID in the initial
// doc, but that means we had to doc-lookup early
if _, err := ut.authAgent(r, &info.AgentID, ut.bulker, ut.cache); err != nil {
return "", fmt.Errorf("error authenticating for upload finalization: %w", err)
}
var req UploadCompleteRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return "", &BadRequestErr{msg: "unable to decode upload complete request"}
}
hash := strings.TrimSpace(req.Transithash.Sha256)
if hash == "" {
return "", ErrTransitHashRequired
}
return hash, nil
}
func (ut *UploadT) handleUploadComplete(_ zerolog.Logger, w http.ResponseWriter, r *http.Request, uplID string) error {
hash, err := ut.validateUploadCompleteRequest(r, uplID)
if err != nil {
return err
}
_, err = ut.uploader.Complete(r.Context(), uplID, hash)
if err != nil {
return err
}
span, _ := apm.StartSpan(r.Context(), "response", "write")
defer span.End()
_, err = w.Write([]byte(`{"status":"ok"}`))
if err != nil {
return err
}
return nil
}