blob: 9cc13f0442ef523a3eedc5e40503a1e1c62e5407 [file] [log] [blame]
import jinja2
from api.xosapi_helpers import PlusModelSerializer, ReadOnlyField, XOSViewSet
from core.models import TenantPrivilege
from rest_framework import serializers
from services.openvpn.models import OpenVPNService, OpenVPNTenant
def get_default_openvpn_service():
openvpn_services = OpenVPNService.get_service_objects().all()
if openvpn_services:
return openvpn_services[0].id
return None
class OpenVPNTenantSerializer(PlusModelSerializer):
"""A Serializer for the OpenVPNTenant that has the minimum information required for clients.
Attributes:
id (ReadOnlyField): The ID of OpenVPNTenant.
server_network (ReadOnlyField): The network of the VPN.
vpn_subnet (ReadOnlyField): The subnet of the VPN.
script_text (SerializerMethodField): The text of the script for the client to use to
connect.
"""
id = ReadOnlyField()
server_network = ReadOnlyField()
vpn_subnet = ReadOnlyField()
script_text = serializers.SerializerMethodField()
class Meta:
model = OpenVPNTenant
fields = ('id', 'service_specific_attribute', 'vpn_subnet',
'server_network', 'script_text')
def get_script_text(self, obj):
"""Gets the text of the client script for the requesting user.
Parameters:
obj (services.openvpn.models.OpenVPNTenant): The OpenVPNTenant to connect to.
Returns:
str: The client script as a str.
"""
env = jinja2.Environment(
loader=jinja2.FileSystemLoader("/opt/xos/services/openvpn/templates"))
template = env.get_template("connect.vpn.j2")
client_name = self.context['request'].user.email + "-" + str(obj.id)
remote_ids = list(obj.failover_server_ids)
remote_ids.insert(0, obj.id)
remotes = OpenVPNTenant.get_tenant_objects().filter(pk__in=remote_ids)
pki_dir = OpenVPNService.get_pki_dir(obj)
fields = {"client_name": client_name,
"remotes": remotes,
"is_persistent": obj.is_persistent,
"ca_crt": obj.get_ca_crt(pki_dir),
"client_crt": obj.get_client_cert(client_name, pki_dir),
"client_key": obj.get_client_key(client_name, pki_dir)
}
return template.render(fields)
class OpenVPNTenantViewSet(XOSViewSet):
"""Class that provides a list of OpenVPNTenants that the user has permission to access."""
base_name = "openvpn"
method_kind = "viewset"
method_name = "list"
serializer_class = OpenVPNTenantSerializer
def get_queryset(self):
# Get every privilege for this user
tenants_privs = TenantPrivilege.objects.all().filter(
user=self.request.user)
vpn_tenants = []
for priv in tenants_privs:
vpn_tenants.append(
OpenVPNTenant.get_tenant_objects().filter(pk=priv.tenant.pk)[0])
return vpn_tenants