1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/network/rights'
describe Puppet::Network::Rights do
before do
@right = Puppet::Network::Rights.new
end
describe "when validating a :head request" do
[:find, :save].each do |allowed_method|
it "should allow the request if only #{allowed_method} is allowed" do
rights = Puppet::Network::Rights.new
right = rights.newright("/")
right.allow("*")
right.restrict_method(allowed_method)
right.restrict_authenticated(:any)
expect(rights.is_request_forbidden_and_why?(:head, "/indirection_name/key", {})).to eq(nil)
end
end
it "should disallow the request if neither :find nor :save is allowed" do
rights = Puppet::Network::Rights.new
why_forbidden = rights.is_request_forbidden_and_why?(:head, "/indirection_name/key", {})
expect(why_forbidden).to be_instance_of(Puppet::Network::AuthorizationError)
expect(why_forbidden.to_s).to eq("Forbidden request: /indirection_name/key [find]")
end
end
it "should throw an error if type can't be determined" do
expect { @right.newright("name") }.to raise_error(ArgumentError, /Unknown right type/)
end
describe "when creating new path ACLs" do
it "should not throw an error if the ACL already exists" do
@right.newright("/name")
expect { @right.newright("/name")}.not_to raise_error
end
it "should throw an error if the acl uri path is not absolute" do
expect { @right.newright("name")}.to raise_error(ArgumentError, /Unknown right type/)
end
it "should create a new ACL with the correct path" do
@right.newright("/name")
expect(@right["/name"]).not_to be_nil
end
it "should create an ACL of type Puppet::Network::AuthStore" do
@right.newright("/name")
expect(@right["/name"]).to be_a_kind_of(Puppet::Network::AuthStore)
end
end
describe "when creating new regex ACLs" do
it "should not throw an error if the ACL already exists" do
@right.newright("~ .rb$")
expect { @right.newright("~ .rb$")}.not_to raise_error
end
it "should create a new ACL with the correct regex" do
@right.newright("~ .rb$")
expect(@right.include?(".rb$")).not_to be_nil
end
it "should be able to lookup the regex" do
@right.newright("~ .rb$")
expect(@right[".rb$"]).not_to be_nil
end
it "should be able to lookup the regex by its full name" do
@right.newright("~ .rb$")
expect(@right["~ .rb$"]).not_to be_nil
end
it "should create an ACL of type Puppet::Network::AuthStore" do
expect(@right.newright("~ .rb$")).to be_a_kind_of(Puppet::Network::AuthStore)
end
end
describe "when checking ACLs existence" do
it "should return false if there are no matching rights" do
expect(@right.include?("name")).to be_falsey
end
it "should return true if a path right exists" do
@right.newright("/name")
expect(@right.include?("/name")).to be_truthy
end
it "should return false if no matching path rights exist" do
@right.newright("/name")
expect(@right.include?("/differentname")).to be_falsey
end
it "should return true if a regex right exists" do
@right.newright("~ .rb$")
expect(@right.include?(".rb$")).to be_truthy
end
it "should return false if no matching path rights exist" do
@right.newright("~ .rb$")
expect(@right.include?(".pp$")).to be_falsey
end
end
describe "when checking if right is allowed" do
before :each do
@right.stubs(:right).returns(nil)
@pathacl = stub 'pathacl', :"<=>" => 1, :line => 0, :file => 'dummy'
Puppet::Network::Rights::Right.stubs(:new).returns(@pathacl)
end
it "should delegate to is_forbidden_and_why?" do
@right.expects(:is_forbidden_and_why?).with("namespace", :node => "host.domain.com", :ip => "127.0.0.1").returns(nil)
@right.allowed?("namespace", "host.domain.com", "127.0.0.1")
end
it "should return true if is_forbidden_and_why? returns nil" do
@right.stubs(:is_forbidden_and_why?).returns(nil)
expect(@right.allowed?("namespace", :args)).to be_truthy
end
it "should return false if is_forbidden_and_why? returns an AuthorizationError" do
@right.stubs(:is_forbidden_and_why?).returns(Puppet::Network::AuthorizationError.new("forbidden"))
expect(@right.allowed?("namespace", :args1, :args2)).to be_falsey
end
it "should pass the match? return to allowed?" do
@right.newright("/path/to/there")
@pathacl.expects(:match?).returns(:match)
@pathacl.expects(:allowed?).with { |node,ip,h| h[:match] == :match }.returns(true)
expect(@right.is_forbidden_and_why?("/path/to/there", {})).to eq(nil)
end
describe "with path acls" do
before :each do
@long_acl = stub 'longpathacl', :name => "/path/to/there", :line => 0, :file => 'dummy'
Puppet::Network::Rights::Right.stubs(:new).with("/path/to/there", 0, nil).returns(@long_acl)
@short_acl = stub 'shortpathacl', :name => "/path/to", :line => 0, :file => 'dummy'
Puppet::Network::Rights::Right.stubs(:new).with("/path/to", 0, nil).returns(@short_acl)
@long_acl.stubs(:"<=>").with(@short_acl).returns(0)
@short_acl.stubs(:"<=>").with(@long_acl).returns(0)
end
it "should select the first match" do
@right.newright("/path/to", 0)
@right.newright("/path/to/there", 0)
@long_acl.stubs(:match?).returns(true)
@short_acl.stubs(:match?).returns(true)
@short_acl.expects(:allowed?).returns(true)
@long_acl.expects(:allowed?).never
expect(@right.is_forbidden_and_why?("/path/to/there/and/there", {})).to eq(nil)
end
it "should select the first match that doesn't return :dunno" do
@right.newright("/path/to/there", 0, nil)
@right.newright("/path/to", 0, nil)
@long_acl.stubs(:match?).returns(true)
@short_acl.stubs(:match?).returns(true)
@long_acl.expects(:allowed?).returns(:dunno)
@short_acl.expects(:allowed?).returns(true)
expect(@right.is_forbidden_and_why?("/path/to/there/and/there", {})).to eq(nil)
end
it "should not select an ACL that doesn't match" do
@right.newright("/path/to/there", 0)
@right.newright("/path/to", 0)
@long_acl.stubs(:match?).returns(false)
@short_acl.stubs(:match?).returns(true)
@long_acl.expects(:allowed?).never
@short_acl.expects(:allowed?).returns(true)
expect(@right.is_forbidden_and_why?("/path/to/there/and/there", {})).to eq(nil)
end
it "should not raise an AuthorizationError if allowed" do
@right.newright("/path/to/there", 0)
@long_acl.stubs(:match?).returns(true)
@long_acl.stubs(:allowed?).returns(true)
expect(@right.is_forbidden_and_why?("/path/to/there/and/there", {})).to eq(nil)
end
it "should raise an AuthorizationError if the match is denied" do
@right.newright("/path/to/there", 0, nil)
@long_acl.stubs(:match?).returns(true)
@long_acl.stubs(:allowed?).returns(false)
expect(@right.is_forbidden_and_why?("/path/to/there", {})).to be_instance_of(Puppet::Network::AuthorizationError)
end
it "should raise an AuthorizationError if no path match" do
expect(@right.is_forbidden_and_why?("/nomatch", {})).to be_instance_of(Puppet::Network::AuthorizationError)
end
end
describe "with regex acls" do
before :each do
@regex_acl1 = stub 'regex_acl1', :name => "/files/(.*)/myfile", :line => 0, :file => 'dummy'
Puppet::Network::Rights::Right.stubs(:new).with("~ /files/(.*)/myfile", 0, nil).returns(@regex_acl1)
@regex_acl2 = stub 'regex_acl2', :name => "/files/(.*)/myfile/", :line => 0, :file => 'dummy'
Puppet::Network::Rights::Right.stubs(:new).with("~ /files/(.*)/myfile/", 0, nil).returns(@regex_acl2)
@regex_acl1.stubs(:"<=>").with(@regex_acl2).returns(0)
@regex_acl2.stubs(:"<=>").with(@regex_acl1).returns(0)
end
it "should select the first match" do
@right.newright("~ /files/(.*)/myfile", 0)
@right.newright("~ /files/(.*)/myfile/", 0)
@regex_acl1.stubs(:match?).returns(true)
@regex_acl2.stubs(:match?).returns(true)
@regex_acl1.expects(:allowed?).returns(true)
@regex_acl2.expects(:allowed?).never
expect(@right.is_forbidden_and_why?("/files/repository/myfile/other", {})).to eq(nil)
end
it "should select the first match that doesn't return :dunno" do
@right.newright("~ /files/(.*)/myfile", 0)
@right.newright("~ /files/(.*)/myfile/", 0)
@regex_acl1.stubs(:match?).returns(true)
@regex_acl2.stubs(:match?).returns(true)
@regex_acl1.expects(:allowed?).returns(:dunno)
@regex_acl2.expects(:allowed?).returns(true)
expect(@right.is_forbidden_and_why?("/files/repository/myfile/other", {})).to eq(nil)
end
it "should not select an ACL that doesn't match" do
@right.newright("~ /files/(.*)/myfile", 0)
@right.newright("~ /files/(.*)/myfile/", 0)
@regex_acl1.stubs(:match?).returns(false)
@regex_acl2.stubs(:match?).returns(true)
@regex_acl1.expects(:allowed?).never
@regex_acl2.expects(:allowed?).returns(true)
expect(@right.is_forbidden_and_why?("/files/repository/myfile/other", {})).to eq(nil)
end
it "should not raise an AuthorizationError if allowed" do
@right.newright("~ /files/(.*)/myfile", 0)
@regex_acl1.stubs(:match?).returns(true)
@regex_acl1.stubs(:allowed?).returns(true)
expect(@right.is_forbidden_and_why?("/files/repository/myfile/other", {})).to eq(nil)
end
it "should raise an error if no regex acl match" do
expect(@right.is_forbidden_and_why?("/path", {})).to be_instance_of(Puppet::Network::AuthorizationError)
end
it "should raise an AuthorizedError on deny" do
expect(@right.is_forbidden_and_why?("/path", {})).to be_instance_of(Puppet::Network::AuthorizationError)
end
end
end
describe Puppet::Network::Rights::Right do
before :each do
@acl = Puppet::Network::Rights::Right.new("/path",0, nil)
end
describe "with path" do
it "should match up to its path length" do
expect(@acl.match?("/path/that/works")).not_to be_nil
end
it "should match up to its path length" do
expect(@acl.match?("/paththatalsoworks")).not_to be_nil
end
it "should return nil if no match" do
expect(@acl.match?("/notpath")).to be_nil
end
end
describe "with regex" do
before :each do
@acl = Puppet::Network::Rights::Right.new("~ .rb$",0, nil)
end
it "should match as a regex" do
expect(@acl.match?("this should work.rb")).not_to be_nil
end
it "should return nil if no match" do
expect(@acl.match?("do not match")).to be_nil
end
end
it "should allow all rest methods by default" do
expect(@acl.methods).to eq(Puppet::Network::Rights::Right::ALL)
end
it "should allow only authenticated request by default" do
expect(@acl.authentication).to be_truthy
end
it "should allow modification of the methods filters" do
@acl.restrict_method(:save)
expect(@acl.methods).to eq([:save])
end
it "should stack methods filters" do
@acl.restrict_method(:save)
@acl.restrict_method(:destroy)
expect(@acl.methods).to eq([:save, :destroy])
end
it "should raise an error if the method is already filtered" do
@acl.restrict_method(:save)
expect { @acl.restrict_method(:save) }.to raise_error(ArgumentError, /'save' is already in the '\/path'/)
end
it "should allow setting an environment filters" do
env = Puppet::Node::Environment.create(:acltest, [])
Puppet.override(:environments => Puppet::Environments::Static.new(env)) do
@acl.restrict_environment(:acltest)
expect(@acl.environment).to eq([env])
end
end
["on", "yes", "true", true].each do |auth|
it "should allow filtering on authenticated requests with '#{auth}'" do
@acl.restrict_authenticated(auth)
expect(@acl.authentication).to be_truthy
end
end
["off", "no", "false", false, "all", "any", :all, :any].each do |auth|
it "should allow filtering on authenticated or unauthenticated requests with '#{auth}'" do
@acl.restrict_authenticated(auth)
expect(@acl.authentication).to be_falsey
end
end
describe "when checking right authorization" do
it "should return :dunno if this right is not restricted to the given method" do
@acl.restrict_method(:destroy)
expect(@acl.allowed?("me","127.0.0.1", { :method => :save } )).to eq(:dunno)
end
it "should return true if this right is restricted to the given method" do
@acl.restrict_method(:save)
@acl.allow("me")
expect(@acl.allowed?("me","127.0.0.1", { :method => :save, :authenticated => true })).to eq true
end
it "should return :dunno if this right is not restricted to the given environment" do
prod = Puppet::Node::Environment.create(:production, [])
dev = Puppet::Node::Environment.create(:development, [])
Puppet.override(:environments => Puppet::Environments::Static.new(prod, dev)) do
@acl.restrict_environment(:production)
expect(@acl.allowed?("me","127.0.0.1", { :method => :save, :environment => dev })).to eq(:dunno)
end
end
it "returns true if the request is permitted for this environment" do
@acl.allow("me")
prod = Puppet::Node::Environment.create(:production, [])
Puppet.override(:environments => Puppet::Environments::Static.new(prod)) do
@acl.restrict_environment(:production)
expect(@acl.allowed?("me", "127.0.0.1", { :method => :save, :authenticated => true, :environment => prod })).to eq true
end
end
it "should return :dunno if this right is not restricted to the given request authentication state" do
@acl.restrict_authenticated(true)
expect(@acl.allowed?("me","127.0.0.1", { :method => :save, :authenticated => false })).to eq(:dunno)
end
it "returns true if this right is restricted to the given request authentication state" do
@acl.restrict_authenticated(false)
@acl.allow("me")
expect(@acl.allowed?("me","127.0.0.1", {:method => :save, :authenticated => false })).to eq true
end
it "should interpolate allow/deny patterns with the given match" do
@acl.expects(:interpolate).with(:match)
@acl.allowed?("me","127.0.0.1", { :method => :save, :match => :match, :authenticated => true })
end
it "should reset interpolation after the match" do
@acl.expects(:reset_interpolation)
@acl.allowed?("me","127.0.0.1", { :method => :save, :match => :match, :authenticated => true })
end
end
end
end
|