github.com/tetratelabs/proxy-wasm-go-sdk@v0.23.1-0.20240517021853-021aa9cf78e8/properties/wasm.go (about)

     1  package properties
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
     8  )
     9  
    10  // This file hosts helper functions to retrieve wasm-related properties as described in:
    11  // https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes#wasm-attributes
    12  
    13  var (
    14  	pluginName                = []string{"plugin_name"}
    15  	pluginRootId              = []string{"plugin_root_id"}
    16  	pluginVmId                = []string{"plugin_vm_id"}
    17  	clusterName               = []string{"cluster_name"}
    18  	routeName                 = []string{"route_name"}
    19  	listenerDirection         = []string{"listener_direction"}
    20  	nodeId                    = []string{"node", "id"}
    21  	nodeCluster               = []string{"node", "cluster"}
    22  	nodeDynamicParams         = []string{"node", "dynamic_parameters", "params"}
    23  	nodeLocalityRegion        = []string{"node", "locality", "region"}
    24  	nodeLocalityZone          = []string{"node", "locality", "zone"}
    25  	nodeLocalitySubzone       = []string{"node", "locality", "subzone"}
    26  	nodeUserAgentName         = []string{"node", "user_agent_name"}
    27  	nodeUserAgentVersion      = []string{"node", "user_agent_version"}
    28  	nodeUserAgentBuildVersion = []string{"node", "user_agent_build_version", "metadata"}
    29  	nodeExtensions            = []string{"node", "extensions"}
    30  	nodeClientFeatures        = []string{"node", "client_features"}
    31  	nodeListeningAddresses    = []string{"node", "listening_addresses"}
    32  	clusterMetadata           = []string{"node", "cluster_metadata", "filter_metadata", "istio"}
    33  	listenerMetadata          = []string{"node", "listener_metadata", "filter_metadata", "istio"}
    34  	routeMetadata             = []string{"node", "route_metadata", "filter_metadata", "istio"}
    35  	upstreamHostMetadata      = []string{"node", "upstream_host_metadata", "filter_metadata", "istio"}
    36  )
    37  
    38  // GetPluginName returns the plugin name.
    39  //
    40  // This matches <metadata.name>.<metadata.namespace> in an istio WasmPlugin CR.
    41  func GetPluginName() (string, error) {
    42  	return getPropertyString(pluginName)
    43  }
    44  
    45  // GetPluginRootId returns the plugin root id.
    46  //
    47  // This matches the <spec.pluginName> in the istio WasmPlugin CR.
    48  func GetPluginRootId() (string, error) {
    49  	return getPropertyString(pluginRootId)
    50  }
    51  
    52  // GetPluginVmId returns the plugin vm id.
    53  func GetPluginVmId() (string, error) {
    54  	return getPropertyString(pluginVmId)
    55  }
    56  
    57  // GetClusterName returns the upstream cluster name.
    58  //
    59  // Example value: "outbound|80||httpbin.org".
    60  func GetClusterName() (string, error) {
    61  	return getPropertyString(clusterName)
    62  }
    63  
    64  // GetRouteName returns the route name, only available in the response path (cfr getXdsRouteName()).
    65  //
    66  // This matches the <spec.http.name> in the istio VirtualService CR.
    67  func GetRouteName() (string, error) {
    68  	return getPropertyString(routeName)
    69  }
    70  
    71  // GetListenerDirection returns the listener direction.
    72  //
    73  // Possible values are:
    74  //
    75  //   - UNSPECIFIED: 0 (default option is unspecified)
    76  //   - INBOUND: 1 (⁣the transport is used for incoming traffic)
    77  //   - OUTBOUND: 2 (the transport is used for outgoing traffic)
    78  func GetListenerDirection() (EnvoyTrafficDirection, error) {
    79  	result, err := getPropertyUint64(listenerDirection)
    80  	if err != nil {
    81  		return EnvoyTrafficDirectionUnspecified, err
    82  	}
    83  	return EnvoyTrafficDirection(int(result)), nil
    84  }
    85  
    86  // GetNodeId returns the node id, an opaque node identifier for the Envoy node. This also
    87  // provides the local service node name. It should be set if any of the following features
    88  // are used: statsd, CDS, and HTTP tracing, either in this message or via --service-node
    89  //
    90  // Example value:
    91  // router~10.244.0.22~istio-ingress-6d78c67d85-qsbtz.istio-ingress~istio-ingress.svc.cluster.local
    92  func GetNodeId() (string, error) {
    93  	return getPropertyString(nodeId)
    94  }
    95  
    96  // GetNodeCluster returns the node cluster, which defines the local service cluster
    97  // name where envoy is running. Though optional, it should be set if any of the following
    98  // features are used: statsd, health check cluster verification, runtime override directory,
    99  // user agent addition, HTTP global rate limiting, CDS, and HTTP tracing, either in this
   100  // message or via --service-cluster
   101  //
   102  // Example value: istio-ingress.istio-ingress
   103  func GetNodeCluster() (string, error) {
   104  	return getPropertyString(nodeCluster)
   105  }
   106  
   107  // GetNodeDynamicParams returns the node dynamic parameters. These may vary at
   108  // runtime (unlike other fields in this message). For example, the xDS client may have a
   109  // shared identifier that changes during the lifetime of the xDS client. In Envoy, this
   110  // would be achieved by updating the dynamic context on the Server::Instance’s LocalInfo
   111  // context provider. The shard ID dynamic parameter then appears in this field during
   112  // future discovery requests
   113  func GetNodeDynamicParams() (string, error) {
   114  	return getPropertyString(nodeDynamicParams)
   115  }
   116  
   117  // GetNodeLocality returns the node locality.
   118  func GetNodeLocality() (EnvoyLocality, error) {
   119  	result := EnvoyLocality{}
   120  	var errors []string
   121  	var successCount int
   122  
   123  	region, err := getPropertyString(nodeLocalityRegion)
   124  	if err != nil {
   125  		errors = append(errors, err.Error())
   126  	} else {
   127  		result.Region = region
   128  		successCount++
   129  	}
   130  
   131  	zone, err := getPropertyString(nodeLocalityZone)
   132  	if err != nil {
   133  		errors = append(errors, err.Error())
   134  	} else {
   135  		result.Zone = zone
   136  		successCount++
   137  	}
   138  
   139  	subzone, err := getPropertyString(nodeLocalitySubzone)
   140  	if err != nil {
   141  		errors = append(errors, err.Error())
   142  	} else {
   143  		result.Subzone = subzone
   144  		successCount++
   145  	}
   146  
   147  	if successCount == 0 {
   148  		return result, fmt.Errorf(strings.Join(errors, "; "))
   149  	}
   150  
   151  	return result, nil
   152  }
   153  
   154  // GetNodeUserAgentName returns the node user agent name.
   155  //
   156  // Example: “envoy” or “grpc”.
   157  func GetNodeUserAgentName() (string, error) {
   158  	return getPropertyString(nodeUserAgentName)
   159  }
   160  
   161  // GetNodeUserAgentVersion returns the node user agent version.
   162  //
   163  // Example “1.12.2” or “abcd1234”, or “SpecialEnvoyBuild”.
   164  func GetNodeUserAgentVersion() (string, error) {
   165  	return getPropertyString(nodeUserAgentVersion)
   166  }
   167  
   168  // GetNodeUserAgentBuildVersion returns the node user agent build version.
   169  func GetNodeUserAgentBuildVersion() (string, error) {
   170  	return getPropertyString(nodeUserAgentBuildVersion)
   171  }
   172  
   173  // GetNodeExtensions returns the node extensions.
   174  func GetNodeExtensions() ([]EnvoyExtension, error) {
   175  	result := make([]EnvoyExtension, 0)
   176  	extensionsRawSlice, err := getPropertyByteSliceSlice(nodeExtensions)
   177  	if err != nil {
   178  		return []EnvoyExtension{}, err
   179  	}
   180  
   181  	for _, extensionRawSlice := range extensionsRawSlice {
   182  		if extensionRawSlice == nil {
   183  			continue
   184  		}
   185  		extensionStringSlice := deserializeProtoStringSlice(extensionRawSlice)
   186  		extension := EnvoyExtension{}
   187  
   188  		if len(extensionStringSlice) > 0 {
   189  			extension.Name = string(extensionStringSlice[0])
   190  		}
   191  		if len(extensionStringSlice) > 1 {
   192  			extension.Category = string(extensionStringSlice[1])
   193  		}
   194  		if len(extensionStringSlice) > 2 {
   195  			extenstionTypeUrls := []string{}
   196  			extenstionTypeUrls = append(extenstionTypeUrls, extensionStringSlice[2:]...)
   197  			extension.TypeUrls = extenstionTypeUrls
   198  		}
   199  
   200  		result = append(result, extension)
   201  	}
   202  
   203  	return result, nil
   204  }
   205  
   206  // GetNodeClientFeatures returns the node client features. These are well known features
   207  // described in the Envoy API repository for a given major version of an API. Client
   208  // features use reverse DNS naming scheme, for example "com.acme.feature".
   209  func GetNodeClientFeatures() ([]string, error) {
   210  	result, err := proxywasm.GetProperty(nodeClientFeatures)
   211  	if err != nil {
   212  		return []string{}, err
   213  	}
   214  	return deserializeProtoStringSlice(result), nil
   215  }
   216  
   217  // GetNodeListeningAddresses returns the node listening addresses.
   218  func GetNodeListeningAddresses() ([]string, error) {
   219  	return getPropertyStringSlice(nodeListeningAddresses)
   220  }
   221  
   222  // GetClusterMetadata returns the cluster metadata.
   223  func GetClusterMetadata() (IstioFilterMetadata, error) {
   224  	return getIstioFilterMetadata(clusterMetadata)
   225  }
   226  
   227  // GetListenerMetadata returns the listener metadata.
   228  func GetListenerMetadata() (IstioFilterMetadata, error) {
   229  	return getIstioFilterMetadata(listenerMetadata)
   230  }
   231  
   232  // GetRouteMetadata returns the route metadata.
   233  func GetRouteMetadata() (IstioFilterMetadata, error) {
   234  	return getIstioFilterMetadata(routeMetadata)
   235  }
   236  
   237  // GetUpstreamHostMetadata returns the upstream host metadata.
   238  func GetUpstreamHostMetadata() (IstioFilterMetadata, error) {
   239  	return getIstioFilterMetadata(upstreamHostMetadata)
   240  }