def check_auth(username, password):
    """This function is called to check if a username /
    password combination is valid.
    """
    if 'config' in get_config():
        if 'user' in get_config()['config'] and 'pwd' in get_config()['config']:
            return username == get_config()['config']['user'] and password == get_config()['config']['pwd']
    return False
def login():
    """ Login the User with a referral code from the github oauth process"""
    session['session_code'] = request.args.get('code')
    if _request_access_token() and _load_user_data():
        logger.info("User " + session['user_data']['login'] + " logged in")
        if request.referrer is not None and 'github' not in request.referrer:
            origin = origin_from_referrer(request.referrer)
            return redirect(origin + get_config()['frontend-redirect'])
        return redirect(get_config()['frontend-host'] + get_config()['frontend-redirect'])
def authenticate():
    """Sends a 401 response that enables basic auth"""
    if 'config' in get_config():
        if 'user' in get_config()['config'] and 'pwd' in get_config()['config']:
            return Response(
                'Could not verify your access level for that URL.\n'
                'You have to login with proper credentials', 401,
                {'WWW-Authenticate': 'Basic realm="Login Required"'})
    return Response('Web configuration was deactivated', 404)
def login():
    """ Login the User with a referral code from the github oauth process"""
    session['session_code'] = request.args.get('code')
    if _request_access_token() and _load_user_data():
        logger.info("User " + session['user_data']['login'] + " logged in")
        if request.referrer is not None and 'github' not in request.referrer:
            origin = origin_from_referrer(request.referrer)
            return redirect(origin + get_config()['frontend-redirect'])
        return redirect(get_config()['frontend-host'] +
                        get_config()['frontend-redirect'])
def load_schemas():
    schemas[SCHEMA_ID_VNF] = []
    for schema_url in get_config()["schemas"][SCHEMA_ID_VNF]:
        response = request.urlopen(schema_url)
        data = response.read()
        schemas[SCHEMA_ID_VNF].append(yaml.safe_load(data.decode('utf-8')))
    schemas[SCHEMA_ID_NS] = []
    for schema_url in get_config()["schemas"][SCHEMA_ID_NS]:
        response = request.urlopen(schema_url)
        data = response.read()
        schemas[SCHEMA_ID_NS].append(yaml.safe_load(data.decode('utf-8')))
def load_schemas():
    schemas[SCHEMA_ID_VNF] = []
    for schema_url in get_config()["schemas"][SCHEMA_ID_VNF]:
        response = request.urlopen(schema_url)
        data = response.read()
        schemas[SCHEMA_ID_VNF].append(yaml.safe_load(data.decode('utf-8')))
    schemas[SCHEMA_ID_NS] = []
    for schema_url in get_config()["schemas"][SCHEMA_ID_NS]:
        response = request.urlopen(schema_url)
        data = response.read()
        schemas[SCHEMA_ID_NS].append(yaml.safe_load(data.decode('utf-8')))
Exemplo n.º 7
0
def get_user(login: str):
    """
    Gets the user from the Database if it exists or
    creates a new user in the Database using the
    login data from the session. If the database does
    not yet have the full user Data it is queried
    from Github using the access Token
    :return: The database user model
    """

    session = db_session()
    user_name = shlex.quote(login)

    user = session.query(User).filter(User.name == user_name).first()
    # for now: if user does not exist we will create a user
    # (that has no access to anything he has not created)
    if user is None:
        user = User(name=user_name)
        session.add(user)
    if user.email is None:
        headers = {
            "Accept": "application/json",
            "Authorization": "token " + flask.session['access_token']
        }
        if 'github-orgs' in get_config():
            # request user orgs
            result = requests.get(
                flask.session['user_data']['organizations_url'],
                headers=headers)
            orgs = json.loads(result.text)
            valid_org_found = False
            for org in orgs:
                if org['login'] in get_config()['github-orgs']:
                    valid_org_found = True
                    break
            if not valid_org_found:
                raise UnauthorizedException(
                    "No valid github org found for this user: "******"Please ask the admin of this server to add "
                    "you to his organization or add your "
                    "orgaization to the list of valid organizations")

        result = requests.get('https://api.github.com/user/emails',
                              headers=headers)
        user_emails = json.loads(result.text)

        for email in user_emails:
            if email['primary']:
                user.email = shlex.quote(email['email'])
                break

    session.commit()
    return user
