summaryrefslogtreecommitdiffstats
path: root/tests/test_xmlrpc/test_hbac_plugin.py
blob: caa916acfad44629673e85e04f8df6a77a346e8f (plain)
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
# Authors:
#   Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2009  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Test the `ipalib/plugins/hbac.py` module.
"""

from xmlrpc_test import XMLRPC_test, assert_attr_equal
from ipalib import api
from ipalib import errors


class test_hbac(XMLRPC_test):
    """
    Test the `hbac` plugin.
    """
    rule_name = u'testing_rule1234'
    rule_type = u'allow'
    rule_type_fail = u'value not allowed'
    rule_service = u'ssh'
    rule_time = u'absolute 20081010000000 ~ 20081015120000'
    # wrong time, has 30th day in February in first date
    rule_time_fail = u'absolute 20080230000000 ~ 20081015120000'
    rule_desc = u'description'
    rule_desc_mod = u'description modified'

    test_user = u'hbac_test_user'
    test_group = u'hbac_test_group'
    test_host = u'hbac._test_netgroup'
    test_hostgroup = u'hbac_test_hostgroup'
    test_sourcehost = u'hbac._test_src_host'
    test_sourcehostgroup = u'hbac_test_src_hostgroup'

    def test_0_hbac_add(self):
        """
        Test adding a new HBAC rule using `xmlrpc.hbac_add`.
        """
        (dn, res) = api.Command['hbac_add'](
            self.rule_name, accessruletype=self.rule_type,
            servicename=self.rule_service, accesstime=self.rule_time,
            description=self.rule_desc
        )
        assert res
        assert_attr_equal(res, 'cn', self.rule_name)
        assert_attr_equal(res, 'accessruletype', self.rule_type)
        assert_attr_equal(res, 'servicename', self.rule_service)
        assert_attr_equal(res, 'ipaenabledflag', 'enabled')
        assert_attr_equal(res, 'accesstime', self.rule_time)
        assert_attr_equal(res, 'description', self.rule_desc)

    def test_1_hbac_add(self):
        """
        Test adding an existing HBAC rule using `xmlrpc.hbac_add'.
        """
        try:
            (dn, res) = api.Command['hbac_add'](
                self.rule_name, accessruletype=self.rule_type
            )
        except errors.DuplicateEntry:
            pass
        else:
            assert False

    def test_2_hbac_show(self):
        """
        Test displaying a HBAC rule using `xmlrpc.hbac_show`.
        """
        (dn, res) = api.Command['hbac_show'](self.rule_name)
        assert res
        assert_attr_equal(res, 'cn', self.rule_name)
        assert_attr_equal(res, 'accessruletype', self.rule_type)
        assert_attr_equal(res, 'servicename', self.rule_service)
        assert_attr_equal(res, 'ipaenabledflag', 'enabled')
        assert_attr_equal(res, 'accesstime', self.rule_time)
        assert_attr_equal(res, 'description', self.rule_desc)

    def test_3_hbac_mod(self):
        """
        Test modifying a HBAC rule using `xmlrpc.hbac_mod`.
        """
        (dn, res) = api.Command['hbac_mod'](
            self.rule_name, description=self.rule_desc_mod
        )
        assert res
        assert_attr_equal(res, 'description', self.rule_desc_mod)

    def test_4_hbac_mod(self):
        """
        Test setting invalid type of HBAC rule using `xmlrpc.hbac_mod`.
        """
        try:
            (dn, res) = api.Command['hbac_mod'](
                self.rule_name, accessruletype=self.rule_type_fail
            )
        except errors.ValidationError:
            pass
        else:
            assert False

    def test_5_hbac_mod(self):
        """
        Test setting invalid time in HBAC rule using `xmlrpc.hbac_mod`.
        """
        try:
            (dn, res) = api.Command['hbac_mod'](
                self.rule_name, accesstime=self.rule_time_fail
            )
        except errors.ValidationError:
            pass
        else:
            assert False

    def test_6_hbac_find(self):
        """
        Test searching for HBAC rules using `xmlrpc.hbac_find`.
        """
        (res, truncated) = api.Command['hbac_find'](
            name=self.rule_name, accessruletype=self.rule_type,
            description=self.rule_desc_mod
        )
        assert res
        assert res[0]
        assert_attr_equal(res[0][1], 'cn', self.rule_name)
        assert_attr_equal(res[0][1], 'accessruletype', self.rule_type)
        assert_attr_equal(res[0][1], 'description', self.rule_desc_mod)

    def test_7_hbac_init_testing_data(self):
        """
        Initialize data for more HBAC plugin testing.
        """
        api.Command['user_add'](self.test_user, givenname=u'first', sn=u'last')
        api.Command['group_add'](self.test_group, description=u'description')
        api.Command['host_add'](self.test_host)
        api.Command['hostgroup_add'](
            self.test_hostgroup, description=u'description'
        )
        api.Command['host_add'](self.test_sourcehost)
        api.Command['hostgroup_add'](
            self.test_sourcehostgroup, description=u'desc'
        )

    def test_8_hbac_add_user(self):
        """
        Test adding user and group to HBAC rule using `xmlrpc.hbac_add_user`.
        """
        (completed, failed, res) = api.Command['hbac_add_user'](
            self.rule_name, user=self.test_user, group=self.test_group
        )
        assert completed == 2
        assert 'memberuser' in failed
        assert 'user' in failed['memberuser']
        assert not failed['memberuser']['user']
        assert 'group' in failed['memberuser']
        assert not failed['memberuser']['group']
        assert res
        assert_attr_equal(res[1], 'memberuser user', self.test_user)
        assert_attr_equal(res[1], 'memberuser group', self.test_group)

    def test_9_hbac_remove_user(self):
        """
        Test removing user and group from HBAC rule using `xmlrpc.hbac_remove_user'.
        """
        (completed, failed, res) = api.Command['hbac_remove_user'](
            self.rule_name, user=self.test_user, group=self.test_group
        )
        assert completed == 2
        assert 'memberuser' in failed
        assert 'user' in failed['memberuser']
        assert not failed['memberuser']['user']
        assert 'group' in failed['memberuser']
        assert not failed['memberuser']['group']
        assert res
        assert 'memberuser user' not in res[1]
        assert 'memberuser group' not in res[1]

    def test_a_hbac_add_host(self):
        """
        Test adding host and hostgroup to HBAC rule using `xmlrpc.hbac_add_host`.
        """
        (completed, failed, res) = api.Command['hbac_add_host'](
            self.rule_name, host=self.test_host, hostgroup=self.test_hostgroup
        )
        assert completed == 2
        assert 'memberhost' in failed
        assert 'host' in failed['memberhost']
        assert not failed['memberhost']['host']
        assert 'hostgroup' in failed['memberhost']
        assert not failed['memberhost']['hostgroup']
        assert res
        assert_attr_equal(res[1], 'memberhost host', self.test_host)
        assert_attr_equal(res[1], 'memberhost hostgroup', self.test_hostgroup)

    def test_b_hbac_remove_host(self):
        """
        Test removing host and hostgroup from HBAC rule using `xmlrpc.hbac_remove_host`.
        """
        (completed, failed, res) = api.Command['hbac_remove_host'](
            self.rule_name, host=self.test_host, hostgroup=self.test_hostgroup
        )
        assert completed == 2
        assert 'memberhost' in failed
        assert 'host' in failed['memberhost']
        assert not failed['memberhost']['host']
        assert 'hostgroup' in failed['memberhost']
        assert not failed['memberhost']['hostgroup']
        assert res
        assert 'memberhost host' not in res[1]
        assert 'memberhost hostgroup' not in res[1]

    def test_a_hbac_add_sourcehost(self):
        """
        Test adding source host and hostgroup to HBAC rule using `xmlrpc.hbac_add_host`.
        """
        (completed, failed, res) = api.Command['hbac_add_sourcehost'](
            self.rule_name, host=self.test_host, hostgroup=self.test_hostgroup
        )
        assert completed == 2
        assert 'sourcehost' in failed
        assert 'host' in failed['sourcehost']
        assert not failed['sourcehost']['host']
        assert 'hostgroup' in failed['sourcehost']
        assert not failed['sourcehost']['hostgroup']
        assert res
        assert_attr_equal(res[1], 'sourcehost host', self.test_host)
        assert_attr_equal(res[1], 'sourcehost hostgroup', self.test_hostgroup)

    def test_b_hbac_remove_host(self):
        """
        Test removing source host and hostgroup from HBAC rule using `xmlrpc.hbac_remove_host`.
        """
        (completed, failed, res) = api.Command['hbac_remove_sourcehost'](
            self.rule_name, host=self.test_host, hostgroup=self.test_hostgroup
        )
        assert completed == 2
        assert 'sourcehost' in failed
        assert 'host' in failed['sourcehost']
        assert not failed['sourcehost']['host']
        assert 'hostgroup' in failed['sourcehost']
        assert not failed['sourcehost']['hostgroup']
        assert res
        assert 'sourcehost host' not in res[1]
        assert 'sourcehost hostgroup' not in res[1]

    def test_c_hbac_clear_testing_data(self):
        """
        Clear data for HBAC plugin testing.
        """
        api.Command['user_del'](self.test_user)
        api.Command['group_del'](self.test_group)
        api.Command['host_del'](self.test_host)
        api.Command['hostgroup_del'](self.test_hostgroup)
        api.Command['host_del'](self.test_sourcehost)
        api.Command['hostgroup_del'](self.test_sourcehostgroup)

    def test_d_hbac_disable(self):
        """
        Test disabling HBAC rule using `xmlrpc.hbac_disable`.
        """
        res = api.Command['hbac_disable'](self.rule_name)
        assert res == True
        # check it's really disabled
        (dn, res) = api.Command['hbac_show'](self.rule_name)
        assert res
        assert_attr_equal(res, 'ipaenabledflag', 'disabled')

    def test_e_hbac_enabled(self):
        """
        Test enabling HBAC rule using `xmlrpc.hbac_enable`.
        """
        res = api.Command['hbac_enable'](self.rule_name)
        assert res == True
        # check it's really enabled
        (dn, res) = api.Command['hbac_show'](self.rule_name)
        assert res
        assert_attr_equal(res, 'ipaenabledflag', 'enabled')

    def test_f_hbac_del(self):
        """
        Test deleting a HBAC rule using `xmlrpc.hbac_remove_sourcehost`.
        """
        res = api.Command['hbac_del'](self.rule_name)
        assert res == True
        # verify that it's gone
        try:
            api.Command['hbac_show'](self.rule_name)
        except errors.NotFound:
            pass
        else:
            assert False