mirror of
https://github.com/fatedier/frp.git
synced 2026-08-04 20:12:55 +08:00
fix(frpc): respect feature gates in verify (#5465)
This commit is contained in:
@@ -51,14 +51,51 @@ func (v *ConfigValidator) ValidateClientCommonConfig(c *v1.ClientCommonConfig) (
|
||||
}
|
||||
|
||||
func validateFeatureGates(c *v1.ClientCommonConfig) (Warning, error) {
|
||||
gates := featuregate.NewFeatureGate()
|
||||
if err := gates.SetFromMap(c.FeatureGates); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.VirtualNet.Address != "" {
|
||||
if !featuregate.Enabled(featuregate.VirtualNet) {
|
||||
if !gates.Enabled(featuregate.VirtualNet) {
|
||||
return nil, fmt.Errorf("VirtualNet feature is not enabled; enable it by setting the appropriate feature gate flag")
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// ClientConfigRequirements describes runtime capabilities needed by a client configuration.
|
||||
type ClientConfigRequirements struct {
|
||||
VirtualNet bool
|
||||
}
|
||||
|
||||
// GetClientConfigRequirements returns the runtime capabilities needed by a client configuration.
|
||||
func GetClientConfigRequirements(
|
||||
common *v1.ClientCommonConfig,
|
||||
proxyCfgs []v1.ProxyConfigurer,
|
||||
visitorCfgs []v1.VisitorConfigurer,
|
||||
) ClientConfigRequirements {
|
||||
requirements := ClientConfigRequirements{}
|
||||
if common != nil && common.VirtualNet.Address != "" {
|
||||
requirements.VirtualNet = true
|
||||
}
|
||||
for _, cfg := range proxyCfgs {
|
||||
if cfg.GetBaseConfig().Plugin.Type == v1.PluginVirtualNet {
|
||||
requirements.VirtualNet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !requirements.VirtualNet {
|
||||
for _, cfg := range visitorCfgs {
|
||||
if cfg.GetBaseConfig().Plugin.Type == v1.VisitorPluginVirtualNet {
|
||||
requirements.VirtualNet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return requirements
|
||||
}
|
||||
|
||||
func (v *ConfigValidator) validateAuthConfig(c *v1.AuthClientConfig) (Warning, error) {
|
||||
var errs error
|
||||
if !slices.Contains(SupportedAuthMethods, c.Method) {
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// Copyright 2026 The frp Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/fatedier/frp/pkg/policy/featuregate"
|
||||
"github.com/fatedier/frp/pkg/policy/security"
|
||||
)
|
||||
|
||||
func validateClientFeatureGates(t *testing.T, gates map[string]bool, virtualNetAddress string) error {
|
||||
t.Helper()
|
||||
|
||||
cfg := &v1.ClientCommonConfig{
|
||||
FeatureGates: gates,
|
||||
VirtualNet: v1.VirtualNetConfig{
|
||||
Address: virtualNetAddress,
|
||||
},
|
||||
}
|
||||
require.NoError(t, cfg.Complete())
|
||||
|
||||
_, err := NewConfigValidator(security.NewUnsafeFeatures(nil)).ValidateClientCommonConfig(cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func TestValidateClientFeatureGates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
featureGates map[string]bool
|
||||
virtualNetAddress string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "VirtualNet enabled",
|
||||
featureGates: map[string]bool{"VirtualNet": true},
|
||||
virtualNetAddress: "100.86.0.4/24",
|
||||
},
|
||||
{
|
||||
name: "VirtualNet explicitly disabled",
|
||||
featureGates: map[string]bool{"VirtualNet": false},
|
||||
virtualNetAddress: "100.86.0.4/24",
|
||||
wantErr: "VirtualNet feature is not enabled",
|
||||
},
|
||||
{
|
||||
name: "VirtualNet disabled by default",
|
||||
virtualNetAddress: "100.86.0.4/24",
|
||||
wantErr: "VirtualNet feature is not enabled",
|
||||
},
|
||||
{
|
||||
name: "unknown feature gate",
|
||||
featureGates: map[string]bool{"UnknownFeature": true},
|
||||
wantErr: "unrecognized feature gate: UnknownFeature",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := validateClientFeatureGates(t, tc.featureGates, tc.virtualNetAddress)
|
||||
if tc.wantErr == "" {
|
||||
require.NoError(t, err)
|
||||
return
|
||||
}
|
||||
require.ErrorContains(t, err, tc.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClientConfigRequirements(t *testing.T) {
|
||||
virtualNetProxy := &v1.STCPProxyConfig{
|
||||
ProxyBaseConfig: v1.ProxyBaseConfig{
|
||||
ProxyBackend: v1.ProxyBackend{
|
||||
Plugin: v1.TypedClientPluginOptions{Type: v1.PluginVirtualNet},
|
||||
},
|
||||
},
|
||||
}
|
||||
virtualNetVisitor := &v1.STCPVisitorConfig{
|
||||
VisitorBaseConfig: v1.VisitorBaseConfig{
|
||||
Plugin: v1.TypedVisitorPluginOptions{Type: v1.VisitorPluginVirtualNet},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
common *v1.ClientCommonConfig
|
||||
proxies []v1.ProxyConfigurer
|
||||
visitors []v1.VisitorConfigurer
|
||||
wantVNet bool
|
||||
}{
|
||||
{name: "no requirements"},
|
||||
{
|
||||
name: "common VirtualNet address",
|
||||
common: &v1.ClientCommonConfig{VirtualNet: v1.VirtualNetConfig{Address: "100.86.0.4/24"}},
|
||||
wantVNet: true,
|
||||
},
|
||||
{name: "VirtualNet proxy", proxies: []v1.ProxyConfigurer{virtualNetProxy}, wantVNet: true},
|
||||
{name: "VirtualNet visitor", visitors: []v1.VisitorConfigurer{virtualNetVisitor}, wantVNet: true},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := GetClientConfigRequirements(tc.common, tc.proxies, tc.visitors)
|
||||
require.Equal(t, tc.wantVNet, got.VirtualNet)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateClientFeatureGatesAreConfigScoped(t *testing.T) {
|
||||
defaultGatesBefore := featuregate.DefaultFeatureGates.String()
|
||||
|
||||
require.NoError(t, validateClientFeatureGates(
|
||||
t,
|
||||
map[string]bool{"VirtualNet": true},
|
||||
"100.86.0.4/24",
|
||||
))
|
||||
require.Equal(t, defaultGatesBefore, featuregate.DefaultFeatureGates.String())
|
||||
|
||||
err := validateClientFeatureGates(
|
||||
t,
|
||||
map[string]bool{"VirtualNet": false},
|
||||
"100.86.0.4/24",
|
||||
)
|
||||
require.ErrorContains(t, err, "VirtualNet feature is not enabled")
|
||||
require.Equal(t, defaultGatesBefore, featuregate.DefaultFeatureGates.String())
|
||||
}
|
||||
Reference in New Issue
Block a user