Exemplo n.º 1
0
def get_driver_script(name, name2=None):  # noqa: E501
    """Retrieve the contents of a script

    Retrieve the contents of a script # noqa: E501

    :param name2: Get status of a driver with this name
    :type name2: str
    :param name: The script name.
    :type name: str

    :rtype: Response
    """
    response = errorIfUnauthorized(role='user')
    if response:
        return response
    else:
        response = ApitaxResponse()

    print(name)
    print(name2)

    driver: Driver = LoadedDrivers.getDriver(name2)

    response.body.add({'content': driver.getDriverScript(name)})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 2
0
    def import_module_file(self,
                           path: str,
                           driver: str = None,
                           as_label: str = None):
        """
        Imports a module file from a driver
        :param path:
        :param driver:
        :param as_label:
        :return:
        """
        # Avoids circular dependencies
        from scriptax.parser.ModuleVisitor import ModuleParser

        if as_label:
            as_label = self.resolve_label(as_label)

        driver_instance: Driver = LoadedDrivers.getPrimaryDriver()
        if driver:
            driver = self.resolve_label(driver)
            driver_instance = LoadedDrivers.getDriver(driver)

        scriptax: InputStream = driver_instance.getDriverScript(path)
        tree = generate_parse_tree(scriptax)

        import_table = SymbolTable(name=as_label + "_import")
        module_parser = ModuleParser(symbol_table=import_table)
        module_parser.visit(tree)
        module = Import(tree=tree, scope=module_parser.symbol_table.scope())
        self.symbol_table.import_module(name=as_label, module=module)
Exemplo n.º 3
0
def rename_driver_script(name, rename=None):  # noqa: E501
    """Rename a script

    Rename a script # noqa: E501

    :param name: Get status of a driver with this name
    :type name: str
    :param rename: The data needed to save this script
    :type rename: dict | bytes

    :rtype: Response
    """
    if connexion.request.is_json:
        rename = Rename.from_dict(connexion.request.get_json())  # noqa: E501

    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    if not driver.renameDriverScript(rename.original.name, rename.new.name):
        return ErrorResponse(status=500,
                             message='Cannot rename to an existing file.')

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 4
0
def delete_driver_script(name, delete=None):  # noqa: E501
    """Delete a script

    Delete a script # noqa: E501

    :param name: Get status of a driver with this name
    :type name: str
    :param delete: The data needed to delete this script
    :type delete: dict | bytes

    :rtype: Response
    """
    if connexion.request.is_json:
        delete = Delete1.from_dict(connexion.request.get_json())  # noqa: E501

    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    driver.deleteDriverScript(delete.script.name)

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 5
0
def delete_user(user, driver, user_delete=None):  # noqa: E501
    """Delete a user

    Delete a user # noqa: E501

    :param user: Delete user with this name
    :type user: str
    :param driver: The driver to use for the request. ie. github
    :type driver: str
    :param user_delete: The data needed to delete this user
    :type user_delete: dict | bytes

    :rtype: Response
    """
    if connexion.request.is_json:
        user_delete = UserDelete.from_dict(
            connexion.request.get_json())  # noqa: E501

    response = errorIfUnauthorized(role='admin')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)

    if driver.deleteApitaxUser(User(username=user)):
        return Response(status=200, body=response.getResponseBody())

    return ErrorResponse(status=500, message='Failed to create user')
Exemplo n.º 6
0
def get_driver_status(name):  # noqa: E501
    """Retrieve the status of a loaded driver

    Retrieve the status of a loaded driver # noqa: E501

    :param name: Get status of a driver with this name
    :type name: str

    :rtype: Response
    """

    response = errorIfUnauthorized(role='admin')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    response.body.add({'name': driver.getDriverName()})
    response.body.add({'description': driver.getDriverDescription()})
    response.body.add({'tips': driver.getDriverTips()})
    response.body.add({'help': driver.getDriverHelpEndpoint()})
    response.body.add({'minimum-role': driver.getDriverMinimumRole()})

    options = {}
    options['configurable'] = driver.isDriverConfigurable()
    options['authenticatable'] = driver.isDriverAuthenticatable()
    options['role-restricted'] = driver.isDriverRoleRestricted()
    options['whitelisted'] = driver.isDriverWhitelisted()
    options['blacklisted'] = driver.isDriverBlacklisted()
    response.body.add({'options': options})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 7
