aboutsummaryrefslogtreecommitdiff
path: root/rpkid/rpki/gui/app/models.py
blob: 0ed01aa8e3eb1123d001179cd255b99a0c35ba5e (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright (C) 2010  SPARTA, Inc. dba Cobham Analytic Solutions
# Copyright (C) 2012  SPARTA, Inc. a Parsons Company
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND SPARTA DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL SPARTA BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

__version__ = '$Id$'

from django.db import models

import rpki.resource_set
import rpki.exceptions
import rpki.irdb.models
import rpki.gui.models
import rpki.gui.routeview.models
from south.modelsinspector import add_introspection_rules


class TelephoneField(models.CharField):
    def __init__(self, **kwargs):
        if 'max_length' not in kwargs:
            kwargs['max_length'] = 40
        models.CharField.__init__(self, **kwargs)

add_introspection_rules([], ['^rpki\.gui\.app\.models\.TelephoneField'])


class Parent(rpki.irdb.models.Parent):
    """proxy model for irdb Parent"""

    def __unicode__(self):
        return u"%s's parent %s" % (self.issuer.handle, self.handle)

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.parent_detail', [str(self.pk)])

    class Meta:
        proxy = True
        verbose_name = 'Parent'


class Child(rpki.irdb.models.Child):
    """proxy model for irdb Child"""

    def __unicode__(self):
        return u"%s's child %s" % (self.issuer.handle, self.handle)

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.child_detail', [str(self.pk)])

    class Meta:
        proxy = True
        verbose_name = 'Child'
        verbose_name_plural = 'Children'


class ChildASN(rpki.irdb.models.ChildASN):
    """Proxy model for irdb ChildASN."""

    class Meta:
        proxy = True

    def __unicode__(self):
        return u'AS%s' % self.as_resource_range()


class ChildNet(rpki.irdb.models.ChildNet):
    """Proxy model for irdb ChildNet."""

    class Meta:
        proxy = True

    def __unicode__(self):
        return u'%s' % self.as_resource_range()


class Conf(rpki.irdb.models.ResourceHolderCA):
    """This is the center of the universe, also known as a place to
    have a handle on a resource-holding entity.  It's the <self>
    in the rpkid schema.

    """
    @property
    def parents(self):
        """Simulates irdb.models.Parent.objects, but returns app.models.Parent
        proxy objects.

        """
        return Parent.objects.filter(issuer=self)

    @property
    def children(self):
        """Simulates irdb.models.Child.objects, but returns app.models.Child
        proxy objects.

        """
        return Child.objects.filter(issuer=self)

    @property
    def ghostbusters(self):
        return GhostbusterRequest.objects.filter(issuer=self)

    @property
    def repositories(self):
        return Repository.objects.filter(issuer=self)

    @property
    def roas(self):
        return ROARequest.objects.filter(issuer=self)

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.user_detail', [str(self.pk)])

    class Meta:
        proxy = True


class ResourceCert(models.Model):
    """Represents a resource certificate.

    This model is used to cache the output of <list_received_resources/>.

    """

    # Handle to which this cert was issued
    conf = models.ForeignKey(Conf, related_name='certs')

    # The parent that issued the cert.  This field is marked null=True because
    # the root has no parent
    parent = models.ForeignKey(Parent, related_name='certs', null=True)

    # certificate validity period
    not_before = models.DateTimeField()
    not_after = models.DateTimeField()

    # Locator for this object.  Used to look up the validation status, expiry
    # of ancestor certs in cacheview
    uri = models.CharField(max_length=255)

    def __unicode__(self):
        if self.parent:
            return u"%s's cert from %s" % (self.conf.handle,
                                           self.parent.handle)
        else:
            return u"%s's root cert" % self.conf.handle


class ResourceRangeAddressV4(rpki.gui.models.PrefixV4):
    cert = models.ForeignKey(ResourceCert, related_name='address_ranges')


class ResourceRangeAddressV6(rpki.gui.models.PrefixV6):
    cert = models.ForeignKey(ResourceCert, related_name='address_ranges_v6')


class ResourceRangeAS(rpki.gui.models.ASN):
    cert = models.ForeignKey(ResourceCert, related_name='asn_ranges')


class ROARequest(rpki.irdb.models.ROARequest):
    class Meta:
        proxy = True

    def __unicode__(self):
        return u"%s's ROA request for AS%d" % (self.issuer.handle, self.asn)

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.roa_detail', [str(self.pk)])


class ROARequestPrefix(rpki.irdb.models.ROARequestPrefix):
    class Meta:
        proxy = True
        verbose_name = 'ROA'

    def __unicode__(self):
        return u'ROA request prefix %s for asn %d' % (str(self.as_roa_prefix()),
                                                      self.roa_request.asn)


class GhostbusterRequest(rpki.irdb.models.GhostbusterRequest):
    """
    Stores the information require to fill out a vCard entry to
    populate a ghostbusters record.

    This model is inherited from the irdb GhostBusterRequest model so
    that the broken out fields can be included for ease of editing.
    """

    full_name = models.CharField(max_length=40)

    # components of the vCard N type
    family_name = models.CharField(max_length=20)
    given_name = models.CharField(max_length=20)
    additional_name = models.CharField(max_length=20, blank=True, null=True)
    honorific_prefix = models.CharField(max_length=10, blank=True, null=True)
    honorific_suffix = models.CharField(max_length=10, blank=True, null=True)

    email_address = models.EmailField(blank=True, null=True)
    organization = models.CharField(blank=True, null=True, max_length=255)
    telephone = TelephoneField(blank=True, null=True)

    # elements of the ADR type
    box = models.CharField(verbose_name='P.O. Box', blank=True, null=True,
                           max_length=40)
    extended = models.CharField(blank=True, null=True, max_length=255)
    street = models.CharField(blank=True, null=True, max_length=255)
    city = models.CharField(blank=True, null=True, max_length=40)
    region = models.CharField(blank=True, null=True, max_length=40,
                              help_text='state or province')
    code = models.CharField(verbose_name='Postal Code', blank=True, null=True,
                            max_length=40)
    country = models.CharField(blank=True, null=True, max_length=40)

    def __unicode__(self):
        return u"%s's GBR: %s" % (self.issuer.handle, self.full_name)

    @models.permalink
    def get_absolute_url(self):
        return ('gbr-detail', [str(self.pk)])

    class Meta:
        ordering = ('family_name', 'given_name')
        verbose_name = 'Ghostbuster'


class Timestamp(models.Model):
    """Model to hold metadata about the collection of external data.

    This model is a hash table mapping a timestamp name to the
    timestamp value.  All timestamps values are in UTC.

    The utility function rpki.gui.app.timestmap.update(name) should be used to
    set timestamps rather than updating this model directly."""

    name = models.CharField(max_length=30, primary_key=True)
    ts = models.DateTimeField(null=False)

    def __unicode__(self):
        return '%s: %s' % (self.name, self.ts)


class Repository(rpki.irdb.models.Repository):
    class Meta:
        proxy = True
        verbose_name = 'Repository'
        verbose_name_plural = 'Repositories'

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.repository_detail', [str(self.pk)])

    def __unicode__(self):
        return "%s's repository %s" % (self.issuer.handle, self.handle)


class Client(rpki.irdb.models.Client):
    "Proxy model for pubd clients."

    class Meta:
        proxy = True
        verbose_name = 'Client'

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.client_detail', [str(self.pk)])

    def __unicode__(self):
        return self.handle


class RouteOrigin(rpki.gui.routeview.models.RouteOrigin):
    class Meta:
        proxy = True

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.route_detail', [str(self.pk)])


class RouteOriginV6(rpki.gui.routeview.models.RouteOriginV6):
    class Meta:
        proxy = True

    @models.permalink
    def get_absolute_url(self):
        return ('rpki.gui.app.views.route_detail', [str(self.pk)])