|
| 1 | +# Generated by Django 4.0.5 on 2022-06-24 06:31 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | +from ..migrations import get_swapped_model |
| 6 | + |
| 7 | + |
| 8 | +def get_wireguard_and_vxlan_wireguard_templates(apps): |
| 9 | + template_model = get_swapped_model(apps, 'config', 'Template') |
| 10 | + return template_model.objects.filter(type='vpn', vpn__backend__contains='Wireguard') |
| 11 | + |
| 12 | + |
| 13 | +def allow_multiple_wireguard_tunneling(apps, schema_editor): |
| 14 | + templates = get_wireguard_and_vxlan_wireguard_templates(apps).iterator() |
| 15 | + for template in templates: |
| 16 | + config = template.config |
| 17 | + interfaces = config['interfaces'] |
| 18 | + vpn_id = template.vpn.pk.hex |
| 19 | + changed = False |
| 20 | + for interface in interfaces: |
| 21 | + interface_type = interface.get('type', None) |
| 22 | + private_key = interface.get('private_key', None) |
| 23 | + if interface_type != 'wireguard' or not private_key: |
| 24 | + continue |
| 25 | + if private_key not in [ |
| 26 | + '{{private_key}}', |
| 27 | + '{{ private_key }}', |
| 28 | + ]: |
| 29 | + continue |
| 30 | + interface['private_key'] = '{{pvt_key_%s}}' % vpn_id |
| 31 | + changed = True |
| 32 | + if not changed: |
| 33 | + continue |
| 34 | + template.config = config |
| 35 | + template.save(update_fields=['config']) |
| 36 | + |
| 37 | + |
| 38 | +def disallow_multiple_wireguard_tunneling(apps, schema_editor): |
| 39 | + templates = get_wireguard_and_vxlan_wireguard_templates(apps).iterator() |
| 40 | + for template in templates: |
| 41 | + config = template.config |
| 42 | + interfaces = config['interfaces'] |
| 43 | + vpn_id = template.vpn.pk.hex |
| 44 | + changed = False |
| 45 | + for interface in interfaces: |
| 46 | + interface_type = interface.get('type', None) |
| 47 | + private_key = interface.get('private_key', None) |
| 48 | + if interface_type != 'wireguard' or not private_key: |
| 49 | + continue |
| 50 | + if f'pvt_key_{vpn_id}' not in private_key: |
| 51 | + continue |
| 52 | + interface['private_key'] = '{{private_key}}' |
| 53 | + changed = True |
| 54 | + if not changed: |
| 55 | + continue |
| 56 | + template.config = config |
| 57 | + template.save(update_fields=['config']) |
| 58 | + |
| 59 | + |
| 60 | +class Migration(migrations.Migration): |
| 61 | + |
| 62 | + dependencies = [ |
| 63 | + ('config', '0041_default_groups_organizationconfigsettings_permission'), |
| 64 | + ] |
| 65 | + |
| 66 | + operations = [ |
| 67 | + migrations.RunPython( |
| 68 | + code=allow_multiple_wireguard_tunneling, |
| 69 | + reverse_code=disallow_multiple_wireguard_tunneling, |
| 70 | + ), |
| 71 | + ] |
0 commit comments