0
    def login(credentials):
        driver: Driver = LoadedDrivers.getAuthDriver()
        role = None
        if not driver.isDriverAuthenticatable():
            return None
        if driver.isApitaxAuthBasedOnApiAuth():
            # apiDriver = LoadedDrivers.getPrimaryDriver()
            apiDriver: Driver = driver

            if 'driver' in credentials.extra:
                apiDriver = LoadedDrivers.getDriver(
                    credentials.extra['driver'])
            result: ApitaxResponse = apiDriver.authenticateApi(credentials)

            if not result.isStatusSuccess():
                return None

            role = driver.authenticateApitax(
                result.getResponseBody()['credentials'])
            if apiDriver.isApiTokenable():
                credentials.token = apiDriver.getApiToken(result)
        else:
            role = driver.authenticateApitax(credentials)

        if not role:
            return None
        return {"role": role, "credentials": credentials}
Exemplo n.º 8
0
def delete_driver_script(driver, script_delete=None):  # noqa: E501
    """Delete a script

    Delete a script # noqa: E501

    :param driver: The driver to use for the request. ie. github
    :type driver: str
    :param script_delete: The data needed to delete this script
    :type script_delete: dict | bytes

    :rtype: Response
    """
    if connexion.request.is_json:
        script_delete = ScriptDelete.from_dict(
            connexion.request.get_json())  # noqa: E501

    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)
    driver.deleteDriverScript(script_delete.script.name)

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 9
0
def create_user(user, name, create=None):  # noqa: E501
    """Create a new script

    Create a new script # noqa: E501

    :param user: Get user with this name
    :type user: str
    :param name: Get status of a driver with this name
    :type name: str
    :param create: The data needed to create this user
    :type create: dict | bytes

    :rtype: Response
    """
    if connexion.request.is_json:
        create = Create.from_dict(connexion.request.get_json())  # noqa: E501

    response = errorIfUnauthorized(role='admin')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    user: User = mapUserToUser(create.script)

    if driver.createApitaxUser(user):
        return Response(status=200, body=response.getResponseBody())

    return ErrorResponse(status=500, message='Failed to create user')
Exemplo n.º 10
0
def rename_driver_script(driver, script_rename=None):  # noqa: E501
    """Rename a script

    Rename a script # noqa: E501

    :param driver: The driver to use for the request. ie. github
    :type driver: str
    :param script_rename: The data needed to rename this script
    :type script_rename: dict | bytes

    :rtype: Response
    """
    if connexion.request.is_json:
        script_rename = ScriptRename.from_dict(
            connexion.request.get_json())  # noqa: E501

    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)
    if not driver.renameDriverScript(script_rename.original.name,
                                     script_rename.new.name):
        return ErrorResponse(status=500,
                             message='Cannot rename to an existing file.')

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 11
0
def get_user(user, name):  # noqa: E501
    """Retrieve a user

    Retrieve a user # noqa: E501

    :param user: Get user with this name
    :type user: str
    :param name: Get status of a driver with this name
    :type name: str

    :rtype: Response
    """
    response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    user: User = driver.getApitaxUser(User(username=user))
    response.body.add({'user': {'username': user.username, 'role': user.role}})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 12
0
    def __init__(self, options=None, credentials=None, command='', parameters={}, request=None):
        self.options: Options = options if options is not None else Options()
        self.parameters = parameters
        self.credentials: Credentials = credentials if credentials is not None else Credentials()
        self.request: ApitaxRequest = request if request is not None else ApitaxRequest()

        self.command: str = command
        self.command = self.command.replace('\\"', '"')
        self.command = self.command.replace('\\\'', '\'')
        self.command: list = shlex.split(self.command.strip())

        self.executionTime = None
        self.commandtax = None
        self.logBuffer = []

        self.options.driver: Driver = LoadedDrivers.getDriver('commandtax')

        self.request.headerBuilder = self.options.driver.addApiHeaders(self.request.headerBuilder)
        self.request.bodyBuilder = self.options.driver.addApiBody(self.request.bodyBuilder)
Exemplo n.º 13
0
def get_user(user, driver):  # noqa: E501
    """Retrieve a user

    Retrieve a user # noqa: E501

    :param user: Get user with this name
    :type user: str
    :param driver: The driver to use for the request. ie. github
    :type driver: str

    :rtype: Response
    """

    response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)
    user: User = driver.getApitaxUser(User(username=user))
    response.body.add({'user': {'username': user.username, 'role': user.role}})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 14
