Repository navigation
Expand file tree
/
Copy pathflow_definition_provider.go
More file actions
583 lines (538 loc) · 19.9 KB
/
Copy pathflow_definition_provider.go
File metadata and controls
583 lines (538 loc) · 19.9 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Copyright (c) 2026 Super Durable, Inc.
//
// Licensed under the Sustainable Use License 1.0.
// You may not use this file except in compliance with the License.
// See the LICENSE file in the repository root.
//
// SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
package web
import (
"context"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/superdurable/dex/sdk-go/gen/dexpb"
"github.com/superdurable/dex/web/api"
)
const (
flowDefinitionSourceInvalid = "FLOW_DEFINITION_INVALID"
flowDefinitionSourceUnavailable = "FLOW_DEFINITION_SOURCE_UNAVAILABLE"
)
// Where one definition came from, as the catalog reports it.
const (
flowDefinitionOriginDirectory = "directory"
flowDefinitionOriginUploaded = "uploaded"
flowDefinitionOriginVisualize = "visualize"
)
// FlowDefinitionProvider loads one immutable, internally consistent definition snapshot.
type FlowDefinitionProvider interface {
Load(context.Context) (*FlowDefinitionSnapshot, error)
}
// flowDefinitionLookup adds the reads that do not load every definition; the routes use it.
type flowDefinitionLookup interface {
FlowDefinitionProvider
// loadFlowTypes loads only the definitions of flowTypes; a Flow type without one is left out.
loadFlowTypes(ctx context.Context, flowTypes []string) (*FlowDefinitionSnapshot, error)
// listFlowTypes names every Flow type that has a definition without reading uploaded content.
listFlowTypes(ctx context.Context) (*flowDefinitionTypeList, error)
}
// FlowDefinitionSnapshot is the validated catalog used throughout one HTTP request.
type FlowDefinitionSnapshot struct {
// Response is the catalog JSON with every graph; Connector setup reads it and it never reaches the browser.
Response []byte
V2Definitions map[string]api.V2Definition
DefinitionRevision string
Source string
LoadedAt time.Time
DefinitionCount int
// metadataResponse is the catalog JSON without graphs that GET /api/flow-definitions returns.
metadataResponse []byte
// flowTypeRevisions maps each Flow type in the snapshot to the revision of its own definitions.
flowTypeRevisions map[string]string
definitions []flowDefinition
}
// flowDefinitionTypeList names the Flow types that have a definition, with where each one comes from.
type flowDefinitionTypeList struct {
Configured bool `json:"configured"`
OperatorAPI bool `json:"operatorAPI"`
FlowTypes []flowDefinitionTypeName `json:"flowTypes"`
SourceErrors []string `json:"sourceErrors,omitempty"`
// source is the catalog source name that readiness reports.
source string
}
type flowDefinitionTypeName struct {
FlowType string `json:"flowType"`
Source string `json:"source"`
}
type flowDefinitionSourceError struct {
code string
err error
}
func (e *flowDefinitionSourceError) Error() string { return e.err.Error() }
func (e *flowDefinitionSourceError) Unwrap() error { return e.err }
func (e *flowDefinitionSourceError) DefinitionErrorCode() string { return e.code }
// flowDefinitionDocument is one definition document before validation.
type flowDefinitionDocument struct {
origin string
// path is the file path relative to the directory; empty for an uploaded document.
path string
// flowType is the Flow type the server stored the document under; empty for a file.
flowType string
data []byte
generatedAt *time.Time
uploadedAt *time.Time
// readErr is set when the document could not be read, so only it is invalid.
readErr error
// claimsNoFlowType is set for a file too large to read, so an upload of its Flow type is still shown.
claimsNoFlowType bool
}
// DirectoryFlowDefinitionProvider reloads directory definitions on every request.
type DirectoryFlowDefinitionProvider struct {
sources flowDefinitionSources
}
// NewDirectoryFlowDefinitionProvider creates a dynamic directory provider.
func NewDirectoryFlowDefinitionProvider(directory string) (*DirectoryFlowDefinitionProvider, error) {
sources, err := newFlowDefinitionSources(directory, nil)
if err != nil {
return nil, err
}
return &DirectoryFlowDefinitionProvider{sources: sources}, nil
}
func (p *DirectoryFlowDefinitionProvider) Load(ctx context.Context) (*FlowDefinitionSnapshot, error) {
return p.sources.Load(ctx)
}
func (p *DirectoryFlowDefinitionProvider) loadFlowTypes(ctx context.Context, flowTypes []string) (*FlowDefinitionSnapshot, error) {
return p.sources.loadFlowTypes(ctx, flowTypes)
}
func (p *DirectoryFlowDefinitionProvider) listFlowTypes(ctx context.Context) (*flowDefinitionTypeList, error) {
return p.sources.listFlowTypes(ctx)
}
// flowDefinitionSources merges a directory and the definitions uploaded to the Dex Server.
// A Flow type that any directory file declares comes only from the directory, even when that file is invalid.
type flowDefinitionSources struct {
// directory is empty when no directory is configured.
directory string
// server is nil when Dex Web has no FlowService, as for a visualize server.
server *serverFlowDefinitionSource
// memory replaces the directory for a visualize server.
memory []flowDefinitionDocument
// memoryLocation describes where memory came from in the catalog.
memoryLocation string
}
func newFlowDefinitionSources(directory string, client dexpb.FlowServiceClient) (flowDefinitionSources, error) {
sources := flowDefinitionSources{}
if client != nil {
sources.server = newServerFlowDefinitionSource(client)
}
directory = strings.TrimSpace(directory)
if directory == "" {
return sources, nil
}
absoluteDirectory, err := filepath.Abs(directory)
if err != nil {
return flowDefinitionSources{}, fmt.Errorf("resolve Flow rendering directory: %w", err)
}
sources.directory = absoluteDirectory
return sources, nil
}
func (s flowDefinitionSources) Load(ctx context.Context) (*FlowDefinitionSnapshot, error) {
local, err := s.readLocalDefinitions()
if err != nil {
return nil, err
}
var uploaded []flowDefinition
var sourceErrors []string
if s.server != nil {
flowTypes, listErr := s.server.listFlowTypes(ctx)
if listErr != nil {
sourceErrors = append(sourceErrors, listErr.Error())
}
documents, readErr := s.server.readFlowTypes(ctx, withoutLocalFlowTypes(flowTypes, local), true)
if readErr != nil {
sourceErrors = append(sourceErrors, readErr.Error())
}
uploaded = buildFlowDefinitions(documents)
}
return s.buildSnapshot(append(local, uploaded...), sourceErrors)
}
func (s flowDefinitionSources) loadFlowTypes(ctx context.Context, flowTypes []string) (*FlowDefinitionSnapshot, error) {
local, err := s.readLocalDefinitions()
if err != nil {
return nil, err
}
requested := make(map[string]bool, len(flowTypes))
for _, flowType := range flowTypes {
requested[flowType] = true
}
selected := make([]flowDefinition, 0, len(flowTypes))
for _, definition := range local {
if requested[definition.FlowName] {
selected = append(selected, definition)
}
}
if s.server != nil {
remaining := withoutLocalFlowTypes(uniqueFlowTypes(flowTypes), local)
uploaded, readErr := s.server.readFlowTypes(ctx, remaining, false)
if readErr != nil {
return nil, unavailableDefinitionSource(readErr)
}
selected = append(selected, buildFlowDefinitions(uploaded)...)
}
return s.buildSnapshot(selected, nil)
}
func (s flowDefinitionSources) listFlowTypes(ctx context.Context) (*flowDefinitionTypeList, error) {
local, err := s.readLocalDefinitions()
if err != nil {
return nil, err
}
list := &flowDefinitionTypeList{
Configured: s.isConfigured(), OperatorAPI: s.server != nil, FlowTypes: []flowDefinitionTypeName{}, source: s.catalogSource(),
}
seen := make(map[string]bool)
for _, definition := range local {
if definition.FlowName != "" && !seen[definition.FlowName] {
seen[definition.FlowName] = true
list.FlowTypes = append(list.FlowTypes, flowDefinitionTypeName{FlowType: definition.FlowName, Source: definition.Source})
}
}
if s.server != nil {
flowTypes, listErr := s.server.listFlowTypes(ctx)
if listErr != nil {
list.SourceErrors = append(list.SourceErrors, listErr.Error())
}
for _, flowType := range flowTypes {
if !seen[flowType] {
seen[flowType] = true
list.FlowTypes = append(list.FlowTypes, flowDefinitionTypeName{FlowType: flowType, Source: flowDefinitionOriginUploaded})
}
}
}
sort.Slice(list.FlowTypes, func(left int, right int) bool {
return list.FlowTypes[left].FlowType < list.FlowTypes[right].FlowType
})
return list, nil
}
func (s flowDefinitionSources) isConfigured() bool {
return s.directory != "" || s.server != nil || s.memory != nil
}
// readLocalDefinitions validates the directory or in-memory documents; only an unreadable directory fails.
func (s flowDefinitionSources) readLocalDefinitions() ([]flowDefinition, error) {
if s.memory != nil {
return buildFlowDefinitions(s.memory), nil
}
if s.directory == "" {
return nil, nil
}
documents, err := readDirectoryDefinitionFiles(s.directory)
if err != nil {
return nil, classifyDefinitionReadError(err)
}
return buildFlowDefinitions(documents), nil
}
func (s flowDefinitionSources) buildSnapshot(definitions []flowDefinition, sourceErrors []string) (*FlowDefinitionSnapshot, error) {
location := s.directory
if s.memory != nil {
location = s.memoryLocation
}
return assembleFlowDefinitionSnapshot(definitions, flowDefinitionCatalogHeader{
configured: s.isConfigured(), source: s.catalogSource(), directory: location, sourceErrors: sourceErrors,
})
}
// catalogSource names the configured sources: memory, local, server, or local+server.
func (s flowDefinitionSources) catalogSource() string {
switch {
case s.memory != nil:
return "memory"
case s.server != nil && s.directory != "":
return "local+server"
case s.server != nil:
return "server"
default:
return "local"
}
}
func readDirectoryDefinitionFiles(directory string) ([]flowDefinitionDocument, error) {
info, err := os.Stat(directory)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, fmt.Errorf("Flow rendering path is not a directory: %s", directory)
}
documents := make([]flowDefinitionDocument, 0)
err = filepath.WalkDir(directory, func(filePath string, entry os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if entry.IsDir() || !strings.EqualFold(filepath.Ext(entry.Name()), ".json") {
return nil
}
relativePath, relativeErr := filepath.Rel(directory, filePath)
if relativeErr != nil {
return relativeErr
}
document := flowDefinitionDocument{origin: flowDefinitionOriginDirectory, path: filepath.ToSlash(relativePath)}
info, infoErr := entry.Info()
if infoErr != nil {
return infoErr
}
if info.Size() > maxFlowDefinitionBytes {
document.readErr = fmt.Errorf("Flow Definition Graph is too large: it exceeds %d bytes: %s", maxFlowDefinitionBytes, document.path)
document.claimsNoFlowType = true
documents = append(documents, document)
return nil
}
data, readErr := os.ReadFile(filePath)
if readErr != nil {
return readErr
}
document.data = data
documents = append(documents, document)
return nil
})
sort.Slice(documents, func(left int, right int) bool { return documents[left].path < documents[right].path })
return documents, err
}
// buildFlowDefinitions validates each document on its own, so a bad document marks only itself invalid.
func buildFlowDefinitions(documents []flowDefinitionDocument) []flowDefinition {
definitions := make([]flowDefinition, 0, len(documents))
for _, document := range documents {
definitions = append(definitions, buildFlowDefinition(document))
}
markRepeatedV2FlowTypes(definitions)
return definitions
}
func buildFlowDefinition(document flowDefinitionDocument) flowDefinition {
name := document.path
if document.origin == flowDefinitionOriginUploaded {
name = "uploaded Flow type " + document.flowType
}
definition, err := validateFlowDefinitionDocument(name, document)
if err != nil {
definition = flowDefinition{FlowName: fallbackFlowName(document), Error: err.Error()}
}
definition.ID = flowDefinitionID(document)
definition.File = document.path
definition.Source = document.origin
definition.GeneratedAt = document.generatedAt
definition.UploadedAt = document.uploadedAt
if document.data != nil {
definition.Digest = sha256Hex(document.data)
}
return definition
}
func validateFlowDefinitionDocument(name string, document flowDefinitionDocument) (flowDefinition, error) {
if document.readErr != nil {
return flowDefinition{}, document.readErr
}
if len(document.data) > maxFlowDefinitionBytes {
return flowDefinition{}, fmt.Errorf("Flow Definition Graph exceeds %d bytes: %s", maxFlowDefinitionBytes, name)
}
if err := checkFlowDefinitionDocumentLimits(document.data); err != nil {
return flowDefinition{}, fmt.Errorf("Flow Definition Graph %s exceeds a limit: %w", name, err)
}
definition, err := flowDefinitionFromGraph(name, document.data)
if err != nil {
return flowDefinition{}, err
}
if document.origin == flowDefinitionOriginUploaded {
if definition.SchemaVersion != "2.0" {
return flowDefinition{}, fmt.Errorf("Flow Definition Graph %s has schemaVersion %q; uploaded definitions must use 2.0", name, definition.SchemaVersion)
}
if definition.FlowName != document.flowType {
return flowDefinition{}, fmt.Errorf("Flow Definition Graph %s names Flow type %q", name, definition.FlowName)
}
}
return definition, nil
}
// fallbackFlowName names an invalid document's Flow type so the directory still claims it. A file too large to
// read gets "", which claims no Flow type.
func fallbackFlowName(document flowDefinitionDocument) string {
if document.flowType != "" {
return document.flowType
}
if document.claimsNoFlowType {
return ""
}
var header struct {
Flow struct {
Name string `json:"name"`
} `json:"flow"`
}
if document.data != nil && json.Unmarshal(document.data, &header) == nil && strings.TrimSpace(header.Flow.Name) != "" {
return strings.TrimSpace(header.Flow.Name)
}
return strings.TrimSuffix(document.path, filepath.Ext(document.path))
}
func flowDefinitionID(document flowDefinitionDocument) string {
if document.origin == flowDefinitionOriginUploaded {
return "uploaded:" + document.flowType
}
return document.path
}
// markRepeatedV2FlowTypes invalidates every valid 2.0 file of a Flow type that more than one file declares.
func markRepeatedV2FlowTypes(definitions []flowDefinition) {
indexesByFlowType := make(map[string][]int)
for index, definition := range definitions {
if definition.Error == "" && definition.SchemaVersion == "2.0" && definition.Valid {
indexesByFlowType[definition.FlowName] = append(indexesByFlowType[definition.FlowName], index)
}
}
for flowType, indexes := range indexesByFlowType {
if len(indexes) < 2 {
continue
}
files := make([]string, 0, len(indexes))
for _, index := range indexes {
files = append(files, definitions[index].File)
}
message := fmt.Sprintf("multiple valid Flow Definition Graph 2.0 files define Flow type %q: %s", flowType, strings.Join(files, " and "))
for _, index := range indexes {
definitions[index] = flowDefinition{
ID: definitions[index].ID, File: definitions[index].File, FlowName: flowType,
Source: definitions[index].Source, Digest: definitions[index].Digest, Error: message,
}
}
}
}
type flowDefinitionCatalogHeader struct {
configured bool
source string
directory string
sourceErrors []string
}
func assembleFlowDefinitionSnapshot(definitions []flowDefinition, header flowDefinitionCatalogHeader) (*FlowDefinitionSnapshot, error) {
flowTypeRevisions := flowDefinitionRevisions(definitions)
revision := catalogRevision(flowTypeRevisions)
catalog := flowDefinitionCatalog{
Configured: header.configured,
Source: header.source,
DefinitionRevision: revision,
DefinitionCount: len(definitions),
Definitions: definitions,
SourceErrors: header.sourceErrors,
}
if header.source == "local" || header.source == "local+server" || header.source == "memory" {
catalog.Directory = header.directory
}
v2Definitions := make(map[string]api.V2Definition)
for _, definition := range definitions {
if definition.Error == "" && definition.SchemaVersion == "2.0" && definition.Valid {
v2Definitions[definition.FlowName] = *definition.V2
}
}
response, err := json.Marshal(catalog)
if err != nil {
return nil, invalidDefinitionSource(fmt.Errorf("encode Flow Definition Graph catalog: %w", err))
}
metadataCatalog := catalog
metadataCatalog.Definitions = make([]flowDefinition, 0, len(definitions))
for _, definition := range definitions {
definition.Graph = nil
definition.V2 = nil
metadataCatalog.Definitions = append(metadataCatalog.Definitions, definition)
}
metadataResponse, err := json.Marshal(metadataCatalog)
if err != nil {
return nil, invalidDefinitionSource(fmt.Errorf("encode Flow Definition Graph catalog metadata: %w", err))
}
return &FlowDefinitionSnapshot{
Response: response,
V2Definitions: v2Definitions,
DefinitionRevision: revision,
Source: header.source,
LoadedAt: time.Now().UTC(),
DefinitionCount: len(definitions),
metadataResponse: metadataResponse,
flowTypeRevisions: flowTypeRevisions,
definitions: definitions,
}, nil
}
// flowDefinitionRevisions digests the source, ID, and content digest of each Flow type's definitions.
// The result is never empty, so a moved or replaced definition always changes it.
func flowDefinitionRevisions(definitions []flowDefinition) map[string]string {
hashes := make(map[string][]string)
for _, definition := range definitions {
hashes[definition.FlowName] = append(hashes[definition.FlowName], definition.Source, definition.ID, definition.Digest, definition.Error)
}
revisions := make(map[string]string, len(hashes))
for flowType, parts := range hashes {
revisions[flowType] = framedDigest(parts)
}
return revisions
}
func catalogRevision(flowTypeRevisions map[string]string) string {
flowTypes := make([]string, 0, len(flowTypeRevisions))
for flowType := range flowTypeRevisions {
flowTypes = append(flowTypes, flowType)
}
sort.Strings(flowTypes)
parts := make([]string, 0, 2*len(flowTypes))
for _, flowType := range flowTypes {
parts = append(parts, flowType, flowTypeRevisions[flowType])
}
return framedDigest(parts)
}
// framedDigest frames each part with its unsigned 64-bit big-endian length before hashing.
func framedDigest(parts []string) string {
hash := sha256.New()
var length [8]byte
for _, part := range parts {
binary.BigEndian.PutUint64(length[:], uint64(len(part)))
// hash.Hash.Write never returns an error.
_, _ = hash.Write(length[:])
_, _ = hash.Write([]byte(part))
}
return "sha256:" + hex.EncodeToString(hash.Sum(nil))
}
func sha256Hex(data []byte) string {
digest := sha256.Sum256(data)
return hex.EncodeToString(digest[:])
}
func withoutLocalFlowTypes(flowTypes []string, local []flowDefinition) []string {
claimed := make(map[string]bool, len(local))
for _, definition := range local {
if definition.FlowName != "" {
claimed[definition.FlowName] = true
}
}
remaining := make([]string, 0, len(flowTypes))
for _, flowType := range flowTypes {
if !claimed[flowType] {
remaining = append(remaining, flowType)
}
}
return remaining
}
func uniqueFlowTypes(flowTypes []string) []string {
seen := make(map[string]bool, len(flowTypes))
unique := make([]string, 0, len(flowTypes))
for _, flowType := range flowTypes {
if flowType != "" && !seen[flowType] {
seen[flowType] = true
unique = append(unique, flowType)
}
}
return unique
}
func invalidDefinitionSource(err error) error {
return &flowDefinitionSourceError{code: flowDefinitionSourceInvalid, err: err}
}
func unavailableDefinitionSource(err error) error {
return &flowDefinitionSourceError{code: flowDefinitionSourceUnavailable, err: err}
}
func classifyDefinitionReadError(err error) error {
var coded interface{ DefinitionErrorCode() string }
if errors.As(err, &coded) {
return err
}
return unavailableDefinitionSource(err)
}