ldap_groups_lookup 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ldap_groups_lookup/configuration.rb +1 -0
- data/lib/ldap_groups_lookup/version.rb +1 -1
- data/spec/lib/ldap_groups_lookup_spec.rb +194 -185
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d6da72dceefbf3ea333aeb46b7288da0d3ec3517c05fbcbc77f53eb3fc955f2
|
4
|
+
data.tar.gz: 1720d92a5e416dbb2f4fafec7c2fc85239d82bb8ef6982909646217260d91457
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b05d74e63567fb0e0272cf2242a395c9a0dc0125306112e1671eba47bd9460c73c1fff154d1b5708fc4669f0877d9d30dd87320256fcbdf7f39ca56269d8d9cc
|
7
|
+
data.tar.gz: 5becbdb0a9356bfa9b53df4133d9ffc955b77a0c50757265d80457ee7a0c86b59d74cca811732fefbb8700879d1210de3cd120c50095bd81cc4fb464f686f468
|
@@ -13,254 +13,263 @@ RSpec.describe LDAPGroupsLookup do
|
|
13
13
|
end
|
14
14
|
let(:user) { user_class.new }
|
15
15
|
|
16
|
-
# Load the example config from fixtures
|
17
|
-
let(:config) { YAML.load(ERB.new(File.read(File.join(File.dirname(__dir__), 'fixtures', 'ldap_groups_lookup.yml.example'))).result) }
|
18
|
-
|
19
|
-
before do
|
20
|
-
allow(LDAPGroupsLookup).to receive(:config).and_return(config)
|
21
|
-
end
|
22
|
-
|
23
16
|
after do
|
24
17
|
LDAPGroupsLookup.reset
|
25
18
|
end
|
26
19
|
|
27
|
-
describe '#
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
expect(File).to receive(:exist?).with(/config\/ldap_groups_lookup\.yml$/)
|
32
|
-
end
|
33
|
-
it 'should return nil' do
|
34
|
-
expect(LDAPGroupsLookup.service).to be_nil
|
35
|
-
end
|
20
|
+
describe '#config' do
|
21
|
+
it 'sets the config directly' do
|
22
|
+
LDAPGroupsLookup.config = { direct_config: 'works' }
|
23
|
+
expect(LDAPGroupsLookup.config).to eq({ direct_config: 'works' })
|
36
24
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with a config file' do
|
28
|
+
# Load the example config from fixtures
|
29
|
+
let(:config) { YAML.load(ERB.new(File.read(File.join(File.dirname(__dir__), 'fixtures', 'ldap_groups_lookup.yml.example'))).result) }
|
30
|
+
|
31
|
+
before do
|
32
|
+
allow(LDAPGroupsLookup).to receive(:config).and_return(config)
|
44
33
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
context 'when the auth credentials are incorrect' do
|
34
|
+
|
35
|
+
describe '#service' do
|
36
|
+
context 'when the config file is missing' do
|
50
37
|
before do
|
51
|
-
|
38
|
+
allow(LDAPGroupsLookup).to receive(:config).and_call_original
|
39
|
+
expect(File).to receive(:exist?).with(/config\/ldap_groups_lookup\.yml$/)
|
52
40
|
end
|
53
|
-
it 'should
|
54
|
-
expect
|
41
|
+
it 'should return nil' do
|
42
|
+
expect(LDAPGroupsLookup.service).to be_nil
|
55
43
|
end
|
56
44
|
end
|
57
|
-
context 'when
|
45
|
+
context 'when disabled in the configuration file' do
|
58
46
|
before do
|
59
|
-
|
47
|
+
config[:enabled] = false
|
60
48
|
end
|
61
|
-
it 'should return
|
62
|
-
expect(LDAPGroupsLookup.service).to
|
49
|
+
it 'should return nil' do
|
50
|
+
expect(LDAPGroupsLookup.service).to be_nil
|
63
51
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
52
|
+
end
|
53
|
+
context 'when enabled in the configuration file' do
|
54
|
+
it 'should be enabled' do
|
55
|
+
expect(config[:enabled]).to eq(true)
|
56
|
+
end
|
57
|
+
context 'when the auth credentials are incorrect' do
|
58
|
+
before do
|
59
|
+
allow_any_instance_of(Net::LDAP).to receive(:bind).and_return(false)
|
60
|
+
end
|
61
|
+
it 'should raise an LdapError' do
|
62
|
+
expect { LDAPGroupsLookup.service }.to raise_error(Net::LDAP::Error)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context 'when the auth credentials are correct' do
|
66
|
+
before do
|
67
|
+
allow_any_instance_of(Net::LDAP).to receive(:bind).and_return(true)
|
68
|
+
end
|
69
|
+
it 'should return a Net::LDAP instance' do
|
70
70
|
expect(LDAPGroupsLookup.service).to be_an_instance_of(Net::LDAP)
|
71
71
|
end
|
72
|
+
|
73
|
+
context 'when the :config key is set' do
|
74
|
+
let(:config_hash) { { host: 'localhost', port: 636, encryption: { method: :simple_tls, tls_options: OpenSSL::SSL::SSLContext::DEFAULT_PARAMS } } }
|
75
|
+
before { config[:config] = config_hash }
|
76
|
+
it 'uses that config' do
|
77
|
+
expect(Net::LDAP).to receive(:new).with(config_hash).and_call_original
|
78
|
+
expect(LDAPGroupsLookup.service).to be_an_instance_of(Net::LDAP)
|
79
|
+
end
|
80
|
+
end
|
72
81
|
end
|
73
82
|
end
|
74
83
|
end
|
75
|
-
end
|
76
84
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
84
|
-
context 'when subject does not provide ldap_lookup_key method' do
|
85
|
-
before(:each) { user.class.send(:remove_method, :ldap_lookup_key) }
|
86
|
-
it 'should return ''' do
|
87
|
-
expect(user.ldap_mail).to eq('')
|
85
|
+
describe '#ldap_mail' do
|
86
|
+
before(:each) do
|
87
|
+
entry = Net::LDAP::Entry.new('CN=user,DC=ads,DC=example,DC=net')
|
88
|
+
entry['mail'] = ['user@domain.ext']
|
89
|
+
allow_any_instance_of(Net::LDAP).to receive(:search).and_return([entry])
|
90
|
+
allow_any_instance_of(Net::LDAP).to receive(:bind).and_return(true)
|
88
91
|
end
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
expect(user.ldap_mail).to eq('')
|
94
|
-
end
|
95
|
-
end
|
96
|
-
context 'when subject provides ldap_lookup_key' do
|
97
|
-
context 'when LDAP is not configured' do
|
98
|
-
before(:each) do
|
99
|
-
config[:enabled] = false
|
92
|
+
context 'when subject does not provide ldap_lookup_key method' do
|
93
|
+
before(:each) { user.class.send(:remove_method, :ldap_lookup_key) }
|
94
|
+
it 'should return ''' do
|
95
|
+
expect(user.ldap_mail).to eq('')
|
100
96
|
end
|
101
|
-
|
97
|
+
end
|
98
|
+
context 'when subject does not provide ldap_lookup_key value' do
|
99
|
+
before(:each) { allow(user).to receive(:ldap_lookup_key).and_return(nil) }
|
100
|
+
it 'should return ''' do
|
102
101
|
expect(user.ldap_mail).to eq('')
|
103
102
|
end
|
104
103
|
end
|
105
|
-
context 'when
|
106
|
-
|
107
|
-
|
104
|
+
context 'when subject provides ldap_lookup_key' do
|
105
|
+
context 'when LDAP is not configured' do
|
106
|
+
before(:each) do
|
107
|
+
config[:enabled] = false
|
108
|
+
end
|
109
|
+
it 'should return a blank string' do
|
110
|
+
expect(user.ldap_mail).to eq('')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
context 'when LDAP is configured' do
|
114
|
+
it 'user should should have a mail attribute in mock LDAP' do
|
115
|
+
expect(user.ldap_mail).to eq 'user@domain.ext'
|
116
|
+
end
|
108
117
|
end
|
109
118
|
end
|
110
119
|
end
|
111
|
-
end
|
112
120
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
end
|
121
|
-
context 'when subject does not provide ldap_lookup_key method' do
|
122
|
-
before(:each) { user.class.send(:remove_method, :ldap_lookup_key) }
|
123
|
-
it 'should return []' do
|
124
|
-
expect(user.ldap_groups).to eq([])
|
125
|
-
end
|
126
|
-
end
|
127
|
-
context 'when subject does not provide ldap_lookup_key value' do
|
128
|
-
before(:each) { allow(user).to receive(:ldap_lookup_key).and_return(nil) }
|
129
|
-
it 'should return []' do
|
130
|
-
expect(user.ldap_groups).to eq([])
|
121
|
+
describe '#ldap_groups' do
|
122
|
+
before(:each) do
|
123
|
+
entry = Net::LDAP::Entry.new('CN=user,DC=ads,DC=example,DC=net')
|
124
|
+
entry['memberof'] = ['CN=Group1,DC=ads,DC=example,DC=net',
|
125
|
+
'CN=Group2,DC=ads,DC=example,DC=net']
|
126
|
+
allow_any_instance_of(Net::LDAP).to receive(:search).and_return([entry])
|
127
|
+
allow_any_instance_of(Net::LDAP).to receive(:bind).and_return(true)
|
131
128
|
end
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
config[:enabled] = false
|
129
|
+
context 'when subject does not provide ldap_lookup_key method' do
|
130
|
+
before(:each) { user.class.send(:remove_method, :ldap_lookup_key) }
|
131
|
+
it 'should return []' do
|
132
|
+
expect(user.ldap_groups).to eq([])
|
137
133
|
end
|
134
|
+
end
|
135
|
+
context 'when subject does not provide ldap_lookup_key value' do
|
136
|
+
before(:each) { allow(user).to receive(:ldap_lookup_key).and_return(nil) }
|
138
137
|
it 'should return []' do
|
139
138
|
expect(user.ldap_groups).to eq([])
|
140
139
|
end
|
141
140
|
end
|
142
|
-
context 'when
|
143
|
-
|
144
|
-
|
141
|
+
context 'when subject provides ldap_lookup_key' do
|
142
|
+
context 'when LDAP is not configured' do
|
143
|
+
before(:each) do
|
144
|
+
config[:enabled] = false
|
145
|
+
end
|
146
|
+
it 'should return []' do
|
147
|
+
expect(user.ldap_groups).to eq([])
|
148
|
+
end
|
149
|
+
end
|
150
|
+
context 'when LDAP is configured' do
|
151
|
+
it 'user should belong to Group1 and Group2 in mock LDAP' do
|
152
|
+
expect(user.ldap_groups).to eq(%w(Group1 Group2))
|
153
|
+
end
|
145
154
|
end
|
146
155
|
end
|
147
156
|
end
|
148
|
-
end
|
149
157
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
end
|
156
|
-
end
|
157
|
-
context 'when subject does not provide ldap_lookup_key value' do
|
158
|
-
before(:each) { allow(user).to receive(:ldap_lookup_key).and_return(nil) }
|
159
|
-
it 'should return false' do
|
160
|
-
expect(user.member_of_ldap_group?('Test-Group')).to eq(false)
|
161
|
-
end
|
162
|
-
end
|
163
|
-
context 'when subject provides ldap_lookup_key' do
|
164
|
-
context 'when LDAP is not configured' do
|
165
|
-
before(:each) do
|
166
|
-
config[:enabled] = false
|
158
|
+
describe '#member_of_ldap_group?' do
|
159
|
+
context 'when subject does not provide ldap_lookup_key method' do
|
160
|
+
before(:each) { user.class.send(:remove_method, :ldap_lookup_key) }
|
161
|
+
it 'should return false' do
|
162
|
+
expect(user.member_of_ldap_group?('Test-Group')).to eq(false)
|
167
163
|
end
|
164
|
+
end
|
165
|
+
context 'when subject does not provide ldap_lookup_key value' do
|
166
|
+
before(:each) { allow(user).to receive(:ldap_lookup_key).and_return(nil) }
|
168
167
|
it 'should return false' do
|
169
168
|
expect(user.member_of_ldap_group?('Test-Group')).to eq(false)
|
170
169
|
end
|
171
170
|
end
|
172
|
-
context 'when
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
allow(LDAPGroupsLookup).to receive(:lookup_dn) do |args|
|
178
|
-
Net::LDAP::Entry.new("CN=#{args},DC=ads,DC=example,DC=net").dn
|
171
|
+
context 'when subject provides ldap_lookup_key' do
|
172
|
+
context 'when LDAP is not configured' do
|
173
|
+
before(:each) do
|
174
|
+
config[:enabled] = false
|
179
175
|
end
|
176
|
+
it 'should return false' do
|
177
|
+
expect(user.member_of_ldap_group?('Test-Group')).to eq(false)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
context 'when LDAP is configured' do
|
181
|
+
before(:each) do
|
182
|
+
@service = double('ldap_service')
|
183
|
+
allow(LDAPGroupsLookup).to receive(:service).and_return(@service)
|
180
184
|
|
181
|
-
|
182
|
-
|
185
|
+
allow(LDAPGroupsLookup).to receive(:lookup_dn) do |args|
|
186
|
+
Net::LDAP::Entry.new("CN=#{args},DC=ads,DC=example,DC=net").dn
|
187
|
+
end
|
183
188
|
|
184
|
-
|
185
|
-
|
189
|
+
@other_group = Net::LDAP::Entry.new('CN=Other-Group,OU=Groups,DC=ads,DC=example,DC=net')
|
190
|
+
@other_group['member;range=0-*'] = ['CN=otheruser,DC=ads,DC=example,DC=net']
|
186
191
|
|
187
|
-
|
188
|
-
|
192
|
+
@nested_group_page_1 = Net::LDAP::Entry.new('CN=Nested-Group,OU=Groups,DC=ads,DC=example,DC=net')
|
193
|
+
@nested_group_page_1['member;range=0-0'] = ['CN=otheruser,DC=ads,DC=example,DC=net']
|
189
194
|
|
190
|
-
|
191
|
-
|
195
|
+
@nested_group_page_2 = Net::LDAP::Entry.new('CN=Nested-Group,OU=Groups,DC=ads,DC=example,DC=net')
|
196
|
+
@nested_group_page_2['member;range=1-*'] = ['CN=user,DC=ads,DC=example,DC=net']
|
192
197
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
expect(@service).to receive(:search).with(
|
198
|
-
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Fake-Group'))).and_return([])
|
199
|
-
expect(user.member_of_ldap_group?('Fake-Group')).to eq(false)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
context 'when searching for a group that user is not a member of' do
|
203
|
-
it 'should return false' do
|
204
|
-
expect(@service).to receive(:search).with(
|
205
|
-
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Other-Group'))).and_return([@other_group])
|
206
|
-
expect(user.member_of_ldap_group?('Other-Group')).to eq(false)
|
207
|
-
end
|
208
|
-
end
|
209
|
-
context 'when searching for a group that has no members' do
|
210
|
-
it 'should return false' do
|
211
|
-
expect(@service).to receive(:search).with(
|
212
|
-
hash_including(filter: Net::LDAP::Filter.equals('cn', 'No-Member-Group'))).and_return([@no_member_group])
|
213
|
-
expect(user.member_of_ldap_group?('No-Member-Group')).to eq(false)
|
198
|
+
@top_group = Net::LDAP::Entry.new('CN=Top-Group,OU=Groups,DC=ads,DC=example,DC=net')
|
199
|
+
@top_group['member;range=0-*'] = ['CN=Nested-Group,OU=Groups,DC=ads,DC=example,DC=net']
|
200
|
+
|
201
|
+
@no_member_group = Net::LDAP::Entry.new('CN=No-Member-Group,OU=Groups,DC=ads,DC=example,DC=net')
|
214
202
|
end
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
expect(@service).to receive(:search).with(
|
222
|
-
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
223
|
-
attributes: ['member;range=1-*'])).and_return([@nested_group_page_2])
|
224
|
-
expect(user.member_of_ldap_group?('Nested-Group')).to eq(true)
|
203
|
+
context 'when searching for a group that does not exist' do
|
204
|
+
it 'should return false' do
|
205
|
+
expect(@service).to receive(:search).with(
|
206
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Fake-Group'))).and_return([])
|
207
|
+
expect(user.member_of_ldap_group?('Fake-Group')).to eq(false)
|
208
|
+
end
|
225
209
|
end
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
233
|
-
attributes: ['member;range=0-*'])).and_return([@nested_group_page_1])
|
234
|
-
allow(@service).to receive(:search).with(
|
235
|
-
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
236
|
-
attributes: ['member;range=1-*'])).and_return([@nested_group_page_2])
|
210
|
+
context 'when searching for a group that user is not a member of' do
|
211
|
+
it 'should return false' do
|
212
|
+
expect(@service).to receive(:search).with(
|
213
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Other-Group'))).and_return([@other_group])
|
214
|
+
expect(user.member_of_ldap_group?('Other-Group')).to eq(false)
|
215
|
+
end
|
237
216
|
end
|
238
|
-
context 'when
|
239
|
-
|
240
|
-
|
217
|
+
context 'when searching for a group that has no members' do
|
218
|
+
it 'should return false' do
|
219
|
+
expect(@service).to receive(:search).with(
|
220
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'No-Member-Group'))).and_return([@no_member_group])
|
221
|
+
expect(user.member_of_ldap_group?('No-Member-Group')).to eq(false)
|
241
222
|
end
|
223
|
+
end
|
224
|
+
context 'when searching for a group that user is a direct member of on the second page' do
|
242
225
|
it 'should return true' do
|
243
|
-
expect(
|
226
|
+
expect(@service).to receive(:search).with(
|
227
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
228
|
+
attributes: ['member;range=0-*'])).and_return([@nested_group_page_1])
|
229
|
+
expect(@service).to receive(:search).with(
|
230
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
231
|
+
attributes: ['member;range=1-*'])).and_return([@nested_group_page_2])
|
232
|
+
expect(user.member_of_ldap_group?('Nested-Group')).to eq(true)
|
244
233
|
end
|
245
234
|
end
|
246
|
-
context 'when
|
235
|
+
context 'when searching for a group that user is a nested member of' do
|
247
236
|
before do
|
248
|
-
|
237
|
+
expect(@service).to receive(:search).with(
|
238
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Top-Group'))).and_return([@top_group])
|
239
|
+
allow(@service).to receive(:search).with(
|
240
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
241
|
+
attributes: ['member;range=0-*'])).and_return([@nested_group_page_1])
|
242
|
+
allow(@service).to receive(:search).with(
|
243
|
+
hash_including(filter: Net::LDAP::Filter.equals('cn', 'Nested-Group'),
|
244
|
+
attributes: ['member;range=1-*'])).and_return([@nested_group_page_2])
|
249
245
|
end
|
250
|
-
|
251
|
-
|
246
|
+
context 'when the group is whitelisted' do
|
247
|
+
before do
|
248
|
+
allow(LDAPGroupsLookup).to receive(:member_whitelist).and_return(['OU=Groups'])
|
249
|
+
end
|
250
|
+
it 'should return true' do
|
251
|
+
expect(user.member_of_ldap_group?('Top-Group')).to eq(true)
|
252
|
+
end
|
252
253
|
end
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
254
|
+
context 'when the whitelist is empty' do
|
255
|
+
before do
|
256
|
+
allow(LDAPGroupsLookup).to receive(:member_whitelist).and_return([])
|
257
|
+
end
|
258
|
+
it 'should return true (whitelisting is disabled)' do
|
259
|
+
expect(user.member_of_ldap_group?('Top-Group')).to eq(true)
|
260
|
+
end
|
257
261
|
end
|
258
|
-
|
259
|
-
|
262
|
+
context 'when the group is not whitelisted' do
|
263
|
+
before do
|
264
|
+
allow(LDAPGroupsLookup).to receive(:member_whitelist).and_return(['OU=Not-A-Match'])
|
265
|
+
end
|
266
|
+
it 'should return false' do
|
267
|
+
expect(user.member_of_ldap_group?('Top-Group')).to eq(false)
|
268
|
+
end
|
260
269
|
end
|
261
270
|
end
|
262
271
|
end
|
263
272
|
end
|
264
273
|
end
|
265
274
|
end
|
266
|
-
end
|
275
|
+
end
|