-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
stream.go
253 lines (228 loc) · 7.28 KB
/
stream.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package manifeststream
import (
"bufio"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/util/io/files"
"github.com/argoproj/argo-cd/v2/util/tgzstream"
log "github.com/sirupsen/logrus"
)
// Defines the contract for the application sender, i.e. the CLI
type ApplicationStreamSender interface {
Send(*applicationpkg.ApplicationManifestQueryWithFilesWrapper) error
}
// Defines the contract for the application receiver, i.e. API server
type ApplicationStreamReceiver interface {
Recv() (*applicationpkg.ApplicationManifestQueryWithFilesWrapper, error)
}
// Defines the contract for the repo stream sender, i.e. the API server
type RepoStreamSender interface {
Send(*apiclient.ManifestRequestWithFiles) error
}
// Defines the contract for the repo stream receiver, i.e. the repo server
type RepoStreamReceiver interface {
Recv() (*apiclient.ManifestRequestWithFiles, error)
}
// SendApplicationManifestQueryWithFiles compresses a folder and sends it over the stream
func SendApplicationManifestQueryWithFiles(ctx context.Context, stream ApplicationStreamSender, appName string, appNs string, dir string, inclusions []string) error {
f, filesWritten, checksum, err := tgzstream.CompressFiles(dir, inclusions, nil)
if filesWritten == 0 {
return fmt.Errorf("no files to send")
}
if err != nil {
return fmt.Errorf("failed to compress files: %w", err)
}
err = stream.Send(&applicationpkg.ApplicationManifestQueryWithFilesWrapper{
Part: &applicationpkg.ApplicationManifestQueryWithFilesWrapper_Query{
Query: &applicationpkg.ApplicationManifestQueryWithFiles{
Name: &appName,
Checksum: &checksum,
AppNamespace: &appNs,
},
},
})
if err != nil {
return fmt.Errorf("failed to send manifest stream header: %w", err)
}
err = sendFile(ctx, stream, f)
if err != nil {
return fmt.Errorf("failed to send manifest stream file: %w", err)
}
return nil
}
func sendFile(ctx context.Context, sender ApplicationStreamSender, file *os.File) error {
reader := bufio.NewReader(file)
chunk := make([]byte, 1024)
for {
if ctx != nil {
if err := ctx.Err(); err != nil {
return fmt.Errorf("client stream context error: %w", err)
}
}
n, err := reader.Read(chunk)
if n > 0 {
fr := &applicationpkg.ApplicationManifestQueryWithFilesWrapper{
Part: &applicationpkg.ApplicationManifestQueryWithFilesWrapper_Chunk{
Chunk: &applicationpkg.FileChunk{
Chunk: chunk[:n],
},
},
}
if e := sender.Send(fr); e != nil {
return fmt.Errorf("error sending stream: %w", e)
}
}
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("buffer reader error: %w", err)
}
}
return nil
}
func ReceiveApplicationManifestQueryWithFiles(stream ApplicationStreamReceiver) (*applicationpkg.ApplicationManifestQueryWithFiles, error) {
header, err := stream.Recv()
if err != nil {
return nil, fmt.Errorf("failed to receive header: %w", err)
}
if header == nil || header.GetQuery() == nil {
return nil, fmt.Errorf("error getting stream query: query is nil")
}
return header.GetQuery(), nil
}
func SendRepoStream(repoStream RepoStreamSender, appStream ApplicationStreamReceiver, req *apiclient.ManifestRequest, checksum string) error {
err := repoStream.Send(&apiclient.ManifestRequestWithFiles{
Part: &apiclient.ManifestRequestWithFiles_Request{
Request: req,
},
})
if err != nil {
return fmt.Errorf("error sending request: %w", err)
}
err = repoStream.Send(&apiclient.ManifestRequestWithFiles{
Part: &apiclient.ManifestRequestWithFiles_Metadata{
Metadata: &apiclient.ManifestFileMetadata{
Checksum: checksum,
},
},
})
if err != nil {
return fmt.Errorf("error sending metadata: %w", err)
}
for {
part, err := appStream.Recv()
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("stream Recv error: %w", err)
}
if part == nil || part.GetChunk() == nil {
return fmt.Errorf("error getting stream chunk: chunk is nil")
}
err = repoStream.Send(&apiclient.ManifestRequestWithFiles{
Part: &apiclient.ManifestRequestWithFiles_Chunk{
Chunk: &apiclient.ManifestFileChunk{
Chunk: part.GetChunk().GetChunk(),
},
},
})
if err != nil {
return fmt.Errorf("error sending chunk: %w", err)
}
}
return nil
}
func ReceiveManifestFileStream(ctx context.Context, receiver RepoStreamReceiver, destDir string, maxTarSize int64, maxExtractedSize int64) (*apiclient.ManifestRequest, *apiclient.ManifestFileMetadata, error) {
header, err := receiver.Recv()
if err != nil {
return nil, nil, fmt.Errorf("failed to receive header: %w", err)
}
if header == nil || header.GetRequest() == nil {
return nil, nil, fmt.Errorf("error getting stream request: request is nil")
}
request := header.GetRequest()
header2, err := receiver.Recv()
if err != nil {
return nil, nil, fmt.Errorf("failed to receive header: %w", err)
}
if header2 == nil || header2.GetMetadata() == nil {
return nil, nil, fmt.Errorf("error getting stream metadata: metadata is nil")
}
metadata := header2.GetMetadata()
tgzFile, err := receiveFile(ctx, receiver, metadata.GetChecksum(), destDir, maxTarSize)
if err != nil {
return nil, nil, fmt.Errorf("error receiving tgz file: %w", err)
}
err = files.Untgz(destDir, tgzFile, maxExtractedSize)
if err != nil {
return nil, nil, fmt.Errorf("error decompressing tgz file: %w", err)
}
err = os.Remove(tgzFile.Name())
if err != nil {
log.Warnf("error removing the tgz file %q: %s", tgzFile.Name(), err)
}
return request, metadata, nil
}
// receiveFile will receive the file from the gRPC stream and save it in the dst folder.
// Returns error if checksum doesn't match the one provided in the fileMetadata.
// It is responsibility of the caller to close the returned file.
func receiveFile(ctx context.Context, receiver RepoStreamReceiver, checksum, dst string, maxSize int64) (*os.File, error) {
hasher := sha256.New()
tmpDir, err := files.CreateTempDir("")
if err != nil {
return nil, fmt.Errorf("error creating tmp dir: %w", err)
}
file, err := os.CreateTemp(tmpDir, "")
if err != nil {
return nil, fmt.Errorf("error creating file: %w", err)
}
size := 0
for {
if ctx != nil {
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("stream context error: %w", err)
}
}
req, err := receiver.Recv()
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("stream Recv error: %w", err)
}
c := req.GetChunk()
if c == nil {
return nil, fmt.Errorf("stream request chunk is nil")
}
size += len(c.Chunk)
if size > int(maxSize) {
return nil, fmt.Errorf("file exceeded max size of %d bytes", maxSize)
}
_, err = file.Write(c.Chunk)
if err != nil {
return nil, fmt.Errorf("error writing file: %w", err)
}
_, err = hasher.Write(c.Chunk)
if err != nil {
return nil, fmt.Errorf("error writing hasher: %w", err)
}
}
hasherChecksum := hex.EncodeToString(hasher.Sum(nil))
if hasherChecksum != checksum {
return nil, fmt.Errorf("file checksum validation error: calc %s sent %s", hasherChecksum, checksum)
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
tgzstream.CloseAndDelete(file)
return nil, fmt.Errorf("seek error: %w", err)
}
return file, nil
}