-
Notifications
You must be signed in to change notification settings - Fork 8
/
bootatrsp_transformers.go
103 lines (92 loc) · 2.98 KB
/
bootatrsp_transformers.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
// Copyright 2023 Greenmask
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package custom
import (
"context"
"fmt"
"time"
"github.com/greenmaskio/greenmask/internal/db/postgres/transformers/utils"
"github.com/greenmaskio/greenmask/pkg/toolkit"
)
const (
DefaultValidationTimeout = 20 * time.Second
DefaultRowTransformationTimeout = 2 * time.Second
DefaultAutoDiscoveryTimeout = 10 * time.Second
)
func BootstrapCustomTransformers(ctx context.Context, registry *utils.TransformerRegistry, customTransformers []*TransformerDefinition) (err error) {
for _, ctd := range customTransformers {
var td *utils.Definition
if ctd.Name == "" && !ctd.AutoDiscover {
return fmt.Errorf("custom transformer without auto discovery must be defined staticly in the config")
}
if ctd.Executable == "" {
return fmt.Errorf(`custom transformer "executable" parameter is required`)
}
if ctd.AutoDiscoveryTimeout == 0 {
ctd.AutoDiscoveryTimeout = DefaultAutoDiscoveryTimeout
}
if ctd.ValidationTimeout == 0 {
ctd.ValidationTimeout = DefaultValidationTimeout
}
if ctd.RowTransformationTimeout == 0 {
ctd.RowTransformationTimeout = DefaultRowTransformationTimeout
}
if ctd.Driver == nil {
ctd.Driver = &(*toolkit.DefaultRowDriverParams)
}
if ctd.AutoDiscover {
// Get custom transformer definition from stdout and override received data with config ctd
err = func() error {
args := make([]string, len(ctd.Args))
copy(args, ctd.Args)
args = append(args, PrintDefinitionArgName)
ctx, cancel := context.WithTimeout(ctx, ctd.AutoDiscoveryTimeout)
defer cancel()
ctdd, err := GetDynamicTransformerDefinition(ctx, ctd.Executable, args...)
if err != nil {
return fmt.Errorf("error getting dynamic transformer definition: %w", err)
}
ctd.Name = ctdd.Name
ctd.Description = ctdd.Description
ctd.Parameters = ctdd.Parameters
ctd.Driver = ctdd.Driver
ctd.ExpectedExitCode = ctdd.ExpectedExitCode
ctd.Validate = ctdd.Validate
return nil
}()
if err != nil {
return err
}
}
for _, p := range ctd.Parameters {
if p.IsColumn && p.ColumnProperties == nil {
p.SetIsColumn(toolkit.
NewColumnProperties().
SetAffected(true),
)
}
}
td = utils.NewDefinition(
&utils.TransformerProperties{
Name: ctd.Name,
Description: ctd.Description,
IsCustom: true,
},
ProduceNewCmdTransformerFunction(ctd),
ctd.Parameters...,
)
registry.MustRegister(td)
}
return nil
}