-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Lua EnvoyExtensionPolicy
Signed-off-by: Rudrakh Panigrahi <[email protected]>
- Loading branch information
Showing
29 changed files
with
2,607 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright Envoy Gateway Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// The full text of the Apache license is available in the LICENSE file at | ||
// the root of the repo. | ||
|
||
package luavalidator | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"strings" | ||
|
||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
// mockData contains mocks of Envoy supported APIs for Lua filters. | ||
// Refer: https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/lua_filter#stream-handle-api | ||
// | ||
//go:embed mocks.lua | ||
var mockData []byte | ||
|
||
// LuaValidator validates user provided Lua for compatibility with Envoy supported Lua HTTP filter | ||
type LuaValidator struct { | ||
body string | ||
} | ||
|
||
// NewLuaValidator returns a LuaValidator for user provided Lua body | ||
func NewLuaValidator(body string) *LuaValidator { | ||
return &LuaValidator{ | ||
body: body, | ||
} | ||
} | ||
|
||
// Validate runs all validations for the LuaValidator | ||
func (l *LuaValidator) Validate() error { | ||
if !strings.Contains(l.body, "envoy_on_request") && !strings.Contains(l.body, "envoy_on_response") { | ||
return fmt.Errorf("expected one of envoy_on_request() or envoy_on_response() to be defined") | ||
} | ||
if strings.Contains(l.body, "envoy_on_request") { | ||
if err := l.runLua(string(mockData) + "\n" + l.body + "\nenvoy_on_request(StreamHandle)"); err != nil { | ||
return fmt.Errorf("failed to mock run envoy_on_request: %w", err) | ||
} | ||
} | ||
if strings.Contains(l.body, "envoy_on_response") { | ||
if err := l.runLua(string(mockData) + "\n" + l.body + "\nenvoy_on_response(StreamHandle)"); err != nil { | ||
return fmt.Errorf("failed to mock run envoy_on_response: %w", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// runLua interprets and runs the provided Lua body in runtime | ||
func (l *LuaValidator) runLua(body string) error { | ||
L := lua.NewState() | ||
defer L.Close() | ||
if err := L.DoString(body); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.