aclatraz 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +0 -11
- data/CHANGELOG.rdoc +6 -0
- data/Rakefile +43 -41
- data/aclatraz.gemspec +20 -91
- data/lib/aclatraz.rb +7 -6
- data/lib/aclatraz/acl.rb +6 -8
- data/lib/aclatraz/store.rb +1 -0
- data/lib/aclatraz/store/cassandra.rb +2 -3
- data/lib/aclatraz/store/mongo.rb +56 -0
- data/lib/aclatraz/store/redis.rb +2 -3
- data/lib/aclatraz/store/riak.rb +2 -3
- data/spec/aclatraz/acl_spec.rb +34 -30
- data/spec/aclatraz/guard_spec.rb +136 -147
- data/spec/aclatraz/stores_spec.rb +54 -50
- data/spec/aclatraz/suspect_spec.rb +72 -70
- data/spec/aclatraz_spec.rb +14 -9
- data/spec/alcatraz_bm.rb +1 -1
- data/spec/spec_helper.rb +20 -0
- metadata +20 -26
- data/.document +0 -5
- data/VERSION +0 -1
data/spec/aclatraz/guard_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe "Aclatraz guard" do
|
4
|
-
before(:all) { Aclatraz.init(:redis, "redis://localhost:6379/0") }
|
5
|
-
let(:suspect) { @foo ||= StubSuspect.new }
|
6
|
-
|
7
4
|
subject { Class.new(StubGuarded) }
|
5
|
+
let(:suspect) { @suspect ||= StubSuspect.new }
|
6
|
+
before(:all) { Aclatraz.init(:redis, "redis://localhost:6379/0") }
|
7
|
+
define_method(:deny_access) { raise_error(Aclatraz::AccessDenied) }
|
8
8
|
|
9
9
|
it "#acl_guard? should be true" do
|
10
10
|
subject.acl_guard?.should be_true
|
@@ -12,19 +12,19 @@ describe "Aclatraz guard" do
|
|
12
12
|
|
13
13
|
it "should properly guard permissions" do
|
14
14
|
guarded_class = subject
|
15
|
-
guarded_class.name = "
|
15
|
+
guarded_class.name = "FirstGuarded"
|
16
16
|
|
17
17
|
guarded_class.suspects suspect do
|
18
|
-
allow :
|
19
|
-
deny
|
18
|
+
allow :manager
|
19
|
+
deny :client
|
20
20
|
on :bar do
|
21
|
-
allow :
|
22
|
-
deny
|
21
|
+
allow :bartender
|
22
|
+
deny :owner => StubTarget
|
23
23
|
end
|
24
|
-
on :
|
25
|
-
deny
|
26
|
-
allow :
|
27
|
-
allow :
|
24
|
+
on :kitchen do
|
25
|
+
deny :bartender
|
26
|
+
allow :client_of => :target
|
27
|
+
allow :cleaner_of => 'bar'
|
28
28
|
end
|
29
29
|
on :deny_all do
|
30
30
|
deny all
|
@@ -34,7 +34,7 @@ describe "Aclatraz guard" do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
guarded_class.suspects do
|
37
|
-
allow :
|
37
|
+
allow :boss
|
38
38
|
end
|
39
39
|
|
40
40
|
guarded_class.class_eval do
|
@@ -43,163 +43,152 @@ describe "Aclatraz guard" do
|
|
43
43
|
|
44
44
|
guarded = guarded_class.new
|
45
45
|
|
46
|
-
lambda { guarded.guard! }.should
|
47
|
-
suspect.is.
|
48
|
-
lambda { guarded.guard! }.
|
49
|
-
suspect.is.
|
50
|
-
|
51
|
-
|
52
|
-
lambda { guarded.guard! }.
|
53
|
-
|
54
|
-
lambda { guarded.guard!(:bar) }.
|
55
|
-
suspect.
|
56
|
-
lambda { guarded.guard!(:bar) }.should
|
57
|
-
|
58
|
-
lambda { guarded.guard!(:
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
lambda { guarded.guard!(:
|
65
|
-
|
66
|
-
|
67
|
-
suspect.is_not.role3!
|
68
|
-
suspect.is.role1!
|
69
|
-
lambda { guarded.guard!(:bla) }.should_not raise_error(Aclatraz::AccessDenied)
|
70
|
-
suspect.is_not.role1!
|
71
|
-
lambda { guarded.guard!(:bla) }.should raise_error(Aclatraz::AccessDenied)
|
72
|
-
suspect.is.role2!(guarded.target)
|
73
|
-
lambda { guarded.guard!(:bla) }.should_not raise_error(Aclatraz::AccessDenied)
|
74
|
-
suspect.is_not.role2!(guarded.target)
|
46
|
+
lambda { guarded.guard! }.should deny_access
|
47
|
+
lambda { suspect.is.manager!; guarded.guard! }.should_not deny_access
|
48
|
+
lambda { suspect.is.client!; guarded.guard! }.should deny_access
|
49
|
+
lambda { suspect.is.boss!; guarded.guard! }.should_not deny_access
|
50
|
+
|
51
|
+
lambda { guarded.guard!(:bar) }.should_not deny_access
|
52
|
+
lambda { suspect.is_not.boss!; guarded.guard!(:bar) }.should deny_access
|
53
|
+
lambda { suspect.is_not.client!; guarded.guard!(:bar) }.should_not deny_access
|
54
|
+
lambda { suspect.is_not.manager!; guarded.guard!(:bar) }.should deny_access
|
55
|
+
lambda { suspect.is.bartender!; guarded.guard!(:bar) }.should_not deny_access
|
56
|
+
lambda { suspect.is.owner!(StubTarget); guarded.guard!(:bar) }.should deny_access
|
57
|
+
|
58
|
+
lambda { guarded.guard!(:kitchen) }.should deny_access
|
59
|
+
lambda {
|
60
|
+
suspect.is_not.bartender!
|
61
|
+
suspect.is.manager!
|
62
|
+
guarded.guard!(:kitchen)
|
63
|
+
}.should_not deny_access
|
64
|
+
lambda { suspect.is_not.manager!; guarded.guard!(:kitchen) }.should deny_access
|
65
|
+
lambda { suspect.is.client!(guarded.target); guarded.guard!(:kitchen) }.should_not deny_access
|
66
|
+
|
75
67
|
bar = StubTarget.new
|
76
68
|
guarded.instance_variable_set('@bar', bar)
|
77
|
-
|
78
|
-
lambda {
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
suspect.
|
85
|
-
lambda {
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
suspect.
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
lambda { guarded.guard!(:
|
98
|
-
lambda { guarded.guard!(:
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
lambda { guarded.guard!(:
|
103
|
-
|
104
|
-
lambda { guarded.guard!(:bar, :allow_all, :
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
lambda {
|
110
|
-
|
111
|
-
|
69
|
+
|
70
|
+
lambda {
|
71
|
+
suspect.is_not.client!(guarded.target)
|
72
|
+
suspect.is.cleaner!(bar)
|
73
|
+
guarded.guard!(:kitchen)
|
74
|
+
}.should_not deny_access
|
75
|
+
|
76
|
+
lambda { suspect.is.bartender!; guarded.guard!(:kitchen) }.should_not deny_access
|
77
|
+
lambda {
|
78
|
+
suspect.is_not.cleaner!(bar)
|
79
|
+
suspect.is.boss!
|
80
|
+
guarded.guard!(:kitchen)
|
81
|
+
}.should deny_access
|
82
|
+
lambda { suspect.is_not.bartender!; guarded.guard!(:kitchen) }.should_not deny_access
|
83
|
+
|
84
|
+
lambda {
|
85
|
+
suspect.is_not.boss!
|
86
|
+
suspect.is_not.owner!(StubTarget)
|
87
|
+
guarded.guard!(:bar, :kitchen)
|
88
|
+
}.should deny_access
|
89
|
+
lambda { suspect.is.manager!; guarded.guard!(:bar, :kitchen) }.should_not deny_access
|
90
|
+
lambda { suspect.is.owner!(StubTarget); guarded.guard!(:bar, :kitchen) }.should deny_access
|
91
|
+
lambda { suspect.is.client!(guarded.target); guarded.guard!(:bar, :kitchen) }.should_not deny_access
|
92
|
+
|
93
|
+
lambda { guarded.guard!(:allow_all) }.should_not deny_access
|
94
|
+
lambda { guarded.guard!(:deny_all) }.should deny_access
|
95
|
+
|
96
|
+
lambda { guarded.guard!(:bar, :allow_all, :kitchen) }.should_not deny_access
|
97
|
+
lambda {
|
98
|
+
suspect.is_not.client!(guarded.target)
|
99
|
+
guarded.guard!(:bar, :allow_all, :kitchen)
|
100
|
+
}.should_not deny_access
|
101
|
+
lambda {
|
102
|
+
suspect.is.bartender!
|
103
|
+
guarded.guard!(:bar, :allow_all, :kitchen)
|
104
|
+
}.should deny_access
|
105
|
+
|
106
|
+
lambda {
|
107
|
+
suspect.is_not.bartender!
|
108
|
+
guarded.guard!(:bar, :deny_all, :kitchen)
|
109
|
+
}.should deny_access
|
110
|
+
lambda {
|
111
|
+
suspect.is.client!(guarded.target)
|
112
|
+
guarded.guard!(:bar, :deny_all, :kitchen)
|
113
|
+
}.should_not deny_access
|
114
|
+
|
115
|
+
lambda { guarded.guard! { deny :client => :target } }.should deny_access
|
112
116
|
end
|
113
117
|
|
114
|
-
it "when invalid permission given
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
118
|
+
it "should raise error when invalid permission given" do
|
119
|
+
lambda {
|
120
|
+
guarded_class = subject
|
121
|
+
guarded_class.name = "SecondGuarded"
|
122
|
+
guarded_class.suspects(suspect) { allow Object.new }
|
123
|
+
guarded = guarded_class.new
|
124
|
+
guarded.guard!
|
125
|
+
}.should raise_error(Aclatraz::InvalidPermission)
|
120
126
|
end
|
121
127
|
|
122
|
-
it "when invalid suspect given
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
+
it "should raise error when invalid suspect given" do
|
129
|
+
lambda {
|
130
|
+
guarded_class = subject
|
131
|
+
guarded_class.name = "ThirdGuarded"
|
132
|
+
guarded_class.suspects('invalid_suspect') { }
|
133
|
+
guarded = guarded_class.new
|
134
|
+
guarded.guard!
|
135
|
+
}.should raise_error(Aclatraz::InvalidSuspect)
|
128
136
|
end
|
129
137
|
|
130
|
-
it "when ACL is not defined
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
138
|
+
it "should raise error when ACL is not defined" do
|
139
|
+
lambda {
|
140
|
+
guarded_class = subject
|
141
|
+
guarded_class.name = "FourthGuarded"
|
142
|
+
guarded = guarded_class.new
|
143
|
+
guarded.guard!
|
144
|
+
}.should raise_error(Aclatraz::UndefinedAccessControlList)
|
135
145
|
end
|
136
146
|
|
137
|
-
it "
|
147
|
+
it "suspect should reference to instance method when given suspect name is kind of symbol" do
|
138
148
|
guarded_class = subject
|
139
|
-
guarded_class.name = "
|
140
|
-
guarded_class.suspects(:
|
149
|
+
guarded_class.name = "FifthGuarded"
|
150
|
+
guarded_class.suspects(:user) {}
|
141
151
|
guarded = guarded_class.new
|
142
|
-
guarded.class.class_eval { def
|
143
|
-
guarded.suspect.should == guarded.
|
152
|
+
guarded.class.class_eval { def user; @user ||= StubSuspect.new; end }
|
153
|
+
guarded.suspect.should == guarded.user
|
144
154
|
end
|
145
155
|
|
146
|
-
it "when given suspect name is
|
156
|
+
it "suspect should reference to instance variable when given suspect name is kind of string" do
|
147
157
|
guarded_class = subject
|
148
|
-
guarded_class.name = "
|
149
|
-
guarded_class.suspects('
|
158
|
+
guarded_class.name = "SixthGuarded"
|
159
|
+
guarded_class.suspects('user') {}
|
150
160
|
guarded = guarded_class.new
|
151
|
-
guarded.instance_variable_set("@
|
152
|
-
guarded.suspect.should == guarded.instance_variable_get("@
|
161
|
+
guarded.instance_variable_set("@user", StubSuspect.new)
|
162
|
+
guarded.suspect.should == guarded.instance_variable_get("@user")
|
153
163
|
end
|
154
164
|
|
155
|
-
it "
|
156
|
-
|
165
|
+
it "suspect should reference to given object if passed" do
|
166
|
+
suspect = StubSuspect.new
|
157
167
|
guarded_class = subject
|
158
|
-
guarded_class.name = "
|
159
|
-
guarded_class.suspects(
|
168
|
+
guarded_class.name = "SeventhGuarded"
|
169
|
+
guarded_class.suspects(suspect) {}
|
160
170
|
guarded = guarded_class.new
|
161
|
-
guarded.suspect.should ==
|
171
|
+
guarded.suspect.should == suspect
|
162
172
|
end
|
163
173
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
suspects :user do
|
169
|
-
allow :nested1
|
170
|
-
deny :nested2
|
171
|
-
end
|
172
|
-
|
173
|
-
def user; @user ||= StubSuspect.new; end
|
174
|
-
end
|
174
|
+
it "should properly resolve inherited permissions" do
|
175
|
+
parent = GuardedParent.new
|
176
|
+
child = GuardedChild.new
|
175
177
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
allow :nested3
|
180
|
-
end
|
181
|
-
end
|
178
|
+
child.user.is_not.cooker!
|
179
|
+
child.user.is_not.waiter!
|
180
|
+
child.user.is_not.manager!
|
182
181
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
bar.user.is_not.nested2!
|
195
|
-
|
196
|
-
lambda { bar.guard! }.should raise_error(Aclatraz::AccessDenied)
|
197
|
-
bar.user.is.nested1!
|
198
|
-
lambda { bar.guard! }.should raise_error(Aclatraz::AccessDenied)
|
199
|
-
bar.user.is.nested2!
|
200
|
-
lambda { bar.guard! }.should raise_error(Aclatraz::AccessDenied)
|
201
|
-
bar.user.is.nested3!
|
202
|
-
lambda { bar.guard! }.should_not raise_error(Aclatraz::AccessDenied)
|
203
|
-
end
|
182
|
+
lambda { parent.guard! }.should deny_access
|
183
|
+
lambda { parent.user.is.cooker!; parent.guard! }.should_not deny_access
|
184
|
+
lambda { parent.user.is.waiter!; parent.guard! }.should deny_access
|
185
|
+
|
186
|
+
child.user.is_not.cooker!
|
187
|
+
child.user.is_not.waiter!
|
188
|
+
|
189
|
+
lambda { child.guard! }.should deny_access
|
190
|
+
lambda { child.user.is.cooker!; child.guard! }.should deny_access
|
191
|
+
lambda { child.user.is.waiter!; child.guard! }.should deny_access
|
192
|
+
lambda { child.user.is.manager!; child.guard! }.should_not deny_access
|
204
193
|
end
|
205
194
|
end
|
@@ -1,53 +1,52 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
it "should
|
3
|
+
COMMON_STORE_SPECS = proc do
|
4
|
+
it "should assign roles to owner and properly check permissions" do
|
5
5
|
subject.clear
|
6
|
-
subject.set("
|
7
|
-
subject.
|
6
|
+
subject.set("admin", owner)
|
7
|
+
subject.set("manager", owner, StubTarget)
|
8
|
+
subject.set("creator", owner, target)
|
8
9
|
|
9
|
-
subject.
|
10
|
-
subject.check("
|
11
|
-
|
12
|
-
|
13
|
-
subject.check("
|
14
|
-
|
15
|
-
subject.check("
|
16
|
-
subject.check("foo", owner, StubTarget).should be_false
|
17
|
-
subject.check("bar", owner).should be_false
|
10
|
+
subject.check("admin", owner).should be_true
|
11
|
+
subject.check("manager", owner, StubTarget).should be_true
|
12
|
+
subject.check("creator", owner, target).should be_true
|
13
|
+
|
14
|
+
subject.check("owner", owner, target).should be_false
|
15
|
+
subject.check("tester", owner, StubTarget).should be_false
|
16
|
+
subject.check("waiter", owner).should be_false
|
18
17
|
end
|
19
18
|
|
20
|
-
it "should
|
19
|
+
it "should delete given permission" do
|
21
20
|
subject.clear
|
22
|
-
subject.set("
|
23
|
-
subject.set("
|
24
|
-
subject.set("
|
21
|
+
subject.set("admin", owner)
|
22
|
+
subject.set("manager", owner, StubTarget)
|
23
|
+
subject.set("creator", owner, target)
|
25
24
|
|
26
|
-
subject.delete("
|
27
|
-
subject.delete("
|
28
|
-
subject.delete("
|
25
|
+
subject.delete("admin", owner)
|
26
|
+
subject.delete("manager", owner, StubTarget)
|
27
|
+
subject.delete("creator", owner, target)
|
29
28
|
|
30
|
-
subject.check("
|
31
|
-
subject.check("
|
32
|
-
subject.check("
|
29
|
+
subject.check("admin", owner).should be_false
|
30
|
+
subject.check("manager", owner, StubTarget).should be_false
|
31
|
+
subject.check("creator", owner, target).should be_false
|
33
32
|
end
|
34
33
|
|
35
|
-
it "should allow to fetch
|
36
|
-
subject.
|
37
|
-
subject.set("
|
38
|
-
subject.set("
|
34
|
+
it "should allow to fetch list of all roles" do
|
35
|
+
subject.clear
|
36
|
+
subject.set("waiter", owner)
|
37
|
+
subject.set("cooker", owner)
|
38
|
+
subject.set("worker", owner)
|
39
39
|
|
40
|
-
subject.roles.
|
41
|
-
(subject.roles - ["foo", "bar", "bla"]).should be_empty
|
40
|
+
(subject.roles - ["waiter", "cooker", "worker"]).should be_empty
|
42
41
|
end
|
43
42
|
|
44
43
|
it "should allow to fetch list of roles for specified member" do
|
45
|
-
subject.
|
46
|
-
subject.set("
|
47
|
-
subject.set("
|
44
|
+
subject.clear
|
45
|
+
subject.set("waiter", owner)
|
46
|
+
subject.set("cooker", owner)
|
47
|
+
subject.set("worker", owner)
|
48
48
|
|
49
|
-
subject.roles(owner).
|
50
|
-
(subject.roles(owner) - ["foo", "bar", "bla"]).should be_empty
|
49
|
+
(subject.roles(owner) - ["waiter", "cooker", "worker"]).should be_empty
|
51
50
|
end
|
52
51
|
end
|
53
52
|
|
@@ -57,8 +56,7 @@ describe "Aclatraz" do
|
|
57
56
|
|
58
57
|
context "for Redis store" do
|
59
58
|
subject { Aclatraz.init(:redis, "redis://localhost:6379/0") }
|
60
|
-
|
61
|
-
class_eval &STORE_SPECS
|
59
|
+
class_eval(&COMMON_STORE_SPECS)
|
62
60
|
|
63
61
|
it "should respect persistent connection given on initalize" do
|
64
62
|
Aclatraz.instance_variable_set("@store", nil)
|
@@ -74,22 +72,9 @@ describe "Aclatraz" do
|
|
74
72
|
end
|
75
73
|
end
|
76
74
|
|
77
|
-
context "for Riak store" do
|
78
|
-
subject { Aclatraz.init(:riak, "roles") }
|
79
|
-
|
80
|
-
class_eval &STORE_SPECS
|
81
|
-
|
82
|
-
it "should respect persistent connection given on initalize" do
|
83
|
-
Aclatraz.instance_variable_set("@store", nil)
|
84
|
-
Aclatraz.init(:riak, "roles", Riak::Client.new)
|
85
|
-
Aclatraz.store.instance_variable_get('@backend').should be_kind_of(Riak::Bucket)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
75
|
context "for Cassandra store" do
|
90
76
|
subject { Aclatraz.init(:cassandra, "Super1", "Keyspace1") }
|
91
|
-
|
92
|
-
class_eval &STORE_SPECS
|
77
|
+
class_eval(&COMMON_STORE_SPECS)
|
93
78
|
|
94
79
|
it "should respect persistent connection given on initialize" do
|
95
80
|
Aclatraz.instance_variable_set("@store", nil)
|
@@ -97,4 +82,23 @@ describe "Aclatraz" do
|
|
97
82
|
Aclatraz.store.instance_variable_get('@backend').should be_kind_of(Cassandra)
|
98
83
|
end
|
99
84
|
end
|
85
|
+
|
86
|
+
context "for MongoDB store" do
|
87
|
+
subject {
|
88
|
+
require 'mongo'
|
89
|
+
Aclatraz.init(:mongo, "roles", @mongo ||= Mongo::Connection.new.db("aclatraz_test"))
|
90
|
+
}
|
91
|
+
class_eval(&COMMON_STORE_SPECS)
|
92
|
+
end
|
93
|
+
|
94
|
+
context "for Riak store" do
|
95
|
+
subject { Aclatraz.init(:riak, "roles") }
|
96
|
+
class_eval(&COMMON_STORE_SPECS)
|
97
|
+
|
98
|
+
it "should respect persistent connection given on initalize" do
|
99
|
+
Aclatraz.instance_variable_set("@store", nil)
|
100
|
+
Aclatraz.init(:riak, "roles", Riak::Client.new)
|
101
|
+
Aclatraz.store.instance_variable_get('@backend').should be_kind_of(Riak::Bucket)
|
102
|
+
end
|
103
|
+
end
|
100
104
|
end
|