def _request_access_token():
    """ Request an access token from Github using the referral code"""
    # TODO add error handling
    data = {'client_id': get_config()['authentication']['ClientID'],
            'client_secret': get_config()['authentication']['ClientSecret'],
            'code': session['session_code']}
    headers = {"Accept": "application/json"}
    access_result = requests.post('https://github.com/login/oauth/access_token',
                                  json=data, headers=headers)
    json_result = json.loads(access_result.text)
    if 'access_token' in json_result:
        session['access_token'] = json_result['access_token']
        return True
    raise Exception(json_result['error_description'])
def get_user(login: str):
    """
    Gets the user from the Database if it exists or
    creates a new user in the Database using the
    login data from the session. If the database does
    not yet have the full user Data it is queried
    from Github using the access Token

    :return: The database user model
    """

    session = db_session()
    user_name = shlex.quote(login)

    user = session.query(User).filter(User.name == user_name).first()
    # for now: if user does not exist we will create a user
    # (that has no access to anything he has not created)
    if user is None:
        user = User(name=user_name)
        session.add(user)
    if user.email is None:
        headers = {"Accept": "application/json",
                   "Authorization": "token " + flask.session['access_token']}
        if 'github-orgs' in get_config():
            # request user orgs
            result = requests.get(flask.session['user_data']['organizations_url'], headers=headers)
            orgs = json.loads(result.text)
            valid_org_found = False
            for org in orgs:
                if org['login'] in get_config()['github-orgs']:
                    valid_org_found = True
                    break
            if not valid_org_found:
                raise UnauthorizedException(
                    "No valid github org found for this user: "******"Please ask the admin of this server to add "
                    "you to his organization or add your "
                    "orgaization to the list of valid organizations")

        result = requests.get('https://api.github.com/user/emails', headers=headers)
        user_emails = json.loads(result.text)

        for email in user_emails:
            if email['primary']:
                user.email = shlex.quote(email['email'])
                break

    session.commit()
    return user
Exemplo n.º 10
0
def handle_unauthorized(msg: str):
    args = {"scope": "user:email repo delete_repo",
            "client_id": get_config()['authentication']['ClientID']}
    session["requested_endpoint"] = request.endpoint
    return prepare_response({
        'authorizationUrl': 'https://github.com/login/oauth/authorize/?{}'.format(urllib.parse.urlencode(args)),
        "message": msg}, 401)
Exemplo n.º 11
0
def update_workspace_descriptor(workspace) -> None:
    """
    Updates the workspace descriptor with data from the workspace model

    :param workspace: The workspace model
    """
    with open(os.path.join(workspace.path, "workspace.yml"), "r") as stream:
        ws_descriptor = yaml.safe_load(stream)

    ws_descriptor['catalogue_servers'] = []
    for cat in workspace.catalogues:
        catalogue_server = {'id': cat.name, 'url': cat.url, 'publish': cat.publish}
        ws_descriptor['catalogue_servers'].append(catalogue_server)
    ws_descriptor['service_platforms'] = {}
    ws_descriptor['default_service_platform'] = ''
    for plat in workspace.platforms:
        platform_server = {'url': plat.url, "credentials": {"token_file": plat.token_path}}
        ws_descriptor['service_platforms'][plat.name] = platform_server
        if plat.publish:
            ws_descriptor['default_service_platform'] = plat.name
    if not ws_descriptor['default_service_platform'] and ws_descriptor['service_platforms']:
        # if no default set, select "first" platform
        ws_descriptor['default_service_platform'] = \
            ws_descriptor['service_platforms'][ws_descriptor['service_platforms'].keys()[0]]['id']
    ws_descriptor['name'] = workspace.name
    ws_descriptor['schema_index'] = workspace.schema_index
    ws_descriptor['schemas_remote_master'] = get_config()["schemas"][workspace.schema_index]['url']

    with open(os.path.join(workspace.path, "workspace.yml"), "w") as stream:
        yaml.safe_dump(ws_descriptor, stream)
