summaryrefslogtreecommitdiff
path: root/src/etcd/tests/integration/test_ssl.py
blob: 16185c279a0b66266d90c27a5fdb373d4bfb58ca (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
import os
import time
import shutil
import logging
import unittest
import multiprocessing
import tempfile

import urllib3

import etcd
from . import helpers
from . import test_simple
from nose.tools import nottest

log = logging.getLogger()

class TestEncryptedAccess(test_simple.EtcdIntegrationTest):

    @classmethod
    def setUpClass(cls):
        program = cls._get_exe()
        cls.directory = tempfile.mkdtemp(prefix='python-etcd')

        cls.ca_cert_path = os.path.join(cls.directory, 'ca.crt')
        ca_key_path = os.path.join(cls.directory, 'ca.key')

        cls.ca2_cert_path = os.path.join(cls.directory, 'ca2.crt')
        ca2_key_path = os.path.join(cls.directory, 'ca2.key')

        server_cert_path = os.path.join(cls.directory, 'server.crt')
        server_key_path = os.path.join(cls.directory, 'server.key')

        ca, ca_key = helpers.TestingCA.create_test_ca_certificate(
            cls.ca_cert_path, ca_key_path, 'TESTCA')

        ca2, ca2_key = helpers.TestingCA.create_test_ca_certificate(
            cls.ca2_cert_path, ca2_key_path, 'TESTCA2')

        helpers.TestingCA.create_test_certificate(
            ca, ca_key, server_cert_path, server_key_path, '127.0.0.1')

        cls.processHelper = helpers.EtcdProcessHelper(
            cls.directory,
            proc_name=program,
            port_range_start=6001,
            internal_port_range_start=8001,
            tls=True
        )

        cls.processHelper.run(number=3,
                              proc_args=[
                                  '-cert-file=%s' % server_cert_path,
                                  '-key-file=%s' % server_key_path
                              ])

    def test_get_set_unauthenticated(self):
        """ INTEGRATION: set/get a new value unauthenticated (http->https) """

        client = etcd.Client(port=6001)

        # Since python 3 raises a MaxRetryError here, this gets caught in
        # different code blocks in python 2 and python 3, thus messages are
        # different. Python 3 does the right thing(TM), for the record
        self.assertRaises(
            etcd.EtcdException, client.set, '/test_set', 'test-key')

        self.assertRaises(etcd.EtcdException, client.get, '/test_set')

    @nottest
    def test_get_set_unauthenticated_missing_ca(self):
        """ INTEGRATION: try unauthenticated w/out validation (https->https)"""
        # This doesn't work for now and will need further inspection
        client = etcd.Client(protocol='https', port=6001)
        set_result = client.set('/test_set', 'test-key')
        get_result = client.get('/test_set')


    def test_get_set_unauthenticated_with_ca(self):
        """ INTEGRATION: try unauthenticated with validation (https->https)"""
        client = etcd.Client(
            protocol='https', port=6001, ca_cert=self.ca2_cert_path)

        self.assertRaises(urllib3.exceptions.SSLError, client.set, '/test-set', 'test-key')
        self.assertRaises(urllib3.exceptions.SSLError, client.get, '/test-set')

    def test_get_set_authenticated(self):
        """ INTEGRATION: set/get a new value authenticated """

        client = etcd.Client(
            port=6001, protocol='https', ca_cert=self.ca_cert_path)

        set_result = client.set('/test_set', 'test-key')
        get_result = client.get('/test_set')


class TestClientAuthenticatedAccess(test_simple.EtcdIntegrationTest):

    @classmethod
    def setUpClass(cls):
        program = cls._get_exe()
        cls.directory = tempfile.mkdtemp(prefix='python-etcd')

        cls.ca_cert_path = os.path.join(cls.directory, 'ca.crt')
        ca_key_path = os.path.join(cls.directory, 'ca.key')

        server_cert_path = os.path.join(cls.directory, 'server.crt')
        server_key_path = os.path.join(cls.directory, 'server.key')

        cls.client_cert_path = os.path.join(cls.directory, 'client.crt')
        cls.client_key_path = os.path.join(cls.directory, 'client.key')

        cls.client_all_cert = os.path.join(cls.directory, 'client-all.crt')

        ca, ca_key = helpers.TestingCA.create_test_ca_certificate(
            cls.ca_cert_path, ca_key_path)

        helpers.TestingCA.create_test_certificate(
            ca, ca_key, server_cert_path, server_key_path, '127.0.0.1')

        helpers.TestingCA.create_test_certificate(
            ca,
            ca_key,
            cls.client_cert_path,
            cls.client_key_path)

        cls.processHelper = helpers.EtcdProcessHelper(
            cls.directory,
            proc_name=program,
            port_range_start=6001,
            internal_port_range_start=8001,
            tls=True
        )

        with open(cls.client_all_cert, 'w') as f:
            with open(cls.client_key_path, 'r') as g:
                f.write(g.read())
            with open(cls.client_cert_path, 'r') as g:
                f.write(g.read())

        cls.processHelper.run(number=3,
                              proc_args=[
                                  '-cert-file=%s' % server_cert_path,
                                  '-key-file=%s' % server_key_path,
                                  '-ca-file=%s' % cls.ca_cert_path,
                              ])


    def test_get_set_unauthenticated(self):
        """ INTEGRATION: set/get a new value unauthenticated (http->https) """

        client = etcd.Client(port=6001)

        # See above for the reason of this change
        self.assertRaises(
            etcd.EtcdException, client.set, '/test_set', 'test-key')
        self.assertRaises(etcd.EtcdException, client.get, '/test_set')

    @nottest
    def test_get_set_authenticated(self):
        """ INTEGRATION: connecting to server with mutual auth """
        # This gives an unexplicable ssl error, as connecting to the same
        # Etcd cluster where this fails with the exact same code this
        # doesn't fail

        client = etcd.Client(
            port=6001,
            protocol='https',
            cert=self.client_all_cert,
            ca_cert=self.ca_cert_path
        )

        set_result = client.set('/test_set', 'test-key')
        self.assertEquals(u'set', set_result.action.lower())
        self.assertEquals(u'/test_set', set_result.key)
        self.assertEquals(u'test-key', set_result.value)
        get_result = client.get('/test_set')
        self.assertEquals('get', get_result.action.lower())
        self.assertEquals('/test_set', get_result.key)
        self.assertEquals('test-key', get_result.value)