From b65105706d982235ea451ba054ba7545925e80d8 Mon Sep 17 00:00:00 2001 From: Mehant Kammakomati Date: Wed, 21 Sep 2022 14:03:44 +0530 Subject: [PATCH 1/2] feat: add starlark custom functions for crypto and archive utils Signed-off-by: Mehant Kammakomati --- transformer/external/starlarktransformer.go | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/transformer/external/starlarktransformer.go b/transformer/external/starlarktransformer.go index a385ac1e0..7b5881c38 100644 --- a/transformer/external/starlarktransformer.go +++ b/transformer/external/starlarktransformer.go @@ -68,6 +68,13 @@ const ( fswriteFnName = "write" fspathBaseFnName = "pathbase" fspathRelFnName = "pathrel" + + // encryption functions + encaescbcpbkdfFnName = "encaescbcpbkdf" + encrsacertFnName = "encrsacert" + // archival functions + archtargzipstrFnName = "archtargzipstr" + archtarstrFnName = "archtarstr" ) // Starlark implements transformer interface and is used to write simple external transformers @@ -296,6 +303,8 @@ func (t *Starlark) setDefaultGlobals() { t.addStarlibModules() t.addFSModules() t.addAppModules() + t.addCryptoModules() + t.addArchiveModules() } func (t *Starlark) addStarlibModules() { @@ -333,6 +342,26 @@ func (t *Starlark) addFSModules() { } } +func (t *Starlark) addCryptoModules() { + t.StarGlobals["crypto"] = &starlarkstruct.Module{ + Name: "crypto", + Members: starlark.StringDict{ + encaescbcpbkdfFnName: t.getStarlarkEncAesCbcPbkdf(), + encrsacertFnName: t.getStarlarkEncRsaCert(), + }, + } +} + +func (t *Starlark) addArchiveModules() { + t.StarGlobals["archive"] = &starlarkstruct.Module{ + Name: "archive", + Members: starlark.StringDict{ + archtargzipstrFnName: t.getStarlarkArchTarGZipStr(), + archtarstrFnName: t.getStarlarkArchTarStr(), + }, + } +} + func (t *Starlark) addAppModules() { t.StarGlobals[types.AppNameShort] = &starlarkstruct.Module{ Name: types.AppNameShort, @@ -661,3 +690,49 @@ func (t *Starlark) getStarlarkFSPathRel() *starlark.Builtin { return starlark.String(path3), nil }) } + +func (t *Starlark) getStarlarkEncAesCbcPbkdf() *starlark.Builtin { + return starlark.NewBuiltin(encaescbcpbkdfFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var key, data string + if err := starlark.UnpackPositionalArgs(encaescbcpbkdfFnName, args, kwargs, 2, &key, &data); err != nil { + return nil, err + } + return starlark.String(common.EncryptAesCbcWithPbkdfWrapper(key, data)), nil + }) +} + +func (t *Starlark) getStarlarkEncRsaCert() *starlark.Builtin { + return starlark.NewBuiltin(encrsacertFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var certificate, data string + if err := starlark.UnpackPositionalArgs(encrsacertFnName, args, kwargs, 2, &certificate, &data); err != nil { + return nil, err + } + return starlark.String(common.EncryptRsaCertWrapper(certificate, data)), nil + }) +} + +func (t *Starlark) getStarlarkArchTarGZipStr() *starlark.Builtin { + return starlark.NewBuiltin(archtargzipstrFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var srcDir string + if err := starlark.UnpackPositionalArgs(archtargzipstrFnName, args, kwargs, 1, &srcDir); err != nil { + return nil, err + } + if !t.Env.IsPathValid(srcDir) { + return starlark.None, fmt.Errorf("invalid path") + } + return starlark.String(common.CreateTarArchiveGZipStringWrapper(srcDir)), nil + }) +} + +func (t *Starlark) getStarlarkArchTarStr() *starlark.Builtin { + return starlark.NewBuiltin(archtarstrFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + var srcDir string + if err := starlark.UnpackPositionalArgs(archtarstrFnName, args, kwargs, 1, &srcDir); err != nil { + return nil, err + } + if !t.Env.IsPathValid(srcDir) { + return starlark.None, fmt.Errorf("invalid path") + } + return starlark.String(common.CreateTarArchiveNoCompressionStringWrapper(srcDir)), nil + }) +} From 09b81d9beeb52306217d6f0abcd20b6a15c93b32 Mon Sep 17 00:00:00 2001 From: Mehant Kammakomati Date: Wed, 21 Sep 2022 18:55:45 +0530 Subject: [PATCH 2/2] fix: use snake case for starlark custom functions Signed-off-by: Mehant Kammakomati --- transformer/external/starlarktransformer.go | 32 ++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/transformer/external/starlarktransformer.go b/transformer/external/starlarktransformer.go index 7b5881c38..4f466d59e 100644 --- a/transformer/external/starlarktransformer.go +++ b/transformer/external/starlarktransformer.go @@ -70,11 +70,11 @@ const ( fspathRelFnName = "pathrel" // encryption functions - encaescbcpbkdfFnName = "encaescbcpbkdf" - encrsacertFnName = "encrsacert" + encAesCbcPbkdfFnName = "enc_aes_cbc_pbkdf" + encRsaCertFnName = "enc_rsa_cert" // archival functions - archtargzipstrFnName = "archtargzipstr" - archtarstrFnName = "archtarstr" + archTarGZipStrFnName = "arch_tar_gzip_str" + archTarStrFnName = "arch_tar_str" ) // Starlark implements transformer interface and is used to write simple external transformers @@ -346,8 +346,8 @@ func (t *Starlark) addCryptoModules() { t.StarGlobals["crypto"] = &starlarkstruct.Module{ Name: "crypto", Members: starlark.StringDict{ - encaescbcpbkdfFnName: t.getStarlarkEncAesCbcPbkdf(), - encrsacertFnName: t.getStarlarkEncRsaCert(), + encAesCbcPbkdfFnName: t.getStarlarkEncAesCbcPbkdf(), + encRsaCertFnName: t.getStarlarkEncRsaCert(), }, } } @@ -356,8 +356,8 @@ func (t *Starlark) addArchiveModules() { t.StarGlobals["archive"] = &starlarkstruct.Module{ Name: "archive", Members: starlark.StringDict{ - archtargzipstrFnName: t.getStarlarkArchTarGZipStr(), - archtarstrFnName: t.getStarlarkArchTarStr(), + archTarGZipStrFnName: t.getStarlarkArchTarGZipStr(), + archTarStrFnName: t.getStarlarkArchTarStr(), }, } } @@ -692,9 +692,9 @@ func (t *Starlark) getStarlarkFSPathRel() *starlark.Builtin { } func (t *Starlark) getStarlarkEncAesCbcPbkdf() *starlark.Builtin { - return starlark.NewBuiltin(encaescbcpbkdfFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + return starlark.NewBuiltin(encAesCbcPbkdfFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var key, data string - if err := starlark.UnpackPositionalArgs(encaescbcpbkdfFnName, args, kwargs, 2, &key, &data); err != nil { + if err := starlark.UnpackPositionalArgs(encAesCbcPbkdfFnName, args, kwargs, 2, &key, &data); err != nil { return nil, err } return starlark.String(common.EncryptAesCbcWithPbkdfWrapper(key, data)), nil @@ -702,9 +702,9 @@ func (t *Starlark) getStarlarkEncAesCbcPbkdf() *starlark.Builtin { } func (t *Starlark) getStarlarkEncRsaCert() *starlark.Builtin { - return starlark.NewBuiltin(encrsacertFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + return starlark.NewBuiltin(encRsaCertFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var certificate, data string - if err := starlark.UnpackPositionalArgs(encrsacertFnName, args, kwargs, 2, &certificate, &data); err != nil { + if err := starlark.UnpackPositionalArgs(encRsaCertFnName, args, kwargs, 2, &certificate, &data); err != nil { return nil, err } return starlark.String(common.EncryptRsaCertWrapper(certificate, data)), nil @@ -712,9 +712,9 @@ func (t *Starlark) getStarlarkEncRsaCert() *starlark.Builtin { } func (t *Starlark) getStarlarkArchTarGZipStr() *starlark.Builtin { - return starlark.NewBuiltin(archtargzipstrFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + return starlark.NewBuiltin(archTarGZipStrFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var srcDir string - if err := starlark.UnpackPositionalArgs(archtargzipstrFnName, args, kwargs, 1, &srcDir); err != nil { + if err := starlark.UnpackPositionalArgs(archTarGZipStrFnName, args, kwargs, 1, &srcDir); err != nil { return nil, err } if !t.Env.IsPathValid(srcDir) { @@ -725,9 +725,9 @@ func (t *Starlark) getStarlarkArchTarGZipStr() *starlark.Builtin { } func (t *Starlark) getStarlarkArchTarStr() *starlark.Builtin { - return starlark.NewBuiltin(archtarstrFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { + return starlark.NewBuiltin(archTarStrFnName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { var srcDir string - if err := starlark.UnpackPositionalArgs(archtarstrFnName, args, kwargs, 1, &srcDir); err != nil { + if err := starlark.UnpackPositionalArgs(archTarStrFnName, args, kwargs, 1, &srcDir); err != nil { return nil, err } if !t.Env.IsPathValid(srcDir) {