Skip to content

Commit

Permalink
feat(apply): add mask apply
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienaury committed Dec 16, 2024
1 parent 4ef3b35 commit f358fbc
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Types of changes
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [1.28.2]
## [1.29.0]

- `Added` mask `apply` to externalize masks
- `Fixed` mock command fails to parse JSON body when root is an array

## [1.28.1]
Expand Down
2 changes: 2 additions & 0 deletions internal/app/pimo/pimo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
over "github.com/adrienaury/zeromdc"
"github.com/cgi-fr/pimo/pkg/add"
"github.com/cgi-fr/pimo/pkg/addtransient"
"github.com/cgi-fr/pimo/pkg/apply"
"github.com/cgi-fr/pimo/pkg/command"
"github.com/cgi-fr/pimo/pkg/constant"
"github.com/cgi-fr/pimo/pkg/dateparser"
Expand Down Expand Up @@ -341,6 +342,7 @@ func injectMaskFactories() []model.MaskFactory {
timeline.Factory,
sequence.Factory,
sha3.Factory,
apply.Factory,
}
}

Expand Down
84 changes: 84 additions & 0 deletions pkg/apply/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (C) 2024 CGI France
//
// This file is part of PIMO.
//
// PIMO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// PIMO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with PIMO. If not, see <http://www.gnu.org/licenses/>.

package apply

import (
"hash/fnv"
"text/template"

"github.com/cgi-fr/pimo/pkg/model"
"github.com/rs/zerolog/log"
)

// MaskEngine is a value that always mask the same way
type MaskEngine struct {
pipeline model.Pipeline
}

// NewMask return a MaskEngine from a value
func NewMask(seed int64, caches map[string]model.Cache, fns template.FuncMap, uri string) (MaskEngine, error) {
var definition model.Definition
var err error

if len(uri) > 0 {
definition, err = model.LoadPipelineDefinitionFromFile(uri)
if err != nil {
return MaskEngine{nil}, err
}
// merge the current seed with the seed provided by configuration on the pipe
definition.Seed += seed
}

pipeline := model.NewPipeline(nil)
pipeline, _, err = model.BuildPipeline(pipeline, definition, caches, fns, "", "")

return MaskEngine{pipeline}, err
}

func (me MaskEngine) Mask(e model.Entry, context ...model.Dictionary) (model.Entry, error) {
log.Info().Msg("Mask apply")

var result []model.Entry

err := me.pipeline.
WithSource(model.NewSourceFromSlice([]model.Dictionary{model.NewDictionary().With(".", e)})).
// Process(model.NewCounterProcessWithCallback("internal", 1, updateContext)).
AddSink(model.NewSinkToSlice(&result)).
Run()
if err != nil {
return nil, err
}

return result[0], nil
}

// Factory create a mask from a configuration
func Factory(conf model.MaskFactoryConfiguration) (model.MaskEngine, bool, error) {
if len(conf.Masking.Mask.Apply.URI) > 0 {
// set differents seeds for differents jsonpath
h := fnv.New64a()
h.Write([]byte(conf.Masking.Selector.Jsonpath))
conf.Seed += int64(h.Sum64()) //nolint:gosec
mask, err := NewMask(conf.Seed, conf.Cache, conf.Functions, conf.Masking.Mask.Apply.URI)
if err != nil {
return mask, true, err
}
return mask, true, nil
}
return nil, false, nil
}
5 changes: 5 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ type Sha3Type struct {
MaxStrLen int `yaml:"maxstrlen,omitempty" json:"maxstrlen,omitempty" jsonschema_description:"an error will occur if the identifier can grow longer than the specified length"`
}

type ApplyType struct {
URI string `yaml:"uri" json:"uri" jsonschema_description:"URI of the mask resource"`
}

type MaskType struct {
Add Entry `yaml:"add,omitempty" json:"add,omitempty" jsonschema:"oneof_required=Add,title=Add Mask,description=Add a new field in the JSON stream"`
AddTransient Entry `yaml:"add-transient,omitempty" json:"add-transient,omitempty" jsonschema:"oneof_required=AddTransient,title=Add Transient Mask" jsonschema_description:"Add a new temporary field, that will not show in the JSON output"`
Expand Down Expand Up @@ -275,6 +279,7 @@ type MaskType struct {
TimeLine TimeLineType `yaml:"timeline,omitempty" json:"timeline,omitempty" jsonschema:"oneof_required=TimeLine,title=TimeLine Mask" jsonschema_description:"Generate a timeline under constraints and rules"`
Sequence SequenceType `yaml:"sequence,omitempty" json:"sequence,omitempty" jsonschema:"oneof_required=Sequence,title=Sequence Mask" jsonschema_description:"Generate a sequenced ID that follows specified format"`
Sha3 Sha3Type `yaml:"sha3,omitempty" json:"sha3,omitempty" jsonschema:"oneof_required=Sha3,title=Sha3 Mask" jsonschema_description:"Generate a variable-length crytographic hash (collision resistant)"`
Apply ApplyType `yaml:"apply,omitempty" json:"apply,omitempty" jsonschema:""`
}

type Masking struct {
Expand Down
16 changes: 16 additions & 0 deletions schema/v1/pimo.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
"$id": "https://github.com/cgi-fr/pimo/pkg/model/definition",
"$ref": "#/$defs/Definition",
"$defs": {
"ApplyType": {
"properties": {
"uri": {
"type": "string",
"description": "URI of the mask resource"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"uri"
]
},
"CacheDefinition": {
"properties": {
"unique": {
Expand Down Expand Up @@ -754,6 +767,9 @@
"$ref": "#/$defs/Sha3Type",
"title": "Sha3 Mask",
"description": "Generate a variable-length crytographic hash (collision resistant)"
},
"apply": {
"$ref": "#/$defs/ApplyType"
}
},
"additionalProperties": false,
Expand Down

0 comments on commit f358fbc

Please sign in to comment.