backend_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package k8s
  15. import (
  16. "net"
  17. "testing"
  18. "github.com/kylelemons/godebug/pretty"
  19. v1 "k8s.io/api/core/v1"
  20. "github.com/squat/kilo/pkg/k8s/apis/kilo/v1alpha1"
  21. "github.com/squat/kilo/pkg/mesh"
  22. "github.com/squat/kilo/pkg/wireguard"
  23. )
  24. func TestTranslateNode(t *testing.T) {
  25. for _, tc := range []struct {
  26. name string
  27. annotations map[string]string
  28. labels map[string]string
  29. out *mesh.Node
  30. subnet string
  31. }{
  32. {
  33. name: "empty",
  34. annotations: nil,
  35. out: &mesh.Node{},
  36. },
  37. {
  38. name: "invalid ips",
  39. annotations: map[string]string{
  40. endpointAnnotationKey: "10.0.0.1",
  41. internalIPAnnotationKey: "foo",
  42. },
  43. out: &mesh.Node{},
  44. },
  45. {
  46. name: "valid ips",
  47. annotations: map[string]string{
  48. endpointAnnotationKey: "10.0.0.1:51820",
  49. internalIPAnnotationKey: "10.0.0.2/32",
  50. },
  51. out: &mesh.Node{
  52. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: mesh.DefaultKiloPort},
  53. InternalIP: &net.IPNet{IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  54. },
  55. },
  56. {
  57. name: "invalid subnet",
  58. annotations: map[string]string{},
  59. out: &mesh.Node{},
  60. subnet: "foo",
  61. },
  62. {
  63. name: "normalize subnet",
  64. annotations: map[string]string{},
  65. out: &mesh.Node{
  66. Subnet: &net.IPNet{IP: net.ParseIP("10.2.0.0"), Mask: net.CIDRMask(24, 32)},
  67. },
  68. subnet: "10.2.0.1/24",
  69. },
  70. {
  71. name: "valid subnet",
  72. annotations: map[string]string{},
  73. out: &mesh.Node{
  74. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  75. },
  76. subnet: "10.2.1.0/24",
  77. },
  78. {
  79. name: "region",
  80. labels: map[string]string{
  81. regionLabelKey: "a",
  82. },
  83. out: &mesh.Node{
  84. Location: "a",
  85. },
  86. },
  87. {
  88. name: "region override",
  89. annotations: map[string]string{
  90. locationAnnotationKey: "b",
  91. },
  92. labels: map[string]string{
  93. regionLabelKey: "a",
  94. },
  95. out: &mesh.Node{
  96. Location: "b",
  97. },
  98. },
  99. {
  100. name: "invalid endpoint override",
  101. annotations: map[string]string{
  102. endpointAnnotationKey: "10.0.0.1:51820",
  103. forceEndpointAnnotationKey: "-10.0.0.2:51821",
  104. },
  105. out: &mesh.Node{
  106. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: mesh.DefaultKiloPort},
  107. },
  108. },
  109. {
  110. name: "endpoint override",
  111. annotations: map[string]string{
  112. endpointAnnotationKey: "10.0.0.1:51820",
  113. forceEndpointAnnotationKey: "10.0.0.2:51821",
  114. },
  115. out: &mesh.Node{
  116. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.2")}, Port: 51821},
  117. },
  118. },
  119. {
  120. name: "wireguard persistent keepalive override",
  121. annotations: map[string]string{
  122. persistentKeepaliveKey: "25",
  123. },
  124. out: &mesh.Node{
  125. PersistentKeepalive: 25,
  126. },
  127. },
  128. {
  129. name: "invalid internal IP override",
  130. annotations: map[string]string{
  131. internalIPAnnotationKey: "10.1.0.1/24",
  132. forceInternalIPAnnotationKey: "-10.1.0.2/24",
  133. },
  134. out: &mesh.Node{
  135. InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.1"), Mask: net.CIDRMask(24, 32)},
  136. },
  137. },
  138. {
  139. name: "internal IP override",
  140. annotations: map[string]string{
  141. internalIPAnnotationKey: "10.1.0.1/24",
  142. forceInternalIPAnnotationKey: "10.1.0.2/24",
  143. },
  144. out: &mesh.Node{
  145. InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(24, 32)},
  146. },
  147. },
  148. {
  149. name: "invalid time",
  150. annotations: map[string]string{
  151. lastSeenAnnotationKey: "foo",
  152. },
  153. out: &mesh.Node{},
  154. },
  155. {
  156. name: "complete",
  157. annotations: map[string]string{
  158. endpointAnnotationKey: "10.0.0.1:51820",
  159. forceEndpointAnnotationKey: "10.0.0.2:51821",
  160. forceInternalIPAnnotationKey: "10.1.0.2/32",
  161. internalIPAnnotationKey: "10.1.0.1/32",
  162. keyAnnotationKey: "foo",
  163. lastSeenAnnotationKey: "1000000000",
  164. leaderAnnotationKey: "",
  165. locationAnnotationKey: "b",
  166. persistentKeepaliveKey: "25",
  167. wireGuardIPAnnotationKey: "10.4.0.1/16",
  168. },
  169. labels: map[string]string{
  170. regionLabelKey: "a",
  171. },
  172. out: &mesh.Node{
  173. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.2")}, Port: 51821},
  174. InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(32, 32)},
  175. Key: []byte("foo"),
  176. LastSeen: 1000000000,
  177. Leader: true,
  178. Location: "b",
  179. PersistentKeepalive: 25,
  180. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  181. WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
  182. },
  183. subnet: "10.2.1.0/24",
  184. },
  185. } {
  186. n := &v1.Node{}
  187. n.ObjectMeta.Annotations = tc.annotations
  188. n.ObjectMeta.Labels = tc.labels
  189. n.Spec.PodCIDR = tc.subnet
  190. node := translateNode(n)
  191. if diff := pretty.Compare(node, tc.out); diff != "" {
  192. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  193. }
  194. }
  195. }
  196. func TestTranslatePeer(t *testing.T) {
  197. for _, tc := range []struct {
  198. name string
  199. out *mesh.Peer
  200. spec v1alpha1.PeerSpec
  201. }{
  202. {
  203. name: "empty",
  204. out: &mesh.Peer{},
  205. },
  206. {
  207. name: "invalid ips",
  208. spec: v1alpha1.PeerSpec{
  209. AllowedIPs: []string{
  210. "10.0.0.1",
  211. "foo",
  212. },
  213. },
  214. out: &mesh.Peer{},
  215. },
  216. {
  217. name: "valid ips",
  218. spec: v1alpha1.PeerSpec{
  219. AllowedIPs: []string{
  220. "10.0.0.1/24",
  221. "10.0.0.2/32",
  222. },
  223. },
  224. out: &mesh.Peer{
  225. Peer: wireguard.Peer{
  226. AllowedIPs: []*net.IPNet{
  227. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  228. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  229. },
  230. },
  231. },
  232. },
  233. {
  234. name: "invalid endpoint ip",
  235. spec: v1alpha1.PeerSpec{
  236. Endpoint: &v1alpha1.PeerEndpoint{
  237. IP: "foo",
  238. Port: mesh.DefaultKiloPort,
  239. },
  240. },
  241. out: &mesh.Peer{},
  242. },
  243. {
  244. name: "valid endpoint",
  245. spec: v1alpha1.PeerSpec{
  246. Endpoint: &v1alpha1.PeerEndpoint{
  247. IP: "10.0.0.1",
  248. Port: mesh.DefaultKiloPort,
  249. },
  250. },
  251. out: &mesh.Peer{
  252. Peer: wireguard.Peer{
  253. Endpoint: &wireguard.Endpoint{
  254. DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")},
  255. Port: mesh.DefaultKiloPort,
  256. },
  257. },
  258. },
  259. },
  260. {
  261. name: "empty key",
  262. spec: v1alpha1.PeerSpec{
  263. PublicKey: "",
  264. },
  265. out: &mesh.Peer{},
  266. },
  267. {
  268. name: "valid key",
  269. spec: v1alpha1.PeerSpec{
  270. PublicKey: "foo",
  271. },
  272. out: &mesh.Peer{
  273. Peer: wireguard.Peer{
  274. PublicKey: []byte("foo"),
  275. },
  276. },
  277. },
  278. {
  279. name: "invalid keepalive",
  280. spec: v1alpha1.PeerSpec{
  281. PersistentKeepalive: -1,
  282. },
  283. out: &mesh.Peer{},
  284. },
  285. {
  286. name: "valid keepalive",
  287. spec: v1alpha1.PeerSpec{
  288. PersistentKeepalive: 1,
  289. },
  290. out: &mesh.Peer{
  291. Peer: wireguard.Peer{
  292. PersistentKeepalive: 1,
  293. },
  294. },
  295. },
  296. {
  297. name: "valid preshared key",
  298. spec: v1alpha1.PeerSpec{
  299. PresharedKey: "psk",
  300. },
  301. out: &mesh.Peer{
  302. Peer: wireguard.Peer{
  303. PresharedKey: []byte("psk"),
  304. },
  305. },
  306. },
  307. } {
  308. p := &v1alpha1.Peer{}
  309. p.Spec = tc.spec
  310. peer := translatePeer(p)
  311. if diff := pretty.Compare(peer, tc.out); diff != "" {
  312. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  313. }
  314. }
  315. }
  316. func TestParseEndpoint(t *testing.T) {
  317. for _, tc := range []struct {
  318. name string
  319. endpoint string
  320. out *wireguard.Endpoint
  321. }{
  322. {
  323. name: "empty",
  324. endpoint: "",
  325. out: nil,
  326. },
  327. {
  328. name: "invalid IP",
  329. endpoint: "10.0.0.:51820",
  330. out: nil,
  331. },
  332. {
  333. name: "invalid hostname",
  334. endpoint: "foo-:51820",
  335. out: nil,
  336. },
  337. {
  338. name: "invalid port",
  339. endpoint: "10.0.0.1:100000000",
  340. out: nil,
  341. },
  342. {
  343. name: "valid IP",
  344. endpoint: "10.0.0.1:51820",
  345. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: mesh.DefaultKiloPort},
  346. },
  347. {
  348. name: "valid IPv6",
  349. endpoint: "[ff02::114]:51820",
  350. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("ff02::114")}, Port: mesh.DefaultKiloPort},
  351. },
  352. {
  353. name: "valid hostname",
  354. endpoint: "foo:51821",
  355. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{DNS: "foo"}, Port: 51821},
  356. },
  357. } {
  358. endpoint := parseEndpoint(tc.endpoint)
  359. if diff := pretty.Compare(endpoint, tc.out); diff != "" {
  360. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  361. }
  362. }
  363. }