backend_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. name: "no InternalIP",
  187. annotations: map[string]string{
  188. endpointAnnotationKey: "10.0.0.1:51820",
  189. internalIPAnnotationKey: "",
  190. keyAnnotationKey: "foo",
  191. lastSeenAnnotationKey: "1000000000",
  192. locationAnnotationKey: "b",
  193. persistentKeepaliveKey: "25",
  194. wireGuardIPAnnotationKey: "10.4.0.1/16",
  195. },
  196. labels: map[string]string{
  197. RegionLabelKey: "a",
  198. },
  199. out: &mesh.Node{
  200. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: 51820},
  201. InternalIP: nil,
  202. Key: []byte("foo"),
  203. LastSeen: 1000000000,
  204. Leader: false,
  205. Location: "b",
  206. PersistentKeepalive: 25,
  207. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  208. WireGuardIP: &net.IPNet{IP: net.ParseIP("10.4.0.1"), Mask: net.CIDRMask(16, 32)},
  209. },
  210. subnet: "10.2.1.0/24",
  211. },
  212. } {
  213. n := &v1.Node{}
  214. n.ObjectMeta.Annotations = tc.annotations
  215. n.ObjectMeta.Labels = tc.labels
  216. n.Spec.PodCIDR = tc.subnet
  217. node := translateNode(n, RegionLabelKey)
  218. if diff := pretty.Compare(node, tc.out); diff != "" {
  219. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  220. }
  221. }
  222. }
  223. func TestTranslatePeer(t *testing.T) {
  224. for _, tc := range []struct {
  225. name string
  226. out *mesh.Peer
  227. spec v1alpha1.PeerSpec
  228. }{
  229. {
  230. name: "empty",
  231. out: &mesh.Peer{},
  232. },
  233. {
  234. name: "invalid ips",
  235. spec: v1alpha1.PeerSpec{
  236. AllowedIPs: []string{
  237. "10.0.0.1",
  238. "foo",
  239. },
  240. },
  241. out: &mesh.Peer{},
  242. },
  243. {
  244. name: "valid ips",
  245. spec: v1alpha1.PeerSpec{
  246. AllowedIPs: []string{
  247. "10.0.0.1/24",
  248. "10.0.0.2/32",
  249. },
  250. },
  251. out: &mesh.Peer{
  252. Peer: wireguard.Peer{
  253. AllowedIPs: []*net.IPNet{
  254. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  255. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32)},
  256. },
  257. },
  258. },
  259. },
  260. {
  261. name: "invalid endpoint ip",
  262. spec: v1alpha1.PeerSpec{
  263. Endpoint: &v1alpha1.PeerEndpoint{
  264. DNSOrIP: v1alpha1.DNSOrIP{
  265. IP: "foo",
  266. },
  267. Port: mesh.DefaultKiloPort,
  268. },
  269. },
  270. out: &mesh.Peer{},
  271. },
  272. {
  273. name: "only endpoint port",
  274. spec: v1alpha1.PeerSpec{
  275. Endpoint: &v1alpha1.PeerEndpoint{
  276. Port: mesh.DefaultKiloPort,
  277. },
  278. },
  279. out: &mesh.Peer{},
  280. },
  281. {
  282. name: "valid endpoint ip",
  283. spec: v1alpha1.PeerSpec{
  284. Endpoint: &v1alpha1.PeerEndpoint{
  285. DNSOrIP: v1alpha1.DNSOrIP{
  286. IP: "10.0.0.1",
  287. },
  288. Port: mesh.DefaultKiloPort,
  289. },
  290. },
  291. out: &mesh.Peer{
  292. Peer: wireguard.Peer{
  293. Endpoint: &wireguard.Endpoint{
  294. DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")},
  295. Port: mesh.DefaultKiloPort,
  296. },
  297. },
  298. },
  299. },
  300. {
  301. name: "valid endpoint DNS",
  302. spec: v1alpha1.PeerSpec{
  303. Endpoint: &v1alpha1.PeerEndpoint{
  304. DNSOrIP: v1alpha1.DNSOrIP{
  305. DNS: "example.com",
  306. },
  307. Port: mesh.DefaultKiloPort,
  308. },
  309. },
  310. out: &mesh.Peer{
  311. Peer: wireguard.Peer{
  312. Endpoint: &wireguard.Endpoint{
  313. DNSOrIP: wireguard.DNSOrIP{DNS: "example.com"},
  314. Port: mesh.DefaultKiloPort,
  315. },
  316. },
  317. },
  318. },
  319. {
  320. name: "empty key",
  321. spec: v1alpha1.PeerSpec{
  322. PublicKey: "",
  323. },
  324. out: &mesh.Peer{},
  325. },
  326. {
  327. name: "valid key",
  328. spec: v1alpha1.PeerSpec{
  329. PublicKey: "foo",
  330. },
  331. out: &mesh.Peer{
  332. Peer: wireguard.Peer{
  333. PublicKey: []byte("foo"),
  334. },
  335. },
  336. },
  337. {
  338. name: "invalid keepalive",
  339. spec: v1alpha1.PeerSpec{
  340. PersistentKeepalive: -1,
  341. },
  342. out: &mesh.Peer{},
  343. },
  344. {
  345. name: "valid keepalive",
  346. spec: v1alpha1.PeerSpec{
  347. PersistentKeepalive: 1,
  348. },
  349. out: &mesh.Peer{
  350. Peer: wireguard.Peer{
  351. PersistentKeepalive: 1,
  352. },
  353. },
  354. },
  355. {
  356. name: "valid preshared key",
  357. spec: v1alpha1.PeerSpec{
  358. PresharedKey: "psk",
  359. },
  360. out: &mesh.Peer{
  361. Peer: wireguard.Peer{
  362. PresharedKey: []byte("psk"),
  363. },
  364. },
  365. },
  366. } {
  367. p := &v1alpha1.Peer{}
  368. p.Spec = tc.spec
  369. peer := translatePeer(p)
  370. if diff := pretty.Compare(peer, tc.out); diff != "" {
  371. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  372. }
  373. }
  374. }
  375. func TestParseEndpoint(t *testing.T) {
  376. for _, tc := range []struct {
  377. name string
  378. endpoint string
  379. out *wireguard.Endpoint
  380. }{
  381. {
  382. name: "empty",
  383. endpoint: "",
  384. out: nil,
  385. },
  386. {
  387. name: "invalid IP",
  388. endpoint: "10.0.0.:51820",
  389. out: nil,
  390. },
  391. {
  392. name: "invalid hostname",
  393. endpoint: "foo-:51820",
  394. out: nil,
  395. },
  396. {
  397. name: "invalid port",
  398. endpoint: "10.0.0.1:100000000",
  399. out: nil,
  400. },
  401. {
  402. name: "valid IP",
  403. endpoint: "10.0.0.1:51820",
  404. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: mesh.DefaultKiloPort},
  405. },
  406. {
  407. name: "valid IPv6",
  408. endpoint: "[ff02::114]:51820",
  409. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("ff02::114")}, Port: mesh.DefaultKiloPort},
  410. },
  411. {
  412. name: "valid hostname",
  413. endpoint: "foo:51821",
  414. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{DNS: "foo"}, Port: 51821},
  415. },
  416. } {
  417. endpoint := parseEndpoint(tc.endpoint)
  418. if diff := pretty.Compare(endpoint, tc.out); diff != "" {
  419. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  420. }
  421. }
  422. }