This file is indexed.

/usr/share/pyshared/txzookeeper/tests/test_security.py is in python-txzookeeper 0.9.5-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#
#  Copyright (C) 2010-2011 Canonical Ltd. All Rights Reserved
#
#  This file is part of txzookeeper.
#
#  Authors:
#   Kapil Thangavelu
#
#  txzookeeper is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  txzookeeper 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 Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with txzookeeper.  If not, see <http://www.gnu.org/licenses/>.
#

import hashlib
import base64

import zookeeper

from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.python.failure import Failure

from txzookeeper.tests import ZookeeperTestCase, utils
from txzookeeper.client import (ZookeeperClient, ZOO_OPEN_ACL_UNSAFE)

PUBLIC_ACL = ZOO_OPEN_ACL_UNSAFE


class SecurityTests(ZookeeperTestCase):

    ident_bob = "bob:bob"
    ident_alice = "alice:alice"
    ident_eve = "eve:eve"
    ident_chuck = "chuck:chuck"

    ident_unittest = "unittest:unittest"

    def setUp(self):
        super(SecurityTests, self).setUp()
        self.clients = []

        self.test_cleanup_connection = ZookeeperClient("127.0.0.1:2181", 2000)
        self.access_control_test_cleanup_entry = self.make_ac(
            self.ident_unittest, all=True, admin=True)
        return self.open_and_authenticate(
            self.test_cleanup_connection, self.ident_unittest)

    def tearDown(self):
        utils.deleteTree(handle=self.test_cleanup_connection.handle)
        for client in self.clients:
            if client.connected:
                client.close()
        self.test_cleanup_connection.close()

    @inlineCallbacks
    def open_and_authenticate(self, client, credentials):
        """authentication so the test always has access to clean up the
        zookeeper node tree. synchronous auth to avoid using deferred
        during setup."""
        yield client.connect()
        d = client.add_auth("digest", credentials)
        # hack to keep auth fast
        yield client.exists("/")
        yield d
        returnValue(client)

    @inlineCallbacks
    def connect_users(self, *users):
        clients = []
        for name in users:
            ident_user = getattr(self, "ident_%s" % (name), None)
            if ident_user is None:
                raise AttributeError("Invalid User %s" % (name))
            client = ZookeeperClient("127.0.0.1:2181", 3000)
            clients.append(client)
            yield self.open_and_authenticate(client, ident_user)
        self.clients.extend(clients)
        returnValue(clients)

    @inlineCallbacks
    def sync_clients(self, *clients):
        for client in clients:
            yield client.sync()

    def ensure_auth_failure(self, result):
        if isinstance(result, Failure):
            self.assertTrue(isinstance(
                result.value, zookeeper.NoAuthException))
            return
        self.fail("should have raised auth exception")

    def make_acl(self, *access_control_entries):
        """
        Take the variable number of access control entries and return a
        list suitable for passing to the txzookeeper's api as an ACL.

        Also automatically appends the test acess control entry to ensure
        that the test can cleanup regardless of node permissions set within
        a test.
        """
        access_control_list = list(access_control_entries)
        access_control_list.append(self.access_control_test_cleanup_entry)
        return access_control_list

    def make_ac(self, credentials, **kw):
        """
        Given a username:password credential and boolean keyword arguments
        corresponding to permissions construct an access control entry.
        """
        user, password = credentials.split(":")
        identity = "%s:%s" % (
            user,
            base64.b64encode(hashlib.new('sha1', credentials).digest()))

        permissions = None

        for name, perm in (('read', zookeeper.PERM_READ),
                           ('write', zookeeper.PERM_WRITE),
                           ('delete', zookeeper.PERM_DELETE),
                           ('create', zookeeper.PERM_CREATE),
                           ('admin', zookeeper.PERM_ADMIN),
                           ('all', zookeeper.PERM_ALL)):
            if name not in kw:
                continue

            if permissions is None:
                permissions = perm
            else:
                permissions = permissions | perm
        if permissions is None:
            raise SyntaxError("No permissions specified")
        access_control_entry = {
            'id': identity, 'scheme': 'digest', 'perms': permissions}
        return access_control_entry

    @inlineCallbacks
    def test_bob_message_for_alice_with_eve_reading(self):
        """
        If bob creates a message for alice to read, eve cannot read
        it.
        """
        bob, alice, eve = yield self.connect_users(
            "bob", "alice", "eve")
        yield bob.create(
            "/message_inbox", "message for alice",
            self.make_acl(
                self.make_ac(self.ident_bob, write=True, read=True),
                self.make_ac(self.ident_alice, read=True)))

        message_content, message_stat = yield alice.get("/message_inbox")
        self.assertEqual(message_content, "message for alice")

        d = eve.get("/message_inbox")
        d.addBoth(self.ensure_auth_failure)

        yield d

    @inlineCallbacks
    def test_alice_message_box_for_bob_with_eve_deleting(self):
        """
        If alice makes a folder to drop off messages to bob, neither bob nor
        eve can write to it, and bob can only read, and delete the messages.
        The permission for deleting is set on the container node. Bob has
        delete permission only on the on the container, and can delete nodes.
        Even if eve has permission to delete on the message node, without the
        container permission it will not succeed.
        """
        bob, alice, eve = yield self.connect_users("bob", "alice", "eve")

        yield alice.create(
            "/from_alice", "messages from alice",
            self.make_acl(
                self.make_ac(self.ident_alice, create=True, write=True),
                self.make_ac(self.ident_bob, read=True, delete=True))),

        # make sure all the clients have a consistent view
        yield self.sync_clients(alice, bob, eve)

        # bob can't create messages in the mailbox
        d = bob.create("/from_alice/love_letter", "test")
        d.addBoth(self.ensure_auth_failure)

        # alice's message can only be read by bob.
        path = yield alice.create(
            "/from_alice/appreciate_letter", "great",
            self.make_acl(
                self.make_ac(self.ident_eve, delete=True),
                self.make_ac(self.ident_bob, read=True),
                self.make_ac(self.ident_alice, create=True, write=True)))

        message_content, node_stat = yield bob.get(path)
        self.assertEqual(message_content, "great")

        # make sure all the clients have a consistent view
        yield self.sync_clients(alice, bob, eve)

        # eve can neither read nor delete
        d = eve.get(path)
        d.addBoth(self.ensure_auth_failure)
        yield d

        d = eve.delete(path)
        d.addBoth(self.ensure_auth_failure)
        yield d

        # bob can delete the message when he's done reading.
        yield bob.delete(path)

    def test_eve_can_discover_node_path(self):
        """
        One weakness of the zookeeper security model, is that it enables
        discovery of a node existance, its node stats, and its acl to
        any inquiring party.

        The acl is read off the node and then used as enforcement to any
        policy. Ideally it should validate exists and get_acl against
        the read permission on the node.

        Here bob creates a node that only he can read or write to, but
        eve can still get node stat on the node if she knows the path.
        """
        bob, eve = yield self.connect_users("bob", "eve")
        yield bob.create("/bobsafeplace", "",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        yield bob.create("/bobsafeplace/secret-a", "supersecret",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        self.sync_clients(bob, eve)
        d = eve.exists("/bobsafeplace")

        def verify_node_stat(node_stat):
            self.assertEqual(node_stat["dataLength"], len("supersecret"))
            self.assertEqual(node_stat["version"], 0)

        d.addCallback(verify_node_stat)
        yield d

    def test_eve_can_discover_node_acl(self):
        """
        One weakness of the zookeeper security model, is that it enables
        discovery of a node existance, its node stats, and its acl to
        any inquiring party.

        The acl is read off the node and then used as enforcement to any
        policy. Ideally it should validate exists and get_acl against
        the read permission on the node.

        Here bob creates a node that only he can read or write to, but
        eve can still get node stat and acl information on the node if
        she knows the path.
        """
        bob, eve = yield self.connect_users("bob", "eve")
        yield bob.create("/bobsafeplace", "",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        yield bob.create("/bobsafeplace/secret-a", "supersecret",
                         self.make_acl(self.make_ac(self.ident_bob, all=True)))

        self.sync_clients(bob, eve)
        d = eve.get_acl("/bobsafeplace/secret-a")

        def verify_node_stat_and_acl((acl, node_stat)):
            self.assertEqual(node_stat["dataLength"], len("supersecret"))
            self.assertEqual(node_stat["version"], 0)
            self.assertEqual(acl[0]["id"].split(":")[0], "bob")
        d.addCallback(verify_node_stat_and_acl)
        yield d