Exemplo n.º 12
0
def get_user(login: str):
    """
    Gets the user from the Database if it exists or
    creates a new user in the Database using the
    login data from the session. If the database does
    not yet have the full user Data it is queried
    from Github using the access Token

    :return: The database user model
    """

    session = db_session()
    user_name = shlex.quote(login)

    user = session.query(User).filter(User.name == user_name).first()
    # for now: if user does not exist we will create a user
    # (that has no access to anything he has not created)
    if user is None:
        user = User(name=user_name)
        session.add(user)
    if user.email is None:

        if 'users' in get_config()['authentication']:
            # check if user is in list of authorized users
            if user_name not in get_config()['authentication']['users']:
                raise UnauthorizedException(
                    user_name + " was not found in the list of valid users,"
                    "Please ask the admin of this server to add "
                    "you to the list of valid users")

        headers = {
            "Accept": "application/json",
            "Authorization": "token " + flask.session['access_token']
        }
        result = requests.get('https://api.github.com/user/emails',
                              headers=headers)
        user_emails = json.loads(result.text)

        for email in user_emails:
            if email['primary']:
                user.email = shlex.quote(email['email'])
                break

    session.commit()
    return user
def _request_access_token():
    """ Request an access token from Github using the referral code"""
    # TODO add error handling
    data = {
        'client_id': get_config()['authentication']['ClientID'],
        'client_secret': get_config()['authentication']['ClientSecret'],
        'code': session['session_code']
    }
    headers = {"Accept": "application/json"}
    access_result = requests.post(
        'https://github.com/login/oauth/access_token',
        json=data,
        headers=headers)
    json_result = json.loads(access_result.text)
    if 'access_token' in json_result:
        session['access_token'] = json_result['access_token']
        return True
    raise Exception(json_result['error_description'])
Exemplo n.º 14
0
def handle_unauthorized(msg: str):
    args = {"scope": "user read:org repo delete_repo", "client_id": get_config()["authentication"]["ClientID"]}
    session["requested_endpoint"] = request.endpoint
    return prepare_response(
        {
            "authorizationUrl": "https://github.com/login/oauth/authorize/?{}".format(urllib.parse.urlencode(args)),
            "message": msg,
        },
        401,
    )
Exemplo n.º 15
0
    def get(self, ws_id):
        """
        List Schemas
        
        Returns a list of all schemas configured for this server

        :param ws_id: The workspace ID
        :return: A list of schemas
        """
        return prepare_response(get_config()["schemas"])
Exemplo n.º 16
0
def get_user(login: str):
    """
    Gets the user from the Database if it exists or
    creates a new user in the Database using the
    login data from the session. If the database does
    not yet have the full user Data it is queried
    from Github using the access Token

    :return: The database user model
    """

    session = db_session()
    user_name = shlex.quote(login)

    user = session.query(User).filter(User.name == user_name).first()
    # for now: if user does not exist we will create a user
    # (that has no access to anything he has not created)
    if user is None:
        user = User(name=user_name)
        session.add(user)
    if user.email is None:

        if 'users' in get_config()['authentication']:
            # check if user is in list of authorized users
            if user_name not in get_config()['authentication']['users']:
                raise UnauthorizedException(
                    user_name + " was not found in the list of valid users,"
                                "Please ask the admin of this server to add "
                                "you to the list of valid users")

        headers = {"Accept": "application/json",
                   "Authorization": "token " + flask.session['access_token']}
        result = requests.get('https://api.github.com/user/emails', headers=headers)
        user_emails = json.loads(result.text)

        for email in user_emails:
            if email['primary']:
                user.email = shlex.quote(email['email'])
                break

    session.commit()
    return user
Exemplo n.º 17
0
def setup():
    setup_logging()
    # Check check if database exists, otherwise create sqlite file
    dbFile = get_config()['database']['location']
    if path.exists(dbFile):
        logger.info('Using database file "%s"' % dbFile)
    else:
        logger.info('Init database on "%s"' % dbFile)
        init_db()
    # parse all workspaces already on the hard drive
    scan_workspaces_dir()
    # Start the flask server
    logger.info("Launch flask server")
Exemplo n.º 18
0
def setup():
    setup_logging()
    # Check check if database exists, otherwise create sqlite file
    dbFile = get_config()['database']['location']
    if path.exists(dbFile):
        logger.info('Using database file "%s"' % dbFile)
    else:
        logger.info('Init database on "%s"' % dbFile)
        init_db()
    # parse all workspaces already on the hard drive
    scan_workspaces_dir()
    # Start the flask server
    logger.info("Launch flask server")
