argus_api.helpers package

Submodules

argus_api.helpers.authentication module

argus_api.helpers.authentication.csrf_token_for(url: str, key: str) → str[source]

Generates a CSRF token for an API URL

Return type:

str

Parameters:
  • url (str) – URL endpoint
  • key (str) – User’s session key
Returns:

CSRF token

argus_api.helpers.authentication.with_api_key(api_key: str) → dict[source]

Authenticates the user towards the API with an API key

Return type:dict
Parameters:api_key (str) – User’s API key
Returns:Authentication headers
argus_api.helpers.authentication.with_authentication(mode: str = 'ldap', base_url: str = None, username: str = '', api_key: str = '') → <built-in function callable>[source]

Creates a decorator that can be used by plugins to have authentication handled for them, by calling with_credentials or with_api_key and then passing the authentication headers to the decorated function as authentication.

Generated API methods accept authentication parameter, so after decorating a function with this, the authentication argument can be passed directly to any API method.

Parameters:mode (str) – LDAP, Radius, Password or API key
Returns:decorator function
argus_api.helpers.authentication.with_credentials(username: str = '', password: str = '', method: str = 'ldap', base_url: str = None) → <built-in function callable>[source]

Authenticates the user with the API via TOTP, Radius or LDAP, and returns a function that creates the correct headers with CSRF token for a given URL, using the user’s sessionKey and cookie.

Parameters:
  • base_url (str) – API URL
  • method (str) – Authentication method
  • username (str) – The user’s username
  • password (str) – The user’s password
Returns:

Authentication data

argus_api.helpers.generator module

argus_api.helpers.generator.write_endpoints_to_disk(endpoints, output, with_plugin_decorators=False) → None[source]

Outputs the directory structure with all endpoints

Parameters:endpoints (list) – List of endpoints generated with build_endpoint_structure

argus_api.helpers.http module

HTTP Helper methods and constants

argus_api.helpers.log module

argus_api.helpers.log.plugin(self, message, *args, **kws)[source]

Custom log level for plugins

argus_api.helpers.module_loader module

argus_api.helpers.module_loader.import_submodules(package: str, exclude_name: str = None, recursive=True) → dict[source]

Import all submodules of a module, recursively.

This is used to import all APIs when Argus is loaded, so that the commands become registered as plugins, but can also be used to recursively import any other package where you want every single file to load.

TODO: Plugin loader can use this function to recursively load argus_plugins package!

Return type:

dict

Parameters:
  • package_name – Package name, e.g “argus_api.api”
  • exclude_name (str) – Any module containing this string will not be imported

argus_api.helpers.parsers module

Supporting methods and classes for parsing request methods from API schemas

class argus_api.helpers.parsers.RequestMethod(url: str, name: str, method: str, description: str, parameters: dict, response: list = None, errors: list = None)[source]

Bases: object

Container for a RequestMethod, accepts all building blocks for a request method parsed by any parser, and provides functionality for creating an actual python method as a string that can be printed to file, or load that string as an actual function that can be executed.

When loaded as an executable function, this function will also have attributes that can be used in tests, such as @function.success(), @function.unauthorized(), which will intercept any calls made to the URL declared in this request method, and respond with a fake response.

For example, to write a test for alarms.get_alarms, you might want to decorate your function with:

@alarms.get_alarms.success() def my_method():

# Receives a fake response, no call to the server will be created: response = alarms.get_alarms()

This class is an ABC, meaning it should not be used in its raw form. Parsers should subclass RequestMethod, and are responsible for overloading _fake_response (to parse and generate the response object into a fake response), and for passing the correct arguments to initialization.

For example, you might want to have different parsers, and therefore different ways of parsing request methods, e.g class RABLRequestMethod(RequestMethod, Swagger2RequestMethod(RequestMethod), class OpenAPI3RequestMethod(RequestMethod).

decorator_template(status_code: int = 200, prefix: str = '') → str[source]

Creates a decorator for successful response and returns a decorator template that can be printed to file and used as a function decorator to fake calls to this method.

Return type:str
Parameters:status_code (int) – 200, 403, 401, or 404
Returns:String template
docstring

Creates the docstring for this request method by combining description, parameters, exceptions raised and response

Returns:String to use as docstring
fake_response() → dict[source]

Returns a fake response for this method

Raises:AttributeError – When the _fake_response method has not been overloaded
Returns:A dict with fake data
to_dict

Returns the request method as a dict that can be used for templating

to_function

Wrapper around _as_function for retrieving a standalone function When this property is used, the function will be attached to the fake scope ‘runtime_generated_api’, rather than the scope of the given module.

To attach this function on a class, or module, use .as_method_on(cls)

to_method_on(cls)[source]

Attaches this as a function on a class / module

to_template

Creates a function from the Request Method specification

url_regex

Returns a regex for matching the URL including its parameters

argus_api.helpers.tests module

Test helpers for the API

argus_api.helpers.tests.fake_data_for(python_type: str)[source]

Generates fake data for a given type, where type can be any basic python type, such as int, str, float, bool; but it also supports more specific types, such as url, username, and email

Parameters:python_type (str) – String representation of python type
Returns:Generated fake value
argus_api.helpers.tests.fake_response(response_definition, key=None)[source]

Recursive method that traverses a dict response and generates fake data for each key, faking a successful response

argus_api.helpers.tests.response(url: str, status_code: int = 200, method: str = 'GET', json=None) → <built-in function callable>[source]

Creates a decorator that can be used to mock the given URL and intercept calls to it with a response of the given status code and JSON.

Parameters:
  • url (str) – URL to intercept
  • status_code (int) – HTTP status to respond with
  • json (dict) – JSON body to respond with (optional)

argus_api.helpers.urls module

argus_api.helpers.urls.extract_url_parameters(url)[source]

Extracts {parameter} from URL and returns the parameters

Parameters:url – A URL
Returns:Names of all URL based parameters
argus_api.helpers.urls.remove_url_parameters(url)[source]

Removes {parameter} from URL and returns the URL without the parameters

Parameters:url – A URL
Returns:URL without parameters

Module contents