Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tengo http support #71

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
73 changes: 71 additions & 2 deletions plugins/internal/tengoutil/secure_script.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package tengoutil

import (
"context"
"fmt"
"io"
"net/http"
"time"

"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
Expand All @@ -15,10 +19,12 @@
func NewSecureScript(input []byte, globals map[string]interface{}) (*tengo.Script, error) {
s := tengo.NewScript(input)

s.SetImports(stdlib.GetModuleMap(
modules := stdlib.GetModuleMap(
// `os` is excluded, should *not* be importable from script.
"math", "text", "times", "rand", "fmt", "json", "base64", "hex", "enum",
))
)
modules.AddBuiltinModule("http", createHTTPModule())
s.SetImports(modules)
s.SetMaxAllocs(maxAllocs)
s.SetMaxConstObjects(maxConsts)

Expand All @@ -30,3 +36,66 @@

return s, nil
}

func createHTTPModule() map[string]tengo.Object {

Check failure on line 40 in plugins/internal/tengoutil/secure_script.go

View workflow job for this annotation

GitHub Actions / golangci

cognitive complexity 27 of func `createHTTPModule` is high (> 20) (gocognit)
return map[string]tengo.Object{
"get": &tengo.UserFunction{
Name: "get",
Value: func(args ...tengo.Object) (tengo.Object, error) {
irainia marked this conversation as resolved.
Show resolved Hide resolved
if len(args) < 1 || len(args) > 2 {
return nil, fmt.Errorf("expected 1 or 2 arguments, got %d", len(args))
}

url, ok := tengo.ToString(args[0])
if !ok {
return nil, fmt.Errorf("expected argument 1 (URL) to be a string")
}

headers := make(map[string]string)
if len(args) == 2 {
headerMap, ok := args[1].(*tengo.Map)
if !ok {
return nil, fmt.Errorf("expected argument 2 (headers) to be a map")
irainia marked this conversation as resolved.
Show resolved Hide resolved
}
for key, value := range headerMap.Value {
strValue, valueOk := tengo.ToString(value)
if !valueOk {
return nil, fmt.Errorf("header values must be strings")
irainia marked this conversation as resolved.
Show resolved Hide resolved
}
headers[key] = strValue
}
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
irainia marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

for key, value := range headers {
req.Header.Add(key, value)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return &tengo.Map{
Value: map[string]tengo.Object{
"body": &tengo.String{Value: string(body)},
"code": &tengo.Int{Value: int64(resp.StatusCode)},
},
}, nil
},
},
}
}
11 changes: 11 additions & 0 deletions plugins/internal/tengoutil/secure_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ func TestNewSecureScript(t *testing.T) {
_, err = s.Compile()
assert.NoError(t, err)
})

t.Run("Allows import of custom http module", func(t *testing.T) {
s, err := NewSecureScript(([]byte)(heredoc.Doc(`
http := import("http")
response := http.get("http://example.com")
Comment on lines +62 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response.body
`)), nil)
assert.NoError(t, err)
_, err = s.Compile()
assert.NoError(t, err)
})
}
Loading