summaryrefslogtreecommitdiff
path: root/src/leap/eip/tests/test_config.py
blob: 05e78de422a40c6a1b484f6a7fae52adfa605d67 (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
from collections import OrderedDict
import json
import os
import platform
import stat

try:
    import unittest2 as unittest
except ImportError:
    import unittest

#from leap.base import constants
#from leap.eip import config as eip_config
#from leap import __branding as BRANDING
from leap.eip import config as eipconfig
from leap.eip.tests.data import EIP_SAMPLE_CONFIG, EIP_SAMPLE_SERVICE
from leap.testing.basetest import BaseLeapTest
from leap.util.fileutil import mkdir_p, mkdir_f

_system = platform.system()

#PROVIDER = BRANDING.get('provider_domain')
#PROVIDER_SHORTNAME = BRANDING.get('short_name')


class EIPConfigTest(BaseLeapTest):

    __name__ = "eip_config_tests"
    provider = "testprovider.example.org"

    maxDiff = None

    def setUp(self):
        pass

    def tearDown(self):
        pass

    #
    # helpers
    #

    def touch_exec(self):
        path = os.path.join(
            self.tempdir, 'bin')
        mkdir_p(path)
        tfile = os.path.join(
            path,
            'openvpn')
        open(tfile, 'wb').close()
        os.chmod(tfile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)

    def write_sample_eipservice(self, vpnciphers=False, extra_vpnopts=None,
                                gateways=None):
        conf = eipconfig.EIPServiceConfig()
        mkdir_f(conf.filename)
        if gateways:
            EIP_SAMPLE_SERVICE['gateways'] = gateways
        if vpnciphers:
            openvpnconfig = OrderedDict({
                "auth": "SHA1",
                "cipher": "AES-128-CBC",
                "tls-cipher": "DHE-RSA-AES128-SHA"})
            if extra_vpnopts:
                for k, v in extra_vpnopts.items():
                    openvpnconfig[k] = v
            EIP_SAMPLE_SERVICE['openvpn_configuration'] = openvpnconfig

        with open(conf.filename, 'w') as fd:
            fd.write(json.dumps(EIP_SAMPLE_SERVICE))

    def write_sample_eipconfig(self):
        conf = eipconfig.EIPConfig()
        folder, f = os.path.split(conf.filename)
        if not os.path.isdir(folder):
            mkdir_p(folder)
        with open(conf.filename, 'w') as fd:
            fd.write(json.dumps(EIP_SAMPLE_CONFIG))

    def get_expected_openvpn_args(self, with_openvpn_ciphers=False):
        """
        yeah, this is almost as duplicating the
        code for building the command
        """
        args = []
        eipconf = eipconfig.EIPConfig(domain=self.provider)
        eipconf.load()
        eipsconf = eipconfig.EIPServiceConfig(domain=self.provider)
        eipsconf.load()

        username = self.get_username()
        groupname = self.get_groupname()

        args.append('--client')
        args.append('--dev')
        #does this have to be tap for win??
        args.append('tun')
        args.append('--persist-tun')
        args.append('--persist-key')
        args.append('--remote')

        args.append('%s' % eipconfig.get_eip_gateway(
            eipconfig=eipconf,
            eipserviceconfig=eipsconf))
        # XXX get port!?
        args.append('1194')
        # XXX get proto
        args.append('udp')
        args.append('--tls-client')
        args.append('--remote-cert-tls')
        args.append('server')

        if with_openvpn_ciphers:
            CIPHERS = [
                "--tls-cipher", "DHE-RSA-AES128-SHA",
                "--cipher", "AES-128-CBC",
                "--auth", "SHA1"]
            for opt in CIPHERS:
                args.append(opt)

        args.append('--user')
        args.append(username)
        args.append('--group')
        args.append(groupname)
        args.append('--management-client-user')
        args.append(username)
        args.append('--management-signal')

        args.append('--management')
        #XXX hey!
        #get platform switches here!
        args.append('/tmp/test.socket')
        args.append('unix')

        args.append('--script-security')
        args.append('2')

        if _system == "Linux":
            args.append('--up')
            args.append('/etc/leap/resolv-update')
            args.append('--down')
            args.append('/etc/leap/resolv-update')
            args.append('--plugin')
            args.append('/usr/lib/openvpn/openvpn-down-root.so')
            args.append("'script_type=down /etc/leap/resolv-update'")

        # certs
        # XXX get values from specs?
        args.append('--cert')
        args.append(os.path.join(
            self.home,
            '.config', 'leap', 'providers',
            '%s' % self.provider,
            'keys', 'client',
            'openvpn.pem'))
        args.append('--key')
        args.append(os.path.join(
            self.home,
            '.config', 'leap', 'providers',
            '%s' % self.provider,
            'keys', 'client',
            'openvpn.pem'))
        args.append('--ca')
        args.append(os.path.join(
            self.home,
            '.config', 'leap', 'providers',
            '%s' % self.provider,
            'keys', 'ca',
            'cacert.pem'))
        return args

    # build command string
    # these tests are going to have to check
    # many combinations. we should inject some
    # params in the function call, to disable
    # some checks.

    def test_get_eip_gateway(self):
        self.write_sample_eipconfig()
        eipconf = eipconfig.EIPConfig(domain=self.provider)

        # default eipservice
        self.write_sample_eipservice()
        eipsconf = eipconfig.EIPServiceConfig(domain=self.provider)

        gateway = eipconfig.get_eip_gateway(
            eipconfig=eipconf,
            eipserviceconfig=eipsconf)

        # in spec is local gateway by default
        self.assertEqual(gateway, '127.0.0.1')

        # change eipservice
        # right now we only check that cluster == selected primary gw in
        # eip.json, and pick first matching ip
        eipconf._config.config['primary_gateway'] = "foo_provider"
        newgateways = [{"cluster": "foo_provider",
                        "ip_address": "127.0.0.99"}]
        self.write_sample_eipservice(gateways=newgateways)
        eipsconf = eipconfig.EIPServiceConfig(domain=self.provider)
        # load from disk file
        eipsconf.load()

        gateway = eipconfig.get_eip_gateway(
            eipconfig=eipconf,
            eipserviceconfig=eipsconf)
        self.assertEqual(gateway, '127.0.0.99')

        # change eipservice, several gateways
        # right now we only check that cluster == selected primary gw in
        # eip.json, and pick first matching ip
        eipconf._config.config['primary_gateway'] = "bar_provider"
        newgateways = [{"cluster": "foo_provider",
                        "ip_address": "127.0.0.99"},
                       {'cluster': "bar_provider",
                        "ip_address": "127.0.0.88"}]
        self.write_sample_eipservice(gateways=newgateways)
        eipsconf = eipconfig.EIPServiceConfig(domain=self.provider)
        # load from disk file
        eipsconf.load()

        gateway = eipconfig.get_eip_gateway(
            eipconfig=eipconf,
            eipserviceconfig=eipsconf)
        self.assertEqual(gateway, '127.0.0.88')

    def test_build_ovpn_command_empty_config(self):
        self.touch_exec()
        self.write_sample_eipservice()
        self.write_sample_eipconfig()

        from leap.eip import config as eipconfig
        from leap.util.fileutil import which
        path = os.environ['PATH']
        vpnbin = which('openvpn', path=path)
        #print 'path =', path
        #print 'vpnbin = ', vpnbin
        vpncommand, vpnargs = eipconfig.build_ovpn_command(
            do_pkexec_check=False, vpnbin=vpnbin,
            socket_path="/tmp/test.socket",
            provider=self.provider)
        self.assertEqual(vpncommand, self.home + '/bin/openvpn')
        self.assertEqual(vpnargs, self.get_expected_openvpn_args())

    def test_build_ovpn_command_openvpnoptions(self):
        self.touch_exec()

        from leap.eip import config as eipconfig
        from leap.util.fileutil import which
        path = os.environ['PATH']
        vpnbin = which('openvpn', path=path)

        self.write_sample_eipconfig()

        # regular run, everything normal
        self.write_sample_eipservice(vpnciphers=True)
        vpncommand, vpnargs = eipconfig.build_ovpn_command(
            do_pkexec_check=False, vpnbin=vpnbin,
            socket_path="/tmp/test.socket",
            provider=self.provider)
        self.assertEqual(vpncommand, self.home + '/bin/openvpn')
        expected = self.get_expected_openvpn_args(
            with_openvpn_ciphers=True)
        self.assertEqual(vpnargs, expected)

        # bad options -- illegal options
        self.write_sample_eipservice(
            vpnciphers=True,
            # WE ONLY ALLOW vpn options in auth, cipher, tls-cipher
            extra_vpnopts={"notallowedconfig": "badvalue"})
        vpncommand, vpnargs = eipconfig.build_ovpn_command(
            do_pkexec_check=False, vpnbin=vpnbin,
            socket_path="/tmp/test.socket",
            provider=self.provider)
        self.assertEqual(vpncommand, self.home + '/bin/openvpn')
        expected = self.get_expected_openvpn_args(
            with_openvpn_ciphers=True)
        self.assertEqual(vpnargs, expected)

        # bad options -- illegal chars
        self.write_sample_eipservice(
            vpnciphers=True,
            # WE ONLY ALLOW A-Z09\-
            extra_vpnopts={"cipher": "AES-128-CBC;FOOTHING"})
        vpncommand, vpnargs = eipconfig.build_ovpn_command(
            do_pkexec_check=False, vpnbin=vpnbin,
            socket_path="/tmp/test.socket",
            provider=self.provider)
        self.assertEqual(vpncommand, self.home + '/bin/openvpn')
        expected = self.get_expected_openvpn_args(
            with_openvpn_ciphers=True)
        self.assertEqual(vpnargs, expected)


if __name__ == "__main__":
    unittest.main()