2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
package lua
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
|
|
|
envoy_lua_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/lua/v3"
|
|
|
|
envoy_http_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
|
|
|
|
envoy_resource_v3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
|
2023-01-30 21:35:26 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2023-02-06 17:14:35 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/extensioncommon"
|
2023-01-06 17:13:40 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
var _ extensioncommon.BasicExtension = (*lua)(nil)
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
type lua struct {
|
2023-05-26 18:10:31 +00:00
|
|
|
extensioncommon.BasicExtensionAdapter
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
ProxyType string
|
|
|
|
Listener string
|
|
|
|
Script string
|
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
// Constructor follows a specific function signature required for the extension registration.
|
|
|
|
func Constructor(ext api.EnvoyExtension) (extensioncommon.EnvoyExtender, error) {
|
|
|
|
var l lua
|
|
|
|
if name := ext.Name; name != api.BuiltinLuaExtension {
|
2023-01-06 17:13:40 +00:00
|
|
|
return nil, fmt.Errorf("expected extension name 'lua' but got %q", name)
|
|
|
|
}
|
2023-01-30 21:35:26 +00:00
|
|
|
if err := l.fromArguments(ext.Arguments); err != nil {
|
|
|
|
return nil, err
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
2023-01-30 21:35:26 +00:00
|
|
|
return &extensioncommon.BasicEnvoyExtender{
|
|
|
|
Extension: &l,
|
|
|
|
}, nil
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (l *lua) fromArguments(args map[string]interface{}) error {
|
|
|
|
if err := mapstructure.Decode(args, l); err != nil {
|
|
|
|
return fmt.Errorf("error decoding extension arguments: %v", err)
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
2023-01-30 21:35:26 +00:00
|
|
|
return l.validate()
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 21:35:26 +00:00
|
|
|
func (l *lua) validate() error {
|
|
|
|
var resultErr error
|
|
|
|
if l.Script == "" {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("missing Script value"))
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
2023-01-30 21:35:26 +00:00
|
|
|
if l.ProxyType != "connect-proxy" {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("unexpected ProxyType %q", l.ProxyType))
|
|
|
|
}
|
|
|
|
if l.Listener != "inbound" && l.Listener != "outbound" {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("unexpected Listener %q", l.Listener))
|
|
|
|
}
|
|
|
|
return resultErr
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanApply determines if the extension can apply to the given extension configuration.
|
2023-01-30 21:35:26 +00:00
|
|
|
func (l *lua) CanApply(config *extensioncommon.RuntimeConfig) bool {
|
2023-05-23 11:55:06 +00:00
|
|
|
return string(config.Kind) == l.ProxyType
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:53:42 +00:00
|
|
|
func (l *lua) matchesListenerDirection(p extensioncommon.FilterPayload) bool {
|
|
|
|
isInboundListener := p.IsInbound()
|
2023-05-23 11:55:06 +00:00
|
|
|
return (!isInboundListener && l.Listener == "outbound") || (isInboundListener && l.Listener == "inbound")
|
2023-01-06 17:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchFilter inserts a lua filter directly prior to envoy.filters.http.router.
|
2023-05-30 18:53:42 +00:00
|
|
|
func (l *lua) PatchFilter(p extensioncommon.FilterPayload) (*envoy_listener_v3.Filter, bool, error) {
|
|
|
|
filter := p.Message
|
2023-05-23 11:55:06 +00:00
|
|
|
// Make sure filter matches extension config.
|
2023-05-30 18:53:42 +00:00
|
|
|
if !l.matchesListenerDirection(p) {
|
2023-05-23 11:55:06 +00:00
|
|
|
return filter, false, nil
|
|
|
|
}
|
|
|
|
|
2023-01-06 17:13:40 +00:00
|
|
|
if filter.Name != "envoy.filters.network.http_connection_manager" {
|
|
|
|
return filter, false, nil
|
|
|
|
}
|
|
|
|
if typedConfig := filter.GetTypedConfig(); typedConfig == nil {
|
|
|
|
return filter, false, errors.New("error getting typed config for http filter")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := envoy_resource_v3.GetHTTPConnectionManager(filter)
|
|
|
|
if config == nil {
|
|
|
|
return filter, false, errors.New("error unmarshalling filter")
|
|
|
|
}
|
2023-04-06 21:12:07 +00:00
|
|
|
luaHttpFilter, err := extensioncommon.MakeEnvoyHTTPFilter(
|
2023-01-06 17:13:40 +00:00
|
|
|
"envoy.filters.http.lua",
|
|
|
|
&envoy_lua_v3.Lua{
|
2023-01-30 21:35:26 +00:00
|
|
|
InlineCode: l.Script,
|
2023-01-06 17:13:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return filter, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
changedFilters = make([]*envoy_http_v3.HttpFilter, 0, len(config.HttpFilters)+1)
|
|
|
|
changed bool
|
|
|
|
)
|
|
|
|
|
|
|
|
// We need to be careful about overwriting http filters completely because
|
|
|
|
// http filters validates intentions with the RBAC filter. This inserts the
|
|
|
|
// lua filter before envoy.filters.http.router while keeping everything
|
|
|
|
// else intact.
|
|
|
|
for _, httpFilter := range config.HttpFilters {
|
|
|
|
if httpFilter.Name == "envoy.filters.http.router" {
|
|
|
|
changedFilters = append(changedFilters, luaHttpFilter)
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
changedFilters = append(changedFilters, httpFilter)
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
config.HttpFilters = changedFilters
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
newFilter, err := extensioncommon.MakeFilter("envoy.filters.network.http_connection_manager", config)
|
2023-01-06 17:13:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return filter, false, errors.New("error making new filter")
|
|
|
|
}
|
|
|
|
|
|
|
|
return newFilter, true, nil
|
|
|
|
}
|