@@ -27,35 +27,39 @@ import (
2727)
2828
2929func init () {
30- Register ("envoy.http11_proxy_transport_socket.proxy_address" , ProxyAddressConvertor {})
30+ Register ("envoy.http11_proxy_transport_socket.proxy_address" , proxyAddressConvertor {})
3131}
3232
3333var (
3434 // metadataregistry is a map from proto type to Converter.
35- metadataregistry = make (map [string ]Converter )
35+ metadataregistry = make (map [string ]converter )
3636)
3737
3838// MetadataValue is the interface for a converted metadata value. It is
3939// implemented by concrete types that hold the converted metadata.
4040type MetadataValue interface {
41+ // Type returns the type.
4142 Type () string
4243}
4344
44- // Converter is the interface for a metadata converter. It is implemented by
45+ // converter is the interface for a metadata converter. It is implemented by
4546// concrete types that convert raw bytes into a MetadataValue.
46- type Converter interface {
47- // Convert parses the raw bytes of an Any proto into a MetadataValue.
48- Convert ([]byte ) (MetadataValue , error )
47+ type converter interface {
48+ // convert parses the proto serialized bytes of an Any proto or
49+ // google.protobuf.Struct into a MetadataValue. The bytes of an Any proto
50+ // are from the value field, which has proto serialized bytes of the
51+ // protobuf message type specified by the type_url field.
52+ convert ([]byte ) (MetadataValue , error )
4953}
5054
5155// Register registers the converter to the map keyed on a proto type. Must be
5256// called at init time. Not thread safe.
53- func Register (protoType string , c Converter ) {
57+ func Register (protoType string , c converter ) {
5458 metadataregistry [protoType ] = c
5559}
5660
5761// ConverterForType retrieves a converter based on key given.
58- func ConverterForType (typeURL string ) Converter {
62+ func ConverterForType (typeURL string ) converter {
5963 return metadataregistry [typeURL ]
6064}
6165
@@ -66,9 +70,11 @@ type JSONMetadata struct {
6670 Data json.RawMessage
6771}
6872
69- // ProxyAddressMetadataValue holds the address parsed from A.86 metadata.
73+ // ProxyAddressMetadataValue holds the address parsed from the
74+ // envoy.config.core.v3.Address proto message, as specified in gRFC A86.
7075type ProxyAddressMetadataValue struct {
71- MetadataValue
76+ // Address stores the proxy address configured (A86). It will be in the form
77+ // of host:port. It has to be either IPv6 or IPv4.
7278 Address string
7379}
7480
@@ -78,11 +84,11 @@ func (ProxyAddressMetadataValue) Type() string {
7884}
7985
8086// ProxyAddressConvertor implements the converter for A86 (Proxy Address) metadata.
81- type ProxyAddressConvertor struct {}
87+ type proxyAddressConvertor struct {}
8288
83- // Convert parses the raw bytes of an Any proto containing an Address proto into
84- // a ProxyAddressMetadataValue.
85- func (ProxyAddressConvertor ) Convert (anyBytes []byte ) (MetadataValue , error ) {
89+ // Convert parses the bytes from the value field of an Any proto containing an
90+ // Address proto into a ProxyAddressMetadataValue.
91+ func (proxyAddressConvertor ) convert (anyBytes []byte ) (MetadataValue , error ) {
8692 addressProto := & v3corepb.Address {}
8793 if err := proto .Unmarshal (anyBytes , addressProto ); err != nil {
8894 return nil , fmt .Errorf ("failed to unmarshal resource: %v" , err )
@@ -91,15 +97,16 @@ func (ProxyAddressConvertor) Convert(anyBytes []byte) (MetadataValue, error) {
9197 if socketaddress == nil {
9298 return nil , fmt .Errorf ("no socket_address field in metadata" )
9399 }
100+ if _ , err := netip .ParseAddr (socketaddress .GetAddress ()); err != nil {
101+ return nil , fmt .Errorf ("address field is not a valid IPv4 or IPv6 address: %q" , socketaddress .GetAddress ())
102+ }
94103 portvalue := socketaddress .GetPortValue ()
95104 if portvalue == 0 {
96105 return nil , fmt .Errorf ("port value not set in socket_address" )
97106 }
98- if _ , err := netip .ParseAddr (socketaddress .GetAddress ()); err != nil {
99- return nil , fmt .Errorf ("address field is not a valid IPv4 or IPv6 address: %q" , socketaddress .GetAddress ())
100- }
107+
101108 metadata := ProxyAddressMetadataValue {
102- Address : socketaddress . Address ,
109+ Address : parseAddress ( socketaddress ) ,
103110 }
104111 return metadata , nil
105112}
0 commit comments