Exemplo n.º 19
0
def handle_unauthorized(msg: str):
    args = {
        "scope": "user:email repo delete_repo",
        "client_id": get_config()['authentication']['ClientID']
    }
    session["requested_endpoint"] = request.endpoint
    return prepare_response(
        {
            'authorizationUrl':
            'https://github.com/login/oauth/authorize/?{}'.format(
                urllib.parse.urlencode(args)),
            "message":
            msg
        }, 401)
def scan_workspaces_dir():
    from son_editor.models.user import User
    wss_dir = os.path.normpath(os.path.expanduser(get_config()["workspaces-location"]))
    if os.path.exists(wss_dir):
        session = db_session()
        for user_name in os.listdir(wss_dir):
            if not Path(os.path.join(wss_dir, user_name)).is_dir():
                continue
            user = session.query(User).filter(User.name == user_name).first()
            if user is None:
                logger.info("Found user: {}!".format(user_name))
                user = User(user_name)
                session.add(user)
                session.commit()
            _scan_user_dir(wss_dir, user)
Exemplo n.º 21
0
def create_workspace(user: User, ws_name: str) -> int:
    """
    Creates a workspace
    :param user: the user for which to insert the workspace
    :param ws_name: Name of the workspace that gets created
    :return: ID of the created workspace
    """

    ws_data = {'name': ws_name,
               'platforms': [
                   {'name': 'sonEmu',
                    'url': get_config()['test']['platform-instance']}
               ]}
    workspace_data = son_editor.impl.workspaceimpl.create_workspace(user.name, ws_data)
    return workspace_data['id']
def scan_workspaces_dir():
    from son_editor.models.user import User
    wss_dir = os.path.normpath(
        os.path.expanduser(get_config()["workspaces-location"]))
    if os.path.exists(wss_dir):
        session = db_session()
        for user_name in os.listdir(wss_dir):
            if not Path(os.path.join(wss_dir, user_name)).is_dir():
                continue
            user = session.query(User).filter(User.name == user_name).first()
            if user is None:
                logger.info("Found user: {}!".format(user_name))
                user = User(user_name)
                session.add(user)
                session.commit()
            _scan_user_dir(wss_dir, user)
    def test_push_project(self):
        package_path = self.test_package_location
        session = db_session()
        ws = session.query(Workspace).filter(Workspace.id == self.wsid).first()
        result = publishutil.push_to_platform(package_path=package_path, ws=ws)
        self.assertTrue('service_uuid' in result)

        caught = False
        try:
            ws.platforms[0].url = get_config()['test']['platform-instance-wrong']
            update_workspace_descriptor(ws)
            result = publishutil.push_to_platform(package_path=package_path,
                                                  ws=ws)  # wrong port
        except ExtNotReachable:
            caught = True
        self.assertTrue(caught)
        session.rollback()
Exemplo n.º 24
0
def load_schemas():
    """ Loads the schemas congigured under "schemas" from the schema remotes """
    schemas[SCHEMA_ID_VNF] = []
    schemas[SCHEMA_ID_NS] = []
    for schema in get_config()["schemas"]:
        # load vnf schema
        vnf_schema = dict(schema)
        response = request.urlopen(vnf_schema['url'] + "function-descriptor/vnfd-schema.yml")
        data = response.read()
        vnf_schema['schema'] = yaml.safe_load(data.decode('utf-8'))
        schemas[SCHEMA_ID_VNF].append(vnf_schema)
        # load ns schema
        ns_schema = dict(schema)
        response = request.urlopen(ns_schema['url'] + "service-descriptor/nsd-schema.yml")
        data = response.read()
        ns_schema['schema'] = yaml.safe_load(data.decode('utf-8'))
        schemas[SCHEMA_ID_NS].append(ns_schema)
    def test_push_project(self):
        package_path = self.test_package_location
        session = db_session()
        ws = session.query(Workspace).filter(Workspace.id == self.wsid).first()
        result = publishutil.push_to_platform(package_path=package_path, ws=ws)
        self.assertTrue('service_uuid' in result)

        caught = False
        try:
            ws.platforms[0].url = get_config(
            )['test']['platform-instance-wrong']
            update_workspace_descriptor(ws)
            result = publishutil.push_to_platform(package_path=package_path,
                                                  ws=ws)  # wrong port
        except ExtNotReachable:
            caught = True
        self.assertTrue(caught)
        session.rollback()
