| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import logging, re, time | 5 import logging, re, time |
| 6 from autotest_lib.client.common_lib import error | 6 from autotest_lib.client.common_lib import error |
| 7 | 7 |
| 8 class LinuxServer(object): | 8 class LinuxServer(object): |
| 9 """ | 9 """ |
| 10 Linux Server: A machine which hosts network services. | 10 Linux Server: A machine which hosts network services. |
| 11 | 11 |
| 12 """ | 12 """ |
| 13 | 13 |
| 14 def __init__(self, server, params): | 14 def __init__(self, server, params): |
| 15 self.server = server # Server host. | 15 self.server = server # Server host. |
| 16 self.vpn_kind = None | 16 self.vpn_kind = None |
| 17 self.conf = {} | 17 self.openvpn_config = {} |
| 18 self.strongswan_config_templates = {} |
| 19 |
| 20 def vpn_strongswan_config_templates(self, params): |
| 21 for k, v in params.iteritems(): |
| 22 self.strongswan_config_templates[k] = v |
| 18 | 23 |
| 19 def vpn_server_config(self, params): | 24 def vpn_server_config(self, params): |
| 20 """ Configure & launch the server side of the VPN. | 25 """ Configure & launch the server side of the VPN. |
| 21 | 26 |
| 22 Parameters, in 'params': | 27 Parameters, in 'params': |
| 23 | 28 |
| 24 kind : required | 29 kind : required |
| 25 | 30 |
| 26 The kind of VPN which should be configured and | 31 The kind of VPN which should be configured and |
| 27 launched. | 32 launched. |
| 28 | 33 |
| 29 Valid values: | 34 Valid values: |
| 30 | 35 |
| 31 openvpn | 36 openvpn |
| 37 l2tpipsec (StrongSwan PSK or certificates) |
| 32 | 38 |
| 33 config: required | 39 config: required |
| 34 | 40 |
| 35 The configuration information associated with | 41 The configuration information associated with |
| 36 the VPN server. | 42 the VPN server. |
| 37 | 43 |
| 38 This is a dict which contains key/value pairs | 44 This is a dict which contains key/value pairs |
| 39 representing the VPN's configuration. | 45 representing the VPN's configuration. |
| 40 | 46 |
| 41 The values stored in the 'config' param must all be | 47 The values stored in the 'config' param must all be |
| 42 supported by the specified VPN kind. | 48 supported by the specified VPN kind. |
| 43 """ | 49 """ |
| 44 self.vpn_server_kill({}) # Must be first. Relies on self.vpn_kind. | 50 self.vpn_server_kill({}) # Must be first. Relies on self.vpn_kind. |
| 45 | |
| 46 self.vpn_kind = params.get('kind', None) | 51 self.vpn_kind = params.get('kind', None) |
| 47 | 52 |
| 48 # Read configuration information & create server configuration file. | |
| 49 # | |
| 50 # As VPN kinds other than 'openvpn' are supported, and | |
| 51 # since 'self.conf' is cummulative, perhaps there should be | |
| 52 # a method which will clear 'self.conf'; different types of | |
| 53 # VPN will likely not have the same configuration | |
| 54 # parameters. This is only really needed if a test is | |
| 55 # written to switch between two differents kinds of VPN. | |
| 56 for k, v in params.get('config', {}).iteritems(): | |
| 57 self.conf[k] = v | |
| 58 self.server.run("cat <<EOF >%s\n%s\nEOF\n" % | |
| 59 ('/tmp/vpn-server.conf', '\n'.join( | |
| 60 "%s %s" % kv for kv in self.conf.iteritems()))) | |
| 61 | |
| 62 # Launch specified VPN server. | 53 # Launch specified VPN server. |
| 63 if self.vpn_kind is None: | 54 if self.vpn_kind is None: |
| 64 raise error.TestFail('No VPN kind specified for this test.'); | 55 raise error.TestFail('No VPN kind specified for this test.') |
| 65 elif self.vpn_kind == 'openvpn': | 56 elif self.vpn_kind == 'openvpn': |
| 66 self.server.run("/usr/sbin/openvpn --config /tmp/vpn-server.conf &") | 57 # Read config information & create server configuration file. |
| 58 for k, v in params.get('config', {}).iteritems(): |
| 59 self.openvpn_config[k] = v |
| 60 self.server.run("cat <<EOF >/tmp/vpn-server.conf\n%s\nEOF\n" % |
| 61 ('\n'.join( "%s %s" % kv for kv in |
| 62 self.openvpn_config.iteritems()))) |
| 63 self.server.run("/usr/sbin/openvpn " |
| 64 "--config /tmp/vpn-server.conf &") |
| 65 elif self.vpn_kind == 'l2tpipsec': # aka 'strongswan' |
| 66 # The replacement values in 'replacements' must match the |
| 67 # template set with vpn_strongswan_config_templates(). |
| 68 replacements = params.get("replacements", None) |
| 69 if replacements is not None: |
| 70 replacements["@ipsecrets-ip@"] = self.server.ip |
| 71 for cfg, template in self.strongswan_config_templates.iteritems(): |
| 72 contents = template |
| 73 if replacements is not None: |
| 74 for k, v in replacements.iteritems(): |
| 75 contents = contents.replace(k, v) |
| 76 self.server.run("cat <<EOF >%s\n%s\nEOF\n" % (cfg, contents)) |
| 77 |
| 78 self.server.run("/usr/sbin/ipsec start") |
| 79 |
| 80 # Restart xl2tpd to ensure use of newly-created config files. |
| 81 self.server.run("sh /etc/init.d/xl2tpd restart") |
| 67 else: | 82 else: |
| 68 raise error.TestFail('(internal error): No config case ' | 83 raise error.TestFail('(internal error): No config case ' |
| 69 'for VPN kind (%s)' % self.vpn_kind) | 84 'for VPN kind (%s)' % self.vpn_kind) |
| 70 | 85 |
| 71 def vpn_server_kill(self, params): | 86 def vpn_server_kill(self, params): |
| 72 """ Kill the VPN server. """ | 87 """ Kill the VPN server. """ |
| 73 if self.vpn_kind is not None: | 88 if self.vpn_kind is not None: |
| 74 if self.vpn_kind == 'openvpn': | 89 if self.vpn_kind == 'openvpn': |
| 75 self.server.run("pkill /usr/sbin/openvpn") | 90 self.server.run("pkill /usr/sbin/openvpn") |
| 91 elif self.vpn_kind == 'l2tpipsec': # aka 'strongswan' |
| 92 self.server.run("pkill /usr/sbin/ipsec") |
| 93 self.server.run("pkill /usr/lib/ipsec/charon") |
| 94 self.server.run("pkill /usr/lib/ipsec/pluto") |
| 76 else: | 95 else: |
| 77 raise error.TestFail('(internal error): No kill case ' | 96 raise error.TestFail('(internal error): No kill case ' |
| 78 'for VPN kind (%s)' % self.vpn_kind) | 97 'for VPN kind (%s)' % self.vpn_kind) |
| 79 self.vpn_kind = None; | 98 self.vpn_kind = None |
| OLD | NEW |