Commit 54394644 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change the module structure to fix broken imports

Fix some hacked imports

Move the content of the auth module to better locations

Remove deferred import

Rename vo module to vos
parent d0d3c2b9
from django.contrib import admin
from django.contrib.admin import site
from django.contrib.auth.models import Group as AuthGroup
from .. import models
from ..auth.v1.models import OIDCConfig
from ..auth.v1.models.vo import VO, Group, Entitlement, EntitlementNameSpace
from feudal.backend.models import Site, Service
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.deployments import VODeployment, ServiceDeployment, Deployment, DeploymentState, CredentialState
from feudal.backend.models.auth import OIDCConfig
from feudal.backend.models.auth.vos import VO, Group, Entitlement, EntitlementNameSpace
from ..models import User
from ..models.deployments import VODeployment, ServiceDeployment, Deployment, DeploymentState, CredentialState
from .users import CustomUserAdmin
from .vos import GroupAdmin, EntitlementAdmin, VOAdmin
from .deployments import VODeploymentAdmin, ServiceDeploymentAdmin, DeploymentAdmin
from feudal.backend.admin.users import CustomUserAdmin
from feudal.backend.admin.vos import GroupAdmin, EntitlementAdmin, VOAdmin
from feudal.backend.admin.deployments import VODeploymentAdmin, ServiceDeploymentAdmin, DeploymentAdmin
# users
admin.site.register(User, CustomUserAdmin)
admin.site.unregister(AuthGroup)
site.register(User, CustomUserAdmin)
site.unregister(AuthGroup)
# vos
admin.site.register(Group, GroupAdmin)
admin.site.register(Entitlement, EntitlementAdmin)
admin.site.register(VO, VOAdmin)
admin.site.register(EntitlementNameSpace)
admin.site.register(OIDCConfig)
site.register(Group, GroupAdmin)
site.register(Entitlement, EntitlementAdmin)
site.register(VO, VOAdmin)
site.register(EntitlementNameSpace)
site.register(OIDCConfig)
# deployments
admin.site.register(VODeployment, VODeploymentAdmin)
admin.site.register(ServiceDeployment, ServiceDeploymentAdmin)
admin.site.register(Deployment, DeploymentAdmin)
site.register(VODeployment, VODeploymentAdmin)
site.register(ServiceDeployment, ServiceDeploymentAdmin)
site.register(Deployment, DeploymentAdmin)
# other
admin.site.register([
site.register([
DeploymentState,
CredentialState,
models.SSHPublicKey,
models.Site,
models.Service,
SSHPublicKey,
Site,
Service,
])
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
from ..auth.v1.models.vo import Entitlement
from ..models import deployments
from feudal.backend.models.deployments import Deployment, VODeployment, ServiceDeployment
class VODeploymentAdmin(PolymorphicChildModelAdmin):
......@@ -15,6 +13,6 @@ class ServiceDeploymentAdmin(PolymorphicChildModelAdmin):
class DeploymentAdmin(PolymorphicParentModelAdmin):
base_model = Entitlement # Explicitly set here!
base_model = Deployment # Explicitly set here!
show_in_index = True # makes child model admin visible in main admin site
child_models = (deployments.VODeployment, deployments.ServiceDeployment)
child_models = (VODeployment, ServiceDeployment)
......@@ -3,9 +3,8 @@ from django.contrib.admin import SimpleListFilter
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm
from ..auth.v1.models import OIDCConfig
from ..models import User
from feudal.backend.models import User
from feudal.backend.models.auth import OIDCConfig
class TypeFilter(SimpleListFilter):
......
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
from ..auth.v1.models.vo import VO, Group, Entitlement
from feudal.backend.models.auth.vos import VO, Group, Entitlement
class GroupAdmin(PolymorphicChildModelAdmin):
......
# pylint: disable=import-outside-toplevel
import logging
from urllib.error import HTTPError
import jwt
from django.contrib.auth.backends import BaseBackend
from rest_framework.authentication import BaseAuthentication
from .utils import get_session, set_session, del_session, SessionError
from .models import OIDCConfig
from feudal.backend.sessions import get_session, set_session, del_session, SessionError
from feudal.backend.models.auth import OIDCConfig
from feudal.backend.models.users import User
LOGGER = logging.getLogger(__name__)
class OIDCTokenAuthBackend(BaseBackend):
class OIDCTokenAuthBackend:
# get_user is part of the authentication backend API
def get_user(self, user_id):
from feudal.backend.models.users import User
try:
return User.objects.get(
user_type=User.TYPE_CHOICE_USER,
......@@ -103,9 +99,6 @@ class OIDCTokenAuthBackend(BaseBackend):
access_token = request.META['HTTP_AUTHORIZATION']
# this import is used in the except blocks below! Don't move it!
from feudal.backend.models.users import User
try:
idp, userinfo = self._get_idp_userinfo(request, access_token)
......
......@@ -7,7 +7,7 @@ from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import django_mysql.models
import feudal.backend.auth.v1.models
import feudal.backend.models.auth
import feudal.backend.models.brokers
import feudal.backend.models.deployments
import feudal.backend.models.users
......@@ -99,7 +99,7 @@ class Migration(migrations.Migration):
('enabled', models.BooleanField(default=False)),
('name', models.CharField(max_length=200)),
('redirect_uri', models.CharField(default='https://<domain>/backend/auth/v1/callback', max_length=200)),
('scopes', django_mysql.models.JSONField(default=feudal.backend.auth.v1.models.scopes_default, help_text='The scopes we use when requesting user infos')),
('scopes', django_mysql.models.JSONField(default=feudal.backend.models.auth.scopes_default, help_text='The scopes we use when requesting user infos')),
('userinfo_field_groups', models.CharField(blank=True, default='', help_text="The field in the userinfo (served by this IdP) that contains groups of the user.\n Leave blank if you don't want to use groups of this IdP", max_length=200)),
('userinfo_field_entitlements', models.CharField(blank=True, default='', help_text="The field in the userinfo (served by this IdP) that contains entitlements of the user.\n Leave blank if you don't want to use entitlements of this IdP", max_length=200)),
],
......
......@@ -2,7 +2,7 @@
import django.core.validators
from django.db import migrations, models
import feudal.backend.auth.v1.models
import feudal.backend.models.auth
class Migration(migrations.Migration):
......@@ -20,6 +20,6 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='oidcconfig',
name='name',
field=models.CharField(max_length=200, validators=[feudal.backend.auth.v1.models.IDPNameValidator()]),
field=models.CharField(max_length=200, validators=[feudal.backend.models.auth.IDPNameValidator()]),
),
]
......@@ -3,10 +3,8 @@ from logging import getLogger
from django.db import models
# these imports are exports!
from ..auth.v1.models.vo import VO
from .brokers import RabbitMQInstance
from .users import User, SSHPublicKey
from feudal.backend.models.users import User
from feudal.backend.models.auth.vos import VO
LOGGER = getLogger(__name__)
......
......@@ -4,8 +4,8 @@
from rest_framework import serializers
from rest_polymorphic.serializers import PolymorphicSerializer
from .. import OIDCConfig
from ..vo import VO, Group, Entitlement, EntitlementNameSpace
from feudal.backend.models.auth import OIDCConfig
from feudal.backend.models.auth.vos import VO, Group, Entitlement, EntitlementNameSpace
class OIDCConfigSerializer(serializers.ModelSerializer):
......
......@@ -2,7 +2,7 @@
from rest_framework import serializers
from rest_polymorphic.serializers import PolymorphicSerializer
from ..vo import VO, Group, Entitlement, EntitlementNameSpace
from feudal.backend.models.auth.vos import VO, Group, Entitlement, EntitlementNameSpace
class EntitlementNameSpaceSerializer(serializers.ModelSerializer):
......
import logging
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from feudal.backend.auth.v1.models import default_idp
from feudal.backend.models.auth import default_idp
LOGGER = logging.getLogger(__name__)
......
......@@ -5,7 +5,7 @@ import re
from polymorphic.models import PolymorphicModel
from django.db import models
from . import OIDCConfig
from feudal.backend.models.auth import OIDCConfig
LOGGER = logging.getLogger(__name__)
......
# pylint: disable=import-outside-toplevel
from logging import getLogger
from json import dumps
......
......@@ -6,13 +6,14 @@ from django.db import models
from django.db.models import Q
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django_mysql.models import JSONField
from polymorphic.models import PolymorphicModel
from feudal.backend.auth.v1.models.vo import VO
from . import Site, Service, brokers
from .users import User, SSHPublicKey
from feudal.backend.models import Site, Service
from feudal.backend.models.brokers import publish_to_user, RabbitMQInstance
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.auth.vos import VO
LOGGER = getLogger(__name__)
......@@ -136,9 +137,6 @@ class Deployment(PolymorphicModel):
# call when you changed Deployment.state_target
def target_changed(self):
# save this instance to be save ;)
self.save()
LOGGER.debug(self.msg('target_changed: {}'.format(self.state_target)))
self._assure_states_exist()
......@@ -181,7 +179,7 @@ class Deployment(PolymorphicModel):
if settings.DEBUG_PUBLISHING:
LOGGER.debug(self.msg('publish_to_user: {}'.format(self.state_target)))
brokers.publish_to_user(
publish_to_user(
self.user,
{
'deployment': self,
......@@ -406,10 +404,12 @@ class DeploymentState(models.Model):
@property
def state_target(self):
# this instance needs to be saved before accessing a many to many field
if self.pk is not None:
for deployment in self.deployments.all():
if deployment.state_target == DEPLOYED:
return DEPLOYED
if self.pk is None:
self.save()
for deployment in self.deployments.all():
if deployment.state_target == DEPLOYED:
return DEPLOYED
return NOT_DEPLOYED
......@@ -599,7 +599,7 @@ class DeploymentState(models.Model):
if settings.DEBUG_PUBLISHING:
LOGGER.debug(self.msg('publish_to_user'))
brokers.publish_to_user(
publish_to_user(
self.user,
{
'deployment_state': self,
......@@ -624,7 +624,7 @@ class DeploymentState(models.Model):
if settings.DEBUG_PUBLISHING:
LOGGER.debug(self.msg('publish_to_client'))
brokers.RabbitMQInstance.load().publish_deployment_state(self)
RabbitMQInstance.load().publish_deployment_state(self)
# DEPRECATED
# only publish to the client using deployments with our target
......
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer, Serializer, CharField, JSONField
from rest_polymorphic.serializers import PolymorphicSerializer
from feudal.backend.auth.v1.models.serializers import VOSerializer
from feudal.backend.models import Site, Service
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.deployments import CredentialState, DeploymentState, Deployment, VODeployment, ServiceDeployment
from ...models import Site, Service
from ..users import User, SSHPublicKey
from ..deployments import CredentialState, DeploymentState, Deployment, VODeployment, ServiceDeployment
from feudal.backend.models.auth.serializers import VOSerializer
class SSHPublicKeySerializer(serializers.ModelSerializer):
class SSHPublicKeySerializer(ModelSerializer):
class Meta:
model = SSHPublicKey
fields = [
......@@ -19,7 +19,7 @@ class SSHPublicKeySerializer(serializers.ModelSerializer):
]
class SSHPublicKeyRefSerializer(serializers.ModelSerializer):
class SSHPublicKeyRefSerializer(ModelSerializer):
class Meta:
model = SSHPublicKey
fields = [
......@@ -28,7 +28,7 @@ class SSHPublicKeyRefSerializer(serializers.ModelSerializer):
]
class CredentialStateSerializer(serializers.ModelSerializer):
class CredentialStateSerializer(ModelSerializer):
credential = SSHPublicKeyRefSerializer()
class Meta:
......@@ -41,7 +41,7 @@ class CredentialStateSerializer(serializers.ModelSerializer):
]
class SiteSerializer(serializers.ModelSerializer):
class SiteSerializer(ModelSerializer):
class Meta:
model = Site
fields = [
......@@ -51,7 +51,7 @@ class SiteSerializer(serializers.ModelSerializer):
]
class ServiceSerializer(serializers.ModelSerializer):
class ServiceSerializer(ModelSerializer):
site = SiteSerializer()
vos = VOSerializer(many=True)
......@@ -68,7 +68,7 @@ class ServiceSerializer(serializers.ModelSerializer):
]
class CompactServiceSerializer(serializers.ModelSerializer):
class CompactServiceSerializer(ModelSerializer):
class Meta:
model = Service
fields = [
......@@ -78,12 +78,12 @@ class CompactServiceSerializer(serializers.ModelSerializer):
]
class DeploymentStateSerializer(serializers.ModelSerializer):
class DeploymentStateSerializer(ModelSerializer):
# why all the read_onlys: the rest interface exposes these fields, but the must not be changed
answers = serializers.JSONField() # not read_only: it is patched by the user
answers = JSONField() # not read_only: it is patched by the user
credential_states = CredentialStateSerializer(many=True, read_only=True)
credentials = serializers.JSONField(read_only=True)
questionnaire = serializers.JSONField(read_only=True)
credentials = JSONField(read_only=True)
questionnaire = JSONField(read_only=True)
service = CompactServiceSerializer(read_only=True)
site = SiteSerializer(read_only=True)
......@@ -116,7 +116,7 @@ DEPLOYMENT_FIELDS = (
)
class AbstractDeploymentSerializer(serializers.ModelSerializer):
class AbstractDeploymentSerializer(ModelSerializer):
states = DeploymentStateSerializer(many=True)
class Meta:
......@@ -124,7 +124,7 @@ class AbstractDeploymentSerializer(serializers.ModelSerializer):
fields = DEPLOYMENT_FIELDS
class VODeploymentSerializer(serializers.ModelSerializer):
class VODeploymentSerializer(ModelSerializer):
states = DeploymentStateSerializer(many=True)
vo = VOSerializer()
services = ServiceSerializer(many=True)
......@@ -137,7 +137,7 @@ class VODeploymentSerializer(serializers.ModelSerializer):
)
class ServiceDeploymentSerializer(serializers.ModelSerializer):
class ServiceDeploymentSerializer(ModelSerializer):
states = DeploymentStateSerializer(many=True, read_only=True)
service = ServiceSerializer()
......@@ -156,7 +156,7 @@ class DeploymentSerializer(PolymorphicSerializer):
}
class UserStateSerializer(serializers.ModelSerializer):
class UserStateSerializer(ModelSerializer):
deployments = DeploymentSerializer(many=True)
services = ServiceSerializer(many=True)
ssh_keys = SSHPublicKeySerializer(many=True)
......@@ -178,18 +178,18 @@ class UserStateSerializer(serializers.ModelSerializer):
# pylint: disable=abstract-method
class StateSerializer(serializers.Serializer):
msg = serializers.CharField(
class StateSerializer(Serializer):
msg = CharField(
allow_blank=True,
required=False,
)
session = serializers.JSONField(
session = JSONField(
required=False,
)
user = UserStateSerializer()
class UpdateSerializer(serializers.Serializer):
error = serializers.CharField(allow_blank=True, required=False)
class UpdateSerializer(Serializer):
error = CharField(allow_blank=True, required=False)
deployment = DeploymentSerializer(required=False)
deployment_state = DeploymentStateSerializer(required=False)
......@@ -2,15 +2,17 @@
# pylint: disable=abstract-method
from django_mysql.models import JSONField
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer, DictField, ListField
from rest_polymorphic.serializers import PolymorphicSerializer
from feudal.backend.auth.v1.models.serializers.clients import VOSerializer
from feudal.backend.models import Service
from feudal.backend.models.brokers import RabbitMQInstance
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.deployments import Deployment, VODeployment, ServiceDeployment, DeploymentState, CredentialState
from feudal.backend.models.auth.serializers.clients import VOSerializer
from .. import Service, SSHPublicKey, User, RabbitMQInstance, deployments
class ServiceSerializer(serializers.ModelSerializer):
class ServiceSerializer(ModelSerializer):
class Meta:
model = Service
fields = [
......@@ -18,7 +20,7 @@ class ServiceSerializer(serializers.ModelSerializer):
]
class CredentialSerializer(serializers.ModelSerializer):
class CredentialSerializer(ModelSerializer):
class Meta:
model = SSHPublicKey
fields = [
......@@ -28,11 +30,11 @@ class CredentialSerializer(serializers.ModelSerializer):
]
class UserSerializer(serializers.ModelSerializer):
class UserSerializer(ModelSerializer):
vos = VOSerializer(many=True)
userinfo = JSONField()
credentials = serializers.DictField(
child=serializers.ListField(
credentials = DictField(
child=ListField(
child=CredentialSerializer()
)
)
......@@ -47,7 +49,7 @@ class UserSerializer(serializers.ModelSerializer):
]
class RabbitMQInstanceSerializer(serializers.ModelSerializer):
class RabbitMQInstanceSerializer(ModelSerializer):
class Meta:
model = RabbitMQInstance
fields = [
......@@ -62,33 +64,33 @@ DEPLOYMENT_FIELDS = (
)
class AbstractDeploymentSerializer(serializers.ModelSerializer):
class AbstractDeploymentSerializer(ModelSerializer):
user = UserSerializer()
class Meta:
model = deployments.Deployment
model = Deployment
fields = DEPLOYMENT_FIELDS
class VODeploymentSerializer(serializers.ModelSerializer):
class VODeploymentSerializer(ModelSerializer):
user = UserSerializer()
vo = VOSerializer()
services = ServiceSerializer(many=True)
class Meta:
model = deployments.VODeployment
model = VODeployment
fields = DEPLOYMENT_FIELDS + (
'vo',
'services',
)
class ServiceDeploymentSerializer(serializers.ModelSerializer):
class ServiceDeploymentSerializer(ModelSerializer):
user = UserSerializer()
service = ServiceSerializer()
class Meta:
model = deployments.ServiceDeployment
model = ServiceDeployment
fields = DEPLOYMENT_FIELDS + (
'service',
)
......@@ -96,30 +98,30 @@ class ServiceDeploymentSerializer(serializers.ModelSerializer):
class DeploymentSerializer(PolymorphicSerializer):
model_serializer_mapping = {
deployments.Deployment: AbstractDeploymentSerializer,
deployments.VODeployment: VODeploymentSerializer,
deployments.ServiceDeployment: ServiceDeploymentSerializer,
Deployment: AbstractDeploymentSerializer,
VODeployment: VODeploymentSerializer,
ServiceDeployment: ServiceDeploymentSerializer,
}
class CredentialStateSerializer(serializers.ModelSerializer):
class CredentialStateSerializer(ModelSerializer):
class Meta:
model = deployments.CredentialState
model = CredentialState
fields = (
'state',
'state_target',
)
class DeploymentStateSerializer(serializers.ModelSerializer):
class DeploymentStateSerializer(ModelSerializer):
user = UserSerializer()
service = ServiceSerializer()
answers = JSONField()
questionnaire = JSONField()
class Meta:
model = deployments.DeploymentState
model = DeploymentState
fields = (
'id',
'user',
......
......@@ -6,13 +6,12 @@ import logging
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Q
from django.db.models.signals import post_save
from django.dispatch import receiver
from django_mysql.models import JSONField