Exemplo n.º 26
0
def check_logged_in():
    if request.method == 'OPTIONS':
        return prepare_response()
    elif request.endpoint in ['login', 'doc', 'specs', 'config_configuration', 'restplus_doc.static']:
        # no github login requiered
        return
    elif get_config()['testing']:
        # Check if the user is allowed to access the requested workspace resource (even for tests)
        check_access(request)
        return
    # Check if the user is not logged in
    elif 'access_token' not in session and request.endpoint not in ['static']:
        # show request for github login
        return handle_unauthorized("Please log in")
    # Check if the user is allowed access the requested workspace resource
    try:
        check_access(request)
    except UnauthorizedException as uae:
        return handle_unauthorized(uae.msg)
Exemplo n.º 27
0
def check_logged_in():
    if request.endpoint == "login":
        return
    if request.endpoint == "config_configuration":
        return
    if request.method == "OPTIONS":
        return prepare_response()
    elif get_config()["testing"]:
        # Check if the user is allowed access the requested workspace resource (even for tests)
        check_access(request)
        return
    # Check if the user is not logged in
    elif "access_token" not in session and request.endpoint not in ["static", "shutdown"]:
        # show request for github login
        return handle_unauthorized("Please log in")
    # Check if the user is allowed access the requested workspace resource
    try:
        check_access(request)
    except UnauthorizedException as uae:
        return handle_unauthorized(uae.msg)
Exemplo n.º 28
0
def create_workspace(user: User, ws_name: str) -> int:
    """
    Creates a workspace

    :param user: the user for which to insert the workspace
    :param ws_name: Name of the workspace that gets created
    :return: ID of the created workspace
    """

    ws_data = {
        'name':
        ws_name,
        'platforms': [{
            'name': 'sonEmu',
            'url': get_config()['test']['platform-instance']
        }]
    }
    workspace_data = son_editor.impl.workspaceimpl.create_workspace(
        user.name, ws_data)
    return workspace_data['id']
Exemplo n.º 29
0
def update_workspace_descriptor(workspace) -> None:
    """
    Updates the workspace descriptor with data from the workspace model

    :param workspace: The workspace model
    """
    with open(os.path.join(workspace.path, "workspace.yml"), "r") as stream:
        ws_descriptor = yaml.safe_load(stream)

    ws_descriptor['catalogue_servers'] = []
    for cat in workspace.catalogues:
        catalogue_server = {
            'id': cat.name,
            'url': cat.url,
            'publish': cat.publish
        }
        ws_descriptor['catalogue_servers'].append(catalogue_server)
    ws_descriptor['service_platforms'] = {}
    ws_descriptor['default_service_platform'] = ''
    for plat in workspace.platforms:
        platform_server = {
            'url': plat.url,
            "credentials": {
                "token_file": plat.token_path
            }
        }
        ws_descriptor['service_platforms'][plat.name] = platform_server
        if plat.publish:
            ws_descriptor['default_service_platform'] = plat.name
    if not ws_descriptor['default_service_platform'] and ws_descriptor[
            'service_platforms']:
        # if no default set, select "first" platform
        ws_descriptor['default_service_platform'] = \
            ws_descriptor['service_platforms'][ws_descriptor['service_platforms'].keys()[0]]['id']
    ws_descriptor['name'] = workspace.name
    ws_descriptor['schema_index'] = workspace.schema_index
    ws_descriptor['schemas_remote_master'] = get_config()["schemas"][
        workspace.schema_index]['url']

    with open(os.path.join(workspace.path, "workspace.yml"), "w") as stream:
        yaml.safe_dump(ws_descriptor, stream)
Exemplo n.º 30
0
def check_logged_in():
    if request.method == 'OPTIONS':
        return prepare_response()
    elif request.endpoint in [
            'login', 'doc', 'specs', 'config_configuration',
            'restplus_doc.static'
    ]:
        # no github login requiered
        return
    elif get_config()['testing']:
        # Check if the user is allowed to access the requested workspace resource (even for tests)
        check_access(request)
        return
    # Check if the user is not logged in
    elif 'access_token' not in session and request.endpoint not in ['static']:
        # show request for github login
        return handle_unauthorized("Please log in")
    # Check if the user is allowed access the requested workspace resource
    try:
        check_access(request)
    except UnauthorizedException as uae:
        return handle_unauthorized(uae.msg)
