Skip to content

Commit c7961b3

Browse files
authored
fix(functions): use functions inside a pipe (#168)
* fix(functions): use fn inside a pipe venom test * fix(functions): use fn inside a pipe * fix(functions): update changelog
1 parent 03a5593 commit c7961b3

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ Types of changes
1616

1717
## [1.15.0]
1818

19+
- `Security` in PIMO Play : disallow usage of mask Command in `masks` property
1920
- `Added` functions section in masking configuration
2021
- `Added` possibility to use masks as template functions (most mask are available as `Mask<mask_name>` : MaskRandDate, MaskRegex, ...)
21-
- `Fixed` preserve notInCache enum in the jsonschema
22-
- `Security` in PIMO Play : disallow usage of mask Command in `masks` property
2322
- `Added` in PIMO Play: adds a tab to visualize a mermaid graph flow of the masking
23+
- `Fixed` preserve notInCache enum in the jsonschema
24+
- `Fixed` functions call from a pipe mask crash pipeline
2425

2526
## [1.14.1]
2627

internal/app/pimo/pimo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (ctx *Context) Configure(cfg Config) error {
141141
model.InjectConfig(cfg.SkipLineOnError, cfg.SkipFieldOnError)
142142

143143
var err error
144-
ctx.pipeline, ctx.caches, err = model.BuildPipeline(ctx.pipeline, ctx.pdef, nil)
144+
ctx.pipeline, ctx.caches, err = model.BuildPipeline(ctx.pipeline, ctx.pdef, nil, nil)
145145
if err != nil {
146146
return fmt.Errorf("Cannot build pipeline: %w", err)
147147
}

pkg/functions/build.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ func (d Definition) AsCall(name string, args ...interface{}) string {
9393
}
9494

9595
// Build returns a FuncMap (map[string]interface{}) that can be used in Go Template API, with all functions.
96-
func (b Builder) Build() (template.FuncMap, error) {
97-
funcMap := make(template.FuncMap, len(b.Definitions))
96+
func (b Builder) Build(fns template.FuncMap) (template.FuncMap, error) {
97+
funcMap := fns
98+
if fns == nil {
99+
funcMap = make(template.FuncMap, len(b.Definitions))
100+
}
98101

99102
if len(b.Definitions) == 0 {
100103
return funcMap, nil

pkg/model/pipeline.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func BuildCaches(caches map[string]CacheDefinition, existing map[string]Cache) m
6666
return existing
6767
}
6868

69-
func BuildFuncMap(funcs map[string]Function) (tmpl.FuncMap, error) {
69+
func BuildFuncMap(funcs map[string]Function, fns tmpl.FuncMap) (tmpl.FuncMap, error) {
7070
b := functions.Builder{
7171
Definitions: map[string]functions.Definition{},
7272
}
@@ -83,12 +83,12 @@ func BuildFuncMap(funcs map[string]Function) (tmpl.FuncMap, error) {
8383
}
8484
}
8585

86-
return b.Build()
86+
return b.Build(fns)
8787
}
8888

89-
func BuildPipeline(pipeline Pipeline, conf Definition, caches map[string]Cache) (Pipeline, map[string]Cache, error) {
89+
func BuildPipeline(pipeline Pipeline, conf Definition, caches map[string]Cache, functions tmpl.FuncMap) (Pipeline, map[string]Cache, error) {
9090
caches = BuildCaches(conf.Caches, caches)
91-
functions, err := BuildFuncMap(conf.Functions)
91+
functions, err := BuildFuncMap(conf.Functions, functions)
9292
if err != nil {
9393
return nil, nil, err
9494
}

pkg/pipe/pipe.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"hash/fnv"
2323
"regexp"
24+
"text/template"
2425

2526
over "github.com/Trendyol/overlog"
2627
"github.com/cgi-fr/pimo/pkg/model"
@@ -36,7 +37,7 @@ type MaskEngine struct {
3637
}
3738

3839
// NewMask return a MaskEngine from a value
39-
func NewMask(seed int64, injectParent string, injectRoot string, caches map[string]model.Cache, filename string, masking ...model.Masking) (MaskEngine, error) {
40+
func NewMask(seed int64, injectParent string, injectRoot string, caches map[string]model.Cache, fns template.FuncMap, filename string, masking ...model.Masking) (MaskEngine, error) {
4041
var definition model.Definition
4142
var err error
4243
if len(filename) > 0 {
@@ -50,7 +51,7 @@ func NewMask(seed int64, injectParent string, injectRoot string, caches map[stri
5051
definition = model.Definition{Seed: seed + 1, Masking: masking}
5152
}
5253
pipeline := model.NewPipeline(nil)
53-
pipeline, _, err = model.BuildPipeline(pipeline, definition, caches)
54+
pipeline, _, err = model.BuildPipeline(pipeline, definition, caches, fns)
5455
return MaskEngine{"", pipeline, injectParent, injectRoot}, err
5556
}
5657

@@ -119,7 +120,7 @@ func Factory(conf model.MaskFactoryConfiguration) (model.MaskContextEngine, bool
119120
h := fnv.New64a()
120121
h.Write([]byte(conf.Masking.Selector.Jsonpath))
121122
conf.Seed += int64(h.Sum64())
122-
mask, err := NewMask(conf.Seed, conf.Masking.Mask.Pipe.InjectParent, conf.Masking.Mask.Pipe.InjectRoot, conf.Cache, conf.Masking.Mask.Pipe.DefinitionFile, conf.Masking.Mask.Pipe.Masking...)
123+
mask, err := NewMask(conf.Seed, conf.Masking.Mask.Pipe.InjectParent, conf.Masking.Mask.Pipe.InjectRoot, conf.Cache, conf.Functions, conf.Masking.Mask.Pipe.DefinitionFile, conf.Masking.Mask.Pipe.Masking...)
123124
if err != nil {
124125
return mask, true, err
125126
}

pkg/pipe/pipe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestMaskEngineShouldMaskNestedArray(t *testing.T) {
4040
var result []model.Dictionary
4141

4242
model.InjectMaskFactories([]model.MaskFactory{templatemask.Factory})
43-
pipe, err := pipe.NewMask(42, "parent", "root", map[string]model.Cache{}, "",
43+
pipe, err := pipe.NewMask(42, "parent", "root", map[string]model.Cache{}, nil, "",
4444
model.Masking{
4545
Selector: model.SelectorType{Jsonpath: "name"},
4646
Mask: model.MaskType{

test/suites/functions.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,39 @@ testcases:
9999
- result.systemout ShouldEqual {"addValue":"15","subValue":"5"}
100100
- result.systemerr ShouldBeEmpty
101101
102+
103+
- name: using function inside a pipe
104+
steps:
105+
- script: rm -f masking.yml
106+
- script: |-
107+
cat > masking.yml <<EOF
108+
version: "1"
109+
functions:
110+
clespi:
111+
params:
112+
- name: i
113+
body: |-
114+
k = i % 511
115+
if k <= 10 {
116+
return "00"+ k
117+
} else if k <= 100 {
118+
return "0"+ k
119+
}
120+
return k
121+
masking:
122+
- selector:
123+
jsonpath: "personne"
124+
mask:
125+
pipe:
126+
masking:
127+
- selector:
128+
jsonpath: "spi"
129+
mask:
130+
template: "{{ clespi .spi}}"
131+
EOF
132+
- script: |-
133+
echo '{"personne":[{ "spi":"158178940"},{"spi":"4588574950"}]}' | pimo
134+
assertions:
135+
- result.code ShouldEqual 0
136+
- result.systemout ShouldEqual {"personne":[{"spi":"423"},{"spi":"372"}]}
137+
- result.systemerr ShouldBeEmpty

0 commit comments

Comments
 (0)