Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
New properties API completion (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
boeboe authored Oct 16, 2023
1 parent 84985ee commit 918caa5
Show file tree
Hide file tree
Showing 22 changed files with 3,534 additions and 206 deletions.
97 changes: 54 additions & 43 deletions properties/connection.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package properties

import "github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"

// This file hosts helper functions to retrieve connection-related properties as described in:
// https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/advanced/attributes#connection-attributes

Expand All @@ -26,140 +24,153 @@ var (

// GetDownstreamRemoteAddress returns the remote address of the downstream connection.
func GetDownstreamRemoteAddress() (string, error) {
downstreamRemoteAddress, err := getPropertyString(sourceAddress)
result, err := getPropertyString(sourceAddress)
if err != nil {
return "", err
}
return downstreamRemoteAddress, nil
return result, nil
}

// GetDownstreamRemotePort returns the remote port of the downstream connection.
func GetDownstreamRemotePort() (uint64, error) {
downstreamRemotePort, err := getPropertyUint64(sourcePort)
result, err := getPropertyUint64(sourcePort)
if err != nil {
return 0, err
}
return downstreamRemotePort, nil
return result, nil
}

// GetDownstreamLocalAddress returns the local address of the downstream connection.
func GetDownstreamLocalAddress() (string, error) {
downstreamLocalAddress, err := getPropertyString(destinationAddress)
result, err := getPropertyString(destinationAddress)
if err != nil {
return "", err
}
return downstreamLocalAddress, nil
return result, nil
}

// GetDownstreamLocalPort returns the local port of the downstream connection.
func GetDownstreamLocalPort() (uint64, error) {
downstreamLocalPort, err := getPropertyUint64(destinationPort)
result, err := getPropertyUint64(destinationPort)
if err != nil {
return 0, err
}
return downstreamLocalPort, nil
return result, nil
}

// GetDownstreamConnectionID returns the connection ID of the downstream connection.
func GetDownstreamConnectionID() (uint64, error) {
downstreamConnectionId, err := getPropertyUint64(connectionID)
result, err := getPropertyUint64(connectionID)
if err != nil {
return 0, err
}
return downstreamConnectionId, nil
return result, nil
}

// IsDownstreamConnectionTls returns true if the downstream connection is TLS.
func IsDownstreamConnectionTls() (bool, error) {
downstreamConnectionTls, err := getPropertyBool(connectionMtls)
result, err := getPropertyBool(connectionMtls)
if err != nil {
return false, err
}
return downstreamConnectionTls, nil
return result, nil
}

// GetDownstreamRequestedServerName returns the requested server name of the downstream connection.
// GetDownstreamRequestedServerName returns the requested server name of the
// downstream connection.
func GetDownstreamRequestedServerName() (string, error) {
downstreamRequestedServerName, err := getPropertyString(connectionRequestedServerName)
result, err := getPropertyString(connectionRequestedServerName)
if err != nil {
return "", err
}
return downstreamRequestedServerName, nil
return result, nil
}

// GetDownstreamTlsVersion returns the TLS version of the downstream connection.
func GetDownstreamTlsVersion() (string, error) {
downstreamTlsVersion, err := getPropertyString(connectionTlsVersion)
result, err := getPropertyString(connectionTlsVersion)
if err != nil {
return "", err
}
return downstreamTlsVersion, nil
return result, nil
}

// GetDownstreamSubjectLocalCertificate returns the subject field of the local certificate in the downstream TLS connection.
// GetDownstreamSubjectLocalCertificate returns the subject field of the local
// certificate in the downstream TLS connection.
func GetDownstreamSubjectLocalCertificate() (string, error) {
downstreamSubjectLocalCertificate, err := getPropertyString(connectionSubjectLocalCert)
result, err := getPropertyString(connectionSubjectLocalCert)
if err != nil {
return "", err
}
return downstreamSubjectLocalCertificate, nil
return result, nil
}

// GetDownstreamSubjectPeerCertificate returns the subject field of the peer certificate in the downstream TLS connection.
// GetDownstreamSubjectPeerCertificate returns the subject field of the peer certificate
// in the downstream TLS connection.
func GetDownstreamSubjectPeerCertificate() (string, error) {
downstreamSubjectPeerCertificate, err := getPropertyString(connectionSubjectPeerCert)
result, err := getPropertyString(connectionSubjectPeerCert)
if err != nil {
return "", err
}
return downstreamSubjectPeerCertificate, nil
return result, nil
}

// GetDownstreamDnsSanLocalCertificate returns The first DNS entry in the SAN field of the local certificate in the downstream TLS connection.
// GetDownstreamDnsSanLocalCertificate returns The first DNS entry in the SAN field of
// the local certificate in the downstream TLS connection.
func GetDownstreamDnsSanLocalCertificate() (string, error) {
downstreamDnsSanLocalCertificate, err := getPropertyString(connectionDnsSanLocalCert)
result, err := getPropertyString(connectionDnsSanLocalCert)
if err != nil {
return "", err
}
return downstreamDnsSanLocalCertificate, nil
return result, nil
}

// GetDownstreamDnsSanPeerCertificate returns The first DNS entry in the SAN field of the peer certificate in the downstream TLS connection.
// GetDownstreamDnsSanPeerCertificate returns The first DNS entry in the SAN field of the
// peer certificate in the downstream TLS connection.
func GetDownstreamDnsSanPeerCertificate() (string, error) {
downstreamDnsSanPeerCertificate, err := getPropertyString(connectionDnsSanPeerCert)
result, err := getPropertyString(connectionDnsSanPeerCert)
if err != nil {
return "", err
}
return downstreamDnsSanPeerCertificate, nil
return result, nil
}

// GetDownstreamUriSanLocalCertificate returns the first URI entry in the SAN field of the local certificate in the downstream TLS connection
// GetDownstreamUriSanLocalCertificate returns the first URI entry in the SAN field of the
// local certificate in the downstream TLS connection
func GetDownstreamUriSanLocalCertificate() (string, error) {
downstreamUriSanLocalCertificate, err := getPropertyString(connectionUriSanLocalCert)
result, err := getPropertyString(connectionUriSanLocalCert)
if err != nil {
return "", err
}
return downstreamUriSanLocalCertificate, nil
return result, nil
}

// GetDownstreamUriSanPeerCertificate returns The first URI entry in the SAN field of the peer certificate in the downstream TLS connection.
// GetDownstreamUriSanPeerCertificate returns The first URI entry in the SAN field of the
// peer certificate in the downstream TLS connection.
func GetDownstreamUriSanPeerCertificate() (string, error) {
downstreamUriSanPeerCertificate, err := getPropertyString(connectionUriSanPeerCert)
result, err := getPropertyString(connectionUriSanPeerCert)
if err != nil {
return "", err
}
return downstreamUriSanPeerCertificate, nil
return result, nil
}

// GetDownstreamSha256PeerCertificateDigest returns the SHA256 digest of a peer certificate digest of the downstream connection.
func GetDownstreamSha256PeerCertificateDigest() ([]byte, error) {
return proxywasm.GetProperty(connectionSha256PeerCertDigest)
// GetDownstreamSha256PeerCertificateDigest returns the SHA256 digest of a peer certificate
// digest of the downstream connection.
func GetDownstreamSha256PeerCertificateDigest() (string, error) {
result, err := getPropertyString(connectionSha256PeerCertDigest)
if err != nil {
return "", err
}
return result, nil
}

// GetDownstreamTerminationDetails returns the internal termination details of the connection (subject to change).
// GetDownstreamTerminationDetails returns the internal termination details of the connection
// (subject to change).
func GetDownstreamTerminationDetails() (string, error) {
downstreamTerminationDetails, err := getPropertyString(connectionTerminationDetails)
result, err := getPropertyString(connectionTerminationDetails)
if err != nil {
return "", err
}
return downstreamTerminationDetails, nil
return result, nil
}
68 changes: 41 additions & 27 deletions properties/connection_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package properties

import (
"encoding/binary"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -10,71 +9,86 @@ import (
)

func TestGetDownstreamRemoteAddress(t *testing.T) {
opt := proxytest.NewEmulatorOption().WithProperty(sourceAddress, []byte("1.2.3.4"))
opt := proxytest.NewEmulatorOption().WithProperty(sourceAddress, []byte("10.244.0.1:63649"))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := GetDownstreamRemoteAddress()
require.NoError(t, err)
require.Equal(t, "1.2.3.4", result)
require.Equal(t, "10.244.0.1:63649", result)
}

func TestGetDownstreamRemotePort(t *testing.T) {
var u64 [8]byte
binary.LittleEndian.PutUint64(u64[:], 1234)
opt := proxytest.NewEmulatorOption().WithProperty(sourcePort, u64[:])

opt := proxytest.NewEmulatorOption().WithProperty(sourcePort, serializeUint64(63649))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := GetDownstreamRemotePort()
require.NoError(t, err)
require.Equal(t, uint64(1234), result)
require.Equal(t, uint64(63649), result)
}

func TestGetDownstreamLocalAddress(t *testing.T) {
opt := proxytest.NewEmulatorOption().WithProperty(destinationAddress, []byte("1.2.3.4"))
opt := proxytest.NewEmulatorOption().WithProperty(destinationAddress, []byte("10.244.0.13:80"))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := GetDownstreamLocalAddress()
require.NoError(t, err)
require.Equal(t, "1.2.3.4", result)
require.Equal(t, "10.244.0.13:80", result)
}

func TestGetDownstreamLocalPort(t *testing.T) {
var u64 [8]byte
binary.LittleEndian.PutUint64(u64[:], 1234)
opt := proxytest.NewEmulatorOption().WithProperty(destinationPort, u64[:])
opt := proxytest.NewEmulatorOption().WithProperty(destinationPort, serializeUint64(80))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := GetDownstreamLocalPort()
require.NoError(t, err)
require.Equal(t, uint64(1234), result)
require.Equal(t, uint64(80), result)
}

func TestGetDownstreamConnectionID(t *testing.T) {
var u64 [8]byte
binary.LittleEndian.PutUint64(u64[:], 1234)
opt := proxytest.NewEmulatorOption().WithProperty(connectionID, u64[:])
opt := proxytest.NewEmulatorOption().WithProperty(connectionID, serializeUint64(1771))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := GetDownstreamConnectionID()
require.NoError(t, err)
require.Equal(t, uint64(1234), result)
require.Equal(t, uint64(1771), result)
}

func TestIsDownstreamConnectionTls(t *testing.T) {
opt := proxytest.NewEmulatorOption().WithProperty(connectionMtls, []byte{1})
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := IsDownstreamConnectionTls()
require.NoError(t, err)
require.Equal(t, true, result)
tests := []struct {
name string
input bool
expect bool
}{
{
name: "Downstream Connection TLS: False",
input: false,
expect: false,
},
{
name: "Downstream Connection TLS: True",
input: true,
expect: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opt := proxytest.NewEmulatorOption().WithProperty(connectionMtls, serializeBool(tt.input))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := IsDownstreamConnectionTls()
require.NoError(t, err)
require.Equal(t, tt.expect, result)
})
}
}

func TestGetDownstreamRequestedServerName(t *testing.T) {
opt := proxytest.NewEmulatorOption().WithProperty(connectionRequestedServerName, []byte("example.com"))
_, reset := proxytest.NewHostEmulator(opt)
Expand Down Expand Up @@ -159,13 +173,13 @@ func TestGetDownstreamUriSanPeerCertificate(t *testing.T) {

func TestGetDownstreamSha256PeerCertificateDigest(t *testing.T) {
opt := proxytest.NewEmulatorOption().WithProperty(connectionSha256PeerCertDigest,
[]byte{0x01, 0x02, 0x03, 0x04})
[]byte("b714f3d6f83efc2fddf80b8feda3e3b21b3e27b5"))
_, reset := proxytest.NewHostEmulator(opt)
defer reset()

result, err := GetDownstreamSha256PeerCertificateDigest()
require.NoError(t, err)
require.Equal(t, []byte{0x01, 0x02, 0x03, 0x04}, result)
require.Equal(t, "b714f3d6f83efc2fddf80b8feda3e3b21b3e27b5", result)
}

func TestGetDownstreamTerminationDetails(t *testing.T) {
Expand Down
Loading

0 comments on commit 918caa5

Please sign in to comment.