2019-03-20 21:42:47 +00:00
|
|
|
"""passbook app_gw models"""
|
2019-03-21 15:21:51 +00:00
|
|
|
import re
|
|
|
|
|
2019-03-20 21:42:47 +00:00
|
|
|
from django.contrib.postgres.fields import ArrayField
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from passbook.core.models import Policy, PropertyMapping, Provider
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationGatewayProvider(Provider):
|
|
|
|
"""Virtual server which proxies requests to any hostname in server_name to upstream"""
|
|
|
|
|
|
|
|
server_name = ArrayField(models.TextField())
|
|
|
|
upstream = ArrayField(models.TextField())
|
|
|
|
enabled = models.BooleanField(default=True)
|
|
|
|
|
2019-04-11 13:29:01 +00:00
|
|
|
authentication_header = models.TextField(default='X-Remote-User', blank=True)
|
2019-03-20 21:42:47 +00:00
|
|
|
default_content_type = models.TextField(default='application/octet-stream')
|
|
|
|
upstream_ssl_verification = models.BooleanField(default=True)
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.providers.app_gw.forms.ApplicationGatewayProviderForm'
|
2019-03-20 21:42:47 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""since this model has no name property, return a joined list of server_names as name"""
|
|
|
|
return ', '.join(self.server_name)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Application Gateway %s" % ', '.join(self.server_name)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _('Application Gateway Provider')
|
|
|
|
verbose_name_plural = _('Application Gateway Providers')
|
|
|
|
|
|
|
|
|
|
|
|
class RewriteRule(PropertyMapping):
|
|
|
|
"""Rewrite requests matching `match` with `replacement`, if all polcies in `conditions` apply"""
|
|
|
|
|
|
|
|
REDIRECT_INTERNAL = 'internal'
|
|
|
|
REDIRECT_PERMANENT = 301
|
|
|
|
REDIRECT_FOUND = 302
|
|
|
|
|
|
|
|
REDIRECTS = (
|
|
|
|
(REDIRECT_INTERNAL, _('Internal')),
|
|
|
|
(REDIRECT_PERMANENT, _('Moved Permanently')),
|
|
|
|
(REDIRECT_FOUND, _('Found')),
|
|
|
|
)
|
|
|
|
|
|
|
|
match = models.TextField()
|
|
|
|
halt = models.BooleanField(default=False)
|
2019-03-21 15:21:51 +00:00
|
|
|
conditions = models.ManyToManyField(Policy, blank=True)
|
2019-03-20 21:42:47 +00:00
|
|
|
replacement = models.TextField() # python formatted strings, use {match.1}
|
|
|
|
redirect = models.CharField(max_length=50, choices=REDIRECTS)
|
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
form = 'passbook.providers.app_gw.forms.RewriteRuleForm'
|
2019-03-21 15:21:51 +00:00
|
|
|
|
|
|
|
_matcher = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def compiled_matcher(self):
|
|
|
|
"""Cache the compiled regex in memory"""
|
|
|
|
if not self._matcher:
|
|
|
|
self._matcher = re.compile(self.match)
|
|
|
|
return self._matcher
|
|
|
|
|
2019-03-20 21:42:47 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "Rewrite Rule %s" % self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
|
|
verbose_name = _('Rewrite Rule')
|
|
|
|
verbose_name_plural = _('Rewrite Rules')
|