0
def get_driver_api_catalog(name):  # noqa: E501
    """Retrieve the api catalog

    Retrieve the api catalog # noqa: E501

    :param name: Get status of a driver with this name
    :type name: str

    :rtype: Response
    """
    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    response.body.add(driver.getApiEndpointCatalog())

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 15
0
def get_driver_api_catalog(driver):  # noqa: E501
    """Retrieve the api catalog

    Retrieve the api catalog # noqa: E501

    :param driver: The driver to use for the request. ie. github
    :type driver: str

    :rtype: Response
    """
    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)
    response.body.add(driver.getApiEndpointCatalog())

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 16
0
def get_driver_blacklist(name):  # noqa: E501
    """Retrieve the blacklist in the driver

    Retrieve the blacklist in the driver # noqa: E501

    :param name: Get status of a driver with this name
    :type name: str

    :rtype: Response
    """

    response = errorIfUnauthorized(role='admin')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    response.body.add({'blacklist': driver.getDriverBlacklist()})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 17
0
def get_driver_blacklist(driver):  # noqa: E501
    """Retrieve the blacklist in the driver

    Retrieve the blacklist in the driver # noqa: E501

    :param driver: The driver to use for the request. ie. github
    :type driver: str

    :rtype: Response
    """

    response = errorIfUnauthorized(role='admin')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)
    response.body.add({'blacklist': driver.getDriverBlacklist()})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 18
0
def get_driver_api_status(name):  # noqa: E501
    """Retrieve the status of an api

    Retrieve the status of an api # noqa: E501

    :param name: Get status of a driver with this name
    :type name: str

    :rtype: Response
    """
    response = errorIfUnauthorized(role='developer')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(name)
    response.body.add({"format": driver.getApiFormat()})
    response.body.add({"description": driver.getApiDescription()})
    response.body.add({"status": driver.getApiStatus()})
    response.body.add({"auth-type": driver.getApiAuthType()})

    endpoints = {}
    endpoints['base'] = driver.getApiBaseEndpoint()
    endpoints['catalog'] = driver.getApiCatalogEndpoint()
    endpoints['auth'] = driver.getApiAuthEndpoint()
    response.body.add({'endpoints': endpoints})

    options = {}
    options['authenticatable'] = driver.isApiAuthenticated()
    options[
        'authentication-separate'] = driver.isApiAuthenticationSeparateRequest(
        )
    options['cataloggable'] = driver.isApiCataloggable()
    options['tokenable'] = driver.isApiTokenable()
    response.body.add({'options': options})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 19
0
def get_driver_script(driver, script=None):  # noqa: E501
    """Retrieve the contents of a script

    Retrieve the contents of a script # noqa: E501

    :param driver: The driver to use for the request. ie. github
    :type driver: str
    :param script: The script name.
    :type script: str

    :rtype: Response
    """

    response = errorIfUnauthorized(role='user')
    if response:
        return response
    else:
        response = ApitaxResponse()

    driver: Driver = LoadedDrivers.getDriver(driver)

    response.body.add({'content': driver.getDriverScript(script)})

    return Response(status=200, body=response.getResponseBody())
Exemplo n.º 20
0
    def handleDriverCommand(self, command: Command) -> ApitaxResponse:
        response: ApitaxResponse = ApitaxResponse()

        driverName = None

        if len(command.command) < 1:
            return response.res_bad_request(
                body={
                    'error': 'Improper command: `blank` command was attempted.'
                })

        if '--apitax-debug' in command.command:
            command.options.debug = True
            del command.command[command.command.index('--apitax-debug')]

        if '--apitax-sensitive' in command.command:
            command.options.sensitive = True
            del command.command[command.command.index('--apitax-sensitive')]

        if '--apitax-driver' in command.command:
            driverName = command.command[
                command.command.index('--apitax-driver') + 1]
            del command.command[command.command.index('--apitax-sensitive') +
                                1]
            del command.command[command.command.index('--apitax-driver')]

        if driverName:
            command.options.driver = LoadedDrivers.getDriver(driverName)
        elif LoadedDrivers.getDriver(command.command[0]):
            command.options.driver = LoadedDrivers.getDriver(
                command.command[0])
            del command.command[0]
        else:
            command.options.driver = LoadedDrivers.getPrimaryDriver()

        if not command.options.driver:
            return response.res_server_error(
                body={
                    'error':
                    'Could not find a valid driver to use for command: ' +
                    str(command.command)
                })

        command.command = Command(command=command.command,
                                  request=command.request,
                                  parameters=command.parameters,
                                  options=command.options)

        if command.options.driver.isDriverCommandable():
            if command.options.driver.isApiAuthenticated():
                self.authenticate(command)
            response = command.options.driver.handleDriverCommand(
                command.command)
        else:
            return response.res_server_error(
                body={
                    'error':
                    'Driver `' + command.options.driver.getDriverName() +
                    '` does not support this operation: ' +
                    str(command.command)
                })

        return response