Exemplo n.º 31
0
def load_schemas():
    """ Loads the schemas congigured under "schemas" from the schema remotes """
    schemas[SCHEMA_ID_VNF] = []
    schemas[SCHEMA_ID_NS] = []

    s = requests.session()

    for schema in get_config()["schemas"]:
        # load vnf schema
        vnf_schema = dict(schema)
        response = s.get(vnf_schema['url'] +
                         "function-descriptor/vnfd-schema.yml")
        data = response.text
        vnf_schema['schema'] = yaml.safe_load(data)
        schemas[SCHEMA_ID_VNF].append(vnf_schema)

        # load ns schema
        ns_schema = dict(schema)
        response = s.get(vnf_schema['url'] +
                         "service-descriptor/nsd-schema.yml")
        data = response.text
        ns_schema['schema'] = yaml.safe_load(data)
        schemas[SCHEMA_ID_NS].append(ns_schema)
Exemplo n.º 32
0
from flask.globals import request
from flask_restplus import Api

from son_editor import apis
from son_editor.app.database import db_session, init_db, scan_workspaces_dir
from son_editor.app.exceptions import NameConflict, NotFound, ExtNotReachable, PackException, InvalidArgument, \
    UnauthorizedException, StillReferenced
from son_editor.app.securityservice import check_access
from son_editor.util.requestutil import get_config, prepare_response, prepare_error

app = Flask(__name__)
# turn off help message for 404 errors, just return error handlers message
app.config["ERROR_404_HELP"] = False
app.config["RESTPLUS_MASK_SWAGGER"] = False
# load secret key from config
if 'session' in get_config() and 'secretKey' in get_config()['session']:
    app.secret_key = get_config()['session']['secretKey']
else:
    app.secret_key = os.urandom(24)
api = Api(app, description="Son Editor Backend API")
logger = logging.getLogger(__name__)


@api.errorhandler(KeyError)
def handle_key_error(err):
    logger.exception(err.args[0])
    return prepare_error(
        {
            "message": "Key '{}' is required in request data!".format(
                err.args[0])
        }, 400)
Exemplo n.º 33
0
 def get(self):
     return prepare_response(get_config())
Exemplo n.º 34
0
from flask.globals import request
from flask_restplus import Api

from son_editor import apis
from son_editor.app.database import db_session, init_db, scan_workspaces_dir
from son_editor.app.exceptions import NameConflict, NotFound, ExtNotReachable, PackException, InvalidArgument, \
    UnauthorizedException
from son_editor.app.securityservice import check_access
from son_editor.util.requestutil import get_config, prepare_response, prepare_error

app = Flask(__name__)
# turn off help message for 404 errors, just return error handlers message
app.config["ERROR_404_HELP"] = False
app.config["RESTPLUS_MASK_SWAGGER"] = False
# load secret key from config
app.secret_key = get_config()['session']['secretKey']
api = Api(app, description="Son Editor Backend API")
logger = logging.getLogger(__name__)


@api.errorhandler(KeyError)
def handle_key_error(err):
    logger.exception(err.args[0])
    return prepare_error(
        {
            "message": "Key '{}' is required in request data!".format(
                err.args[0])
        }, 400)


