0018_update_cds_llo_certs.py 1.37 KB
Newer Older
Tanner Prestegard's avatar
Tanner Prestegard committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-12-14 15:55
from __future__ import unicode_literals

from django.db import migrations

ACCOUNT_NAME = 'cds_llo'
NEW_CERT = '/DC=org/DC=cilogon/C=US/O=LIGO/OU=Robots/CN=cds-outbound.ligo-la.caltech.edu/CN=exttrig/CN=Keith Thorne/CN=UID:keith.thorne.robot'
OLD_CERTS = [
    '/DC=org/DC=ligo/O=LIGO/OU=Services/CN=exttrig/cds-outbound.ligo-la.caltech.edu',
]


def update_certs(apps, schema_editor):
    User = apps.get_model('auth', 'User')

    # Get user
    user = User.objects.get(username=ACCOUNT_NAME)

    # Create new certificate
    user.x509cert_set.create(subject=NEW_CERT)

    # Delete old certificates
    for subj in OLD_CERTS:
        old_cert = user.x509cert_set.get(subject=subj)
        old_cert.delete()


def revert_certs(apps, schema_editor):
    User = apps.get_model('auth', 'User')
    X509Cert = apps.get_model('ligoauth', 'X509Cert')

    # Delete new certificate
    new_cert = X509Cert.objects.get(subject=NEW_CERT)
    new_cert.delete()

    # Get user
    user = User.objects.get(username=ACCOUNT_NAME)

    # Create old certificates
    for subj in OLD_CERTS:
        user.x509cert_set.create(subject=subj)


class Migration(migrations.Migration):

    dependencies = [
        ('ligoauth', '0017_update_cds_lho_certs'),
    ]

    operations = [
        migrations.RunPython(update_certs, revert_certs),
    ]