PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/unit/network/authstore_spec.rb

https://github.com/imdhmd/puppet
Ruby | 403 lines | 375 code | 27 blank | 1 comment | 6 complexity | f4f03e2f79e3b7695cb699e0f5dce4b4 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-3.0
  1. #! /usr/bin/env ruby
  2. require 'spec_helper'
  3. require 'puppet/network/authconfig'
  4. describe Puppet::Network::AuthStore do
  5. before :each do
  6. @authstore = Puppet::Network::AuthStore.new
  7. @authstore.reset_interpolation
  8. end
  9. describe "when checking if the acl has some entries" do
  10. it "should be empty if no ACE have been entered" do
  11. @authstore.should be_empty
  12. end
  13. it "should not be empty if it is a global allow" do
  14. @authstore.allow('*')
  15. @authstore.should_not be_empty
  16. end
  17. it "should not be empty if at least one allow has been entered" do
  18. @authstore.allow_ip('1.1.1.*')
  19. @authstore.should_not be_empty
  20. end
  21. it "should not be empty if at least one deny has been entered" do
  22. @authstore.deny_ip('1.1.1.*')
  23. @authstore.should_not be_empty
  24. end
  25. end
  26. describe "when checking global allow" do
  27. it "should not be enabled by default" do
  28. @authstore.should_not be_globalallow
  29. @authstore.should_not be_allowed('foo.bar.com', '192.168.1.1')
  30. end
  31. it "should always allow when enabled" do
  32. @authstore.allow('*')
  33. @authstore.should be_globalallow
  34. @authstore.should be_allowed('foo.bar.com', '192.168.1.1')
  35. end
  36. end
  37. describe "when checking a regex type of allow" do
  38. before :each do
  39. @authstore.allow('/^(test-)?host[0-9]+\.other-domain\.(com|org|net)$|some-domain\.com/')
  40. @ip = '192.168.1.1'
  41. end
  42. ['host5.other-domain.com', 'test-host12.other-domain.net', 'foo.some-domain.com'].each { |name|
  43. it "should allow the host #{name}" do
  44. @authstore.should be_allowed(name, @ip)
  45. end
  46. }
  47. ['host0.some-other-domain.com',''].each { |name|
  48. it "should not allow the host #{name}" do
  49. @authstore.should_not be_allowed(name, @ip)
  50. end
  51. }
  52. end
  53. end
  54. describe Puppet::Network::AuthStore::Declaration do
  55. ['100.101.99.98','100.100.100.100','1.2.3.4','11.22.33.44'].each { |ip|
  56. describe "when the pattern is a simple numeric IP such as #{ip}" do
  57. before :each do
  58. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow_ip,ip)
  59. end
  60. it "should match the specified IP" do
  61. @declaration.should be_match('www.testsite.org',ip)
  62. end
  63. it "should not match other IPs" do
  64. @declaration.should_not be_match('www.testsite.org','200.101.99.98')
  65. end
  66. end
  67. (1..3).each { |n|
  68. describe "when the pattern is a IP mask with #{n} numeric segments and a *" do
  69. before :each do
  70. @ip_pattern = ip.split('.')[0,n].join('.')+'.*'
  71. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow_ip,@ip_pattern)
  72. end
  73. it "should match an IP in the range" do
  74. @declaration.should be_match('www.testsite.org',ip)
  75. end
  76. it "should not match other IPs" do
  77. @declaration.should_not be_match('www.testsite.org','200.101.99.98')
  78. end
  79. it "should not match IPs that differ in the last non-wildcard segment" do
  80. other = ip.split('.')
  81. other[n-1].succ!
  82. @declaration.should_not be_match('www.testsite.org',other.join('.'))
  83. end
  84. end
  85. }
  86. }
  87. describe "when the pattern is a numeric IP with a back reference" do
  88. pending("implementation of backreferences for IP") do
  89. before :each do
  90. @ip = '100.101.$1'
  91. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow_ip,@ip).interpolate('12.34'.match(/(.*)/))
  92. end
  93. it "should match an IP with the appropriate interpolation" do
  94. @declaration.should be_match('www.testsite.org',@ip.sub(/\$1/,'12.34'))
  95. end
  96. it "should not match other IPs" do
  97. @declaration.should_not be_match('www.testsite.org',@ip.sub(/\$1/,'66.34'))
  98. end
  99. end
  100. end
  101. [
  102. "02001:0000:1234:0000:0000:C1C0:ABCD:0876",
  103. "2001:0000:1234:0000:00001:C1C0:ABCD:0876",
  104. " 2001:0000:1234:0000:0000:C1C0:ABCD:0876 0",
  105. "2001:0000:1234: 0000:0000:C1C0:ABCD:0876",
  106. "3ffe:0b00:0000:0001:0000:0000:000a",
  107. "FF02:0000:0000:0000:0000:0000:0000:0000:0001",
  108. "3ffe:b00::1::a",
  109. "1:2:3::4:5::7:8",
  110. "12345::6:7:8",
  111. "1::5:400.2.3.4",
  112. "1::5:260.2.3.4",
  113. "1::5:256.2.3.4",
  114. "1::5:1.256.3.4",
  115. "1::5:1.2.256.4",
  116. "1::5:1.2.3.256",
  117. "1::5:300.2.3.4",
  118. "1::5:1.300.3.4",
  119. "1::5:1.2.300.4",
  120. "1::5:1.2.3.300",
  121. "1::5:900.2.3.4",
  122. "1::5:1.900.3.4",
  123. "1::5:1.2.900.4",
  124. "1::5:1.2.3.900",
  125. "1::5:300.300.300.300",
  126. "1::5:3000.30.30.30",
  127. "1::400.2.3.4",
  128. "1::260.2.3.4",
  129. "1::256.2.3.4",
  130. "1::1.256.3.4",
  131. "1::1.2.256.4",
  132. "1::1.2.3.256",
  133. "1::300.2.3.4",
  134. "1::1.300.3.4",
  135. "1::1.2.300.4",
  136. "1::1.2.3.300",
  137. "1::900.2.3.4",
  138. "1::1.900.3.4",
  139. "1::1.2.900.4",
  140. "1::1.2.3.900",
  141. "1::300.300.300.300",
  142. "1::3000.30.30.30",
  143. "::400.2.3.4",
  144. "::260.2.3.4",
  145. "::256.2.3.4",
  146. "::1.256.3.4",
  147. "::1.2.256.4",
  148. "::1.2.3.256",
  149. "::300.2.3.4",
  150. "::1.300.3.4",
  151. "::1.2.300.4",
  152. "::1.2.3.300",
  153. "::900.2.3.4",
  154. "::1.900.3.4",
  155. "::1.2.900.4",
  156. "::1.2.3.900",
  157. "::300.300.300.300",
  158. "::3000.30.30.30",
  159. "2001:DB8:0:0:8:800:200C:417A:221", # unicast, full
  160. "FF01::101::2" # multicast, compressed
  161. ].each { |invalid_ip|
  162. describe "when the pattern is an invalid IPv6 address such as #{invalid_ip}" do
  163. it "should raise an exception" do
  164. lambda { Puppet::Network::AuthStore::Declaration.new(:allow,invalid_ip) }.should raise_error
  165. end
  166. end
  167. }
  168. [
  169. "1.2.3.4",
  170. "2001:0000:1234:0000:0000:C1C0:ABCD:0876",
  171. "3ffe:0b00:0000:0000:0001:0000:0000:000a",
  172. "FF02:0000:0000:0000:0000:0000:0000:0001",
  173. "0000:0000:0000:0000:0000:0000:0000:0001",
  174. "0000:0000:0000:0000:0000:0000:0000:0000",
  175. "::ffff:192.168.1.26",
  176. "2::10",
  177. "ff02::1",
  178. "fe80::",
  179. "2002::",
  180. "2001:db8::",
  181. "2001:0db8:1234::",
  182. "::ffff:0:0",
  183. "::1",
  184. "::ffff:192.168.1.1",
  185. "1:2:3:4:5:6:7:8",
  186. "1:2:3:4:5:6::8",
  187. "1:2:3:4:5::8",
  188. "1:2:3:4::8",
  189. "1:2:3::8",
  190. "1:2::8",
  191. "1::8",
  192. "1::2:3:4:5:6:7",
  193. "1::2:3:4:5:6",
  194. "1::2:3:4:5",
  195. "1::2:3:4",
  196. "1::2:3",
  197. "1::8",
  198. "::2:3:4:5:6:7:8",
  199. "::2:3:4:5:6:7",
  200. "::2:3:4:5:6",
  201. "::2:3:4:5",
  202. "::2:3:4",
  203. "::2:3",
  204. "::8",
  205. "1:2:3:4:5:6::",
  206. "1:2:3:4:5::",
  207. "1:2:3:4::",
  208. "1:2:3::",
  209. "1:2::",
  210. "1::",
  211. "1:2:3:4:5::7:8",
  212. "1:2:3:4::7:8",
  213. "1:2:3::7:8",
  214. "1:2::7:8",
  215. "1::7:8",
  216. "1:2:3:4:5:6:1.2.3.4",
  217. "1:2:3:4:5::1.2.3.4",
  218. "1:2:3:4::1.2.3.4",
  219. "1:2:3::1.2.3.4",
  220. "1:2::1.2.3.4",
  221. "1::1.2.3.4",
  222. "1:2:3:4::5:1.2.3.4",
  223. "1:2:3::5:1.2.3.4",
  224. "1:2::5:1.2.3.4",
  225. "1::5:1.2.3.4",
  226. "1::5:11.22.33.44",
  227. "fe80::217:f2ff:254.7.237.98",
  228. "fe80::217:f2ff:fe07:ed62",
  229. "2001:DB8:0:0:8:800:200C:417A", # unicast, full
  230. "FF01:0:0:0:0:0:0:101", # multicast, full
  231. "0:0:0:0:0:0:0:1", # loopback, full
  232. "0:0:0:0:0:0:0:0", # unspecified, full
  233. "2001:DB8::8:800:200C:417A", # unicast, compressed
  234. "FF01::101", # multicast, compressed
  235. "::1", # loopback, compressed, non-routable
  236. "::", # unspecified, compressed, non-routable
  237. "0:0:0:0:0:0:13.1.68.3", # IPv4-compatible IPv6 address, full, deprecated
  238. "0:0:0:0:0:FFFF:129.144.52.38", # IPv4-mapped IPv6 address, full
  239. "::13.1.68.3", # IPv4-compatible IPv6 address, compressed, deprecated
  240. "::FFFF:129.144.52.38", # IPv4-mapped IPv6 address, compressed
  241. "2001:0DB8:0000:CD30:0000:0000:0000:0000/60", # full, with prefix
  242. "2001:0DB8::CD30:0:0:0:0/60", # compressed, with prefix
  243. "2001:0DB8:0:CD30::/60", # compressed, with prefix #2
  244. "::/128", # compressed, unspecified address type, non-routable
  245. "::1/128", # compressed, loopback address type, non-routable
  246. "FF00::/8", # compressed, multicast address type
  247. "FE80::/10", # compressed, link-local unicast, non-routable
  248. "FEC0::/10", # compressed, site-local unicast, deprecated
  249. "127.0.0.1", # standard IPv4, loopback, non-routable
  250. "0.0.0.0", # standard IPv4, unspecified, non-routable
  251. "255.255.255.255", # standard IPv4
  252. "fe80:0000:0000:0000:0204:61ff:fe9d:f156",
  253. "fe80:0:0:0:204:61ff:fe9d:f156",
  254. "fe80::204:61ff:fe9d:f156",
  255. "fe80:0000:0000:0000:0204:61ff:254.157.241.086",
  256. "fe80:0:0:0:204:61ff:254.157.241.86",
  257. "fe80::204:61ff:254.157.241.86",
  258. "::1",
  259. "fe80::",
  260. "fe80::1"
  261. ].each { |ip|
  262. describe "when the pattern is a valid IP such as #{ip}" do
  263. before :each do
  264. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow_ip,ip)
  265. end
  266. it "should match the specified IP" do
  267. @declaration.should be_match('www.testsite.org',ip)
  268. end
  269. it "should not match other IPs" do
  270. @declaration.should_not be_match('www.testsite.org','200.101.99.98')
  271. end
  272. end unless ip =~ /:.*\./ # Hybrid IPs aren't supported by ruby's ipaddr
  273. }
  274. {
  275. 'spirit.mars.nasa.gov' => 'a PQDN',
  276. 'ratchet.2ndsiteinc.com' => 'a PQDN with digits',
  277. 'a.c.ru' => 'a PQDN with short segments',
  278. }.each {|pqdn,desc|
  279. describe "when the pattern is #{desc}" do
  280. before :each do
  281. @host = pqdn
  282. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@host)
  283. end
  284. it "should match the specified PQDN" do
  285. @declaration.should be_match(@host,'200.101.99.98')
  286. end
  287. it "should not match a similar FQDN" do
  288. pending "FQDN consensus"
  289. @declaration.should_not be_match(@host+'.','200.101.99.98')
  290. end
  291. end
  292. }
  293. ['abc.12seps.edu.phisher.biz','www.google.com','slashdot.org'].each { |host|
  294. (1...(host.split('.').length)).each { |n|
  295. describe "when the pattern is #{"*."+host.split('.')[-n,n].join('.')}" do
  296. before :each do
  297. @pattern = "*."+host.split('.')[-n,n].join('.')
  298. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@pattern)
  299. end
  300. it "should match #{host}" do
  301. @declaration.should be_match(host,'1.2.3.4')
  302. end
  303. it "should not match www.testsite.gov" do
  304. @declaration.should_not be_match('www.testsite.gov','200.101.99.98')
  305. end
  306. it "should not match hosts that differ in the first non-wildcard segment" do
  307. other = host.split('.')
  308. other[-n].succ!
  309. @declaration.should_not be_match(other.join('.'),'1.2.3.4')
  310. end
  311. end
  312. }
  313. }
  314. describe "when the pattern is a FQDN" do
  315. before :each do
  316. @host = 'spirit.mars.nasa.gov.'
  317. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,@host)
  318. end
  319. it "should match the specified FQDN" do
  320. pending "FQDN consensus"
  321. @declaration.should be_match(@host,'200.101.99.98')
  322. end
  323. it "should not match a similar PQDN" do
  324. @declaration.should_not be_match(@host[0..-2],'200.101.99.98')
  325. end
  326. end
  327. describe "when the pattern is an opaque string with a back reference" do
  328. before :each do
  329. @host = 'c216f41a-f902-4bfb-a222-850dd957bebb'
  330. @item = "/catalog/#{@host}"
  331. @pattern = %{^/catalog/([^/]+)$}
  332. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,'$1')
  333. end
  334. it "should match an IP with the appropriate interpolation" do
  335. @declaration.interpolate(@item.match(@pattern)).should be_match(@host,'10.0.0.5')
  336. end
  337. end
  338. describe "when the pattern is an opaque string with a back reference and the matched data contains dots" do
  339. before :each do
  340. @host = 'admin.mgmt.nym1'
  341. @item = "/catalog/#{@host}"
  342. @pattern = %{^/catalog/([^/]+)$}
  343. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,'$1')
  344. end
  345. it "should match a name with the appropriate interpolation" do
  346. @declaration.interpolate(@item.match(@pattern)).should be_match(@host,'10.0.0.5')
  347. end
  348. end
  349. describe "when the pattern is an opaque string with a back reference and the matched data contains dots with an initial prefix that looks like an IP address" do
  350. before :each do
  351. @host = '01.admin.mgmt.nym1'
  352. @item = "/catalog/#{@host}"
  353. @pattern = %{^/catalog/([^/]+)$}
  354. @declaration = Puppet::Network::AuthStore::Declaration.new(:allow,'$1')
  355. end
  356. it "should match a name with the appropriate interpolation" do
  357. @declaration.interpolate(@item.match(@pattern)).should be_match(@host,'10.0.0.5')
  358. end
  359. end
  360. describe "when comparing patterns" do
  361. before :each do
  362. @ip = Puppet::Network::AuthStore::Declaration.new(:allow,'127.0.0.1')
  363. @host_name = Puppet::Network::AuthStore::Declaration.new(:allow,'www.hard_knocks.edu')
  364. @opaque = Puppet::Network::AuthStore::Declaration.new(:allow,'hey_dude')
  365. end
  366. it "should consider ip addresses before host names" do
  367. (@ip < @host_name).should be_true
  368. end
  369. it "should consider ip addresses before opaque strings" do
  370. (@ip < @opaque).should be_true
  371. end
  372. it "should consider host_names before opaque strings" do
  373. (@host_name < @opaque).should be_true
  374. end
  375. end
  376. end