2022-01-30 16:38:45 +00:00
|
|
|
// Copyright 2021 the Kilo authors
|
2019-01-18 01:50:10 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package k8s
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
2022-01-30 16:38:45 +00:00
|
|
|
"time"
|
2019-01-18 01:50:10 +00:00
|
|
|
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
2022-01-30 16:38:45 +00:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2019-04-02 16:25:08 +00:00
|
|
|
v1 "k8s.io/api/core/v1"
|
2019-01-18 01:50:10 +00:00
|
|
|
|
2019-05-03 10:53:40 +00:00
|
|
|
"github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1"
|
2019-01-18 01:50:10 +00:00
|
|
|
"github.com/squat/kilo/pkg/mesh"
|
2019-05-03 10:53:40 +00:00
|
|
|
"github.com/squat/kilo/pkg/wireguard"
|
2019-01-18 01:50:10 +00:00
|
|
|
)
|
|
|
|
|
2022-01-30 16:38:45 +00:00
|
|
|
func mustKey() (k wgtypes.Key) {
|
|
|
|
var err error
|
|
|
|
if k, err = wgtypes.GeneratePrivateKey(); err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustPSKKey() (key *wgtypes.Key) {
|
|
|
|
if k, err := wgtypes.GenerateKey(); err != nil {
|
|
|
|
panic(err.Error())
|
|
|
|
} else {
|
|
|
|
key = &k
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
fooKey = mustKey()
|
|
|
|
pskKey = mustPSKKey()
|
|
|
|
second = time.Second
|
|
|
|
zero = time.Duration(0)
|
|
|
|
)
|
|
|
|
|
2019-01-18 01:50:10 +00:00
|
|
|
func TestTranslateNode(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
annotations map[string]string
|
|
|
|
labels map[string]string
|
|
|
|
out *mesh.Node
|
|
|
|
subnet string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
annotations: nil,
|
|
|
|
out: &mesh.Node{},
|
|
|
|
},
|
|
|
|
{
|
2019-04-02 16:25:08 +00:00
|
|
|
name: "invalid ips",
|
2019-01-18 01:50:10 +00:00
|
|
|
annotations: map[string]string{
|
2020-02-22 16:17:13 +00:00
|
|
|
endpointAnnotationKey: "10.0.0.1",
|
2019-04-02 16:25:08 +00:00
|
|
|
internalIPAnnotationKey: "foo",
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Node{},
|
|
|
|
},
|
|
|
|
{
|
2019-04-02 16:25:08 +00:00
|
|
|
name: "valid ips",
|
2019-01-18 01:50:10 +00:00
|
|
|
annotations: map[string]string{
|
2020-02-22 16:17:13 +00:00
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
2019-01-18 01:50:10 +00:00
|
|
|
internalIPAnnotationKey: "10.0.0.2/32",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.1").To4(), mesh.DefaultKiloPort),
|
|
|
|
InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2").To4(), Mask: net.CIDRMask(32, 32)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid ips with ipv6",
|
|
|
|
annotations: map[string]string{
|
|
|
|
endpointAnnotationKey: "[ff10::10]:51820",
|
|
|
|
internalIPAnnotationKey: "ff60::10/64",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("ff10::10").To16(), mesh.DefaultKiloPort),
|
|
|
|
InternalIP: &net.IPNet{IP: net.ParseIP("ff60::10").To16(), Mask: net.CIDRMask(64, 128)},
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid subnet",
|
|
|
|
annotations: map[string]string{},
|
|
|
|
out: &mesh.Node{},
|
|
|
|
subnet: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "normalize subnet",
|
|
|
|
annotations: map[string]string{},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0").To4(), Mask: net.CIDRMask(24, 32)},
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
subnet: "10.2.0.1/24",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid subnet",
|
|
|
|
annotations: map[string]string{},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0").To4(), Mask: net.CIDRMask(24, 32)},
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
subnet: "10.2.1.0/24",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "region",
|
|
|
|
labels: map[string]string{
|
2020-12-11 14:44:20 +00:00
|
|
|
RegionLabelKey: "a",
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
|
|
|
Location: "a",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "region override",
|
|
|
|
annotations: map[string]string{
|
|
|
|
locationAnnotationKey: "b",
|
|
|
|
},
|
|
|
|
labels: map[string]string{
|
2020-12-11 14:44:20 +00:00
|
|
|
RegionLabelKey: "a",
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
|
|
|
Location: "b",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-02-22 16:17:13 +00:00
|
|
|
name: "invalid endpoint override",
|
2019-01-18 01:50:10 +00:00
|
|
|
annotations: map[string]string{
|
2020-02-22 16:17:13 +00:00
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
|
|
|
forceEndpointAnnotationKey: "-10.0.0.2:51821",
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.1").To4(), mesh.DefaultKiloPort),
|
2020-02-22 16:17:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "endpoint override",
|
|
|
|
annotations: map[string]string{
|
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
|
|
|
forceEndpointAnnotationKey: "10.0.0.2:51821",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.2").To4(), 51821),
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-13 09:16:55 +00:00
|
|
|
{
|
|
|
|
name: "wireguard persistent keepalive override",
|
|
|
|
annotations: map[string]string{
|
|
|
|
persistentKeepaliveKey: "25",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
PersistentKeepalive: 25 * time.Second,
|
2020-02-13 09:16:55 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-22 16:17:13 +00:00
|
|
|
{
|
|
|
|
name: "invalid internal IP override",
|
|
|
|
annotations: map[string]string{
|
|
|
|
internalIPAnnotationKey: "10.1.0.1/24",
|
|
|
|
forceInternalIPAnnotationKey: "-10.1.0.2/24",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.1").To4(), Mask: net.CIDRMask(24, 32)},
|
2021-02-22 19:28:16 +00:00
|
|
|
NoInternalIP: false,
|
2020-02-22 16:17:13 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-15 15:24:21 +00:00
|
|
|
{
|
|
|
|
name: "internal IP override",
|
|
|
|
annotations: map[string]string{
|
|
|
|
internalIPAnnotationKey: "10.1.0.1/24",
|
|
|
|
forceInternalIPAnnotationKey: "10.1.0.2/24",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2").To4(), Mask: net.CIDRMask(24, 32)},
|
2021-02-22 19:28:16 +00:00
|
|
|
NoInternalIP: false,
|
2019-07-15 15:24:21 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-02 16:25:08 +00:00
|
|
|
{
|
|
|
|
name: "invalid time",
|
|
|
|
annotations: map[string]string{
|
|
|
|
lastSeenAnnotationKey: "foo",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{},
|
|
|
|
},
|
2019-01-18 01:50:10 +00:00
|
|
|
{
|
|
|
|
name: "complete",
|
|
|
|
annotations: map[string]string{
|
2020-02-22 16:17:13 +00:00
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
|
|
|
forceEndpointAnnotationKey: "10.0.0.2:51821",
|
2019-07-15 15:24:21 +00:00
|
|
|
forceInternalIPAnnotationKey: "10.1.0.2/32",
|
|
|
|
internalIPAnnotationKey: "10.1.0.1/32",
|
2022-01-30 16:38:45 +00:00
|
|
|
keyAnnotationKey: fooKey.String(),
|
2019-04-02 16:25:08 +00:00
|
|
|
lastSeenAnnotationKey: "1000000000",
|
2019-01-18 01:50:10 +00:00
|
|
|
leaderAnnotationKey: "",
|
|
|
|
locationAnnotationKey: "b",
|
2020-02-13 09:16:55 +00:00
|
|
|
persistentKeepaliveKey: "25",
|
2019-05-10 00:05:57 +00:00
|
|
|
wireGuardIPAnnotationKey: "10.4.0.1/16",
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
labels: map[string]string{
|
2020-12-11 14:44:20 +00:00
|
|
|
RegionLabelKey: "a",
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.2").To4(), 51821),
|
|
|
|
NoInternalIP: false,
|
|
|
|
InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2").To4(), Mask: net.CIDRMask(32, 32)},
|
|
|
|
Key: fooKey,
|
|
|
|
LastSeen: 1000000000,
|
|
|
|
Leader: true,
|
|
|
|
Location: "b",
|
|
|
|
PersistentKeepalive: 25 * time.Second,
|
|
|
|
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0").To4(), Mask: net.CIDRMask(24, 32)},
|
|
|
|
WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1").To4(), Mask: net.CIDRMask(16, 32)},
|
|
|
|
},
|
|
|
|
subnet: "10.2.1.0/24",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "complete with ipv6",
|
|
|
|
annotations: map[string]string{
|
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
|
|
|
forceEndpointAnnotationKey: "[1100::10]:51821",
|
|
|
|
forceInternalIPAnnotationKey: "10.1.0.2/32",
|
|
|
|
internalIPAnnotationKey: "10.1.0.1/32",
|
|
|
|
keyAnnotationKey: fooKey.String(),
|
|
|
|
lastSeenAnnotationKey: "1000000000",
|
|
|
|
leaderAnnotationKey: "",
|
|
|
|
locationAnnotationKey: "b",
|
|
|
|
persistentKeepaliveKey: "25",
|
|
|
|
wireGuardIPAnnotationKey: "10.4.0.1/16",
|
|
|
|
},
|
|
|
|
labels: map[string]string{
|
|
|
|
RegionLabelKey: "a",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("1100::10"), 51821),
|
2021-02-22 19:28:16 +00:00
|
|
|
NoInternalIP: false,
|
2020-02-13 09:16:55 +00:00
|
|
|
InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(32, 32)},
|
2022-01-30 16:38:45 +00:00
|
|
|
Key: fooKey,
|
2020-02-13 09:16:55 +00:00
|
|
|
LastSeen: 1000000000,
|
|
|
|
Leader: true,
|
|
|
|
Location: "b",
|
2022-01-30 16:38:45 +00:00
|
|
|
PersistentKeepalive: 25 * time.Second,
|
2020-02-13 09:16:55 +00:00
|
|
|
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
|
|
|
|
WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
|
2019-01-18 01:50:10 +00:00
|
|
|
},
|
|
|
|
subnet: "10.2.1.0/24",
|
|
|
|
},
|
2021-01-24 13:19:01 +00:00
|
|
|
{
|
|
|
|
name: "no InternalIP",
|
|
|
|
annotations: map[string]string{
|
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
|
|
|
internalIPAnnotationKey: "",
|
2022-01-30 16:38:45 +00:00
|
|
|
keyAnnotationKey: fooKey.String(),
|
2021-01-24 13:19:01 +00:00
|
|
|
lastSeenAnnotationKey: "1000000000",
|
|
|
|
locationAnnotationKey: "b",
|
|
|
|
persistentKeepaliveKey: "25",
|
|
|
|
wireGuardIPAnnotationKey: "10.4.0.1/16",
|
|
|
|
},
|
|
|
|
labels: map[string]string{
|
|
|
|
RegionLabelKey: "a",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.1"), 51820),
|
2021-01-24 13:19:01 +00:00
|
|
|
InternalIP: nil,
|
2022-01-30 16:38:45 +00:00
|
|
|
Key: fooKey,
|
2021-01-24 13:19:01 +00:00
|
|
|
LastSeen: 1000000000,
|
|
|
|
Leader: false,
|
|
|
|
Location: "b",
|
2022-01-30 16:38:45 +00:00
|
|
|
PersistentKeepalive: 25 * time.Second,
|
2021-01-24 13:19:01 +00:00
|
|
|
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
|
|
|
|
WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
|
|
|
|
},
|
|
|
|
subnet: "10.2.1.0/24",
|
|
|
|
},
|
2021-02-22 19:28:16 +00:00
|
|
|
{
|
|
|
|
name: "Force no internal IP",
|
|
|
|
annotations: map[string]string{
|
|
|
|
endpointAnnotationKey: "10.0.0.1:51820",
|
|
|
|
internalIPAnnotationKey: "10.1.0.1/32",
|
|
|
|
forceInternalIPAnnotationKey: "",
|
2022-01-30 16:38:45 +00:00
|
|
|
keyAnnotationKey: fooKey.String(),
|
2021-02-22 19:28:16 +00:00
|
|
|
lastSeenAnnotationKey: "1000000000",
|
|
|
|
locationAnnotationKey: "b",
|
|
|
|
persistentKeepaliveKey: "25",
|
|
|
|
wireGuardIPAnnotationKey: "10.4.0.1/16",
|
|
|
|
},
|
|
|
|
labels: map[string]string{
|
|
|
|
RegionLabelKey: "a",
|
|
|
|
},
|
|
|
|
out: &mesh.Node{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.1"), 51820),
|
2021-02-22 19:28:16 +00:00
|
|
|
NoInternalIP: true,
|
|
|
|
InternalIP: nil,
|
2022-01-30 16:38:45 +00:00
|
|
|
Key: fooKey,
|
2021-02-22 19:28:16 +00:00
|
|
|
LastSeen: 1000000000,
|
|
|
|
Leader: false,
|
|
|
|
Location: "b",
|
2022-01-30 16:38:45 +00:00
|
|
|
PersistentKeepalive: 25 * time.Second,
|
2021-02-22 19:28:16 +00:00
|
|
|
Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
|
|
|
|
WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
|
|
|
|
},
|
|
|
|
subnet: "10.2.1.0/24",
|
|
|
|
},
|
2019-01-18 01:50:10 +00:00
|
|
|
} {
|
|
|
|
n := &v1.Node{}
|
|
|
|
n.ObjectMeta.Annotations = tc.annotations
|
|
|
|
n.ObjectMeta.Labels = tc.labels
|
|
|
|
n.Spec.PodCIDR = tc.subnet
|
2020-12-11 14:44:20 +00:00
|
|
|
node := translateNode(n, RegionLabelKey)
|
2019-01-18 01:50:10 +00:00
|
|
|
if diff := pretty.Compare(node, tc.out); diff != "" {
|
|
|
|
t.Errorf("test case %q: got diff: %v", tc.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 10:53:40 +00:00
|
|
|
|
|
|
|
func TestTranslatePeer(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
out *mesh.Peer
|
|
|
|
spec v1alpha1.PeerSpec
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
2022-01-30 16:38:45 +00:00
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid ips",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
AllowedIPs: []string{
|
|
|
|
"10.0.0.1",
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
},
|
2022-01-30 16:38:45 +00:00
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid ips",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
AllowedIPs: []string{
|
|
|
|
"10.0.0.1/24",
|
|
|
|
"10.0.0.2/32",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
AllowedIPs: []net.IPNet{
|
|
|
|
{IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
|
|
|
|
{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
|
|
|
|
},
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid endpoint ip",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
Endpoint: &v1alpha1.PeerEndpoint{
|
2020-02-28 14:48:32 +00:00
|
|
|
DNSOrIP: v1alpha1.DNSOrIP{
|
|
|
|
IP: "foo",
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
Port: mesh.DefaultKiloPort,
|
|
|
|
},
|
|
|
|
},
|
2022-01-30 16:38:45 +00:00
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
{
|
2020-02-28 14:48:32 +00:00
|
|
|
name: "only endpoint port",
|
2019-05-03 10:53:40 +00:00
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
Endpoint: &v1alpha1.PeerEndpoint{
|
2020-02-28 14:48:32 +00:00
|
|
|
Port: mesh.DefaultKiloPort,
|
|
|
|
},
|
|
|
|
},
|
2022-01-30 16:38:45 +00:00
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-28 14:48:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid endpoint ip",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
Endpoint: &v1alpha1.PeerEndpoint{
|
|
|
|
DNSOrIP: v1alpha1.DNSOrIP{
|
|
|
|
IP: "10.0.0.1",
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
Port: mesh.DefaultKiloPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("10.0.0.1").To4(), mesh.DefaultKiloPort),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid endpoint ipv6",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
Endpoint: &v1alpha1.PeerEndpoint{
|
|
|
|
DNSOrIP: v1alpha1.DNSOrIP{
|
|
|
|
IP: "ff60::2",
|
|
|
|
},
|
|
|
|
Port: mesh.DefaultKiloPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.NewEndpoint(net.ParseIP("ff60::2").To16(), mesh.DefaultKiloPort),
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-02-28 14:48:32 +00:00
|
|
|
{
|
|
|
|
name: "valid endpoint DNS",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
Endpoint: &v1alpha1.PeerEndpoint{
|
|
|
|
DNSOrIP: v1alpha1.DNSOrIP{
|
|
|
|
DNS: "example.com",
|
|
|
|
},
|
|
|
|
Port: mesh.DefaultKiloPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
Endpoint: wireguard.ParseEndpoint("example.com:51820"),
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
2020-02-28 14:48:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
{
|
|
|
|
name: "empty key",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
PublicKey: "",
|
|
|
|
},
|
2022-01-30 16:38:45 +00:00
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid key",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
2022-01-30 16:38:45 +00:00
|
|
|
PublicKey: fooKey.String(),
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PublicKey: fooKey,
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid keepalive",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
|
|
|
PersistentKeepalive: -1,
|
|
|
|
},
|
2022-01-30 16:38:45 +00:00
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid keepalive",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
2022-01-30 16:38:45 +00:00
|
|
|
PersistentKeepalive: 1 * int(time.Second),
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &second,
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-05-05 09:36:39 +00:00
|
|
|
{
|
|
|
|
name: "valid preshared key",
|
|
|
|
spec: v1alpha1.PeerSpec{
|
2022-01-30 16:38:45 +00:00
|
|
|
PresharedKey: pskKey.String(),
|
2020-05-05 09:36:39 +00:00
|
|
|
},
|
|
|
|
out: &mesh.Peer{
|
|
|
|
Peer: wireguard.Peer{
|
2022-01-30 16:38:45 +00:00
|
|
|
PeerConfig: wgtypes.PeerConfig{
|
|
|
|
PersistentKeepaliveInterval: &zero,
|
|
|
|
PresharedKey: pskKey,
|
|
|
|
},
|
2020-05-05 09:36:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-03 10:53:40 +00:00
|
|
|
} {
|
|
|
|
p := &v1alpha1.Peer{}
|
|
|
|
p.Spec = tc.spec
|
|
|
|
peer := translatePeer(p)
|
|
|
|
if diff := pretty.Compare(peer, tc.out); diff != "" {
|
|
|
|
t.Errorf("test case %q: got diff: %v", tc.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|