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

Add publish by site logic

parent 99fba34f
......@@ -131,7 +131,8 @@ def _resource_authorized_apiclient(request):
and name.startswith('amq.gen-')
) or (
resource == 'exchange'
and name == RabbitMQInstance.load().exchange
# TODO
#and name in RabbitMQInstance.load().exchanges
and not 'write' in permission
)
......@@ -209,13 +210,20 @@ def topic_endpoint(request):
user = _apiclient_get(request)
if user:
routing_key = request.POST.get('routing_key', '')
if routing_key.startswith('service.'):
match = re.search('service.(.+)', routing_key)
if match:
service_name = match.group(1)
for service in user.site.services.all():
if service_name == service.name:
return ALLOW
if name == 'deployments':
if routing_key.startswith('service.'):
match = re.search('service.(.+)', routing_key)
if match:
service_name = match.group(1)
for service in user.site.services.all():
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)
return DENY
......@@ -55,4 +55,4 @@ class SiteSerializer(serializers.Serializer):
class RabbitMQInstanceSerializer(serializers.ModelSerializer):
class Meta:
model = models.RabbitMQInstance
fields = ['vhost', 'exchange']
fields = ['vhost']
......@@ -34,6 +34,7 @@ class ConfigurationView(views.APIView):
'rabbitmq_config': RabbitMQInstanceSerializer(
RabbitMQInstance.load(),
).data,
'site': request.user.site.name,
}
return Response(response)
......
......@@ -44,6 +44,9 @@ class SingletonModel(models.Model):
obj.set_cache()
return cache.get(cls.__name__)
def exchanges_default():
return []
# clients are registerred at rabbitmq, when they are assigned to a site
# (because we only then know what services they provide)
......@@ -56,9 +59,10 @@ class RabbitMQInstance(SingletonModel):
max_length=150,
default='%2f',
)
exchange = models.CharField(
max_length=150,
default='deployments',
exchanges = JSONField(
default=exchanges_default,
null=True,
blank=True,
)
port = models.IntegerField(
default=15672,
......@@ -87,12 +91,17 @@ class RabbitMQInstance(SingletonModel):
def _init_exchanges(self, channel):
channel.exchange_declare(
exchange=self.exchange,
exchange='deployments',
durable=True,
auto_delete=False,
exchange_type='topic',
)
channel.exchange_declare(
exchange='sites',
durable=True,
auto_delete=False,
exchange_type='topic',
)
# TODO put in config
channel.exchange_declare(
exchange='update',
durable=True,
......@@ -154,11 +163,18 @@ class RabbitMQInstance(SingletonModel):
# PUBLIC API
def publish_by_service(self, service, msg):
self._publish(
self.exchange,
'deployments',
service.routing_key,
msg,
)
def publish_by_site(self, site, msg):
self._publish(
'sites',
site.name,
msg,
)
def publish_to_webpage(self, user, msg):
# noise
# LOGGER.debug('Signalling webpage of user %s', user)
......@@ -920,6 +936,22 @@ class DeploymentTaskItem(models.Model):
self.questionnaire = answers
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):
return "{}@{}#{}".format(
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