models.py 21.3 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
# django senders need their arguments
# pylint: disable=unused-argument

import json
5
import time
Lukas Burgey's avatar
Lukas Burgey committed
6
import logging
7
import pika
8
from pika.exceptions import ConnectionClosed
Lukas Burgey's avatar
Lukas Burgey committed
9
10
import requests
from requests.auth import HTTPBasicAuth
Lukas Burgey's avatar
Lukas Burgey committed
11
from django.conf import settings
Lukas Burgey's avatar
Lukas Burgey committed
12
from django.contrib.auth.models import AbstractUser, Group
13
from django.core.cache import cache
Lukas Burgey's avatar
Lukas Burgey committed
14
from django.db import models
15
from django.db.models.signals import post_save, pre_delete
Lukas Burgey's avatar
Lukas Burgey committed
16
from django.dispatch import receiver
17
from django_mysql.models import JSONField
Lukas Burgey's avatar
Lukas Burgey committed
18
from rest_framework.authtoken.models import Token
Lukas Burgey's avatar
Lukas Burgey committed
19
from .auth.v1.models import OIDCConfig
Lukas Burgey's avatar
Lukas Burgey committed
20

Lukas Burgey's avatar
Lukas Burgey committed
21
LOGGER = logging.getLogger(__name__)
22
RECONNECT_TIMEOUT = 3
23

Lukas Burgey's avatar
Lukas Burgey committed
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
# singleton for simple configs
# https://steelkiwi.com/blog/practical-application-singleton-design-pattern/
class SingletonModel(models.Model):
    class Meta:
        abstract = True

    def set_cache(self):
        cache.set(self.__class__.__name__, self)

    # pylint: disable=invalid-name, arguments-differ
    def save(self, *args, **kwargs):
        self.pk = 1
        super(SingletonModel, self).save(*args, **kwargs)
        self.set_cache()

    @classmethod
    def load(cls):
        if cache.get(cls.__name__) is None:
            obj, created = cls.objects.get_or_create(pk=1)
            if not created:
                obj.set_cache()
        return cache.get(cls.__name__)


Lukas Burgey's avatar
Lukas Burgey committed
49
50
# clients are registerred at rabbitmq, when they are assigned to a site
# (because we only then know what services they provide)
51
class RabbitMQInstance(SingletonModel):
Lukas Burgey's avatar
Lukas Burgey committed
52
    host = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
53
54
55
        max_length=150,
        default='localhost',
    )
Lukas Burgey's avatar
Lukas Burgey committed
56
57
58
59
    vhost = models.CharField(
        max_length=150,
        default='%2f',
    )
Lukas Burgey's avatar
Lukas Burgey committed
60
    exchange = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
61
62
63
        max_length=150,
        default='deployments',
    )
Lukas Burgey's avatar
Lukas Burgey committed
64
    port = models.IntegerField(
Lukas Burgey's avatar
Lukas Burgey committed
65
        default=15672,
Lukas Burgey's avatar
Lukas Burgey committed
66
    )
Lukas Burgey's avatar
Lukas Burgey committed
67
    path = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
68
69
70
        max_length=150,
        default='api',
    )
Lukas Burgey's avatar
Lukas Burgey committed
71
    username = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
72
73
74
        max_length=150,
        default='guest',
    )
Lukas Burgey's avatar
Lukas Burgey committed
75
    password = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
76
77
78
        max_length=150,
        default='guest',
    )
Lukas Burgey's avatar
Lukas Burgey committed
79
80
    is_active = models.BooleanField(
        default=True,
Lukas Burgey's avatar
Lukas Burgey committed
81
    )
Lukas Burgey's avatar
Lukas Burgey committed
82
83
84
85

    def __str__(self):
        return self.host

Lukas Burgey's avatar
Lukas Burgey committed
86
    def msg(self, msg):
Lukas Burgey's avatar
Lukas Burgey committed
87
        return '[RabbitMQ:{}] {}'.format(self.host, msg)
Lukas Burgey's avatar
Lukas Burgey committed
88
89
90
91

    @property
    def auth(self):
        return HTTPBasicAuth(
Lukas Burgey's avatar
Lukas Burgey committed
92
            self.username,
Lukas Burgey's avatar
Lukas Burgey committed
93
            self.password,
Lukas Burgey's avatar
Lukas Burgey committed
94
        )
Lukas Burgey's avatar
Lukas Burgey committed
95
96

    @property
Lukas Burgey's avatar
Lukas Burgey committed
97
98
99
100
101
    def _connection_parameters(self):
        return pika.ConnectionParameters(
            host=self.host,
            ssl=True,
        )
102
103
104

    @property
    def connection(self):
Lukas Burgey's avatar
Lukas Burgey committed
105
        LOGGER.debug(self.msg('connection opened'))
Lukas Burgey's avatar
Lukas Burgey committed
106
107
108
        return pika.BlockingConnection(
            self._connection_parameters,
        )
109
110
111

    @property
    def channel(self):
Lukas Burgey's avatar
Lukas Burgey committed
112
113
114
115
116
117
118
119
        rabbitmq_channel = self.connection.channel()
        rabbitmq_channel.exchange_declare(
            exchange=self.exchange,
            durable=True,
            auto_delete=False,
            exchange_type='topic',
        )
        rabbitmq_channel.confirm_delivery()
Lukas Burgey's avatar
Lukas Burgey committed
120
        LOGGER.debug(self.msg('channel opened'))
Lukas Burgey's avatar
Lukas Burgey committed
121
122
123
124
125

        return rabbitmq_channel

    def _get_api_uri(self, path):
        return 'http://{}:{}/{}/{}'.format(
Lukas Burgey's avatar
Lukas Burgey committed
126
127
128
            self.host,
            self.port,
            self.path,
Lukas Burgey's avatar
Lukas Burgey committed
129
            path,
Lukas Burgey's avatar
Lukas Burgey committed
130
131
        )

Lukas Burgey's avatar
Lukas Burgey committed
132
    def _rest_get(self, api_path):
