Пример #1
0
 def get(self, id):
     lab = Lab.fetchone(id=id)
     if lab is None:
         return jsonify(message="Lab doesn't existed"), 410
     if lab.status == 'inactive':
         return jsonify({
             'id': lab.id,
             'name': lab.name,
             'status': lab.status
         })
     else:
         slices = []
         for sl in Slice.fetchall(lab_id=lab.id):
             user = User.fetchone(id=sl.user_id)
             slices.append({
                 'id': sl.id,
                 'name': sl.name,
                 'status': sl.status,
                 'username': user.fullname
             })
         return jsonify({
             'id': lab.id,
             'name': lab.name,
             'status': lab.status,
             'slices': slices,
             'errors': lab.error_msgs
         })
Пример #2
0
 def get(self, id):
     user = User.fetchone(id=id)
     userinfo = UserInfo.fetchone(user_id=id)
     return jsonify({
         'id': user.id,
         'fullname': user.fullname,
         'email': user.email,
         'permission_groups': userinfo.permission_groups.value
     })
Пример #3
0
def create_root(email, password, fullname):
    hash = argon2.hash(password)
    new_root = User(fullname=fullname, email=email, hash=hash, is_root=True)
    try:
        new_root.save()
        root = User.fetchone(email=email)
        root_info = UserInfo(
            user_id=root.id,
            permission_groups=[group.name for group in all_permission_groups])
        root_info.save()
    except UniqueViolatedError:
        print('Duplicated root user')
Пример #4
0
    def patch(self, id):
        user = User.fetchone(id=id)
        userinfo = UserInfo.fetchone(user_id=id)
        user.email = request.get_json()['email']
        user.fullname = request.get_json()['fullname']
        password = request.get_json()['password']
        if password is not None and len(password) > 0:
            user.hash = argon2.hash(request.get_json()['password'])

        userinfo.permission_groups = request.get_json()['permissionGroups']
        try:
            user.save()
            userinfo.save()
        except UniqueViolatedError:
            # TODO: throw UniqueViolatedError in .save() method
            return jsonify(errors=["Duplicated email address"]), 409

        return jsonify(message='ok')
Пример #5
0
def post_token():
    """Check email/password then return a token. Actually, it's the 'login' function"""
    email = request.get_json()['email']
    password = request.get_json()['password']
    user = User.fetchone(email=email)
    if user is None:
        return jsonify(error='email address does not exist'), 401

    hash = user.hash
    if argon2.verify(password, hash):
        g.user = user
        token = secrets.token_urlsafe(32)
        value = {
            'id': user.id,
            'email': email,
            'token': token,
            'is_root': user.is_root.value
        }
        redis_token_db.set(email, json.dumps(value), ex=ONE_DAY)
        return jsonify(value)
    else:
        return jsonify(error='email password mismatch'), 401
Пример #6
0
def _extract_configurations(lab_id, slice: Slice, instance: Dict[str, Any], topo: Dict[str, Any]) \
        -> Tuple[List[Dict[str, Any]], str]:
    """extract configurations of an instance base on the topology
    the extracted configurations is a dict of configuration name ('name') and and rendering parameters ('params')
    rendering parameters can be None"""

    lab = Lab.fetchone(id=lab_id)
    configurations = []

    # configurations.append({"name": "staticroute", "params": _extract_static_route(instance, topo)})
    configurations.append({"name": "add-local-host", "params": {}})

    if instance.get('type') == "Router":
        """ Get the number of interfaces """
        interfaces_count = sum(1 for link in topo['links']
                               if link['target']['gid'] == instance['gid'])
        configurations.append({
            "name": "shorewall",
            "params": {
                "interfaces_count": interfaces_count
            }
        })

    password = None

    for conf in instance.get('configurations', []):
        params: Dict[str, Any] = {}
        if conf == "Enable password authentication" or conf == "noVNC":
            if password is None:
                password = randomword(8)
            params['password'] = password
        elif conf == "grr-client":
            user = User.fetchone(id=slice.user_id)
            params['labels'] = [lab.name, user.email, instance['name']]

        configurations.append({"name": conf, "params": params})

    return configurations, password
Пример #7
0
 def delete(self, id):
     user = User.fetchone(id=id)
     user.delete()
     return jsonify(message='ok')
Пример #8
0
def delete_user(email):
    user = User.fetchone(email=email)
    if user is not None:
        user.delete()
Пример #9
0
    def get(self, id):
        sl = Slice.fetchone(id=id)

        # only user or the lab creator can access
        if sl.user_id != g.user['id']:
            lab = Lab.fetchone(id=sl.lab_id)
            if lab.owner_id != g.user['id']:
                return jsonify(
                    message="Access denied to this slice resource"), 403

        # get networks
        with Select(NetworkNode, 'slice_id=%s and status!=%s') as select:
            select.execute((sl.id, 'inactive'))
            networks = [{
                'id': n.id,
                'name': n.name,
                'cidr': n.cidr,
                'status': n.status,
                'x': n.x,
                'y': n.y,
                'type': 'NetworkNode'
            } for n in select.fetchall()]

        instances = []
        links = []

        # get instances
        with Select(
                Instance,
                'instances.slice_id=%s and instances.status!=%s') as select:
            select.execute((sl.id, 'inactive'))
            results = select.fetchall()
            for res in results:
                instance = res
                instances.append({
                    'id': instance.id,
                    'name': instance.name,
                    'public_ip': instance.public_ip,
                    'status': instance.status,
                    'password': instance.password,
                    'x': instance.x,
                    'y': instance.y,
                    'configurations': instance.configurations.
                    value,  # TODO: remove .value here after implement it in postgrespy
                    'type': 'Instance'
                })

                for link in instance.links:
                    links.append({
                        'gid': link['gid'],
                        'ip': link['ip'],
                        'network': link['network'],
                        'target': link['target'],
                        'type': 'NetworkLink'
                    })

        # get routers
        routers = []
        with Select(Router,
                    'routers.slice_id=%s and routers.status!=%s') as select:
            select.execute((sl.id, 'inactive'))
            results = select.fetchall()
            for res in results:
                router = res
                routers.append({
                    'id': router.id,
                    'name': router.name,
                    'public_ip': router.public_ip,
                    'status': router.status,
                    'password': router.password,
                    'x': router.x,
                    'y': router.y,
                    'configurations': router.configurations.
                    value,  # TODO: remove .value here after implement it in postgrespy
                    'type': 'Router'
                })

                for link in router.links:
                    links.append({
                        'gid': link['gid'],
                        'ip': link['ip'],
                        'network': link['network'],
                        'target': link['target'],
                        'type': 'NetworkLink'
                    })
        user = User.fetchone(id=sl.user_id)
        return jsonify({
            'status': sl.status,
            'name': sl.name,
            'username': user.fullname,
            'networks': networks,
            'instances': instances,
            'routers': routers,
            'links': links
        })