Skip to content

Commit 1c9c472

Browse files
committed
make parse destination string into a func
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
1 parent 68f040a commit 1c9c472

File tree

2 files changed

+97
-29
lines changed

2 files changed

+97
-29
lines changed

hscontrol/policy/acls.go

+34-29
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,39 @@ func (pol *ACLPolicy) getNetPortRangeFromDestination(
375375
machines types.Machines,
376376
needsWildcard bool,
377377
) ([]tailcfg.NetPortRange, error) {
378-
var tokens []string
378+
alias, port, err := parseDestination(dest)
379+
if err != nil {
380+
return nil, err
381+
}
382+
383+
expanded, err := pol.ExpandAlias(
384+
machines,
385+
alias,
386+
)
387+
if err != nil {
388+
return nil, err
389+
}
390+
ports, err := expandPorts(port, needsWildcard)
391+
if err != nil {
392+
return nil, err
393+
}
394+
395+
dests := []tailcfg.NetPortRange{}
396+
for _, dest := range expanded.Prefixes() {
397+
for _, port := range *ports {
398+
pr := tailcfg.NetPortRange{
399+
IP: dest.String(),
400+
Ports: port,
401+
}
402+
dests = append(dests, pr)
403+
}
404+
}
405+
406+
return dests, nil
407+
}
379408

380-
log.Trace().Str("destination", dest).Msg("generating policy destination")
409+
func parseDestination(dest string) (string, string, error) {
410+
var tokens []string
381411

382412
// Check if there is a IPv4/6:Port combination, IPv6 has more than
383413
// three ":".
@@ -397,7 +427,7 @@ func (pol *ACLPolicy) getNetPortRangeFromDestination(
397427
if maybeIPv6, err := netip.ParseAddr(filteredMaybeIPv6Str); err != nil && !maybeIPv6.Is6() {
398428
log.Trace().Err(err).Msg("trying to parse as IPv6")
399429

400-
return nil, fmt.Errorf(
430+
return "", "", fmt.Errorf(
401431
"failed to parse destination, tokens %v: %w",
402432
tokens,
403433
ErrInvalidPortFormat,
@@ -407,8 +437,6 @@ func (pol *ACLPolicy) getNetPortRangeFromDestination(
407437
}
408438
}
409439

410-
log.Trace().Strs("tokens", tokens).Msg("generating policy destination")
411-
412440
var alias string
413441
// We can have here stuff like:
414442
// git-server:*
@@ -424,30 +452,7 @@ func (pol *ACLPolicy) getNetPortRangeFromDestination(
424452
alias = fmt.Sprintf("%s:%s", tokens[0], tokens[1])
425453
}
426454

427-
expanded, err := pol.ExpandAlias(
428-
machines,
429-
alias,
430-
)
431-
if err != nil {
432-
return nil, err
433-
}
434-
ports, err := expandPorts(tokens[len(tokens)-1], needsWildcard)
435-
if err != nil {
436-
return nil, err
437-
}
438-
439-
dests := []tailcfg.NetPortRange{}
440-
for _, dest := range expanded.Prefixes() {
441-
for _, port := range *ports {
442-
pr := tailcfg.NetPortRange{
443-
IP: dest.String(),
444-
Ports: port,
445-
}
446-
dests = append(dests, pr)
447-
}
448-
}
449-
450-
return dests, nil
455+
return alias, tokens[len(tokens)-1], nil
451456
}
452457

453458
// parseProtocol reads the proto field of the ACL and generates a list of

hscontrol/policy/acls_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -2557,3 +2557,66 @@ func TestSSHRules(t *testing.T) {
25572557
})
25582558
}
25592559
}
2560+
2561+
func TestParseDestination(t *testing.T) {
2562+
tests := []struct {
2563+
dest string
2564+
wantAlias string
2565+
wantPort string
2566+
}{
2567+
{
2568+
dest: "git-server:*",
2569+
wantAlias: "git-server",
2570+
wantPort: "*",
2571+
},
2572+
{
2573+
dest: "192.168.1.0/24:22",
2574+
wantAlias: "192.168.1.0/24",
2575+
wantPort: "22",
2576+
},
2577+
{
2578+
dest: "192.168.1.1:22",
2579+
wantAlias: "192.168.1.1",
2580+
wantPort: "22",
2581+
},
2582+
{
2583+
dest: "fd7a:115c:a1e0::2:22",
2584+
wantAlias: "fd7a:115c:a1e0::2",
2585+
wantPort: "22",
2586+
},
2587+
{
2588+
dest: "fd7a:115c:a1e0::2/128:22",
2589+
wantAlias: "fd7a:115c:a1e0::2/128",
2590+
wantPort: "22",
2591+
},
2592+
{
2593+
dest: "tag:montreal-webserver:80,443",
2594+
wantAlias: "tag:montreal-webserver",
2595+
wantPort: "80,443",
2596+
},
2597+
{
2598+
dest: "tag:api-server:443",
2599+
wantAlias: "tag:api-server",
2600+
wantPort: "443",
2601+
},
2602+
{
2603+
dest: "example-host-1:*",
2604+
wantAlias: "example-host-1",
2605+
wantPort: "*",
2606+
},
2607+
}
2608+
2609+
for _, tt := range tests {
2610+
t.Run(tt.dest, func(t *testing.T) {
2611+
alias, port, _ := parseDestination(tt.dest)
2612+
2613+
if alias != tt.wantAlias {
2614+
t.Errorf("unexpected alias: want(%s) != got(%s)", tt.wantAlias, alias)
2615+
}
2616+
2617+
if port != tt.wantPort {
2618+
t.Errorf("unexpected port: want(%s) != got(%s)", tt.wantPort, port)
2619+
}
2620+
})
2621+
}
2622+
}

0 commit comments

Comments
 (0)