Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 1 | import time |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 2 | from subprocess import PIPE, Popen |
| 3 | |
Jeremy Mowery | 8b664f7 | 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 | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 9 | from services.vpn.models import VPN_KIND, VPNService, VPNTenant |
| 10 | |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 11 | |
| 12 | class VPNServiceAdmin(ReadOnlyAwareAdmin): |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 13 | """Defines the admin for the VPNService.""" |
Jeremy Mowery | 8b664f7 | 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 | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 44 | |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 45 | class VPNTenantForm(forms.ModelForm): |
Jeremy Mowery | 8276082 | 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. |
| 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 Mowery | c951d5b | 2016-01-08 17:07:46 -0700 | [diff] [blame] | 56 | |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 57 | """ |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 58 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 59 | server_key = forms.CharField(required=True, widget=forms.Textarea) |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 60 | server_address = forms.GenericIPAddressField( |
| 61 | protocol='IPv4', required=True) |
| 62 | client_address = forms.GenericIPAddressField( |
| 63 | protocol='IPv4', required=True) |
Jeremy Mowery | b3fcf1b | 2016-01-06 15:46:02 -0700 | [diff] [blame] | 64 | is_persistent = forms.BooleanField(required=False) |
| 65 | can_view_subnet = forms.BooleanField(required=False) |
Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 66 | file_name = forms.CharField(required=True) |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 67 | |
| 68 | def __init__(self, *args, **kwargs): |
| 69 | super(VPNTenantForm, self).__init__(*args, **kwargs) |
| 70 | self.fields['kind'].widget.attrs['readonly'] = True |
Jeremy Mowery | b05f251 | 2015-12-07 20:59:23 -0700 | [diff] [blame] | 71 | self.fields['server_key'].widget.attrs['readonly'] = True |
Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 72 | self.fields['file_name'].widget.attrs['readonly'] = True |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 73 | 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 Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 81 | self.fields[ |
| 82 | 'server_address'].initial = self.instance.server_address |
| 83 | self.fields[ |
| 84 | 'client_address'].initial = self.instance.client_address |
Jeremy Mowery | 4a23e7d | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 85 | self.fields['is_persistent'].initial = self.instance.is_persistent |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 86 | self.fields[ |
| 87 | 'can_view_subnet'].initial = self.instance.can_view_subnet |
Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 88 | self.fields['file_name'].initial = self.instance.file_name |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 89 | |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 90 | if (not self.instance) or (not self.instance.pk): |
| 91 | self.fields['creator'].initial = get_request().user |
Jeremy Mowery | e1260de | 2015-12-06 23:10:49 -0700 | [diff] [blame] | 92 | self.fields['server_key'].initial = self.generate_VPN_key() |
Jeremy Mowery | bd2ed3a | 2016-01-05 16:52:43 -0700 | [diff] [blame] | 93 | self.fields['server_address'].initial = "10.8.0.1" |
| 94 | self.fields['client_address'].initial = "10.8.0.2" |
Jeremy Mowery | 4a23e7d | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 95 | self.fields['is_persistent'].initial = True |
| 96 | self.fields['can_view_subnet'].initial = False |
Jeremy Mowery | 8aac627 | 2016-01-10 19:37:07 -0700 | [diff] [blame] | 97 | self.fields['file_name'].initial = "/static/vpn/" + time.time() + ".vpn" |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 98 | if VPNService.get_service_objects().exists(): |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 99 | self.fields["provider_service"].initial = VPNService.get_service_objects().all()[ |
| 100 | 0] |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 101 | |
| 102 | def save(self, commit=True): |
| 103 | self.instance.creator = self.cleaned_data.get("creator") |
Jeremy Mowery | 4be1fb0 | 2015-12-07 21:40:36 -0700 | [diff] [blame] | 104 | self.instance.server_key = self.cleaned_data.get("server_key") |
Jeremy Mowery | bd2ed3a | 2016-01-05 16:52:43 -0700 | [diff] [blame] | 105 | self.instance.server_address = self.cleaned_data.get("server_address") |
| 106 | self.instance.client_address = self.cleaned_data.get("client_address") |
Jeremy Mowery | 4a23e7d | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 107 | self.instance.is_persistent = self.cleaned_data.get('is_persistent') |
Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 108 | self.instance.file_name = self.cleaned_data.get('file_name') |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 109 | self.instance.can_view_subnet = self.cleaned_data.get( |
| 110 | 'can_view_subnet') |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 111 | return super(VPNTenantForm, self).save(commit=commit) |
| 112 | |
Jeremy Mowery | e02d4b6 | 2016-01-10 15:21:52 -0700 | [diff] [blame] | 113 | |
Jeremy Mowery | e1260de | 2015-12-06 23:10:49 -0700 | [diff] [blame] | 114 | def generate_VPN_key(self): |
Jeremy Mowery | c951d5b | 2016-01-08 17:07:46 -0700 | [diff] [blame] | 115 | """str: Generates a VPN key using the openvpn command.""" |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 116 | proc = Popen("openvpn --genkey --secret /dev/stdout", |
| 117 | shell=True, stdout=PIPE) |
Jeremy Mowery | 8e91e87 | 2015-12-06 22:43:19 -0700 | [diff] [blame] | 118 | (stdout, stderr) = proc.communicate() |
| 119 | return stdout |
| 120 | |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 121 | class Meta: |
| 122 | model = VPNTenant |
| 123 | |
Jeremy Mowery | 8276082 | 2016-01-08 16:36:22 -0700 | [diff] [blame] | 124 | |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 125 | class 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 Mowery | c2e8f16 | 2016-01-10 20:36:51 -0700 | [diff] [blame^] | 131 | 'server_key', 'file_name', 'script_link', |
Jeremy Mowery | 4a23e7d | 2016-01-06 15:16:33 -0700 | [diff] [blame] | 132 | 'server_address', 'client_address', |
| 133 | 'is_persistent', 'can_view_subnet'], |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 134 | 'classes': ['suit-tab suit-tab-general']})] |
Jeremy Mowery | c2e8f16 | 2016-01-10 20:36:51 -0700 | [diff] [blame^] | 135 | readonly_fields = ('backend_status_text', 'instance', 'script_link') |
Jeremy Mowery | 8b664f7 | 2015-12-04 11:52:16 -0700 | [diff] [blame] | 136 | 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. |
| 144 | admin.site.register(VPNService, VPNServiceAdmin) |
| 145 | admin.site.register(VPNTenant, VPNTenantAdmin) |