PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/integration/network/rest_authconfig_spec.rb

https://github.com/glarizza/puppet-1
Ruby | 145 lines | 104 code | 41 blank | 0 comment | 0 complexity | 27249a4b855b1acf328bbcccc4bb36de MD5 | raw file
  1. require 'spec_helper'
  2. require 'puppet/network/rest_authconfig'
  3. RSpec::Matchers.define :allow do |params|
  4. match do |auth|
  5. begin
  6. auth.check_authorization(params[0], params[1], params[2], params[3])
  7. true
  8. rescue Puppet::Network::AuthorizationError
  9. false
  10. end
  11. end
  12. failure_message_for_should do |instance|
  13. "expected #{params[3][:node]}/#{params[3][:ip]} to be allowed"
  14. end
  15. failure_message_for_should_not do |instance|
  16. "expected #{params[3][:node]}/#{params[3][:ip]} to be forbidden"
  17. end
  18. end
  19. describe Puppet::Network::RestAuthConfig do
  20. include PuppetSpec::Files
  21. before(:each) do
  22. Puppet[:rest_authconfig] = tmpfile('auth.conf')
  23. end
  24. def add_rule(rule)
  25. File.open(Puppet[:rest_authconfig],"w+") do |f|
  26. f.print "path /test\n#{rule}\n"
  27. end
  28. @auth = Puppet::Network::RestAuthConfig.new(Puppet[:rest_authconfig], true)
  29. end
  30. def add_regex_rule(regex, rule)
  31. File.open(Puppet[:rest_authconfig],"w+") do |f|
  32. f.print "path ~ #{regex}\n#{rule}\n"
  33. end
  34. @auth = Puppet::Network::RestAuthConfig.new(Puppet[:rest_authconfig], true)
  35. end
  36. def request(args = {})
  37. { :ip => '10.1.1.1', :node => 'host.domain.com', :key => 'key', :authenticated => true }.each do |k,v|
  38. args[k] ||= v
  39. end
  40. ['test', :find, args[:key], args]
  41. end
  42. it "should support IPv4 address" do
  43. add_rule("allow 10.1.1.1")
  44. @auth.should allow(request)
  45. end
  46. it "should support CIDR IPv4 address" do
  47. add_rule("allow 10.0.0.0/8")
  48. @auth.should allow(request)
  49. end
  50. it "should support wildcard IPv4 address" do
  51. add_rule("allow 10.1.1.*")
  52. @auth.should allow(request)
  53. end
  54. it "should support IPv6 address" do
  55. add_rule("allow 2001:DB8::8:800:200C:417A")
  56. @auth.should allow(request(:ip => '2001:DB8::8:800:200C:417A'))
  57. end
  58. it "should support hostname" do
  59. add_rule("allow host.domain.com")
  60. @auth.should allow(request)
  61. end
  62. it "should support wildcard host" do
  63. add_rule("allow *.domain.com")
  64. @auth.should allow(request)
  65. end
  66. it "should support hostname backreferences" do
  67. add_regex_rule('^/test/([^/]+)$', "allow $1.domain.com")
  68. @auth.should allow(request(:key => 'host'))
  69. end
  70. it "should support opaque strings" do
  71. add_rule("allow this-is-opaque@or-not")
  72. @auth.should allow(request(:node => 'this-is-opaque@or-not'))
  73. end
  74. it "should support opaque strings and backreferences" do
  75. add_regex_rule('^/test/([^/]+)$', "allow $1")
  76. @auth.should allow(request(:key => 'this-is-opaque@or-not', :node => 'this-is-opaque@or-not'))
  77. end
  78. it "should support hostname ending with '.'" do
  79. pending('bug #7589')
  80. add_rule("allow host.domain.com.")
  81. @auth.should allow(request(:node => 'host.domain.com.'))
  82. end
  83. it "should support hostname ending with '.' and backreferences" do
  84. pending('bug #7589')
  85. add_regex_rule('^/test/([^/]+)$',"allow $1")
  86. @auth.should allow(request(:node => 'host.domain.com.'))
  87. end
  88. it "should support trailing whitespace" do
  89. add_rule('allow host.domain.com ')
  90. @auth.should allow(request)
  91. end
  92. it "should support inlined comments" do
  93. add_rule('allow host.domain.com # will it work?')
  94. @auth.should allow(request)
  95. end
  96. it "should deny non-matching host" do
  97. add_rule("allow inexistant")
  98. @auth.should_not allow(request)
  99. end
  100. it "should deny denied hosts" do
  101. add_rule("deny host.domain.com")
  102. @auth.should_not allow(request)
  103. end
  104. end