@api.errorhandler(NotFound)
Exemplo n.º 35
0
@author: Jonas
'''
import os, stat
import shlex
import shutil
from subprocess import Popen, PIPE

from son_editor.app.database import db_session, scan_project_dir
from son_editor.app.exceptions import NotFound, NameConflict
from son_editor.impl import gitimpl
from son_editor.models.project import Project
from son_editor.models.workspace import Workspace
from son_editor.util.descriptorutil import sync_project_descriptor
from son_editor.util.requestutil import get_config, rreplace

WORKSPACES_DIR = os.path.expanduser(get_config()["workspaces-location"])
# make ws paths prettier
WORKSPACES_DIR = os.path.normpath(WORKSPACES_DIR)


def get_projects(ws_id: int) -> list:
    """
    Get a list of projects in this workspace

    :param ws_id: The workspace ID
    :return: List of all projects
    """
    session = db_session()
    projects = session.query(Project). \
        join(Workspace). \
        filter(Workspace.id == ws_id).all()
Exemplo n.º 36
0
    NameConflict,
    NotFound,
    ExtNotReachable,
    PackException,
    InvalidArgument,
    UnauthorizedException,
)
from son_editor.app.securityservice import check_access
from son_editor.util.requestutil import get_config, prepare_response, prepare_error

app = Flask(__name__)
# turn off help message for 404 errors, just return error handlers message
app.config["ERROR_404_HELP"] = False
app.config["RESTPLUS_MASK_SWAGGER"] = False
# load secret key from config
app.secret_key = get_config()["session"]["secretKey"]
api = Api(app, description="Son Editor Backend API")
logger = logging.getLogger(__name__)


@api.errorhandler(KeyError)
def handle_key_error(err):
    logger.exception(err.args[0])
    return prepare_error({"message": "Key '{}' is required in request data!".format(err.args[0])}, 400)


@api.errorhandler(NotFound)
def handle_not_found(err):
    logger.warn(err.msg)
    return prepare_error({"message": err.msg}, 404)
Exemplo n.º 37
0
from son_editor.app import __main__
from son_editor.app.database import reset_db
from son_editor.util.requestutil import CONFIG, get_config
from os import path
import shutil

# URL
CATALOGUE_INSTANCE_URL = get_config()['test']['catalogue-instance']


def init_test_context():
    """
     Initializes a test-case context and cleans up workspace location beforehand

    :return: The test client
    """
    CONFIG['testing'] = True
    # Delete existing workspaces
    shutil.rmtree(path.expanduser(CONFIG["workspaces-location"]), ignore_errors=True)
    reset_db()
    return __main__.app.test_client()
import os
from pathlib import Path

import shutil
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker

from son_editor.util.descriptorutil import load_ns_vnf_from_disk, load_workspace_descriptor, get_file_path, \
    get_file_name, \
    sync_project_descriptor
from son_editor.util.requestutil import get_config

# DB URI
logger = logging.getLogger(__name__)
DATABASE_SQLITE_URI = "sqlite:///%s" % get_config()['database']['location']
logger.info("DBSQLITE_URI: " + DATABASE_SQLITE_URI)

engine = create_engine(DATABASE_SQLITE_URI, convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()


def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    import son_editor.models.project
import os
from pathlib import Path

import shutil
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker

from son_editor.util.descriptorutil import load_ns_vnf_from_disk, load_workspace_descriptor, get_file_path, \
    get_file_name, \
    sync_project_descriptor
from son_editor.util.requestutil import get_config

# DB URI
logger = logging.getLogger(__name__)
DATABASE_SQLITE_URI = "sqlite:///%s" % get_config()['database']['location']
logger.info("DBSQLITE_URI: " + DATABASE_SQLITE_URI)

engine = create_engine(DATABASE_SQLITE_URI, convert_unicode=True)
db_session = scoped_session(
    sessionmaker(autocommit=False, autoflush=False, bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()


def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    import son_editor.models.project
    import son_editor.models.user
Exemplo n.º 40
0
from flask.globals import request
from flask_restplus import Api

from son_editor import apis
from son_editor.app.database import db_session, init_db, scan_workspaces_dir
from son_editor.app.exceptions import NameConflict, NotFound, ExtNotReachable, PackException, InvalidArgument, \
    UnauthorizedException, StillReferenced
from son_editor.app.securityservice import check_access
from son_editor.util.requestutil import get_config, prepare_response, prepare_error

app = Flask(__name__)
# turn off help message for 404 errors, just return error handlers message
app.config["ERROR_404_HELP"] = False
app.config["RESTPLUS_MASK_SWAGGER"] = False
# load secret key from config
if 'session' in get_config() and 'secretKey' in get_config()['session']:
    app.secret_key = get_config()['session']['secretKey']
else:
    app.secret_key = os.urandom(24)
api = Api(app, description="Son Editor Backend API")
logger = logging.getLogger(__name__)


@api.errorhandler(KeyError)
def handle_key_error(err):
    logger.exception(err.args[0])
    return prepare_error({"message": "Key '{}' is required in request data!".format(err.args[0])}, 400)


@api.errorhandler(NotFound)
def handle_not_found(err):
Exemplo n.º 41
0
import unittest

from son_editor.models.project import Project
from son_editor.tests.utils import *
from son_editor.util.context import init_test_context
from son_editor.util.requestutil import get_config

logger = logging.getLogger(__name__)

# Test constants
GITHUB_URL = "http://github.com"
REMOTE_REPO_NAME = 'test-create'
REMOTE_INVALID_REPO_NAME = 'invalid-son-repo'
REMOTE_DOES_NOT_EXIST_REPO_NAME = 'does-not-exist'

CONFIG = get_config()

# Check if there exist testing entries in environment (for travis configuration), otherwise use config.yaml

if 'github_bot_user' in os.environ and "github_access_token" in os.environ:
    GITHUB_USER = os.environ["github_bot_user"]
    GITHUB_ACCESS_TOKEN = os.environ["github_access_token"]
elif 'test' in CONFIG and 'github' in CONFIG['test'] and 'user' in CONFIG['test']['github'] and 'access-token' in \
        CONFIG['test']['github']:
    GITHUB_USER = CONFIG['test']['github']['user']
    GITHUB_ACCESS_TOKEN = CONFIG['test']['github']['access-token']


class GitAPITest(unittest.TestCase):
    def setUp(self):
        # Initializes test context
Exemplo n.º 42
0
 def get(self):
     """ Show Configuration
     
     Show the current server configuration (requires authentication via Basic Auth)
     """
     return prepare_response(get_config())
@author: Jonas
'''
import os, stat
import shlex
import shutil
from subprocess import Popen, PIPE

