Jeremy Mowery | da2c313 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 1 | import time |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 2 | from subprocess import PIPE, Popen |
| 3 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 4 | from core.admin import ReadOnlyAwareAdmin, SliceInline |
| 5 | from core.middleware import get_request |
| 6 | from core.models import User |
| 7 | from django import forms |
| 8 | from django.contrib import admin |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 9 | from services.vpn.models import VPN_KIND, VPNService, VPNTenant |
| 10 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 11 | |
| 12 | class VPNServiceAdmin(ReadOnlyAwareAdmin): |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 13 | """Defines the admin for the VPNService.""" |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 14 | 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 Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 44 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 45 | class VPNTenantForm(forms.ModelForm): |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 46 | """The form used to create and edit a VPNTenant. |
| 47 | |
| 48 | Attributes: |
| 49 | creator (forms.ModelChoiceField): The XOS user that created this tenant. |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 50 | client_conf (forms.CharField): The readonly configuration used on the client to connect to this Tenant. |
| 51 | server_address (forms.GenericIPAddressField): The ip address on the VPN of this Tenant. |
| 52 | client_address (forms.GenericIPAddressField): The ip address on the VPN of the client. |
| 53 | is_persistent (forms.BooleanField): Determines if this Tenant keeps this connection alive through failures. |
| 54 | can_view_subnet (forms.BooleanField): Determins if this Tenant makes it's subnet available to the client. |
Jeremy Mowery | fb7df5a | 2016-01-08 17:07:46 -0700 | [diff] [blame] | 55 | |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 56 | """ |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 57 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 58 | server_address = forms.GenericIPAddressField( |
| 59 | protocol='IPv4', required=True) |
| 60 | client_address = forms.GenericIPAddressField( |
| 61 | protocol='IPv4', required=True) |
Jeremy Mowery | 59da8f6 | 2016-01-06 15:46:02 -0700 | [diff] [blame] | 62 | is_persistent = forms.BooleanField(required=False) |
| 63 | can_view_subnet = forms.BooleanField(required=False) |
Jeremy Mowery | 2e6ac15 | 2016-01-11 17:47:05 -0700 | [diff] [blame] | 64 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 65 | |
| 66 | def __init__(self, *args, **kwargs): |
| 67 | super(VPNTenantForm, self).__init__(*args, **kwargs) |
| 68 | self.fields['kind'].widget.attrs['readonly'] = True |
Jeremy Mowery | b143dfc | 2016-01-11 22:17:06 -0700 | [diff] [blame] | 69 | # self.fields['script_name'].widget.attrs['readonly'] = True |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 70 | self.fields[ |
| 71 | 'provider_service'].queryset = VPNService.get_service_objects().all() |
| 72 | |
| 73 | self.fields['kind'].initial = VPN_KIND |
| 74 | |
| 75 | if self.instance: |
| 76 | self.fields['creator'].initial = self.instance.creator |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 77 | self.fields[ |
| 78 | 'server_address'].initial = self.instance.server_address |
| 79 | self.fields[ |
| 80 | 'client_address'].initial = self.instance.client_address |
Jeremy Mowery | 6853b2e | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 81 | self.fields['is_persistent'].initial = self.instance.is_persistent |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 82 | self.fields[ |
| 83 | 'can_view_subnet'].initial = self.instance.can_view_subnet |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 84 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 85 | if (not self.instance) or (not self.instance.pk): |
| 86 | self.fields['creator'].initial = get_request().user |
Jeremy Mowery | d2da28f | 2016-01-05 16:52:43 -0700 | [diff] [blame] | 87 | self.fields['server_address'].initial = "10.8.0.1" |
| 88 | self.fields['client_address'].initial = "10.8.0.2" |
Jeremy Mowery | 6853b2e | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 89 | self.fields['is_persistent'].initial = True |
| 90 | self.fields['can_view_subnet'].initial = False |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 91 | if VPNService.get_service_objects().exists(): |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 92 | self.fields["provider_service"].initial = VPNService.get_service_objects().all()[ |
| 93 | 0] |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 94 | |
| 95 | def save(self, commit=True): |
| 96 | self.instance.creator = self.cleaned_data.get("creator") |
Jeremy Mowery | d2da28f | 2016-01-05 16:52:43 -0700 | [diff] [blame] | 97 | self.instance.server_address = self.cleaned_data.get("server_address") |
| 98 | self.instance.client_address = self.cleaned_data.get("client_address") |
Jeremy Mowery | 6853b2e | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 99 | self.instance.is_persistent = self.cleaned_data.get('is_persistent') |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 100 | self.instance.can_view_subnet = self.cleaned_data.get( |
| 101 | 'can_view_subnet') |
Jeremy Mowery | 70912c9 | 2016-01-24 17:40:44 -0700 | [diff] [blame^] | 102 | |
| 103 | if self.instance.script_name == None: |
| 104 | self.instance.script_name = str(time.time()) + ".vpn" |
| 105 | |
| 106 | if self.instance.server_key == None: |
| 107 | self.instance.server_key = self.generate_VPN_key() |
| 108 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 109 | return super(VPNTenantForm, self).save(commit=commit) |
| 110 | |
Jeremy Mowery | 4fb886b | 2015-12-06 23:10:49 -0700 | [diff] [blame] | 111 | def generate_VPN_key(self): |
Jeremy Mowery | fb7df5a | 2016-01-08 17:07:46 -0700 | [diff] [blame] | 112 | """str: Generates a VPN key using the openvpn command.""" |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 113 | proc = Popen("openvpn --genkey --secret /dev/stdout", |
| 114 | shell=True, stdout=PIPE) |
Jeremy Mowery | ef2af0a | 2015-12-06 22:43:19 -0700 | [diff] [blame] | 115 | (stdout, stderr) = proc.communicate() |
| 116 | return stdout |
| 117 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 118 | class Meta: |
| 119 | model = VPNTenant |
| 120 | |
Jeremy Mowery | 4cf74cf | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 121 | |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 122 | class VPNTenantAdmin(ReadOnlyAwareAdmin): |
| 123 | verbose_name = "VPN Tenant Admin" |
| 124 | list_display = ('id', 'backend_status_icon', 'instance') |
| 125 | list_display_links = ('id', 'backend_status_icon', 'instance') |
| 126 | fieldsets = [(None, {'fields': ['backend_status_text', 'kind', |
| 127 | 'provider_service', 'instance', 'creator', |
Jeremy Mowery | 6853b2e | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 128 | 'server_address', 'client_address', |
| 129 | 'is_persistent', 'can_view_subnet'], |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 130 | 'classes': ['suit-tab suit-tab-general']})] |
Jeremy Mowery | 9990a86 | 2016-01-11 17:23:44 -0700 | [diff] [blame] | 131 | readonly_fields = ('backend_status_text', 'instance') |
Jeremy Mowery | 3cd52b7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 132 | form = VPNTenantForm |
| 133 | |
| 134 | suit_form_tabs = (('general', 'Details'),) |
| 135 | |
| 136 | def queryset(self, request): |
| 137 | return VPNTenant.get_tenant_objects_by_user(request.user) |
| 138 | |
| 139 | # Associate the admin forms with the models. |
| 140 | admin.site.register(VPNService, VPNServiceAdmin) |
| 141 | admin.site.register(VPNTenant, VPNTenantAdmin) |