Lukas Burgey's avatar
Lukas Burgey committed
133
        req = requests.get(
Lukas Burgey's avatar
Lukas Burgey committed
134
            self._get_api_uri(api_path),
Lukas Burgey's avatar
Lukas Burgey committed
135
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
136
137
        req.raise_for_status()
        return req.json()
Lukas Burgey's avatar
Lukas Burgey committed
138
139
140

    # send a rest call with path and data to the rest interface of
    # the rabbitmq instance
Lukas Burgey's avatar
Lukas Burgey committed
141
    def _rest_put(self, api_path, data):
Lukas Burgey's avatar
Lukas Burgey committed
142
        req = requests.put(
Lukas Burgey's avatar
Lukas Burgey committed
143
            self._get_api_uri(api_path),
Lukas Burgey's avatar
Lukas Burgey committed
144
145
            json=data,
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
146
147
        req.raise_for_status()
        return req
Lukas Burgey's avatar
Lukas Burgey committed
148

Lukas Burgey's avatar
Lukas Burgey committed
149
    def _rest_del(self, api_path):
Lukas Burgey's avatar
Lukas Burgey committed
150
        req = requests.delete(
Lukas Burgey's avatar
Lukas Burgey committed
151
            self._get_api_uri(api_path),
Lukas Burgey's avatar
Lukas Burgey committed
152
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
153
154
        req.raise_for_status()
        return req
Lukas Burgey's avatar
Lukas Burgey committed
155

Lukas Burgey's avatar
Lukas Burgey committed
156
    def _set_topic_permissions(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
157
158
        username = site.client.username
        path = 'topic-permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
159
160
161
            self.vhost,
            username,
        )
Lukas Burgey's avatar
Lukas Burgey committed
162
163
164
165

        # set permissions for the correct topics
        # we construct a regex to match the services of the site
        services = ''
Lukas Burgey's avatar
Lukas Burgey committed
166
        omit_bar = True
Lukas Burgey's avatar
Lukas Burgey committed
167
168
        for service in site.services.all():
            prefix = '|'
Lukas Burgey's avatar
Lukas Burgey committed
169
            if omit_bar:
Lukas Burgey's avatar
Lukas Burgey committed
170
                prefix = ''
Lukas Burgey's avatar
Lukas Burgey committed
171
                omit_bar = False
Lukas Burgey's avatar
Lukas Burgey committed
172
173
174
175

            services = services + prefix + service.name

        set_topic_permission_data = {
Lukas Burgey's avatar
Lukas Burgey committed
176
177
            'exchange': self.exchange,
            'write': '^$',
Lukas Burgey's avatar
Lukas Burgey committed
178
            'read': r'^service\.({})$'.format(services),
Lukas Burgey's avatar
Lukas Burgey committed
179
        }
Lukas Burgey's avatar
Lukas Burgey committed
180

Lukas Burgey's avatar
Lukas Burgey committed
181
        return self._rest_put(path, set_topic_permission_data)
Lukas Burgey's avatar
Lukas Burgey committed
182
183

    # set permissions for the user
Lukas Burgey's avatar
Lukas Burgey committed
184
    def _set_permissions(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
185
186
        username = site.client.username
        path = 'permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
187
188
189
            self.vhost,
            username,
        )
Lukas Burgey's avatar
Lukas Burgey committed
190
        permission = r'^(amq\.gen.*|{})'.format(self.exchange)
Lukas Burgey's avatar
Lukas Burgey committed
191
        set_permission_data = {
Lukas Burgey's avatar
Lukas Burgey committed
192
193
194
195
            'configure': permission,
            'write': permission,
            'read': permission,
        }
Lukas Burgey's avatar
Lukas Burgey committed
196

Lukas Burgey's avatar
Lukas Burgey committed
197
        return self._rest_put(path, set_permission_data)
Lukas Burgey's avatar
Lukas Burgey committed
198
199

    # create user at the rabbitmq instance
Lukas Burgey's avatar
Lukas Burgey committed
200
    def _create_user(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
201
202
203
204
        username = site.client.username
        path = 'users/{}/'.format(username)

        user_creation_data = {
Lukas Burgey's avatar
Lukas Burgey committed
205
206
207
            'password': str(site.client.auth_token.key),
            'tags': '',
        }
Lukas Burgey's avatar
Lukas Burgey committed
208

Lukas Burgey's avatar
Lukas Burgey committed
209
        return self._rest_put(path, user_creation_data)
Lukas Burgey's avatar
Lukas Burgey committed
210
211

    # delete user at the rabbitmq instance
Lukas Burgey's avatar
Lukas Burgey committed
212
    def _delete_user(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
213
214
215
        username = site.client.username
        path = 'users/{}/'.format(username)

Lukas Burgey's avatar
Lukas Burgey committed
216
217
218
        return self._rest_del(path)

    def _disconnect(self):
Lukas Burgey's avatar
Lukas Burgey committed
219
        LOGGER.debug(self.msg('closing connection'))
Lukas Burgey's avatar
Lukas Burgey committed
220
        self.connection.close()
Lukas Burgey's avatar
Lukas Burgey committed
221
222
223
224

    # PUBLIC API

    def register_site(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
225
226
227
        self._create_user(site)
        self._set_permissions(site)
        self._set_topic_permissions(site)
Lukas Burgey's avatar
Lukas Burgey committed
228
        LOGGER.info(self.msg('registered {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
229
230

    def update_site(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
231
        self._set_topic_permissions(site)
Lukas Burgey's avatar
Lukas Burgey committed
232
        LOGGER.info(self.msg('updated permissions for {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
233
234

    def deregister_site(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
235
        self._delete_user(site)
Lukas Burgey's avatar
Lukas Burgey committed
236
        LOGGER.info(self.msg('deregistered {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
237
238

    def is_client_connected(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
239
        connections = self._rest_get("connections/")
Lukas Burgey's avatar
Lukas Burgey committed
240
241
242
243
244
245
246
247
248
249
        clients_for_site = [c
                            for c in connections
                            if c['user'] == site.client.username]
        return len(clients_for_site) > 0

    def online_clients(self, service):
        return [site
                for site in service.site.all()
                if self.is_client_connected(site)]

250
    def publish_by_service(self, service, msg):
251
252
253
254
255
256
257
258
259
260
261
        while True:
            try:
                return self.channel.basic_publish(
                    exchange=self.exchange,
                    routing_key=service.routing_key,
                    body=msg,
                    properties=pika.BasicProperties(
                        delivery_mode=1,
                    ),
                )
            except ConnectionClosed as exception:
Lukas Burgey's avatar
Lukas Burgey committed
262
                LOGGER.info(self.msg('ConnectionClosed: {}'.format(exception)))
263
                time.sleep(RECONNECT_TIMEOUT)
Lukas Burgey's avatar
Lukas Burgey committed
264
265


266
267
268
269
def user_info_default():
    return {}


Lukas Burgey's avatar
Lukas Burgey committed
270
class User(AbstractUser):
271
    TYPE_CHOICES = (
Lukas Burgey's avatar
Lukas Burgey committed
272
273
274
275
        ('apiclient', 'API-Client'),
        ('oidcuser', 'OIDC User'),
        ('admin', 'Admin'),
    )
276
    user_type = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
277
278
279
280
        max_length=20,
        choices=TYPE_CHOICES,
        default='oidcuser',
    )
281
282
283
284
    sub = models.CharField(
        max_length=150,
        blank=True,
        null=True,
285
        editable=False,
286
287
288
289
290
291
292
293
294
295
296
297
    )
    password = models.CharField(
        max_length=150,
        blank=True,
        null=True,
    )
    # the real state of the user
    # (self.is_active is the supposed state of the user)
    _is_active = models.BooleanField(
        default=True,
        editable=False,
    )
Lukas Burgey's avatar
Lukas Burgey committed
298
299
300
301
302
    # the idp which authenticated the user
    idp = models.ForeignKey(
        OIDCConfig,
        related_name='users',
        on_delete=models.CASCADE,
303
304
305
        blank=True,
        null=True,
        editable=False,
Lukas Burgey's avatar
Lukas Burgey committed
306
    )
307
308
309
310
    userinfo = JSONField(
        default=user_info_default,
        null=True,
        blank=True,
311
        editable=False,
312
    )
Lukas Burgey's avatar
Lukas Burgey committed
313

Lukas Burgey's avatar
Lukas Burgey committed
314
    # we hide deleted keys here
315
    # the full list of ssh keys is self._ssh_keys
Lukas Burgey's avatar
Lukas Burgey committed
316
317
318
319
    @property
    def ssh_keys(self):
        return self._ssh_keys.filter(deleted=False)

320
321
322
323
    @property
    def is_active_at_clients(self):
        return self._is_active

324
325
326
    def __str__(self):
        if self.user_type == 'admin':
            return 'ADMIN {}'.format(self.username)
Lukas Burgey's avatar
Lukas Burgey committed
327
        elif self.user_type == 'oidcuser':
328
329
330
            if not self.is_active:
                return 'DEACTIVATED USER {}'.format(self.username)
            return 'USER {}'.format(self.username)
Lukas Burgey's avatar
Lukas Burgey committed
331
        elif self.user_type == 'apiclient':
332
333
334
335
336
            try:
                return 'APICLIENT {}@{}'.format(self.username, self.site)
            except:
                return 'APICLIENT {}'.format(self.username)

Lukas Burgey's avatar
Lukas Burgey committed
337
338
        else:
            raise Exception()
Lukas Burgey's avatar
Lukas Burgey committed
339

Lukas Burgey's avatar
Lukas Burgey committed
340
    def msg(self, msg):
341
342
343
344
345
346
347
        return '[{}] {}'.format(self, msg)

    # oidcuser: withdraw and delete all credentials and delete the user
    def remove(self):
        if self.user_type == 'oidcuser':
            self.deactivate()

Lukas Burgey's avatar
Lukas Burgey committed
348
            # FIXME: deleting the user brings problems:
349
350
            # the deletion cascades down to DeploymentTask and DeploymentTaskItem
            # but these need to be conserved so all clients withdrawals can be tracked
Lukas Burgey's avatar
Lukas Burgey committed
351
            LOGGER.info(self.msg('Deleting'))
352
353
354
            self.delete()

    def activate(self):
355
        if self._is_active:
Lukas Burgey's avatar
Lukas Burgey committed
356
            LOGGER.error(self.msg('already activated'))
357
358
359
360
            return

        if self.user_type == 'oidcuser':
            self.is_active = True
361
            self._is_active = True
362
363
364
365
366
            self.save()

            for dep in self.deployments.all():
                dep.activate()

Lukas Burgey's avatar
Lukas Burgey committed
367
            LOGGER.info(self.msg('activated'))
368
369
370

    # oidcuser: withdraw all credentials
    def deactivate(self):
371
        if not self._is_active:
Lukas Burgey's avatar
Lukas Burgey committed
372
            LOGGER.error(self.msg('already deactivated'))
373
374
375
376
            return

        if self.user_type == 'oidcuser':
            self.is_active = False
377
            self._is_active = False
378
379
380
381
382
            self.save()

            for dep in self.deployments.all():
                dep.deactivate()

Lukas Burgey's avatar
Lukas Burgey committed
383
            LOGGER.info(self.msg('deactivated'))
384
385


386
387
388
389
390
391
392
393
394
395
    @classmethod
    def construct_from_user_info(cls, user_info, idp):
        LOGGER.debug('User: constructing from %s', user_info)
        return cls(
            sub=user_info.get('sub', ''),
            first_name=user_info.get('given_name', ''),
            last_name=user_info.get('family_name', ''),
            email=user_info.get('email', ''),
            username=user_info.get('email', ''),
            idp=idp,
396
            userinfo=user_info,
397
        )
Lukas Burgey's avatar
Lukas Burgey committed
398
399
400


class Site(models.Model):
401
    client = models.OneToOneField(
Lukas Burgey's avatar
Lukas Burgey committed
402
403
404
        User,
        related_name='site',
    )
Lukas Burgey's avatar
Lukas Burgey committed
405
406
407
408
409
410
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)

    def __str__(self):
        return self.name

411
412
413
414
415
416
    # tasks which are still to be executed on this site
    @property
    def tasks(self):
        return [item.task
                for item
                in self.task_items.all()]
Lukas Burgey's avatar
Lukas Burgey committed
417

Lukas Burgey's avatar
Lukas Burgey committed
418
419
420
421

class Service(models.Model):
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)
422
    site = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
423
424
        Site,
        related_name='services')
Lukas Burgey's avatar
Lukas Burgey committed
425
    groups = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
426
427
428
        Group,
        related_name='services',
        blank=True)
Lukas Burgey's avatar
Lukas Burgey committed
429

430
431
432
433
    @property
    def routing_key(self):
        return 'service.{}'.format(self.name)

Lukas Burgey's avatar
Lukas Burgey committed
434
    def __str__(self):
Lukas Burgey's avatar
Lukas Burgey committed
435
        return self.name
Lukas Burgey's avatar
Lukas Burgey committed
436
437
438


class SSHPublicKey(models.Model):
Lukas Burgey's avatar
Lukas Burgey committed
439
440
441
442
443
444
    name = models.CharField(
        max_length=150,
    )
    key = models.TextField(
        max_length=1000
    )
Lukas Burgey's avatar
Lukas Burgey committed
445
    # hidden field at the user
Lukas Burgey's avatar
Lukas Burgey committed
446
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
447
448
449
        User,
        related_name='_ssh_keys',
    )
Lukas Burgey's avatar
Lukas Burgey committed
450

Lukas Burgey's avatar
Lukas Burgey committed
451
452
    # has the user triggered the deletion of this key
    deleted = models.BooleanField(
Lukas Burgey's avatar
Lukas Burgey committed
453
454
455
        default=False,
        editable=False,
    )
456

Lukas Burgey's avatar
Lukas Burgey committed
457
    def msg(self, msg):
Lukas Burgey's avatar
Lukas Burgey committed
458
        return '[SSHPublicKey:{}] {}'.format(self, msg)
459

460
461
462
    # does not directly delete the key if the key is deployed or withdrawn
    # somewhere
    # the receiver 'delete_withdrawn_ssh_key' does the actual deletion
463
    def delete_key(self):
Lukas Burgey's avatar
Lukas Burgey committed
464
        if (not self.tasks.exists() and not self.deployments.exists()):
Lukas Burgey's avatar
Lukas Burgey committed
465
            LOGGER.info(self.msg('Direct deletion of key'))
466
467
468
            self.delete()
            return

Lukas Burgey's avatar
Lukas Burgey committed
469
        LOGGER.info(self.msg('Deletion of key started'))
470
471
472
        self.deleted = True
        self.save()

Lukas Burgey's avatar
Lukas Burgey committed
473
        # delete implies withdrawing the key from all clients
474
475
476
        for deployment in self.deployments.all():
            deployment.withdraw_key(self)

Lukas Burgey's avatar
Lukas Burgey committed
477
478
    # when a key is withdrawn by a client we try to finally delete it
    def try_final_deletion(self):
Lukas Burgey's avatar
Lukas Burgey committed
479
        if (self.deleted and not self.tasks.exists()):
Lukas Burgey's avatar
Lukas Burgey committed
480
            LOGGER.info(self.msg(
Lukas Burgey's avatar
Lukas Burgey committed
481
                'All clients have withdrawn this key. Final deletion'))
Lukas Burgey's avatar
Lukas Burgey committed
482
483
484
            self.delete()
            return

Lukas Burgey's avatar
Lukas Burgey committed
485
    def __str__(self):
Lukas Burgey's avatar
Lukas Burgey committed
486
487
        if self.deleted:
            return "DELETED: {}".format(self.name)
Lukas Burgey's avatar
Lukas Burgey committed
488
489
490
        return self.name


491
# Deployment describes the credential state per user as it is supposed to be
492
493
494
495
#
# (exception: if is_active=False the ssh_keys contain the keys to be deployed
# if the deployment is reactivated)
#
496
497
# DeploymentTask is what is sent to the clients via rabbitmq
# The DeploymentTaskItem track the acknowledgements from the clients
Lukas Burgey's avatar
Lukas Burgey committed
498
499
class Deployment(models.Model):
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
500
501
502
503
        User,
        related_name='deployments',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
504
    service = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
505
506
507
508
        Service,
        related_name='deployments',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
509
    ssh_keys = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
510
511
512
513
        SSHPublicKey,
        related_name='deployments',
        blank=True,
    )
514
    ssh_keys_to_withdraw = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
515
516
517
518
        SSHPublicKey,
        related_name='withdrawn_deployments',
        blank=True,
    )
519
    is_active = models.BooleanField(
Lukas Burgey's avatar
Lukas Burgey committed
520
521
        default=True,
    )
522

523
524
525
    @property
    def withdrawals(self):
        return self.tasks.filter(action='withdraw')
Lukas Burgey's avatar
Lukas Burgey committed
526

527
528
529
    @property
    def deploys(self):
        return self.tasks.filter(action='deploy')
Lukas Burgey's avatar
Lukas Burgey committed
530

531
532
    def __str__(self):
        return '{}:{}'.format(self.service, self.user)
533

Lukas Burgey's avatar
Lukas Burgey committed
534
    def msg(self, msg):
535
        return '[Deployment:{}] {}'.format(self, msg)
536

537
538
539
    # deploy credentials which were deployed prior to deactivation
    def activate(self):
        if self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
540
            LOGGER.error(self.msg('already active'))
541
542
            return

Lukas Burgey's avatar
Lukas Burgey committed
543
        LOGGER.debug(self.msg(str(self.ssh_keys.all())))
544
545
546
547
548
        for key in self.ssh_keys.all():
            self._deploy_key(key)

        self.is_active = True
        self.save()
Lukas Burgey's avatar
Lukas Burgey committed
549
        LOGGER.info(self.msg('activated'))
550
551
552
553

    # withdraw all credentials
    def deactivate(self):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
554
            LOGGER.error(self.msg('already deactivated'))
555
556
557
            return

        self.is_active = False
558
        self.save()
559

560
561
562
        for key in self.ssh_keys.all():
            self._withdraw_key(key)

Lukas Burgey's avatar
Lukas Burgey committed
563
        LOGGER.info(self.msg('deactivated'))
564
565
566
567

    # only deploy the key
    def _deploy_key(self, key):
        # delete outstanding tasks which are made obsolete by this task
568
        for withdrawal in self.withdrawals.filter(key=key):
Lukas Burgey's avatar
Lukas Burgey committed
569
            LOGGER.debug(withdrawal.msg('now obsolete'))
Lukas Burgey's avatar
Lukas Burgey committed
570
            withdrawal.delete()
571
572
573

        # generate task
        task = DeploymentTask(
Lukas Burgey's avatar
Lukas Burgey committed
574
575
576
577
            action='deploy',
            deployment=self,
            key=key,
        )
578
        task.save()
Lukas Burgey's avatar
Lukas Burgey committed
579
        LOGGER.debug(task.msg('generated'))
580
581
582
583

        # generate task items
        for site in self.service.site.all():
            deploy = DeploymentTaskItem(
Lukas Burgey's avatar
Lukas Burgey committed
584
585
586
                task=task,
                site=site,
            )
587
            deploy.save()
Lukas Burgey's avatar
Lukas Burgey committed
588
            LOGGER.debug(deploy.msg('generated'))
589
590
591

        # publish the task
        task.publish()
592

593
594
    def _withdraw_key(self, key):
        # delete outstanding tasks which are made obsolete by this task
595
        for deploy in self.deploys.filter(key=key):
Lukas Burgey's avatar
Lukas Burgey committed
596
            LOGGER.debug(deploy.msg("now obsolete"))
Lukas Burgey's avatar
Lukas Burgey committed
597
            deploy.delete()
Lukas Burgey's avatar
Lukas Burgey committed
598

599
600
        # generate task
        task = DeploymentTask(
Lukas Burgey's avatar
Lukas Burgey committed
601
602
603
604
            action='withdraw',
            deployment=self,
            key=key,
        )
605
        task.save()
Lukas Burgey's avatar
Lukas Burgey committed
606
        LOGGER.debug(task.msg('generated'))
Lukas Burgey's avatar
Lukas Burgey committed
607

608
609
610
        # generate task items
        for site in self.service.site.all():
            withdrawal = DeploymentTaskItem(
Lukas Burgey's avatar
Lukas Burgey committed
611
612
613
                task=task,
                site=site,
            )
614
            withdrawal.save()
Lukas Burgey's avatar
Lukas Burgey committed
615
            LOGGER.debug(withdrawal.msg('generated'))
616

617
618
        # publish the task
        task.publish()
Lukas Burgey's avatar
Lukas Burgey committed
619

620
621
622
    # deploy key and track changes in the key lists
    def deploy_key(self, key):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
623
            LOGGER.error(self.msg('cannot deploy while deactivated'))
624
625
626
627
628
629
630
631
632
633
634
635
636
            raise Exception('deployment deactivated')

        self.ssh_keys.add(key)

        if key in self.ssh_keys_to_withdraw.all():
            self.ssh_keys_to_withdraw.remove(key)
        self.save()

        self._deploy_key(key)

    # withdraw key and track changes in the key lists
    def withdraw_key(self, key):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
637
            LOGGER.error(self.msg('cannot withdraw while deactivated'))
638
639
640
641
642
643
644
645
646
647
            raise Exception('deployment deactivated')

        self.ssh_keys.remove(key)

        # keys which are to be withdrawn by the clients
        self.ssh_keys_to_withdraw.add(key)
        self.save()

        self._withdraw_key(key)

Lukas Burgey's avatar
Lukas Burgey committed
648

649
650
class DeploymentTask(models.Model):
    ACTION_CHOICES = (
Lukas Burgey's avatar
Lukas Burgey committed
651
652
653
        ('deploy', 'deploy'),
        ('withdraw', 'withdraw'),
    )
654
    action = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
655
656
657
        max_length=10,
        choices=ACTION_CHOICES,
    )
658
    key = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
659
660
661
662
        SSHPublicKey,
        related_name='tasks',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
663
    deployment = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
664
665
666
667
        Deployment,
        related_name='tasks',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
668
669
670
671
672
673
674
675
676

    @property
    def user(self):
        return self.deployment.user

    @property
    def service(self):
        return self.deployment.service

677
    def __str__(self):
678
        return "{}:{}:{} - {}#{}".format(
Lukas Burgey's avatar
Lukas Burgey committed
679
680
681
682
            self.deployment.service,
            self.deployment.user,
            self.key,
            self.action,
683
            self.id,
Lukas Burgey's avatar
Lukas Burgey committed
684
        )
685

Lukas Burgey's avatar
Lukas Burgey committed
686
    def msg(self, msg):
Lukas Burgey's avatar
Lukas Burgey committed
687
        return '[DeploymentTask:{}] {}'.format(self, msg)
688
689

    def publish(self):
690
        # FIXME mitigating circular dependencies here
691
692
693
        from .clientapi.serializers import DeploymentTaskSerializer
        msg = json.dumps(DeploymentTaskSerializer(self).data)

694
        RabbitMQInstance.load().publish_by_service(
Lukas Burgey's avatar
Lukas Burgey committed
695
696
697
            self.service,
            msg,
        )
698
699
700

    # the client acked the receipt and execution of the task for his site
    def item_finished(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
701
        item = self.task_items.get(site=site)
Lukas Burgey's avatar
Lukas Burgey committed
702
        LOGGER.debug(item.msg('done'))
Lukas Burgey's avatar
Lukas Burgey committed
703
        item.delete()
704
705
706
707
708
709

        if not self.task_items.exists():
            self.finished()

    # maintenance after all task items are done
    def finished(self):
Lukas Burgey's avatar
Lukas Burgey committed
710
        LOGGER.info(self.msg('done'))
Lukas Burgey's avatar
Lukas Burgey committed
711
        self.delete()
712
713
714
715
716
717
718
719

        # check if this was the final withdraw in a key deletion
        if self.action == 'withdraw':
            self.key.try_final_deletion()


class DeploymentTaskItem(models.Model):
    task = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
720
721
722
723
        DeploymentTask,
        related_name='task_items',
        on_delete=models.CASCADE,
    )
724
    site = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
725
726
727
728
        Site,
        related_name='task_items',
        on_delete=models.CASCADE,
    )
729

Lukas Burgey's avatar
Lukas Burgey committed
730
    def __str__(self):
731
        return "{}@{}#{}".format(
Lukas Burgey's avatar
Lukas Burgey committed
732
733
            self.task,
            self.site,
734
            self.id,
Lukas Burgey's avatar
Lukas Burgey committed
735
        )
736

Lukas Burgey's avatar
Lukas Burgey committed
737
    def msg(self, msg):
Lukas Burgey's avatar
Lukas Burgey committed
738
        return '[DeploymentTaskItem:{}] {}'.format(self, msg)
739

Lukas Burgey's avatar
Lukas Burgey committed
740

741
742
743
#
# RECEIVERS
#
Lukas Burgey's avatar
Lukas Burgey committed
744

745
746
747
748
#@receiver(post_save, sender=settings.AUTH_USER_MODEL)
#def create_auth_token(sender, instance=None, created=False, **kwargs):
#    if instance.user_type == 'apiclient' and created:
#        Token.objects.create(user=instance)
Lukas Burgey's avatar
Lukas Burgey committed
749
750


751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#@receiver(post_save, sender=Site)
#def register_at_rabbitmq(sender, instance=None, created=False, **kwargs):
#    if not created:
#        return
#
#    RabbitMQInstance.load().register_site(instance)
#
#
#@receiver(pre_delete, sender=Site)
#def deregister_at_rabbitmq(sender, instance=None, **kwargs):
#    RabbitMQInstance.load().deregister_site(instance)
#
#
#@receiver(post_save, sender=Service)
#def update_at_rabbitmq(sender, instance=None, **kwargs):
#    for site in instance.site.all():
#        RabbitMQInstance.load().update_site(site)
768
769
770
771
772
773
774


@receiver(post_save, sender=User)
def deactivate_user(sender, instance=None, created=False, **kwargs):
    if created:
        return

775
    if not instance.is_active and instance.is_active_at_clients:
776
777
778
779
780
781
782
783
        instance.deactivate()


@receiver(post_save, sender=User)
def activate_user(sender, instance=None, created=False, **kwargs):
    if created:
        return

784
    if instance.is_active and not instance.is_active_at_clients:
785
        instance.activate()