Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[management] Add structs for new networks concept #3006

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

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

import "github.com/rs/xid"

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

Routers []string `gorm:"serializer:json"`
Resources []string `gorm:"serializer:json"`
}

func NewNetwork(name, description string) *Network {
return &Network{
ID: xid.New().String(),
Name: name,
Description: description,
}
}
66 changes: 66 additions & 0 deletions management/server/networks/network_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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"`
Type NetworkResourceType
Address string
}

func NewNetworkResource(networkID string, 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(),
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")
}
})
}
}
31 changes: 31 additions & 0 deletions management/server/networks/network_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package networks

import (
"errors"

"github.com/rs/xid"
)

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

func NewNetworkRouter(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(),
NetworkID: networkID,
Peer: peer,
PeerGroups: peerGroups,
Masquerade: masquerade,
Metric: metric,
}, nil
}
Loading
Loading