PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/integration/network/authconfig_spec.rb

https://github.com/david-caro/puppet
Ruby | 238 lines | 180 code | 58 blank | 0 comment | 1 complexity | 85a350daaf7266abdabcb67774d2bfd5 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-3.0
  1. require 'spec_helper'
  2. require 'puppet/network/authconfig'
  3. require 'puppet/network/auth_config_parser'
  4. RSpec::Matchers.define :allow do |params|
  5. match do |auth|
  6. begin
  7. auth.check_authorization(params[0], params[1], params[2], params[3])
  8. true
  9. rescue Puppet::Network::AuthorizationError
  10. false
  11. end
  12. end
  13. failure_message_for_should do |instance|
  14. "expected #{params[3][:node]}/#{params[3][:ip]} to be allowed"
  15. end
  16. failure_message_for_should_not do |instance|
  17. "expected #{params[3][:node]}/#{params[3][:ip]} to be forbidden"
  18. end
  19. end
  20. describe Puppet::Network::AuthConfig do
  21. include PuppetSpec::Files
  22. def add_rule(rule)
  23. parser = Puppet::Network::AuthConfigParser.new(
  24. "path /test\n#{rule}\n"
  25. )
  26. @auth = parser.parse
  27. end
  28. def add_regex_rule(regex, rule)
  29. parser = Puppet::Network::AuthConfigParser.new(
  30. "path ~ #{regex}\n#{rule}\n"
  31. )
  32. @auth = parser.parse
  33. end
  34. def request(args = {})
  35. args = {
  36. :key => 'key',
  37. :node => 'host.domain.com',
  38. :ip => '10.1.1.1',
  39. :authenticated => true
  40. }.merge(args)
  41. ['test', :find, args[:key], args]
  42. end
  43. describe "allow" do
  44. it "should not match IP addresses" do
  45. add_rule("allow 10.1.1.1")
  46. @auth.should_not allow(request)
  47. end
  48. it "should not accept CIDR IPv4 address" do
  49. expect {
  50. add_rule("allow 10.0.0.0/8")
  51. }.to raise_error Puppet::ConfigurationError, /Invalid pattern 10\.0\.0\.0\/8/
  52. end
  53. it "should not match wildcard IPv4 address" do
  54. expect {
  55. add_rule("allow 10.1.1.*")
  56. }.to raise_error Puppet::ConfigurationError, /Invalid pattern 10\.1\.1\.*/
  57. end
  58. it "should not match IPv6 address" do
  59. expect {
  60. add_rule("allow 2001:DB8::8:800:200C:417A")
  61. }.to raise_error Puppet::ConfigurationError, /Invalid pattern 2001/
  62. end
  63. it "should support hostname" do
  64. add_rule("allow host.domain.com")
  65. @auth.should allow(request)
  66. end
  67. it "should support wildcard host" do
  68. add_rule("allow *.domain.com")
  69. @auth.should allow(request)
  70. end
  71. it "should support hostname backreferences" do
  72. add_regex_rule('^/test/([^/]+)$', "allow $1.domain.com")
  73. @auth.should allow(request(:key => 'host'))
  74. end
  75. it "should support opaque strings" do
  76. add_rule("allow this-is-opaque@or-not")
  77. @auth.should allow(request(:node => 'this-is-opaque@or-not'))
  78. end
  79. it "should support opaque strings and backreferences" do
  80. add_regex_rule('^/test/([^/]+)$', "allow $1")
  81. @auth.should allow(request(:key => 'this-is-opaque@or-not', :node => 'this-is-opaque@or-not'))
  82. end
  83. it "should support hostname ending with '.'" do
  84. pending('bug #7589')
  85. add_rule("allow host.domain.com.")
  86. @auth.should allow(request(:node => 'host.domain.com.'))
  87. end
  88. it "should support hostname ending with '.' and backreferences" do
  89. pending('bug #7589')
  90. add_regex_rule('^/test/([^/]+)$',"allow $1")
  91. @auth.should allow(request(:node => 'host.domain.com.'))
  92. end
  93. it "should support trailing whitespace" do
  94. add_rule('allow host.domain.com ')
  95. @auth.should allow(request)
  96. end
  97. it "should support inlined comments" do
  98. add_rule('allow host.domain.com # will it work?')
  99. @auth.should allow(request)
  100. end
  101. it "should deny non-matching host" do
  102. add_rule("allow inexistant")
  103. @auth.should_not allow(request)
  104. end
  105. end
  106. describe "allow_ip" do
  107. it "should not warn when matches against IP addresses fail" do
  108. add_rule("allow_ip 10.1.1.2")
  109. @auth.should_not allow(request)
  110. @logs.should_not be_any {|log| log.level == :warning and log.message =~ /Authentication based on IP address is deprecated/}
  111. end
  112. it "should support IPv4 address" do
  113. add_rule("allow_ip 10.1.1.1")
  114. @auth.should allow(request)
  115. end
  116. it "should support CIDR IPv4 address" do
  117. add_rule("allow_ip 10.0.0.0/8")
  118. @auth.should allow(request)
  119. end
  120. it "should support wildcard IPv4 address" do
  121. add_rule("allow_ip 10.1.1.*")
  122. @auth.should allow(request)
  123. end
  124. it "should support IPv6 address" do
  125. add_rule("allow_ip 2001:DB8::8:800:200C:417A")
  126. @auth.should allow(request(:ip => '2001:DB8::8:800:200C:417A'))
  127. end
  128. it "should support hostname" do
  129. expect {
  130. add_rule("allow_ip host.domain.com")
  131. }.to raise_error Puppet::ConfigurationError, /Invalid IP pattern host.domain.com/
  132. end
  133. end
  134. describe "deny" do
  135. it "should deny denied hosts" do
  136. add_rule <<-EOALLOWRULE
  137. deny host.domain.com
  138. allow *.domain.com
  139. EOALLOWRULE
  140. @auth.should_not allow(request)
  141. end
  142. it "denies denied hosts after allowing them" do
  143. add_rule <<-EOALLOWRULE
  144. allow *.domain.com
  145. deny host.domain.com
  146. EOALLOWRULE
  147. @auth.should_not allow(request)
  148. end
  149. it "should not deny based on IP" do
  150. add_rule <<-EOALLOWRULE
  151. deny 10.1.1.1
  152. allow host.domain.com
  153. EOALLOWRULE
  154. @auth.should allow(request)
  155. end
  156. it "should not deny based on IP (ordering #2)" do
  157. add_rule <<-EOALLOWRULE
  158. allow host.domain.com
  159. deny 10.1.1.1
  160. EOALLOWRULE
  161. @auth.should allow(request)
  162. end
  163. end
  164. describe "deny_ip" do
  165. it "should deny based on IP" do
  166. add_rule <<-EOALLOWRULE
  167. deny_ip 10.1.1.1
  168. allow host.domain.com
  169. EOALLOWRULE
  170. @auth.should_not allow(request)
  171. end
  172. it "should deny based on IP (ordering #2)" do
  173. add_rule <<-EOALLOWRULE
  174. allow host.domain.com
  175. deny_ip 10.1.1.1
  176. EOALLOWRULE
  177. @auth.should_not allow(request)
  178. end
  179. end
  180. end