Commit 179c1bb1 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add publish by site logic

parent 99fba34f
...@@ -131,7 +131,8 @@ def _resource_authorized_apiclient(request): ...@@ -131,7 +131,8 @@ def _resource_authorized_apiclient(request):
and name.startswith('amq.gen-') and name.startswith('amq.gen-')
) or ( ) or (
resource == 'exchange' resource == 'exchange'
and name == RabbitMQInstance.load().exchange # TODO
#and name in RabbitMQInstance.load().exchanges
and not 'write' in permission and not 'write' in permission
) )
...@@ -209,13 +210,20 @@ def topic_endpoint(request): ...@@ -209,13 +210,20 @@ def topic_endpoint(request):
user = _apiclient_get(request) user = _apiclient_get(request)
if user: if user:
routing_key = request.POST.get('routing_key', '') routing_key = request.POST.get('routing_key', '')
if routing_key.startswith('service.'): if name == 'deployments':
match = re.search('service.(.+)', routing_key) if routing_key.startswith('service.'):
if match: match = re.search('service.(.+)', routing_key)
service_name = match.group(1) if match:
for service in user.site.services.all(): service_name = match.group(1)
if service_name == service.name: for service in user.site.services.all():
return ALLOW if service_name == service.name:
return ALLOW
elif name == 'sites':
LOGGER.debug(routing_key)
if routing_key == user.site.name:
return ALLOW
else:
LOGGER.error("Client of site %s tried to access site %s", user.site, routing_key)
LOGGER.error('Authorization check for topic failed for %s', request.POST) LOGGER.error('Authorization check for topic failed for %s', request.POST)
return DENY return DENY
...@@ -55,4 +55,4 @@ class SiteSerializer(serializers.Serializer): ...@@ -55,4 +55,4 @@ class SiteSerializer(serializers.Serializer):
class RabbitMQInstanceSerializer(serializers.ModelSerializer): class RabbitMQInstanceSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = models.RabbitMQInstance model = models.RabbitMQInstance
fields = ['vhost', 'exchange'] fields = ['vhost']
...@@ -34,6 +34,7 @@ class ConfigurationView(views.APIView): ...@@ -34,6 +34,7 @@ class ConfigurationView(views.APIView):
'rabbitmq_config': RabbitMQInstanceSerializer( 'rabbitmq_config': RabbitMQInstanceSerializer(
RabbitMQInstance.load(), RabbitMQInstance.load(),
).data, ).data,
'site': request.user.site.name,
} }
return Response(response) return Response(response)
......
...@@ -44,6 +44,9 @@ class SingletonModel(models.Model): ...@@ -44,6 +44,9 @@ class SingletonModel(models.Model):
obj.set_cache() obj.set_cache()
return cache.get(cls.__name__) return cache.get(cls.__name__)
def exchanges_default():
return []
# clients are registerred at rabbitmq, when they are assigned to a site # clients are registerred at rabbitmq, when they are assigned to a site
# (because we only then know what services they provide) # (because we only then know what services they provide)
...@@ -56,9 +59,10 @@ class RabbitMQInstance(SingletonModel): ...@@ -56,9 +59,10 @@ class RabbitMQInstance(SingletonModel):
max_length=150, max_length=150,
default='%2f', default='%2f',
) )
exchange = models.CharField( exchanges = JSONField(
max_length=150, default=exchanges_default,
default='deployments', null=True,
blank=True,
) )
port = models.IntegerField( port = models.IntegerField(
default=15672, default=15672,
...@@ -87,12 +91,17 @@ class RabbitMQInstance(SingletonModel): ...@@ -87,12 +91,17 @@ class RabbitMQInstance(SingletonModel):
def _init_exchanges(self, channel): def _init_exchanges(self, channel):
channel.exchange_declare( channel.exchange_declare(
exchange=self.exchange, exchange='deployments',
durable=True,
auto_delete=False,
exchange_type='topic',
)
channel.exchange_declare(
exchange='sites',
durable=True, durable=True,
auto_delete=False, auto_delete=False,
exchange_type='topic', exchange_type='topic',
) )
# TODO put in config
channel.exchange_declare( channel.exchange_declare(
exchange='update', exchange='update',
durable=True, durable=True,
...@@ -154,11 +163,18 @@ class RabbitMQInstance(SingletonModel): ...@@ -154,11 +163,18 @@ class RabbitMQInstance(SingletonModel):
# PUBLIC API # PUBLIC API
def publish_by_service(self, service, msg): def publish_by_service(self, service, msg):
self._publish( self._publish(
self.exchange, 'deployments',
service.routing_key, service.routing_key,
msg, msg,
) )
def publish_by_site(self, site, msg):
self._publish(
'sites',
site.name,
msg,
)
def publish_to_webpage(self, user, msg): def publish_to_webpage(self, user, msg):
# noise # noise
# LOGGER.debug('Signalling webpage of user %s', user) # LOGGER.debug('Signalling webpage of user %s', user)
...@@ -920,6 +936,22 @@ class DeploymentTaskItem(models.Model): ...@@ -920,6 +936,22 @@ class DeploymentTaskItem(models.Model):
self.questionnaire = answers self.questionnaire = answers
self.save() self.save()
# publish the task item
self.publish()
# only used when we got a questionnaire_answered
def publish(self):
# mitigating circular dependencies here
from .clientapi.serializers import DeploymentTaskSerializer
data = DeploymentTaskSerializer(self.task).data
data['questionnaire'] = self.questionnaire
RabbitMQInstance.load().publish_by_site(
self.site,
json.dumps(data),
)
def __str__(self): def __str__(self):
return "{}@{}#{}".format( return "{}@{}#{}".format(
self.task, self.task,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment