backend_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. DNSOrIP: v1alpha1.DNSOrIP{
  238. IP: "foo",
  239. },
  240. Port: mesh.DefaultKiloPort,
  241. },
  242. },
  243. out: &mesh.Peer{},
  244. },
  245. {
  246. name: "only endpoint port",
  247. spec: v1alpha1.PeerSpec{
  248. Endpoint: &v1alpha1.PeerEndpoint{
  249. Port: mesh.DefaultKiloPort,
  250. },
  251. },
  252. out: &mesh.Peer{},
  253. },
  254. {
  255. name: "valid endpoint ip",
  256. spec: v1alpha1.PeerSpec{
  257. Endpoint: &v1alpha1.PeerEndpoint{
  258. DNSOrIP: v1alpha1.DNSOrIP{
  259. IP: "10.0.0.1",
  260. },
  261. Port: mesh.DefaultKiloPort,
  262. },
  263. },
  264. out: &mesh.Peer{
  265. Peer: wireguard.Peer{
  266. Endpoint: &wireguard.Endpoint{
  267. DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")},
  268. Port: mesh.DefaultKiloPort,
  269. },
  270. },
  271. },
  272. },
  273. {
  274. name: "valid endpoint DNS",
  275. spec: v1alpha1.PeerSpec{
  276. Endpoint: &v1alpha1.PeerEndpoint{
  277. DNSOrIP: v1alpha1.DNSOrIP{
  278. DNS: "example.com",
  279. },
  280. Port: mesh.DefaultKiloPort,
  281. },
  282. },
  283. out: &mesh.Peer{
  284. Peer: wireguard.Peer{
  285. Endpoint: &wireguard.Endpoint{
  286. DNSOrIP: wireguard.DNSOrIP{DNS: "example.com"},
  287. Port: mesh.DefaultKiloPort,
  288. },
  289. },
  290. },
  291. },
  292. {
  293. name: "empty key",
  294. spec: v1alpha1.PeerSpec{
  295. PublicKey: "",
  296. },
  297. out: &mesh.Peer{},
  298. },
  299. {
  300. name: "valid key",
  301. spec: v1alpha1.PeerSpec{
  302. PublicKey: "foo",
  303. },
  304. out: &mesh.Peer{
  305. Peer: wireguard.Peer{
  306. PublicKey: []byte("foo"),
  307. },
  308. },
  309. },
  310. {
  311. name: "invalid keepalive",
  312. spec: v1alpha1.PeerSpec{
  313. PersistentKeepalive: -1,
  314. },
  315. out: &mesh.Peer{},
  316. },
  317. {
  318. name: "valid keepalive",
  319. spec: v1alpha1.PeerSpec{
  320. PersistentKeepalive: 1,
  321. },
  322. out: &mesh.Peer{
  323. Peer: wireguard.Peer{
  324. PersistentKeepalive: 1,
  325. },
  326. },
  327. },
  328. {
  329. name: "valid preshared key",
  330. spec: v1alpha1.PeerSpec{
  331. PresharedKey: "psk",
  332. },
  333. out: &mesh.Peer{
  334. Peer: wireguard.Peer{
  335. PresharedKey: []byte("psk"),
  336. },
  337. },
  338. },
  339. } {
  340. p := &v1alpha1.Peer{}
  341. p.Spec = tc.spec
  342. peer := translatePeer(p)
  343. if diff := pretty.Compare(peer, tc.out); diff != "" {
  344. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  345. }
  346. }
  347. }
  348. func TestParseEndpoint(t *testing.T) {
  349. for _, tc := range []struct {
  350. name string
  351. endpoint string
  352. out *wireguard.Endpoint
  353. }{
  354. {
  355. name: "empty",
  356. endpoint: "",
  357. out: nil,
  358. },
  359. {
  360. name: "invalid IP",
  361. endpoint: "10.0.0.:51820",
  362. out: nil,
  363. },
  364. {
  365. name: "invalid hostname",
  366. endpoint: "foo-:51820",
  367. out: nil,
  368. },
  369. {
  370. name: "invalid port",
  371. endpoint: "10.0.0.1:100000000",
  372. out: nil,
  373. },
  374. {
  375. name: "valid IP",
  376. endpoint: "10.0.0.1:51820",
  377. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("10.0.0.1")}, Port: mesh.DefaultKiloPort},
  378. },
  379. {
  380. name: "valid IPv6",
  381. endpoint: "[ff02::114]:51820",
  382. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("ff02::114")}, Port: mesh.DefaultKiloPort},
  383. },
  384. {
  385. name: "valid hostname",
  386. endpoint: "foo:51821",
  387. out: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{DNS: "foo"}, Port: 51821},
  388. },
  389. } {
  390. endpoint := parseEndpoint(tc.endpoint)
  391. if diff := pretty.Compare(endpoint, tc.out); diff != "" {
  392. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  393. }
  394. }
  395. }