Skip to content

Commit

Permalink
Rename allocation.Context to allocation.Metadata
Browse files Browse the repository at this point in the history
Also fixes a lint error
Fixes #420
  • Loading branch information
enobufs committed Dec 30, 2024
1 parent 93ca8f6 commit f6d1385
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 56 deletions.
18 changes: 9 additions & 9 deletions internal/allocation/allocation_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
// Context contains contextual information for TURN server allocation tasks.

Check failure on line 15 in internal/allocation/allocation_manager.go

View workflow job for this annotation

GitHub Actions / lint / Go

exported: comment on exported type Metadata should be of the form "Metadata ..." (with optional leading article) (revive)
// It provides necessary context for the allocation of relay addresses and related
// operations during the TURN server's handling of allocation requests.
type Context struct {
type Metadata struct {
Realm string
Username string
}

// ManagerConfig a bag of config params for Manager.
type ManagerConfig struct {
LeveledLogger logging.LeveledLogger
AllocatePacketConn func(network string, requestedPort int, allocCtx Context) (net.PacketConn, net.Addr, error)
AllocateConn func(network string, requestedPort int, allocCtx Context) (net.Conn, net.Addr, error)
AllocatePacketConn func(network string, requestedPort int, metadata Metadata) (net.PacketConn, net.Addr, error)
AllocateConn func(network string, requestedPort int, metadata Metadata) (net.Conn, net.Addr, error)
PermissionHandler func(sourceAddr net.Addr, peerIP net.IP) bool
}

Expand All @@ -41,8 +41,8 @@ type Manager struct {
allocations map[FiveTupleFingerprint]*Allocation
reservations []*reservation

allocatePacketConn func(network string, requestedPort int, allocCtx Context) (net.PacketConn, net.Addr, error)
allocateConn func(network string, requestedPort int, allocCtx Context) (net.Conn, net.Addr, error)
allocatePacketConn func(network string, requestedPort int, metadata Metadata) (net.PacketConn, net.Addr, error)
allocateConn func(network string, requestedPort int, metadata Metadata) (net.Conn, net.Addr, error)
permissionHandler func(sourceAddr net.Addr, peerIP net.IP) bool
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func (m *Manager) Close() error {
}

// CreateAllocation creates a new allocation and starts relaying
func (m *Manager) CreateAllocation(fiveTuple *FiveTuple, turnSocket net.PacketConn, requestedPort int, lifetime time.Duration, allocCtx Context) (*Allocation, error) {
func (m *Manager) CreateAllocation(fiveTuple *FiveTuple, turnSocket net.PacketConn, requestedPort int, lifetime time.Duration, metadata Metadata) (*Allocation, error) {
switch {
case fiveTuple == nil:
return nil, errNilFiveTuple
Expand All @@ -113,7 +113,7 @@ func (m *Manager) CreateAllocation(fiveTuple *FiveTuple, turnSocket net.PacketCo
}
a := NewAllocation(turnSocket, fiveTuple, m.log)

conn, relayAddr, err := m.allocatePacketConn("udp4", requestedPort, allocCtx)
conn, relayAddr, err := m.allocatePacketConn("udp4", requestedPort, metadata)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -188,9 +188,9 @@ func (m *Manager) GetReservation(reservationToken string) (int, bool) {
}

// GetRandomEvenPort returns a random un-allocated udp4 port
func (m *Manager) GetRandomEvenPort(allocCtx Context) (int, error) {
func (m *Manager) GetRandomEvenPort(metadata Metadata) (int, error) {
for i := 0; i < 128; i++ {
conn, addr, err := m.allocatePacketConn("udp4", 0, allocCtx)
conn, addr, err := m.allocatePacketConn("udp4", 0, metadata)
if err != nil {
return 0, err
}

Check warning on line 196 in internal/allocation/allocation_manager.go

View check run for this annotation

Codecov / codecov/patch

internal/allocation/allocation_manager.go#L195-L196

Added lines #L195 - L196 were not covered by tests
Expand Down
44 changes: 22 additions & 22 deletions internal/allocation/allocation_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func subTestCreateInvalidAllocation(t *testing.T, turnSocket net.PacketConn, rea
m, err := newTestManager(realm, username)
assert.NoError(t, err)

allocCtx := Context{Realm: realm, Username: username}
if a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime, allocCtx); a != nil || err == nil {
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(nil, turnSocket, 0, proto.DefaultLifetime, metadata); a != nil || err == nil {
t.Errorf("Illegally created allocation with nil FiveTuple")
}
if a, err := m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime, allocCtx); a != nil || err == nil {
if a, err := m.CreateAllocation(randomFiveTuple(), nil, 0, proto.DefaultLifetime, metadata); a != nil || err == nil {
t.Errorf("Illegally created allocation with nil turnSocket")
}
if a, err := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0, allocCtx); a != nil || err == nil {
if a, err := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, 0, metadata); a != nil || err == nil {
t.Errorf("Illegally created allocation with 0 lifetime")
}
}
Expand All @@ -76,8 +76,8 @@ func subTestCreateAllocation(t *testing.T, turnSocket net.PacketConn, realm, use
assert.NoError(t, err)

fiveTuple := randomFiveTuple()
allocCtx := Context{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, allocCtx); a == nil || err != nil {
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}

Expand All @@ -92,12 +92,12 @@ func subTestCreateAllocationDuplicateFiveTuple(t *testing.T, turnSocket net.Pack
assert.NoError(t, err)

fiveTuple := randomFiveTuple()
allocCtx := Context{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, allocCtx); a == nil || err != nil {
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}

if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, allocCtx); a != nil || err == nil {
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a != nil || err == nil {
t.Errorf("Was able to create allocation with same FiveTuple twice")
}
}
Expand All @@ -107,8 +107,8 @@ func subTestDeleteAllocation(t *testing.T, turnSocket net.PacketConn, realm, use
assert.NoError(t, err)

fiveTuple := randomFiveTuple()
allocCtx := Context{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, allocCtx); a == nil || err != nil {
metadata := Metadata{Realm: realm, Username: username}
if a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, proto.DefaultLifetime, metadata); a == nil || err != nil {
t.Errorf("Failed to create allocation %v %v", a, err)
}

Expand All @@ -129,11 +129,11 @@ func subTestAllocationTimeout(t *testing.T, turnSocket net.PacketConn, realm, us

allocations := make([]*Allocation, 5)
lifetime := time.Second
allocCtx := Context{Realm: realm, Username: username}
metadata := Metadata{Realm: realm, Username: username}
for index := range allocations {
fiveTuple := randomFiveTuple()

a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, lifetime, allocCtx)
a, err := m.CreateAllocation(fiveTuple, turnSocket, 0, lifetime, metadata)
if err != nil {
t.Errorf("Failed to create allocation with %v", fiveTuple)
}
Expand All @@ -156,10 +156,10 @@ func subTestManagerClose(t *testing.T, turnSocket net.PacketConn, realm, usernam
assert.NoError(t, err)

allocations := make([]*Allocation, 2)
allocCtx := Context{Realm: realm, Username: username}
a1, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Second, allocCtx)
metadata := Metadata{Realm: realm, Username: username}
a1, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Second, metadata)
allocations[0] = a1
a2, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Minute, allocCtx)
a2, _ := m.CreateAllocation(randomFiveTuple(), turnSocket, 0, time.Minute, metadata)
allocations[1] = a2

// Make a1 timeout
Expand Down Expand Up @@ -189,11 +189,11 @@ func newTestManager(expectedRealm, expectedUsername string) (*Manager, error) {

config := ManagerConfig{
LeveledLogger: loggerFactory.NewLogger("test"),
AllocatePacketConn: func(_ string, _ int, allocCtx Context) (net.PacketConn, net.Addr, error) {
if allocCtx.Realm != expectedRealm {
AllocatePacketConn: func(_ string, _ int, metadata Metadata) (net.PacketConn, net.Addr, error) {
if metadata.Realm != expectedRealm {
return nil, nil, errUnexpectedTestRealm
}
if allocCtx.Username != expectedUsername {
if metadata.Username != expectedUsername {
return nil, nil, errUnexpectedTestUsername
}
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
Expand All @@ -203,7 +203,7 @@ func newTestManager(expectedRealm, expectedUsername string) (*Manager, error) {

return conn, conn.LocalAddr(), nil
},
AllocateConn: func(string, int, Context) (net.Conn, net.Addr, error) { return nil, nil, nil },
AllocateConn: func(string, int, Metadata) (net.Conn, net.Addr, error) { return nil, nil, nil },
}

return NewManager(config)
Expand All @@ -218,8 +218,8 @@ func subTestGetRandomEvenPort(t *testing.T, _ net.PacketConn, realm, username st
m, err := newTestManager(realm, username)
assert.NoError(t, err)

allocCtx := Context{Realm: realm, Username: username}
port, err := m.GetRandomEvenPort(allocCtx)
metadata := Metadata{Realm: realm, Username: username}
port, err := m.GetRandomEvenPort(metadata)
assert.NoError(t, err)
assert.True(t, port > 0)
assert.True(t, port%2 == 0)
Expand Down
2 changes: 1 addition & 1 deletion internal/allocation/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func subTestPacketHandler(t *testing.T) {
a, err := m.CreateAllocation(&FiveTuple{
SrcAddr: clientListener.LocalAddr(),
DstAddr: turnSocket.LocalAddr(),
}, turnSocket, 0, proto.DefaultLifetime, Context{Realm: testRealm, Username: testUsername})
}, turnSocket, 0, proto.DefaultLifetime, Metadata{Realm: testRealm, Username: testUsername})

assert.Nil(t, err, "should succeed")

Expand Down
6 changes: 3 additions & 3 deletions internal/server/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func handleAllocateRequest(r Request, m *stun.Message) error {
if !authResult.hasAuth {
return err
}
allocCtx := allocation.Context{
metadata := allocation.Metadata{
Realm: authResult.realm,
Username: authResult.username,
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func handleAllocateRequest(r Request, m *stun.Message) error {
var evenPort proto.EvenPort
if err = evenPort.GetFrom(m); err == nil {
var randomPort int
randomPort, err = r.AllocationManager.GetRandomEvenPort(allocCtx)
randomPort, err = r.AllocationManager.GetRandomEvenPort(metadata)

Check warning on line 111 in internal/server/turn.go

View check run for this annotation

Codecov / codecov/patch

internal/server/turn.go#L111

Added line #L111 was not covered by tests
if err != nil {
return buildAndSendErr(r.Conn, r.SrcAddr, err, insufficientCapacityMsg...)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func handleAllocateRequest(r Request, m *stun.Message) error {
r.Conn,
requestedPort,
lifetimeDuration,
allocCtx)
metadata)
if err != nil {
return buildAndSendErr(r.Conn, r.SrcAddr, err, insufficientCapacityMsg...)
}
Expand Down
23 changes: 12 additions & 11 deletions internal/server/turn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"github.com/stretchr/testify/assert"
)

var (
errUnexpectedTestRealm = errors.New("unexpected test realm")
errUnexpectedTestUsername = errors.New("unexpected user name")
)

func TestAllocationLifeTime(t *testing.T) {
t.Run("Parsing", func(t *testing.T) {
lifetime := proto.Lifetime{
Expand Down Expand Up @@ -68,28 +73,24 @@ func TestAllocationLifeTime(t *testing.T) {
realm = "test"
username = "tester"
)
var (
errUnexpectedRealm = errors.New("unexpected realm")
errUnexpectecUsername = errors.New("unexpected username")
)

allocationManager, err := allocation.NewManager(allocation.ManagerConfig{
AllocatePacketConn: func(network string, _ int, allocCtx allocation.Context) (net.PacketConn, net.Addr, error) {
AllocatePacketConn: func(network string, _ int, metadata allocation.Metadata) (net.PacketConn, net.Addr, error) {
conn, listenErr := net.ListenPacket(network, "0.0.0.0:0")
if err != nil {
return nil, nil, listenErr
}

if allocCtx.Realm != realm {
return nil, nil, errUnexpectedRealm
if metadata.Realm != realm {
return nil, nil, errUnexpectedTestRealm
}
if allocCtx.Username != username {
return nil, nil, errUnexpectecUsername
if metadata.Username != username {
return nil, nil, errUnexpectedTestUsername
}

return conn, conn.LocalAddr(), nil
},
AllocateConn: func(string, int, allocation.Context) (net.Conn, net.Addr, error) {
AllocateConn: func(string, int, allocation.Metadata) (net.Conn, net.Addr, error) {
return nil, nil, nil
},
LeveledLogger: logger,
Expand All @@ -114,7 +115,7 @@ func TestAllocationLifeTime(t *testing.T) {

fiveTuple := &allocation.FiveTuple{SrcAddr: r.SrcAddr, DstAddr: r.Conn.LocalAddr(), Protocol: allocation.UDP}

_, err = r.AllocationManager.CreateAllocation(fiveTuple, r.Conn, 0, time.Hour, allocation.Context{Realm: realm, Username: username})
_, err = r.AllocationManager.CreateAllocation(fiveTuple, r.Conn, 0, time.Hour, allocation.Metadata{Realm: realm, Username: username})
assert.NoError(t, err)

assert.NotNil(t, r.AllocationManager.GetAllocation(fiveTuple))
Expand Down
4 changes: 2 additions & 2 deletions relay_address_generator_none.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r *RelayAddressGeneratorNone) Validate() error {
}

// AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port to populate the allocation response with
func (r *RelayAddressGeneratorNone) AllocatePacketConn(network string, requestedPort int, _ allocation.Context) (net.PacketConn, net.Addr, error) {
func (r *RelayAddressGeneratorNone) AllocatePacketConn(network string, requestedPort int, _ allocation.Metadata) (net.PacketConn, net.Addr, error) {

Check warning on line 43 in relay_address_generator_none.go

View check run for this annotation

Codecov / codecov/patch

relay_address_generator_none.go#L43

Added line #L43 was not covered by tests
conn, err := r.Net.ListenPacket(network, r.Address+":"+strconv.Itoa(requestedPort))
if err != nil {
return nil, nil, err
Expand All @@ -50,6 +50,6 @@ func (r *RelayAddressGeneratorNone) AllocatePacketConn(network string, requested
}

// AllocateConn generates a new Conn to receive traffic on and the IP/Port to populate the allocation response with
func (r *RelayAddressGeneratorNone) AllocateConn(string, int, allocation.Context) (net.Conn, net.Addr, error) {
func (r *RelayAddressGeneratorNone) AllocateConn(string, int, allocation.Metadata) (net.Conn, net.Addr, error) {

Check warning on line 53 in relay_address_generator_none.go

View check run for this annotation

Codecov / codecov/patch

relay_address_generator_none.go#L53

Added line #L53 was not covered by tests
return nil, nil, errTODO
}
4 changes: 2 additions & 2 deletions relay_address_generator_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (r *RelayAddressGeneratorPortRange) Validate() error {
}

// AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port to populate the allocation response with
func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(network string, requestedPort int, _ allocation.Context) (net.PacketConn, net.Addr, error) {
func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(network string, requestedPort int, _ allocation.Metadata) (net.PacketConn, net.Addr, error) {

Check warning on line 72 in relay_address_generator_range.go

View check run for this annotation

Codecov / codecov/patch

relay_address_generator_range.go#L72

Added line #L72 was not covered by tests
if requestedPort != 0 {
conn, err := r.Net.ListenPacket(network, fmt.Sprintf("%s:%d", r.Address, requestedPort))
if err != nil {
Expand Down Expand Up @@ -104,6 +104,6 @@ func (r *RelayAddressGeneratorPortRange) AllocatePacketConn(network string, requ
}

// AllocateConn generates a new Conn to receive traffic on and the IP/Port to populate the allocation response with
func (r *RelayAddressGeneratorPortRange) AllocateConn(string, int, allocation.Context) (net.Conn, net.Addr, error) {
func (r *RelayAddressGeneratorPortRange) AllocateConn(string, int, allocation.Metadata) (net.Conn, net.Addr, error) {

Check warning on line 107 in relay_address_generator_range.go

View check run for this annotation

Codecov / codecov/patch

relay_address_generator_range.go#L107

Added line #L107 was not covered by tests
return nil, nil, errTODO
}
4 changes: 2 additions & 2 deletions relay_address_generator_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (r *RelayAddressGeneratorStatic) Validate() error {
}

// AllocatePacketConn generates a new PacketConn to receive traffic on and the IP/Port to populate the allocation response with
func (r *RelayAddressGeneratorStatic) AllocatePacketConn(network string, requestedPort int, _ allocation.Context) (net.PacketConn, net.Addr, error) {
func (r *RelayAddressGeneratorStatic) AllocatePacketConn(network string, requestedPort int, _ allocation.Metadata) (net.PacketConn, net.Addr, error) {
conn, err := r.Net.ListenPacket(network, r.Address+":"+strconv.Itoa(requestedPort))
if err != nil {
return nil, nil, err
Expand All @@ -64,6 +64,6 @@ func (r *RelayAddressGeneratorStatic) AllocatePacketConn(network string, request
}

// AllocateConn generates a new Conn to receive traffic on and the IP/Port to populate the allocation response with
func (r *RelayAddressGeneratorStatic) AllocateConn(string, int, allocation.Context) (net.Conn, net.Addr, error) {
func (r *RelayAddressGeneratorStatic) AllocateConn(string, int, allocation.Metadata) (net.Conn, net.Addr, error) {

Check warning on line 67 in relay_address_generator_static.go

View check run for this annotation

Codecov / codecov/patch

relay_address_generator_static.go#L67

Added line #L67 was not covered by tests
return nil, nil, errTODO
}
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ type nilAddressGenerator struct{}

func (n *nilAddressGenerator) Validate() error { return errRelayAddressGeneratorNil }

func (n *nilAddressGenerator) AllocatePacketConn(string, int, allocation.Context) (net.PacketConn, net.Addr, error) {
func (n *nilAddressGenerator) AllocatePacketConn(string, int, allocation.Metadata) (net.PacketConn, net.Addr, error) {

Check warning on line 174 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L174

Added line #L174 was not covered by tests
return nil, nil, errRelayAddressGeneratorNil
}

func (n *nilAddressGenerator) AllocateConn(string, int, allocation.Context) (net.Conn, net.Addr, error) {
func (n *nilAddressGenerator) AllocateConn(string, int, allocation.Metadata) (net.Conn, net.Addr, error) {

Check warning on line 178 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L178

Added line #L178 was not covered by tests
return nil, nil, errRelayAddressGeneratorNil
}

Expand Down
4 changes: 2 additions & 2 deletions server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type RelayAddressGenerator interface {
Validate() error

// Allocate a PacketConn (UDP) RelayAddress
AllocatePacketConn(network string, requestedPort int, allocCtx allocation.Context) (net.PacketConn, net.Addr, error)
AllocatePacketConn(network string, requestedPort int, metadata allocation.Metadata) (net.PacketConn, net.Addr, error)

// Allocate a Conn (TCP) RelayAddress
AllocateConn(network string, requestedPort int, allocCtx allocation.Context) (net.Conn, net.Addr, error)
AllocateConn(network string, requestedPort int, metadata allocation.Metadata) (net.Conn, net.Addr, error)
}

// PermissionHandler is a callback to filter incoming CreatePermission and ChannelBindRequest
Expand Down

0 comments on commit f6d1385

Please sign in to comment.