merge master
diff --git a/xos/configurations/common/Dockerfile.common b/xos/configurations/common/Dockerfile.common
index ac8931f..8bfd813 100644
--- a/xos/configurations/common/Dockerfile.common
+++ b/xos/configurations/common/Dockerfile.common
@@ -28,7 +28,8 @@
python-dev \
libyaml-dev \
pkg-config \
- python-pycurl
+ python-pycurl \
+ openvpn
RUN pip install django==1.7
RUN pip install djangorestframework==2.4.4
diff --git a/xos/observers/vpn/__init__.py b/xos/observers/vpn/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/xos/observers/vpn/__init__.py
diff --git a/xos/observers/vpn/model-deps b/xos/observers/vpn/model-deps
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/xos/observers/vpn/model-deps
@@ -0,0 +1 @@
+{}
diff --git a/xos/observers/vpn/run.sh b/xos/observers/vpn/run.sh
new file mode 100755
index 0000000..2fc0ca5
--- /dev/null
+++ b/xos/observers/vpn/run.sh
@@ -0,0 +1,2 @@
+export XOS_DIR=/opt/xos
+python vpn-observer.py -C $XOS_DIR/observers/vpn/vpn_config
diff --git a/xos/observers/vpn/steps/__init__.py b/xos/observers/vpn/steps/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/xos/observers/vpn/steps/__init__.py
diff --git a/xos/observers/vpn/steps/sync_vpntenant.py b/xos/observers/vpn/steps/sync_vpntenant.py
new file mode 100644
index 0000000..c7c3c9d
--- /dev/null
+++ b/xos/observers/vpn/steps/sync_vpntenant.py
@@ -0,0 +1,56 @@
+import os
+import sys
+from django.db.models import Q, F
+from observers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
+from services.vpn.models import VPNTenant
+
+parentdir = os.path.join(os.path.dirname(__file__), "..")
+sys.path.insert(0, parentdir)
+
+class SyncVPNTenant(SyncInstanceUsingAnsible):
+ """Class for syncing a VPNTenant using Ansible."""
+ provides = [VPNTenant]
+ observes = VPNTenant
+ requested_interval = 0
+ template_name = "sync_vpntenant.yaml"
+ service_key_name = "/opt/xos/observers/vpn/vpn_private_key"
+
+ def __init__(self, *args, **kwargs):
+ super(SyncVPNTenant, self).__init__(*args, **kwargs)
+
+ def fetch_pending(self, deleted):
+ if (not deleted):
+ objs = VPNTenant.get_tenant_objects().filter(
+ Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False))
+ for tenant in objs:
+ tenant.client_conf = self.generate_client_conf(tenant)
+ else:
+ objs = VPNTenant.get_deleted_tenant_objects()
+
+ return objs
+
+ def get_extra_attributes(self, o):
+ return {"server_key": o.server_key.splitlines(),
+ "is_persistent": o.is_persistent,
+ "can_view_subnet": o.can_view_subnet,
+ "server_address": o.server_address,
+ "client_address": o.client_address}
+
+ def generate_client_conf(self, tenant):
+ """str: Generates the client configuration to use to connect to this VPN server.
+
+ Args:
+ tenant (VPNTenant): The tenant to generate the client configuration for.
+
+ """
+ conf = "remote " + str(tenant.nat_ip) + "\n"
+ conf += "dev tun\n"
+ conf += "ifconfig " + tenant.client_address + " " + tenant.server_address + "\n"
+ conf += "secret static.key"
+ if tenant.is_persistent:
+ conf += "\nkeepalive 10 60\n"
+ conf += "ping-timer-rem\n"
+ conf += "persist-tun\n"
+ conf += "persist-key"
+
+ return conf
diff --git a/xos/observers/vpn/steps/sync_vpntenant.yaml b/xos/observers/vpn/steps/sync_vpntenant.yaml
new file mode 100644
index 0000000..8dd1d12
--- /dev/null
+++ b/xos/observers/vpn/steps/sync_vpntenant.yaml
@@ -0,0 +1,47 @@
+---
+- hosts: {{ instance_name }}
+ gather_facts: False
+ connection: ssh
+ user: ubuntu
+ sudo: yes
+ vars:
+ server_address: {{ server_address }}
+ client_address: {{ client_address }}
+ server_key: {{ server_key }}
+ is_persistent: {{ is_persistent }}
+
+ tasks:
+ - name: install openvpn
+ apt: name=openvpn state=present update_cache=yes
+
+ - name: stop openvpn
+ shell: killall openvpn
+
+ - name: erase key
+ shell: rm -f static.key
+
+ - name: write key
+ shell: echo {{ '{{' }} item {{ '}}' }} >> static.key
+ with_items: "{{ server_key }}"
+
+ - name: erase config
+ shell: rm -f server.conf
+
+ - name: write base config
+ shell:
+ |
+ printf "dev tun
+ ifconfig {{ server_address }} {{ client_address }}
+ secret static.key" > server.conf
+
+ - name: write persistent config
+ shell:
+ |
+ printf "\nkeepalive 10 60
+ ping-timer-rem
+ persist-tun
+ persist-key" >> server.conf
+ when: {{ is_persistent }}
+
+ - name: start openvpn
+ shell: openvpn server.conf &
diff --git a/xos/observers/vpn/stop.sh b/xos/observers/vpn/stop.sh
new file mode 100755
index 0000000..5feca3c
--- /dev/null
+++ b/xos/observers/vpn/stop.sh
@@ -0,0 +1,2 @@
+# Kill the observer
+pkill -9 -f vpn-observer.py
diff --git a/xos/observers/vpn/vpn-observer.py b/xos/observers/vpn/vpn-observer.py
new file mode 100755
index 0000000..2d7644f
--- /dev/null
+++ b/xos/observers/vpn/vpn-observer.py
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+
+import importlib
+import os
+import sys
+observer_path = os.path.join(os.path.dirname(
+ os.path.realpath(__file__)), "../..")
+sys.path.append(observer_path)
+mod = importlib.import_module("xos-observer")
+mod.main()
diff --git a/xos/observers/vpn/vpn_config b/xos/observers/vpn/vpn_config
new file mode 100644
index 0000000..8165ae1
--- /dev/null
+++ b/xos/observers/vpn/vpn_config
@@ -0,0 +1,23 @@
+# Required by XOS
+[db]
+name=xos
+user=postgres
+password=password
+host=localhost
+port=5432
+
+# Required by XOS
+[api]
+nova_enabled=True
+
+# Sets options for the observer
+[observer]
+name=vpn
+dependency_graph=/opt/xos/observers/vpn/model-deps
+steps_dir=/opt/xos/observers/vpn/steps
+sys_dir=/opt/xos/observers/vpn/sys
+logfile=/var/log/xos_backend.log
+pretend=False
+backoff_disabled=True
+save_ansible_output=True
+proxy_ssh=False
diff --git a/xos/scripts/opencloud b/xos/scripts/opencloud
index 6b3e737..966ad9b 100755
--- a/xos/scripts/opencloud
+++ b/xos/scripts/opencloud
@@ -60,13 +60,13 @@
echo Waiting for postgres to start
sleep 1
sudo -u postgres psql -c '\q'
- done
+ done
}
function db_exists {
- sudo -u postgres psql $DBNAME -c '\q' 2>/dev/null
+ sudo -u postgres psql $DBNAME -c '\q' 2>/dev/null
return $?
-}
+}
function createdb {
wait_postgres
@@ -145,6 +145,7 @@
python ./manage.py makemigrations cord
python ./manage.py makemigrations ceilometer
python ./manage.py makemigrations helloworldservice_complete
+ python ./manage.py makemigrations vpn
python ./manage.py makemigrations onos
#python ./manage.py makemigrations servcomp
}
diff --git a/xos/services/vpn/__init__.py b/xos/services/vpn/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/xos/services/vpn/__init__.py
diff --git a/xos/services/vpn/admin.py b/xos/services/vpn/admin.py
new file mode 100644
index 0000000..ceb59dc
--- /dev/null
+++ b/xos/services/vpn/admin.py
@@ -0,0 +1,142 @@
+
+from subprocess import PIPE, Popen
+
+from core.admin import ReadOnlyAwareAdmin, SliceInline
+from core.middleware import get_request
+from core.models import User
+from django import forms
+from django.contrib import admin
+from services.vpn.models import VPN_KIND, VPNService, VPNTenant
+
+
+class VPNServiceAdmin(ReadOnlyAwareAdmin):
+ """Defines the admin for the VPNService."""
+ model = VPNService
+ verbose_name = "VPN Service"
+
+ list_display = ("backend_status_icon", "name", "enabled")
+
+ list_display_links = ('backend_status_icon', 'name', )
+
+ fieldsets = [(None, {'fields': ['backend_status_text', 'name', 'enabled',
+ 'versionNumber', 'description', "view_url"],
+ 'classes':['suit-tab suit-tab-general']})]
+
+ readonly_fields = ('backend_status_text', )
+
+ inlines = [SliceInline]
+
+ extracontext_registered_admins = True
+
+ user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
+
+ suit_form_tabs = (('general', 'VPN Service Details'),
+ ('administration', 'Tenants'),
+ ('slices', 'Slices'),)
+
+ suit_form_includes = (('vpnserviceadmin.html',
+ 'top',
+ 'administration'),)
+
+ def queryset(self, request):
+ return VPNService.get_service_objects_by_user(request.user)
+
+
+class VPNTenantForm(forms.ModelForm):
+ """The form used to create and edit a VPNTenant.
+
+ Attributes:
+ creator (forms.ModelChoiceField): The XOS user that created this tenant.
+ server_key (forms.CharField): The readonly static key used to the connect to this Tenant.
+ client_conf (forms.CharField): The readonly configuration used on the client to connect to this Tenant.
+ server_address (forms.GenericIPAddressField): The ip address on the VPN of this Tenant.
+ client_address (forms.GenericIPAddressField): The ip address on the VPN of the client.
+ is_persistent (forms.BooleanField): Determines if this Tenant keeps this connection alive through failures.
+ can_view_subnet (forms.BooleanField): Determins if this Tenant makes it's subnet available to the client.
+
+ """
+ creator = forms.ModelChoiceField(queryset=User.objects.all())
+ server_key = forms.CharField(required=False, widget=forms.Textarea)
+ client_conf = forms.CharField(required=False, widget=forms.Textarea)
+ server_address = forms.GenericIPAddressField(
+ protocol='IPv4', required=True)
+ client_address = forms.GenericIPAddressField(
+ protocol='IPv4', required=True)
+ is_persistent = forms.BooleanField(required=False)
+ can_view_subnet = forms.BooleanField(required=False)
+
+ def __init__(self, *args, **kwargs):
+ super(VPNTenantForm, self).__init__(*args, **kwargs)
+ self.fields['kind'].widget.attrs['readonly'] = True
+ self.fields['server_key'].widget.attrs['readonly'] = True
+ self.fields['client_conf'].widget.attrs['readonly'] = True
+ self.fields[
+ 'provider_service'].queryset = VPNService.get_service_objects().all()
+
+ self.fields['kind'].initial = VPN_KIND
+
+ if self.instance:
+ self.fields['creator'].initial = self.instance.creator
+ self.fields['server_key'].initial = self.instance.server_key
+ self.fields['client_conf'].initial = self.instance.client_conf
+ self.fields[
+ 'server_address'].initial = self.instance.server_address
+ self.fields[
+ 'client_address'].initial = self.instance.client_address
+ self.fields['is_persistent'].initial = self.instance.is_persistent
+ self.fields[
+ 'can_view_subnet'].initial = self.instance.can_view_subnet
+
+ if (not self.instance) or (not self.instance.pk):
+ self.fields['creator'].initial = get_request().user
+ self.fields['server_key'].initial = self.generate_VPN_key()
+ self.fields['server_address'].initial = "10.8.0.1"
+ self.fields['client_address'].initial = "10.8.0.2"
+ self.fields['is_persistent'].initial = True
+ self.fields['can_view_subnet'].initial = False
+ if VPNService.get_service_objects().exists():
+ self.fields["provider_service"].initial = VPNService.get_service_objects().all()[
+ 0]
+
+ def save(self, commit=True):
+ self.instance.creator = self.cleaned_data.get("creator")
+ self.instance.server_key = self.cleaned_data.get("server_key")
+ self.instance.server_address = self.cleaned_data.get("server_address")
+ self.instance.client_address = self.cleaned_data.get("client_address")
+ self.instance.is_persistent = self.cleaned_data.get('is_persistent')
+ self.instance.can_view_subnet = self.cleaned_data.get(
+ 'can_view_subnet')
+ return super(VPNTenantForm, self).save(commit=commit)
+
+ def generate_VPN_key(self):
+ """str: Generates a VPN key using the openvpn command."""
+ proc = Popen("openvpn --genkey --secret /dev/stdout",
+ shell=True, stdout=PIPE)
+ (stdout, stderr) = proc.communicate()
+ return stdout
+
+ class Meta:
+ model = VPNTenant
+
+
+class VPNTenantAdmin(ReadOnlyAwareAdmin):
+ verbose_name = "VPN Tenant Admin"
+ list_display = ('id', 'backend_status_icon', 'instance')
+ list_display_links = ('id', 'backend_status_icon', 'instance')
+ fieldsets = [(None, {'fields': ['backend_status_text', 'kind',
+ 'provider_service', 'instance', 'creator',
+ 'server_key', 'client_conf',
+ 'server_address', 'client_address',
+ 'is_persistent', 'can_view_subnet'],
+ 'classes': ['suit-tab suit-tab-general']})]
+ readonly_fields = ('backend_status_text', 'instance')
+ form = VPNTenantForm
+
+ suit_form_tabs = (('general', 'Details'),)
+
+ def queryset(self, request):
+ return VPNTenant.get_tenant_objects_by_user(request.user)
+
+# Associate the admin forms with the models.
+admin.site.register(VPNService, VPNServiceAdmin)
+admin.site.register(VPNTenant, VPNTenantAdmin)
diff --git a/xos/services/vpn/models.py b/xos/services/vpn/models.py
new file mode 100644
index 0000000..8b2c5d8
--- /dev/null
+++ b/xos/services/vpn/models.py
@@ -0,0 +1,161 @@
+from core.models import Service, TenantWithContainer
+from django.db import transaction
+
+VPN_KIND = "vpn"
+
+
+class VPNService(Service):
+ """Defines the Service for creating VPN servers."""
+ KIND = VPN_KIND
+
+ class Meta:
+ proxy = True
+ # The name used to find this service, all directories are named this
+ app_label = "vpn"
+ verbose_name = "VPN Service"
+
+
+class VPNTenant(TenantWithContainer):
+ """Defines the Tenant for creating VPN servers."""
+
+ class Meta:
+ proxy = True
+ verbose_name = "VPN Tenant"
+
+ KIND = VPN_KIND
+
+ sync_attributes = ("nat_ip", "nat_mac",)
+
+ default_attributes = {'server_key': 'Error key not found',
+ 'client_conf': 'Configuration not found',
+ 'server_address': '10.8.0.1',
+ 'client_address': '10.8.0.2',
+ 'can_view_subnet': False,
+ 'is_persistent': True}
+
+ def __init__(self, *args, **kwargs):
+ vpn_services = VPNService.get_service_objects().all()
+ if vpn_services:
+ self._meta.get_field(
+ "provider_service").default = vpn_services[0].id
+ super(VPNTenant, self).__init__(*args, **kwargs)
+
+ def save(self, *args, **kwargs):
+ super(VPNTenant, self).save(*args, **kwargs)
+ model_policy_vpn_tenant(self.pk)
+
+ def delete(self, *args, **kwargs):
+ self.cleanup_container()
+ super(VPNTenant, self).delete(*args, **kwargs)
+
+ @property
+ def server_key(self):
+ """str: The server_key used to connect to the VPN server."""
+ return self.get_attribute(
+ "server_key",
+ self.default_attributes['server_key'])
+
+ @server_key.setter
+ def server_key(self, value):
+ self.set_attribute("server_key", value)
+
+ @property
+ def addresses(self):
+ """Mapping[str, str]: The ip, mac address, and subnet of the NAT network of this Tenant."""
+ if (not self.id) or (not self.instance):
+ return {}
+
+ addresses = {}
+ for ns in self.instance.ports.all():
+ if "nat" in ns.network.name.lower():
+ addresses["ip"] = ns.ip
+ addresses["mac"] = ns.mac
+ addresses["subnet"] = ns.network.subnet
+ break
+
+ return addresses
+
+ # This getter is necessary because nat_ip is a sync_attribute
+ @property
+ def nat_ip(self):
+ """str: The IP of this Tenant on the NAT network."""
+ return self.addresses.get("ip", None)
+
+ # This getter is necessary because nat_mac is a sync_attribute
+ @property
+ def nat_mac(self):
+ """str: The MAC address of this Tenant on the NAT network."""
+ return self.addresses.get("mac", None)
+
+ @property
+ def subnet(self):
+ """str: The subnet of this Tenant on the NAT network."""
+ return self.addresses.get("subnet", None)
+
+ @property
+ def server_address(self):
+ """str: The IP address of the server on the VPN."""
+ return self.get_attribute(
+ 'server_address',
+ self.default_attributes['server_address'])
+
+ @server_address.setter
+ def server_address(self, value):
+ self.set_attribute("server_address", value)
+
+ @property
+ def client_address(self):
+ """str: The IP address of the client on the VPN."""
+ return self.get_attribute(
+ 'client_address',
+ self.default_attributes['client_address'])
+
+ @client_address.setter
+ def client_address(self, value):
+ self.set_attribute("client_address", value)
+
+ @property
+ def client_conf(self):
+ """str: The client configuration for the client to connect to this server."""
+ return self.get_attribute(
+ "client_conf",
+ self.default_attributes['client_conf'])
+
+ @client_conf.setter
+ def client_conf(self, value):
+ self.set_attribute("client_conf", value)
+
+ @property
+ def is_persistent(self):
+ """bool: True if the VPN connection is persistence, false otherwise."""
+ return self.get_attribute(
+ "is_persistent",
+ self.default_attributes['is_persistent'])
+
+ @is_persistent.setter
+ def is_persistent(self, value):
+ self.set_attribute("is_persistent", value)
+
+ @property
+ def can_view_subnet(self):
+ """bool: True if the client can see the subnet of the server, false otherwise."""
+ return self.get_attribute(
+ "can_view_subnet",
+ self.default_attributes['can_view_subnet'])
+
+ @can_view_subnet.setter
+ def can_view_subnet(self, value):
+ self.set_attribute("can_view_subnet", value)
+
+
+def model_policy_vpn_tenant(pk):
+ """Manages the contain for the VPN Tenant."""
+ # This section of code is atomic to prevent race conditions
+ with transaction.atomic():
+ # We find all of the tenants that are waiting to update
+ tenant = VPNTenant.objects.select_for_update().filter(pk=pk)
+ if not tenant:
+ return
+ # Since this code is atomic it is safe to always use the first tenant
+ tenant = tenant[0]
+ tenant.manage_container()
diff --git a/xos/services/vpn/templates/vpnserviceadmin.html b/xos/services/vpn/templates/vpnserviceadmin.html
new file mode 100644
index 0000000..d983771
--- /dev/null
+++ b/xos/services/vpn/templates/vpnserviceadmin.html
@@ -0,0 +1,10 @@
+<!-- Template used to for the button leading to the HelloWorldTenantComplete form. -->
+<div class = "left-nav">
+ <ul>
+ <li>
+ <a href="/admin/vpn/vpntenant/">
+ VPN Tenants
+ </a>
+ </li>
+ </ul>
+</div>
diff --git a/xos/xos/settings.py b/xos/xos/settings.py
index 96ac52c..5c1e88e 100644
--- a/xos/xos/settings.py
+++ b/xos/xos/settings.py
@@ -30,7 +30,7 @@
GEOIP_PATH = "/usr/share/GeoIP"
XOS_DIR = "/opt/xos"
-DEBUG = False
+DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
@@ -180,6 +180,7 @@
'services.ceilometer',
'services.requestrouter',
'services.syndicate_storage',
+ 'services.vpn',
'geoposition',
'rest_framework_swagger',
)