blob: 67943b8531754fde8cb842f6235da167221f942d [file] [log] [blame]
Jeremy Mowerye02d4b62016-01-10 15:21:52 -07001import time
Jeremy Mowery82760822016-01-08 16:36:22 -07002from subprocess import PIPE, Popen
3
Jeremy Mowery8b664f72015-12-04 11:52:16 -07004from core.admin import ReadOnlyAwareAdmin, SliceInline
5from core.middleware import get_request
6from core.models import User
7from django import forms
8from django.contrib import admin
Jeremy Mowery82760822016-01-08 16:36:22 -07009from services.vpn.models import VPN_KIND, VPNService, VPNTenant
10
Jeremy Mowery8b664f72015-12-04 11:52:16 -070011
12class VPNServiceAdmin(ReadOnlyAwareAdmin):
Jeremy Mowery82760822016-01-08 16:36:22 -070013 """Defines the admin for the VPNService."""
Jeremy Mowery8b664f72015-12-04 11:52:16 -070014 model = VPNService
15 verbose_name = "VPN Service"
16
17 list_display = ("backend_status_icon", "name", "enabled")
18
19 list_display_links = ('backend_status_icon', 'name', )
20
21 fieldsets = [(None, {'fields': ['backend_status_text', 'name', 'enabled',
22 'versionNumber', 'description', "view_url"],
23 'classes':['suit-tab suit-tab-general']})]
24
25 readonly_fields = ('backend_status_text', )
26
27 inlines = [SliceInline]
28
29 extracontext_registered_admins = True
30
31 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
32
33 suit_form_tabs = (('general', 'VPN Service Details'),
34 ('administration', 'Tenants'),
35 ('slices', 'Slices'),)
36
37 suit_form_includes = (('vpnserviceadmin.html',
38 'top',
39 'administration'),)
40
41 def queryset(self, request):
42 return VPNService.get_service_objects_by_user(request.user)
43
Jeremy Mowery82760822016-01-08 16:36:22 -070044
Jeremy Mowery8b664f72015-12-04 11:52:16 -070045class VPNTenantForm(forms.ModelForm):
Jeremy Mowery82760822016-01-08 16:36:22 -070046 """The form used to create and edit a VPNTenant.
47
48 Attributes:
49 creator (forms.ModelChoiceField): The XOS user that created this tenant.
50 server_key (forms.CharField): The readonly static key used to the connect to this Tenant.
51 client_conf (forms.CharField): The readonly configuration used on the client to connect to this Tenant.
52 server_address (forms.GenericIPAddressField): The ip address on the VPN of this Tenant.
53 client_address (forms.GenericIPAddressField): The ip address on the VPN of the client.
54 is_persistent (forms.BooleanField): Determines if this Tenant keeps this connection alive through failures.
55 can_view_subnet (forms.BooleanField): Determins if this Tenant makes it's subnet available to the client.
Jeremy Moweryc951d5b2016-01-08 17:07:46 -070056
Jeremy Mowery82760822016-01-08 16:36:22 -070057 """
Jeremy Mowery8b664f72015-12-04 11:52:16 -070058 creator = forms.ModelChoiceField(queryset=User.objects.all())
Jeremy Mowerye02d4b62016-01-10 15:21:52 -070059 server_key = forms.CharField(required=True, widget=forms.Textarea)
Jeremy Mowery82760822016-01-08 16:36:22 -070060 server_address = forms.GenericIPAddressField(
61 protocol='IPv4', required=True)
62 client_address = forms.GenericIPAddressField(
63 protocol='IPv4', required=True)
Jeremy Moweryb3fcf1b2016-01-06 15:46:02 -070064 is_persistent = forms.BooleanField(required=False)
65 can_view_subnet = forms.BooleanField(required=False)
Jeremy Mowerye02d4b62016-01-10 15:21:52 -070066 file_name = forms.CharField(required=True)
Jeremy Mowery8b664f72015-12-04 11:52:16 -070067
68 def __init__(self, *args, **kwargs):
69 super(VPNTenantForm, self).__init__(*args, **kwargs)
70 self.fields['kind'].widget.attrs['readonly'] = True
Jeremy Moweryb05f2512015-12-07 20:59:23 -070071 self.fields['server_key'].widget.attrs['readonly'] = True
Jeremy Mowerye02d4b62016-01-10 15:21:52 -070072 self.fields['file_name'].widget.attrs['readonly'] = True
Jeremy Mowery8b664f72015-12-04 11:52:16 -070073 self.fields[
74 'provider_service'].queryset = VPNService.get_service_objects().all()
75
76 self.fields['kind'].initial = VPN_KIND
77
78 if self.instance:
79 self.fields['creator'].initial = self.instance.creator
80 self.fields['server_key'].initial = self.instance.server_key
Jeremy Mowery82760822016-01-08 16:36:22 -070081 self.fields[
82 'server_address'].initial = self.instance.server_address
83 self.fields[
84 'client_address'].initial = self.instance.client_address
Jeremy Mowery4a23e7d2016-01-06 15:16:33 -070085 self.fields['is_persistent'].initial = self.instance.is_persistent
Jeremy Mowery82760822016-01-08 16:36:22 -070086 self.fields[
87 'can_view_subnet'].initial = self.instance.can_view_subnet
Jeremy Mowerye02d4b62016-01-10 15:21:52 -070088 self.fields['file_name'].initial = self.instance.file_name
Jeremy Mowery8b664f72015-12-04 11:52:16 -070089
Jeremy Mowery8b664f72015-12-04 11:52:16 -070090 if (not self.instance) or (not self.instance.pk):
91 self.fields['creator'].initial = get_request().user
Jeremy Mowerye1260de2015-12-06 23:10:49 -070092 self.fields['server_key'].initial = self.generate_VPN_key()
Jeremy Mowerybd2ed3a2016-01-05 16:52:43 -070093 self.fields['server_address'].initial = "10.8.0.1"
94 self.fields['client_address'].initial = "10.8.0.2"
Jeremy Mowery4a23e7d2016-01-06 15:16:33 -070095 self.fields['is_persistent'].initial = True
96 self.fields['can_view_subnet'].initial = False
Jeremy Mowery8aac6272016-01-10 19:37:07 -070097 self.fields['file_name'].initial = "/static/vpn/" + time.time() + ".vpn"
Jeremy Mowery8b664f72015-12-04 11:52:16 -070098 if VPNService.get_service_objects().exists():
Jeremy Mowery82760822016-01-08 16:36:22 -070099 self.fields["provider_service"].initial = VPNService.get_service_objects().all()[
100 0]
Jeremy Mowery8b664f72015-12-04 11:52:16 -0700101
102 def save(self, commit=True):
103 self.instance.creator = self.cleaned_data.get("creator")
Jeremy Mowery4be1fb02015-12-07 21:40:36 -0700104 self.instance.server_key = self.cleaned_data.get("server_key")
Jeremy Mowerybd2ed3a2016-01-05 16:52:43 -0700105 self.instance.server_address = self.cleaned_data.get("server_address")
106 self.instance.client_address = self.cleaned_data.get("client_address")
Jeremy Mowery4a23e7d2016-01-06 15:16:33 -0700107 self.instance.is_persistent = self.cleaned_data.get('is_persistent')
Jeremy Mowerye02d4b62016-01-10 15:21:52 -0700108 self.instance.file_name = self.cleaned_data.get('file_name')
Jeremy Mowery82760822016-01-08 16:36:22 -0700109 self.instance.can_view_subnet = self.cleaned_data.get(
110 'can_view_subnet')
Jeremy Mowery8b664f72015-12-04 11:52:16 -0700111 return super(VPNTenantForm, self).save(commit=commit)
112
Jeremy Mowerye02d4b62016-01-10 15:21:52 -0700113
Jeremy Mowerye1260de2015-12-06 23:10:49 -0700114 def generate_VPN_key(self):
Jeremy Moweryc951d5b2016-01-08 17:07:46 -0700115 """str: Generates a VPN key using the openvpn command."""
Jeremy Mowery82760822016-01-08 16:36:22 -0700116 proc = Popen("openvpn --genkey --secret /dev/stdout",
117 shell=True, stdout=PIPE)
Jeremy Mowery8e91e872015-12-06 22:43:19 -0700118 (stdout, stderr) = proc.communicate()
119 return stdout
120
Jeremy Mowery8b664f72015-12-04 11:52:16 -0700121 class Meta:
122 model = VPNTenant
123
Jeremy Mowery82760822016-01-08 16:36:22 -0700124
Jeremy Mowery8b664f72015-12-04 11:52:16 -0700125class VPNTenantAdmin(ReadOnlyAwareAdmin):
126 verbose_name = "VPN Tenant Admin"
127 list_display = ('id', 'backend_status_icon', 'instance')
128 list_display_links = ('id', 'backend_status_icon', 'instance')
129 fieldsets = [(None, {'fields': ['backend_status_text', 'kind',
130 'provider_service', 'instance', 'creator',
Jeremy Moweryc2e8f162016-01-10 20:36:51 -0700131 'server_key', 'file_name', 'script_link',
Jeremy Mowery4a23e7d2016-01-06 15:16:33 -0700132 'server_address', 'client_address',
133 'is_persistent', 'can_view_subnet'],
Jeremy Mowery8b664f72015-12-04 11:52:16 -0700134 'classes': ['suit-tab suit-tab-general']})]
Jeremy Moweryc2e8f162016-01-10 20:36:51 -0700135 readonly_fields = ('backend_status_text', 'instance', 'script_link')
Jeremy Mowery8b664f72015-12-04 11:52:16 -0700136 form = VPNTenantForm
137
138 suit_form_tabs = (('general', 'Details'),)
139
140 def queryset(self, request):
141 return VPNTenant.get_tenant_objects_by_user(request.user)
142
143# Associate the admin forms with the models.
144admin.site.register(VPNService, VPNServiceAdmin)
145admin.site.register(VPNTenant, VPNTenantAdmin)