2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-02-08 02:56:15 +00:00
|
|
|
package localratelimit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
|
|
|
|
envoy_ratelimit "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/local_ratelimit/v3"
|
|
|
|
envoy_http_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
|
|
|
|
envoy_type_v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
|
|
|
|
envoy_resource_v3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2023-06-05 20:08:39 +00:00
|
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
2023-02-08 02:56:15 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/envoyextensions/extensioncommon"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ratelimit struct {
|
2023-05-26 18:10:31 +00:00
|
|
|
extensioncommon.BasicExtensionAdapter
|
|
|
|
|
2023-02-08 02:56:15 +00:00
|
|
|
ProxyType string
|
|
|
|
|
|
|
|
// Token bucket of the rate limit
|
|
|
|
MaxTokens *int
|
|
|
|
TokensPerFill *int
|
|
|
|
FillInterval *int
|
|
|
|
|
|
|
|
// Percent of requests to be rate limited
|
|
|
|
FilterEnabled *uint32
|
|
|
|
FilterEnforced *uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ extensioncommon.BasicExtension = (*ratelimit)(nil)
|
|
|
|
|
|
|
|
// Constructor follows a specific function signature required for the extension registration.
|
|
|
|
func Constructor(ext api.EnvoyExtension) (extensioncommon.EnvoyExtender, error) {
|
|
|
|
var r ratelimit
|
|
|
|
if name := ext.Name; name != api.BuiltinLocalRatelimitExtension {
|
|
|
|
return nil, fmt.Errorf("expected extension name 'ratelimit' but got %q", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.fromArguments(ext.Arguments); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &extensioncommon.BasicEnvoyExtender{
|
|
|
|
Extension: &r,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ratelimit) fromArguments(args map[string]interface{}) error {
|
|
|
|
if err := mapstructure.Decode(args, r); err != nil {
|
|
|
|
return fmt.Errorf("error decoding extension arguments: %v", err)
|
|
|
|
}
|
2023-06-12 17:47:31 +00:00
|
|
|
if r.ProxyType == "" {
|
|
|
|
r.ProxyType = string(api.ServiceKindConnectProxy)
|
|
|
|
}
|
2023-02-08 02:56:15 +00:00
|
|
|
return r.validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ratelimit) validate() error {
|
|
|
|
var resultErr error
|
|
|
|
|
|
|
|
// NOTE: Envoy requires FillInterval value must be greater than 0.
|
|
|
|
// If unset, it is considered as 0.
|
|
|
|
if r.FillInterval == nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("FillInterval(in second) is missing"))
|
|
|
|
} else if *r.FillInterval <= 0 {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("FillInterval(in second) must be greater than 0, got %d", *r.FillInterval))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Envoy requires MaxToken value must be greater than 0.
|
|
|
|
// If unset, it is considered as 0.
|
|
|
|
if r.MaxTokens == nil {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("MaxTokens is missing"))
|
|
|
|
} else if *r.MaxTokens <= 0 {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("MaxTokens must be greater than 0, got %d", r.MaxTokens))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokensPerFill is allowed to unset. In this case, envoy
|
|
|
|
// uses its default value, which is 1.
|
|
|
|
if r.TokensPerFill != nil && *r.TokensPerFill <= 0 {
|
|
|
|
resultErr = multierror.Append(resultErr, fmt.Errorf("TokensPerFill must be greater than 0, got %d", *r.TokensPerFill))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateProxyType(r.ProxyType); err != nil {
|
|
|
|
resultErr = multierror.Append(resultErr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanApply determines if the extension can apply to the given extension configuration.
|
|
|
|
func (p *ratelimit) CanApply(config *extensioncommon.RuntimeConfig) bool {
|
2023-05-23 11:55:06 +00:00
|
|
|
return string(config.Kind) == p.ProxyType
|
2023-02-08 02:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PatchFilter inserts a http local rate_limit filter at the head of
|
|
|
|
// envoy.filters.network.http_connection_manager filters
|
2023-05-30 18:53:42 +00:00
|
|
|
func (r ratelimit) PatchFilter(p extensioncommon.FilterPayload) (*envoy_listener_v3.Filter, bool, error) {
|
|
|
|
filter := p.Message
|
2023-05-23 11:55:06 +00:00
|
|
|
// rate limit is only applied to the inbound listener of the service itself
|
|
|
|
// since the limit is aggregated from all downstream connections.
|
2023-05-30 18:53:42 +00:00
|
|
|
if !p.IsInbound() {
|
2023-05-23 11:55:06 +00:00
|
|
|
return filter, false, nil
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:56:15 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenBucket := envoy_type_v3.TokenBucket{}
|
|
|
|
|
2023-05-30 18:53:42 +00:00
|
|
|
if r.TokensPerFill != nil {
|
2023-06-05 20:08:39 +00:00
|
|
|
tokenBucket.TokensPerFill = &wrapperspb.UInt32Value{
|
2023-05-30 18:53:42 +00:00
|
|
|
Value: uint32(*r.TokensPerFill),
|
2023-02-08 02:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-30 18:53:42 +00:00
|
|
|
if r.MaxTokens != nil {
|
|
|
|
tokenBucket.MaxTokens = uint32(*r.MaxTokens)
|
2023-02-08 02:56:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 18:53:42 +00:00
|
|
|
if r.FillInterval != nil {
|
|
|
|
tokenBucket.FillInterval = durationpb.New(time.Duration(*r.FillInterval) * time.Second)
|
2023-02-08 02:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var FilterEnabledDefault *envoy_core_v3.RuntimeFractionalPercent
|
2023-05-30 18:53:42 +00:00
|
|
|
if r.FilterEnabled != nil {
|
2023-02-08 02:56:15 +00:00
|
|
|
FilterEnabledDefault = &envoy_core_v3.RuntimeFractionalPercent{
|
|
|
|
DefaultValue: &envoy_type_v3.FractionalPercent{
|
2023-05-30 18:53:42 +00:00
|
|
|
Numerator: *r.FilterEnabled,
|
2023-02-08 02:56:15 +00:00
|
|
|
Denominator: envoy_type_v3.FractionalPercent_HUNDRED,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var FilterEnforcedDefault *envoy_core_v3.RuntimeFractionalPercent
|
2023-05-30 18:53:42 +00:00
|
|
|
if r.FilterEnforced != nil {
|
2023-02-08 02:56:15 +00:00
|
|
|
FilterEnforcedDefault = &envoy_core_v3.RuntimeFractionalPercent{
|
|
|
|
DefaultValue: &envoy_type_v3.FractionalPercent{
|
2023-05-30 18:53:42 +00:00
|
|
|
Numerator: *r.FilterEnforced,
|
2023-02-08 02:56:15 +00:00
|
|
|
Denominator: envoy_type_v3.FractionalPercent_HUNDRED,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
ratelimitHttpFilter, err := extensioncommon.MakeEnvoyHTTPFilter(
|
2023-02-08 02:56:15 +00:00
|
|
|
"envoy.filters.http.local_ratelimit",
|
|
|
|
&envoy_ratelimit.LocalRateLimit{
|
|
|
|
TokenBucket: &tokenBucket,
|
|
|
|
StatPrefix: "local_ratelimit",
|
|
|
|
FilterEnabled: FilterEnabledDefault,
|
|
|
|
FilterEnforced: FilterEnforcedDefault,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return filter, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
changedFilters := make([]*envoy_http_v3.HttpFilter, 0, len(config.HttpFilters)+1)
|
|
|
|
|
|
|
|
// The ratelimitHttpFilter is inserted as the first element of the http
|
|
|
|
// filter chain.
|
|
|
|
changedFilters = append(changedFilters, ratelimitHttpFilter)
|
|
|
|
changedFilters = append(changedFilters, config.HttpFilters...)
|
|
|
|
config.HttpFilters = changedFilters
|
|
|
|
|
2023-04-06 21:12:07 +00:00
|
|
|
newFilter, err := extensioncommon.MakeFilter("envoy.filters.network.http_connection_manager", config)
|
2023-02-08 02:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return filter, false, errors.New("error making new filter")
|
|
|
|
}
|
|
|
|
|
|
|
|
return newFilter, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateProxyType(t string) error {
|
2023-06-12 17:47:31 +00:00
|
|
|
if t != string(api.ServiceKindConnectProxy) {
|
2023-02-08 02:56:15 +00:00
|
|
|
return fmt.Errorf("unexpected ProxyType %q", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|