2
0

backend_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/kilo-io/kilo/pkg/k8s/apis/kilo/v1alpha1"
  21. "github.com/kilo-io/kilo/pkg/mesh"
  22. "github.com/kilo-io/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. NoInternalIP: false,
  137. },
  138. },
  139. {
  140. name: "internal IP override",
  141. annotations: map[string]string{
  142. internalIPAnnotationKey: "10.1.0.1/24",
  143. forceInternalIPAnnotationKey: "10.1.0.2/24",
  144. },
  145. out: &mesh.Node{
  146. InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(24, 32)},
  147. NoInternalIP: false,
  148. },
  149. },
  150. {
  151. name: "invalid time",
  152. annotations: map[string]string{
  153. lastSeenAnnotationKey: "foo",
  154. },
  155. out: &mesh.Node{},
  156. },
  157. {
  158. name: "complete",
  159. annotations: map[string]string{
  160. endpointAnnotationKey: "10.0.0.1:51820",
  161. forceEndpointAnnotationKey: "10.0.0.2:51821",
  162. forceInternalIPAnnotationKey: "10.1.0.2/32",
  163. internalIPAnnotationKey: "10.1.0.1/32",
  164. keyAnnotationKey: "foo",
  165. lastSeenAnnotationKey: "1000000000",
  166. leaderAnnotationKey: "",
  167. locationAnnotationKey: "b",
  168. persistentKeepaliveKey: "25",
  169. wireGuardIPAnnotationKey: "10.4.0.1/16",
  170. },
  171. labels: map[string]string{
  172. RegionLabelKey: "a",
  173. },
  174. out: &mesh.Node{
  175. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.2")}, Port: 51821},
  176. NoInternalIP: false,
  177. InternalIP: &net.IPNet{IP: net.ParseIP("10.1.0.2"), Mask: net.CIDRMask(32, 32)},
  178. Key: []byte("foo"),
  179. LastSeen: 1000000000,
  180. Leader: true,
  181. Location: "b",
  182. PersistentKeepalive: 25,
  183. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  184. WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
  185. },
  186. subnet: "10.2.1.0/24",
  187. },
  188. {
  189. name: "no InternalIP",
  190. annotations: map[string]string{
  191. endpointAnnotationKey: "10.0.0.1:51820",
  192. internalIPAnnotationKey: "",
  193. keyAnnotationKey: "foo",
  194. lastSeenAnnotationKey: "1000000000",
  195. locationAnnotationKey: "b",
  196. persistentKeepaliveKey: "25",
  197. wireGuardIPAnnotationKey: "10.4.0.1/16",
  198. },
  199. labels: map[string]string{
  200. RegionLabelKey: "a",
  201. },
  202. out: &mesh.Node{
  203. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: 51820},
  204. InternalIP: nil,
  205. Key: []byte("foo"),
  206. LastSeen: 1000000000,
  207. Leader: false,
  208. Location: "b",
  209. PersistentKeepalive: 25,
  210. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  211. WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
  212. },
  213. subnet: "10.2.1.0/24",
  214. },
  215. {
  216. name: "Force no internal IP",
  217. annotations: map[string]string{
  218. endpointAnnotationKey: "10.0.0.1:51820",
  219. internalIPAnnotationKey: "10.1.0.1/32",
  220. forceInternalIPAnnotationKey: "",
  221. keyAnnotationKey: "foo",
  222. lastSeenAnnotationKey: "1000000000",
  223. locationAnnotationKey: "b",
  224. persistentKeepaliveKey: "25",
  225. wireGuardIPAnnotationKey: "10.4.0.1/16",
  226. },
  227. labels: map[string]string{
  228. RegionLabelKey: "a",
  229. },
  230. out: &mesh.Node{
  231. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: 51820},
  232. NoInternalIP: true,
  233. InternalIP: nil,
  234. Key: []byte("foo"),
  235. LastSeen: 1000000000,
  236. Leader: false,
  237. Location: "b",
  238. PersistentKeepalive: 25,
  239. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  240. WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
  241. },
  242. subnet: "10.2.1.0/24",
  243. },
  244. } {
  245. n := &v1.Node{}
  246. n.ObjectMeta.Annotations = tc.annotations
  247. n.ObjectMeta.Labels = tc.labels
  248. n.Spec.PodCIDR = tc.subnet
  249. node := translateNode(n, RegionLabelKey)
  250. if diff := pretty.Compare(node, tc.out); diff != "" {
  251. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  252. }
  253. }
  254. }
  255. func TestTranslatePeer(t *testing.T) {
  256. for _, tc := range []struct {
  257. name string
  258. out *mesh.Peer
  259. spec v1alpha1.PeerSpec
  260. }{
  261. {
  262. name: "empty",
  263. out: &mesh.Peer{},
  264. },
  265. {
  266. name: "invalid ips",
  267. spec: v1alpha1.PeerSpec{
  268. AllowedIPs: []string{
  269. "10.0.0.1",
  270. "foo",
  271. },
  272. },
  273. out: &mesh.Peer{},
  274. },
  275. {
  276. name: "valid ips",
  277. spec: v1alpha1.PeerSpec{
  278. AllowedIPs: []string{
  279. "10.0.0.1/24",
  280. "10.0.0.2/32",
  281. },
  282. },
  283. out: &mesh.Peer{
  284. Peer: wireguard.Peer{
  285. AllowedIPs: []*net.IPNet{
  286. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  287. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  288. },
  289. },
  290. },
  291. },
  292. {
  293. name: "invalid endpoint ip",
  294. spec: v1alpha1.PeerSpec{
  295. Endpoint: &v1alpha1.PeerEndpoint{
  296. DNSOrIP: v1alpha1.DNSOrIP{
  297. IP: "foo",
  298. },
  299. Port: mesh.DefaultKiloPort,
  300. },
  301. },
  302. out: &mesh.Peer{},
  303. },
  304. {
  305. name: "only endpoint port",
  306. spec: v1alpha1.PeerSpec{
  307. Endpoint: &v1alpha1.PeerEndpoint{
  308. Port: mesh.DefaultKiloPort,
  309. },
  310. },
  311. out: &mesh.Peer{},
  312. },
  313. {
  314. name: "valid endpoint ip",
  315. spec: v1alpha1.PeerSpec{
  316. Endpoint: &v1alpha1.PeerEndpoint{
  317. DNSOrIP: v1alpha1.DNSOrIP{
  318. IP: "10.0.0.1",
  319. },
  320. Port: mesh.DefaultKiloPort,
  321. },
  322. },
  323. out: &mesh.Peer{
  324. Peer: wireguard.Peer{
  325. Endpoint: &wireguard.Endpoint{
  326. DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")},
  327. Port: mesh.DefaultKiloPort,
  328. },
  329. },
  330. },
  331. },
  332. {
  333. name: "valid endpoint DNS",
  334. spec: v1alpha1.PeerSpec{
  335. Endpoint: &v1alpha1.PeerEndpoint{
  336. DNSOrIP: v1alpha1.DNSOrIP{
  337. DNS: "example.com",
  338. },
  339. Port: mesh.DefaultKiloPort,
  340. },
  341. },
  342. out: &mesh.Peer{
  343. Peer: wireguard.Peer{
  344. Endpoint: &wireguard.Endpoint{
  345. DNSOrIP: wireguard.DNSOrIP{DNS: "example.com"},
  346. Port: mesh.DefaultKiloPort,
  347. },
  348. },
  349. },
  350. },
  351. {
  352. name: "empty key",
  353. spec: v1alpha1.PeerSpec{
  354. PublicKey: "",
  355. },
  356. out: &mesh.Peer{},
  357. },
  358. {
  359. name: "valid key",
  360. spec: v1alpha1.PeerSpec{
  361. PublicKey: "foo",
  362. },
  363. out: &mesh.Peer{
  364. Peer: wireguard.Peer{
  365. PublicKey: []byte("foo"),
  366. },
  367. },
  368. },
  369. {
  370. name: "invalid keepalive",
  371. spec: v1alpha1.PeerSpec{
  372. PersistentKeepalive: -1,
  373. },
  374. out: &mesh.Peer{},
  375. },
  376. {
  377. name: "valid keepalive",
  378. spec: v1alpha1.PeerSpec{
  379. PersistentKeepalive: 1,
  380. },
  381. out: &mesh.Peer{
  382. Peer: wireguard.Peer{
  383. PersistentKeepalive: 1,
  384. },
  385. },
  386. },
  387. {
  388. name: "valid preshared key",
  389. spec: v1alpha1.PeerSpec{
  390. PresharedKey: "psk",
  391. },
  392. out: &mesh.Peer{
  393. Peer: wireguard.Peer{
  394. PresharedKey: []byte("psk"),
  395. },
  396. },
  397. },
  398. } {
  399. p := &v1alpha1.Peer{}
  400. p.Spec = tc.spec
  401. peer := translatePeer(p)
  402. if diff := pretty.Compare(peer, tc.out); diff != "" {
  403. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  404. }
  405. }
  406. }
  407. func TestParseEndpoint(t *testing.T) {
  408. for _, tc := range []struct {
  409. name string
  410. endpoint string
  411. out *wireguard.Endpoint
  412. }{
  413. {
  414. name: "empty",
  415. endpoint: "",
  416. out: nil,
  417. },
  418. {
  419. name: "invalid IP",
  420. endpoint: "10.0.0.:51820",
  421. out: nil,
  422. },
  423. {
  424. name: "invalid hostname",
  425. endpoint: "foo-:51820",
  426. out: nil,
  427. },
  428. {
  429. name: "invalid port",
  430. endpoint: "10.0.0.1:100000000",
  431. out: nil,
  432. },
  433. {
  434. name: "valid IP",
  435. endpoint: "10.0.0.1:51820",
  436. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: mesh.DefaultKiloPort},
  437. },
  438. {
  439. name: "valid IPv6",
  440. endpoint: "[ff02::114]:51820",
  441. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("ff02::114")}, Port: mesh.DefaultKiloPort},
  442. },
  443. {
  444. name: "valid hostname",
  445. endpoint: "foo:51821",
  446. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{DNS: "foo"}, Port: 51821},
  447. },
  448. } {
  449. endpoint := parseEndpoint(tc.endpoint)
  450. if diff := pretty.Compare(endpoint, tc.out); diff != "" {
  451. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  452. }
  453. }
  454. }