istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/core/extension/wasmplugin_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package extension
    16  
    17  import (
    18  	"testing"
    19  
    20  	core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
    21  	httpwasm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/wasm/v3"
    22  	networkwasm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/wasm/v3"
    23  	wasmextension "github.com/envoyproxy/go-control-plane/envoy/extensions/wasm/v3"
    24  	"github.com/google/go-cmp/cmp"
    25  	"google.golang.org/protobuf/testing/protocmp"
    26  	"google.golang.org/protobuf/types/known/anypb"
    27  	"google.golang.org/protobuf/types/known/durationpb"
    28  	"google.golang.org/protobuf/types/known/wrapperspb"
    29  
    30  	extensions "istio.io/api/extensions/v1alpha1"
    31  	"istio.io/istio/pilot/pkg/model"
    32  	"istio.io/istio/pilot/pkg/util/protoconv"
    33  )
    34  
    35  var (
    36  	someAuthNFilter = &model.WasmPluginWrapper{
    37  		Name:         "someAuthNFilter",
    38  		Namespace:    "istio-system",
    39  		ResourceName: "istio-system.someAuthNFilter",
    40  		WasmPlugin: &extensions.WasmPlugin{
    41  			Priority: &wrapperspb.Int32Value{Value: 1},
    42  		},
    43  	}
    44  	someAuthZFilter = &model.WasmPluginWrapper{
    45  		Name:         "someAuthZFilter",
    46  		Namespace:    "istio-system",
    47  		ResourceName: "istio-system.someAuthZFilter",
    48  		WasmPlugin: &extensions.WasmPlugin{
    49  			Priority: &wrapperspb.Int32Value{Value: 1000},
    50  		},
    51  	}
    52  	someNetworkFilter = &model.WasmPluginWrapper{
    53  		Name:         "someNetworkFilter",
    54  		Namespace:    "istio-system",
    55  		ResourceName: "istio-system.someNetworkFilter",
    56  		WasmPlugin: &extensions.WasmPlugin{
    57  			Priority: &wrapperspb.Int32Value{Value: 1000},
    58  			Type:     extensions.PluginType_NETWORK,
    59  		},
    60  	}
    61  )
    62  
    63  func TestInsertedExtensionConfigurations(t *testing.T) {
    64  	httpFilter := protoconv.MessageToAny(&httpwasm.Wasm{
    65  		Config: &wasmextension.PluginConfig{
    66  			Name:          "istio-system.someAuthNFilter",
    67  			Configuration: &anypb.Any{},
    68  			Vm: &wasmextension.PluginConfig_VmConfig{
    69  				VmConfig: &wasmextension.VmConfig{
    70  					Runtime: "envoy.wasm.runtime.v8",
    71  					Code: &core.AsyncDataSource{
    72  						Specifier: &core.AsyncDataSource_Remote{
    73  							Remote: &core.RemoteDataSource{
    74  								HttpUri: &core.HttpUri{
    75  									Uri: "oci:",
    76  									HttpUpstreamType: &core.HttpUri_Cluster{
    77  										Cluster: "_",
    78  									},
    79  									Timeout: &durationpb.Duration{
    80  										Seconds: 30,
    81  									},
    82  								},
    83  							},
    84  						},
    85  					},
    86  					EnvironmentVariables: &wasmextension.EnvironmentVariables{
    87  						KeyValues: map[string]string{
    88  							"ISTIO_META_WASM_PLUGIN_RESOURCE_VERSION": "",
    89  						},
    90  					},
    91  				},
    92  			},
    93  		},
    94  	})
    95  	networkFilter := protoconv.MessageToAny(&networkwasm.Wasm{
    96  		Config: &wasmextension.PluginConfig{
    97  			Name:          "istio-system.someNetworkFilter",
    98  			Configuration: &anypb.Any{},
    99  			Vm: &wasmextension.PluginConfig_VmConfig{
   100  				VmConfig: &wasmextension.VmConfig{
   101  					Runtime: "envoy.wasm.runtime.v8",
   102  					Code: &core.AsyncDataSource{
   103  						Specifier: &core.AsyncDataSource_Remote{
   104  							Remote: &core.RemoteDataSource{
   105  								HttpUri: &core.HttpUri{
   106  									Uri: "oci:",
   107  									HttpUpstreamType: &core.HttpUri_Cluster{
   108  										Cluster: "_",
   109  									},
   110  									Timeout: &durationpb.Duration{
   111  										Seconds: 30,
   112  									},
   113  								},
   114  							},
   115  						},
   116  					},
   117  					EnvironmentVariables: &wasmextension.EnvironmentVariables{
   118  						KeyValues: map[string]string{
   119  							"ISTIO_META_WASM_PLUGIN_RESOURCE_VERSION": "",
   120  						},
   121  					},
   122  				},
   123  			},
   124  		},
   125  	})
   126  	testCases := []struct {
   127  		name        string
   128  		wasmPlugins []*model.WasmPluginWrapper
   129  		names       []string
   130  		expectedECs []*core.TypedExtensionConfig
   131  	}{
   132  		{
   133  			name:        "empty",
   134  			wasmPlugins: []*model.WasmPluginWrapper{},
   135  			names:       []string{someAuthNFilter.Name},
   136  			expectedECs: []*core.TypedExtensionConfig{},
   137  		},
   138  		{
   139  			name: "authn",
   140  			wasmPlugins: []*model.WasmPluginWrapper{
   141  				someAuthNFilter,
   142  				someAuthZFilter,
   143  			},
   144  			names: []string{someAuthNFilter.Namespace + "." + someAuthNFilter.Name},
   145  			expectedECs: []*core.TypedExtensionConfig{
   146  				{
   147  					Name:        "istio-system.someAuthNFilter",
   148  					TypedConfig: httpFilter,
   149  				},
   150  			},
   151  		},
   152  		{
   153  			name: "network",
   154  			wasmPlugins: []*model.WasmPluginWrapper{
   155  				someNetworkFilter,
   156  			},
   157  			names: []string{
   158  				someNetworkFilter.Namespace + "." + someNetworkFilter.Name,
   159  			},
   160  			expectedECs: []*core.TypedExtensionConfig{
   161  				{
   162  					Name:        "istio-system.someNetworkFilter",
   163  					TypedConfig: networkFilter,
   164  				},
   165  			},
   166  		},
   167  		{
   168  			name: "combination of http and network",
   169  			wasmPlugins: []*model.WasmPluginWrapper{
   170  				someAuthNFilter,
   171  				someNetworkFilter,
   172  			},
   173  			names: []string{
   174  				someAuthNFilter.Namespace + "." + someAuthNFilter.Name,
   175  				someNetworkFilter.Namespace + "." + someNetworkFilter.Name,
   176  			},
   177  			expectedECs: []*core.TypedExtensionConfig{
   178  				{
   179  					Name:        "istio-system.someAuthNFilter",
   180  					TypedConfig: httpFilter,
   181  				},
   182  				{
   183  					Name:        "istio-system.someNetworkFilter",
   184  					TypedConfig: networkFilter,
   185  				},
   186  			},
   187  		},
   188  	}
   189  	for _, tc := range testCases {
   190  		t.Run(tc.name, func(t *testing.T) {
   191  			ecs := InsertedExtensionConfigurations(tc.wasmPlugins, tc.names, nil)
   192  			if diff := cmp.Diff(tc.expectedECs, ecs, protocmp.Transform()); diff != "" {
   193  				t.Fatal(diff)
   194  			}
   195  		})
   196  	}
   197  }