summaryrefslogtreecommitdiff
path: root/kazoo/tests/test_sasl.py
blob: efdf965ebac0873329c337c9005620ce9d123c15 (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
import os
import subprocess
import time

import pytest

from kazoo.testing import KazooTestHarness
from kazoo.exceptions import (
    AuthFailedError,
    NoAuthError,
)
from kazoo.tests.util import TRAVIS_ZK_VERSION


class TestLegacySASLDigestAuthentication(KazooTestHarness):
    def setUp(self):
        try:
            import puresasl  # NOQA
        except ImportError:
            pytest.skip("PureSASL not available.")

        os.environ["ZOOKEEPER_JAAS_AUTH"] = "digest"
        self.setup_zookeeper()

        if TRAVIS_ZK_VERSION:
            version = TRAVIS_ZK_VERSION
        else:
            version = self.client.server_version()
        if not version or version < (3, 4):
            pytest.skip("Must use Zookeeper 3.4 or above")

    def tearDown(self):
        self.teardown_zookeeper()

    def test_connect_sasl_auth(self):
        from kazoo.security import make_acl

        username = "jaasuser"
        password = "jaas_password"

        acl = make_acl("sasl", credential=username, all=True)

        sasl_auth = "%s:%s" % (username, password)
        client = self._get_client(auth_data=[("sasl", sasl_auth)])

        client.start()
        try:
            client.create("/1", acl=(acl,))
            # give ZK a chance to copy data to other node
            time.sleep(0.1)
            with pytest.raises(NoAuthError):
                self.client.get("/1")
        finally:
            client.delete("/1")
            client.stop()
            client.close()

    def test_invalid_sasl_auth(self):
        client = self._get_client(auth_data=[("sasl", "baduser:badpassword")])
        with pytest.raises(AuthFailedError):
            client.start()


class TestSASLDigestAuthentication(KazooTestHarness):
    def setUp(self):
        try:
            import puresasl  # NOQA
        except ImportError:
            pytest.skip("PureSASL not available.")

        os.environ["ZOOKEEPER_JAAS_AUTH"] = "digest"
        self.setup_zookeeper()

        if TRAVIS_ZK_VERSION:
            version = TRAVIS_ZK_VERSION
        else:
            version = self.client.server_version()
        if not version or version < (3, 4):
            pytest.skip("Must use Zookeeper 3.4 or above")

    def tearDown(self):
        self.teardown_zookeeper()

    def test_connect_sasl_auth(self):
        from kazoo.security import make_acl

        username = "jaasuser"
        password = "jaas_password"

        acl = make_acl("sasl", credential=username, all=True)

        client = self._get_client(
            sasl_options={
                "mechanism": "DIGEST-MD5",
                "username": username,
                "password": password,
            }
        )
        client.start()
        try:
            client.create("/1", acl=(acl,))
            # give ZK a chance to copy data to other node
            time.sleep(0.1)
            with pytest.raises(NoAuthError):
                self.client.get("/1")
        finally:
            client.delete("/1")
            client.stop()
            client.close()

    def test_invalid_sasl_auth(self):
        client = self._get_client(
            sasl_options={
                "mechanism": "DIGEST-MD5",
                "username": "baduser",
                "password": "badpassword",
            }
        )
        with pytest.raises(AuthFailedError):
            client.start()


class TestSASLGSSAPIAuthentication(KazooTestHarness):
    def setUp(self):
        try:
            import puresasl  # NOQA
        except ImportError:
            pytest.skip("PureSASL not available.")
        try:
            import kerberos  # NOQA
        except ImportError:
            pytest.skip("Kerberos support not available.")
        if not os.environ.get("KRB5_TEST_ENV"):
            pytest.skip("Test Kerberos environ not setup.")

        os.environ["ZOOKEEPER_JAAS_AUTH"] = "gssapi"
        self.setup_zookeeper()

        if TRAVIS_ZK_VERSION:
            version = TRAVIS_ZK_VERSION
        else:
            version = self.client.server_version()
        if not version or version < (3, 4):
            pytest.skip("Must use Zookeeper 3.4 or above")

    def tearDown(self):
        self.teardown_zookeeper()

    def test_connect_gssapi_auth(self):
        from kazoo.security import make_acl

        # Ensure we have a client ticket
        subprocess.check_call(
            [
                "kinit",
                "-kt",
                os.path.expandvars("${KRB5_TEST_ENV}/client.keytab"),
                "client",
            ]
        )

        acl = make_acl("sasl", credential="client@KAZOOTEST.ORG", all=True)

        client = self._get_client(sasl_options={"mechanism": "GSSAPI"})
        client.start()
        try:
            client.create("/1", acl=(acl,))
            # give ZK a chance to copy data to other node
            time.sleep(0.1)
            with pytest.raises(NoAuthError):
                self.client.get("/1")
        finally:
            client.delete("/1")
            client.stop()
            client.close()

    def test_invalid_gssapi_auth(self):
        # Request a post-datated ticket, so that it is currently invalid.
        subprocess.check_call(
            [
                "kinit",
                "-kt",
                os.path.expandvars("${KRB5_TEST_ENV}/client.keytab"),
                "-s",
                "30min",
                "client",
            ]
        )

        client = self._get_client(sasl_options={"mechanism": "GSSAPI"})
        with pytest.raises(AuthFailedError):
            client.start()