from son_editor.app.database import db_session, scan_project_dir
from son_editor.app.exceptions import NotFound, NameConflict
from son_editor.impl import gitimpl
from son_editor.models.project import Project
from son_editor.models.workspace import Workspace
from son_editor.util.descriptorutil import sync_project_descriptor
from son_editor.util.requestutil import get_config, rreplace

WORKSPACES_DIR = os.path.expanduser(get_config()["workspaces-location"])
# make ws paths prettier
WORKSPACES_DIR = os.path.normpath(WORKSPACES_DIR)


def get_projects(ws_id: int) -> list:
    """
    Get a list of projects in this workspace
    :param ws_id:
    :return:
    """
    session = db_session()
    projects = session.query(Project). \
        join(Workspace). \
        filter(Workspace.id == ws_id).all()
    session.commit()
import unittest

from son_editor.models.project import Project
from son_editor.tests.utils import *
from son_editor.util.context import init_test_context
from son_editor.util.requestutil import get_config

logger = logging.getLogger(__name__)

# Test constants
GITHUB_URL = "http://github.com"
REMOTE_REPO_NAME = 'test-create'
REMOTE_INVALID_REPO_NAME = 'invalid-son-repo'
REMOTE_DOES_NOT_EXIST_REPO_NAME = 'does-not-exist'

CONFIG = get_config()

# Check if there exist testing entries in environment (for travis configuration), otherwise use config.yaml

if 'github_bot_user' in os.environ and "github_access_token" in os.environ:
    GITHUB_USER = os.environ["github_bot_user"]
    GITHUB_ACCESS_TOKEN = os.environ["github_access_token"]
elif 'test' in CONFIG and 'github' in CONFIG['test'] and 'user' in CONFIG['test']['github'] and 'access-token' in \
        CONFIG['test']['github']:
    GITHUB_USER = CONFIG['test']['github']['user']
    GITHUB_ACCESS_TOKEN = CONFIG['test']['github']['access-token']


class GitAPITest(unittest.TestCase):
    def setUp(self):
        # Initializes test context
Exemplo n.º 45
0
from son_editor.app import __main__
from son_editor.app.database import reset_db
from son_editor.util.requestutil import CONFIG, get_config
from os import path
import shutil

# URL
CATALOGUE_INSTANCE_URL = get_config()['test']['catalogue-instance']


def init_test_context():
    """
     Initializes a test-case context and cleans up workspace location beforehand

    :return: The test client
    """
    CONFIG['testing'] = True
    # Delete existing workspaces
    shutil.rmtree(path.expanduser(CONFIG["workspaces-location"]),
                  ignore_errors=True)
    reset_db()
    return __main__.app.test_client()