Skip to content

Commit 94ea4e0

Browse files
committed
apic
1 parent 58148a9 commit 94ea4e0

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

pkg/apiserver/apic.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"math/rand"
10-
"net"
10+
"net/netip"
1111
"net/http"
1212
"net/url"
1313
"slices"
@@ -844,26 +844,30 @@ func (a *apic) UpdateAllowlists(ctx context.Context, allowlistsLinks []*modelsca
844844

845845
// if decisions is whitelisted: return representation of the whitelist ip or cidr
846846
// if not whitelisted: empty string
847-
func (a *apic) whitelistedBy(decision *models.Decision, additionalIPs []net.IP, additionalRanges []*net.IPNet) string {
847+
func (a *apic) whitelistedBy(decision *models.Decision, additionalIPs []netip.Addr, additionalRanges []netip.Prefix) string {
848848
if decision.Value == nil {
849849
return ""
850850
}
851851

852-
ipval := net.ParseIP(*decision.Value)
852+
ipval, err := netip.ParseAddr(*decision.Value)
853+
if err != nil {
854+
// XXX: handle error
855+
}
856+
853857
for _, cidr := range a.whitelists.Cidrs {
854858
if cidr.Contains(ipval) {
855859
return cidr.String()
856860
}
857861
}
858862

859863
for _, ip := range a.whitelists.Ips {
860-
if ip != nil && ip.Equal(ipval) {
864+
if ip == ipval {
861865
return ip.String()
862866
}
863867
}
864868

865869
for _, ip := range additionalIPs {
866-
if ip.Equal(ipval) {
870+
if ip == ipval {
867871
return ip.String()
868872
}
869873
}

pkg/apiserver/apic_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"net"
8+
"net/netip"
99
"net/http"
1010
"net/url"
1111
"os"
@@ -528,14 +528,14 @@ func TestAPICWhitelists(t *testing.T) {
528528
api := getAPIC(t, ctx)
529529
// one whitelist on IP, one on CIDR
530530
api.whitelists = &csconfig.CapiWhitelist{}
531-
api.whitelists.Ips = append(api.whitelists.Ips, net.ParseIP("9.2.3.4"), net.ParseIP("7.2.3.4"))
531+
api.whitelists.Ips = append(api.whitelists.Ips, netip.MustParseAddr("9.2.3.4"), netip.MustParseAddr("7.2.3.4"))
532532

533-
_, tnet, err := net.ParseCIDR("13.2.3.0/24")
533+
tnet, err := netip.ParsePrefix("13.2.3.0/24")
534534
require.NoError(t, err)
535535

536536
api.whitelists.Cidrs = append(api.whitelists.Cidrs, tnet)
537537

538-
_, tnet, err = net.ParseCIDR("11.2.3.0/24")
538+
tnet, err = netip.ParsePrefix("11.2.3.0/24")
539539
require.NoError(t, err)
540540

541541
api.whitelists.Cidrs = append(api.whitelists.Cidrs, tnet)

pkg/csconfig/api.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"net"
11+
"net/netip"
1112
"os"
1213
"strings"
1314
"time"
@@ -276,8 +277,8 @@ func toValidCIDR(ip string) string {
276277
}
277278

278279
type CapiWhitelist struct {
279-
Ips []net.IP `yaml:"ips,omitempty"`
280-
Cidrs []*net.IPNet `yaml:"cidrs,omitempty"`
280+
Ips []netip.Addr `yaml:"ips,omitempty"`
281+
Cidrs []netip.Prefix `yaml:"cidrs,omitempty"`
281282
}
282283

283284
type LocalAPIAutoRegisterCfg struct {
@@ -450,21 +451,21 @@ func parseCapiWhitelists(fd io.Reader) (*CapiWhitelist, error) {
450451
}
451452

452453
ret := &CapiWhitelist{
453-
Ips: make([]net.IP, len(fromCfg.Ips)),
454-
Cidrs: make([]*net.IPNet, len(fromCfg.Cidrs)),
454+
Ips: make([]netip.Addr, len(fromCfg.Ips)),
455+
Cidrs: make([]netip.Prefix, len(fromCfg.Cidrs)),
455456
}
456457

457458
for idx, v := range fromCfg.Ips {
458-
ip := net.ParseIP(v)
459-
if ip == nil {
460-
return nil, fmt.Errorf("invalid IP address: %s", v)
459+
ip, err := netip.ParseAddr(v)
460+
if err != nil {
461+
return nil, err
461462
}
462463

463464
ret.Ips[idx] = ip
464465
}
465466

466467
for idx, v := range fromCfg.Cidrs {
467-
_, tnet, err := net.ParseCIDR(v)
468+
tnet, err := netip.ParsePrefix(v)
468469
if err != nil {
469470
return nil, err
470471
}

pkg/database/allowlists.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package database
33
import (
44
"context"
55
"fmt"
6-
"net"
6+
"net/netip"
77
"strings"
88
"time"
99

@@ -354,31 +354,31 @@ func (c *Client) IsAllowlisted(ctx context.Context, value string) (bool, string,
354354
return true, reason, nil
355355
}
356356

357-
func (c *Client) GetAllowlistsContentForAPIC(ctx context.Context) ([]net.IP, []*net.IPNet, error) {
357+
func (c *Client) GetAllowlistsContentForAPIC(ctx context.Context) ([]netip.Addr, []netip.Prefix, error) {
358358
allowlists, err := c.ListAllowLists(ctx, true)
359359
if err != nil {
360360
return nil, nil, fmt.Errorf("unable to get allowlists: %w", err)
361361
}
362362

363363
var (
364-
ips []net.IP
365-
nets []*net.IPNet
364+
ips []netip.Addr
365+
nets []netip.Prefix
366366
)
367367

368368
for _, allowlist := range allowlists {
369369
for _, item := range allowlist.Edges.AllowlistItems {
370370
if item.ExpiresAt.IsZero() || item.ExpiresAt.After(time.Now().UTC()) {
371371
if strings.Contains(item.Value, "/") {
372-
_, ipNet, err := net.ParseCIDR(item.Value)
372+
ipNet, err := netip.ParsePrefix(item.Value)
373373
if err != nil {
374374
c.Log.Errorf("unable to parse CIDR %s: %s", item.Value, err)
375375
continue
376376
}
377377

378378
nets = append(nets, ipNet)
379379
} else {
380-
ip := net.ParseIP(item.Value)
381-
if ip == nil {
380+
ip, err := netip.ParseAddr(item.Value)
381+
if err != nil {
382382
c.Log.Errorf("unable to parse IP %s", item.Value)
383383
continue
384384
}

0 commit comments

Comments
 (0)