Skip to content

Commit

Permalink
[management] Add structs for new networks concept (#3006)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fischer authored and pappz committed Dec 11, 2024
1 parent 97bb74f commit 65ec185
Show file tree
Hide file tree
Showing 8 changed files with 358 additions and 43 deletions.
112 changes: 69 additions & 43 deletions management/proto/management.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions management/proto/management.proto
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ enum RuleProtocol {
TCP = 2;
UDP = 3;
ICMP = 4;
CUSTOM = 5;
}

enum RuleDirection {
Expand Down Expand Up @@ -459,5 +460,11 @@ message RouteFirewallRule {

// IsDynamic indicates if the route is a DNS route.
bool isDynamic = 6;

// Domains is a list of domains for which the rule is applicable.
repeated string domains = 7;

// CustomProtocol is a custom protocol ID.
uint32 customProtocol = 8;
}

19 changes: 19 additions & 0 deletions management/server/networks/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package networks

import "github.com/rs/xid"

type Network struct {
ID string `gorm:"index"`
AccountID string `gorm:"index"`
Name string
Description string
}

func NewNetwork(accountId, name, description string) *Network {
return &Network{
ID: xid.New().String(),
AccountID: accountId,
Name: name,
Description: description,
}
}
68 changes: 68 additions & 0 deletions management/server/networks/network_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package networks

import (
"errors"
"fmt"
"net"
"regexp"
"strings"

"github.com/rs/xid"
)

type NetworkResourceType string

const (
host NetworkResourceType = "Host"
subnet NetworkResourceType = "Subnet"
domain NetworkResourceType = "Domain"
)

func (p NetworkResourceType) String() string {
return string(p)
}

type NetworkResource struct {
ID string `gorm:"index"`
NetworkID string `gorm:"index"`
AccountID string `gorm:"index"`
Type NetworkResourceType
Address string
}

func NewNetworkResource(accountID, networkID, address string) (*NetworkResource, error) {
resourceType, err := getResourceType(address)
if err != nil {
return nil, fmt.Errorf("invalid address: %w", err)
}

return &NetworkResource{
ID: xid.New().String(),
AccountID: accountID,
NetworkID: networkID,
Type: resourceType,
Address: address,
}, nil
}

// getResourceType returns the type of the resource based on the address
func getResourceType(address string) (NetworkResourceType, error) {
if ip, cidr, err := net.ParseCIDR(address); err == nil {
ones, _ := cidr.Mask.Size()
if strings.HasSuffix(address, "/32") || (ip != nil && ones == 32) {
return host, nil
}
return subnet, nil
}

if net.ParseIP(address) != nil {
return host, nil
}

domainRegex := regexp.MustCompile(`^(\*\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$`)
if domainRegex.MatchString(address) {
return domain, nil
}

return "", errors.New("not a host, subnet, or domain")
}
41 changes: 41 additions & 0 deletions management/server/networks/network_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package networks

import (
"testing"
)

func TestGetResourceType(t *testing.T) {
tests := []struct {
input string
expectedType NetworkResourceType
expectedErr bool
}{
// Valid host IPs
{"1.1.1.1", host, false},
{"1.1.1.1/32", host, false},
// Valid subnets
{"192.168.1.0/24", subnet, false},
{"10.0.0.0/16", subnet, false},
// Valid domains
{"example.com", domain, false},
{"*.example.com", domain, false},
{"sub.example.com", domain, false},
// Invalid inputs
{"invalid", "", true},
{"1.1.1.1/abc", "", true},
{"1234", "", true},
}

for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result, err := getResourceType(tt.input)
if result != tt.expectedType {
t.Errorf("Expected type %v, got %v", tt.expectedType, result)
}

if tt.expectedErr && err == nil {
t.Errorf("Expected error, got nil")
}
})
}
}
33 changes: 33 additions & 0 deletions management/server/networks/network_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package networks

import (
"errors"

"github.com/rs/xid"
)

type NetworkRouter struct {
ID string `gorm:"index"`
NetworkID string `gorm:"index"`
AccountID string `gorm:"index"`
Peer string
PeerGroups []string `gorm:"serializer:json"`
Masquerade bool
Metric int
}

func NewNetworkRouter(accountID string, networkID string, peer string, peerGroups []string, masquerade bool, metric int) (*NetworkRouter, error) {
if peer != "" && len(peerGroups) > 0 {
return nil, errors.New("peer and peerGroups cannot be set at the same time")
}

return &NetworkRouter{
ID: xid.New().String(),
AccountID: accountID,
NetworkID: networkID,
Peer: peer,
PeerGroups: peerGroups,
Masquerade: masquerade,
Metric: metric,
}, nil
}
Loading

0 comments on commit 65ec185

Please sign in to comment.