Esempio n. 1
0
def login(base_url=None,
          username=None,
          password=None,
          verify_ssl=True,
          proxy=None,
          debug=False):
    configuration_orig = Configuration()
    configuration_orig.host = base_url or os.getenv('DEMISTO_BASE_URL', None)
    if isinstance(configuration_orig.host, str):
        configuration_orig.host = configuration_orig.host.rstrip('/')
    configuration_orig.verify_ssl = verify_ssl
    configuration_orig.proxy = proxy
    configuration_orig.debug = debug
    api_client = ApiClient(configuration_orig)
    api_client.user_agent = 'demisto-py/' + __version__
    api_instance = demisto_api.DefaultApi(api_client)
    body = {"user": username, "password": password}
    res = generic_request_func(self=api_instance,
                               path='/',
                               method='GET',
                               body=body,
                               accept='application/json',
                               content_type='application/json')
    cookies = res[2]['Set-Cookie']
    cookie_jar = cookies.split(';')
    xsrf_token_raw = cookie_jar[0]
    xsrf_token = xsrf_token_raw.replace('XSRF-TOKEN=', '')
    configuration = Configuration()
    configuration.host = base_url or os.getenv('DEMISTO_BASE_URL', None)
    if isinstance(configuration.host, str):
        configuration.host = configuration.host.rstrip('/')
    configuration.verify_ssl = verify_ssl
    configuration.proxy = proxy
    configuration.debug = debug
    api_client = ApiClient(configuration,
                           header_name="X-XSRF-TOKEN",
                           header_value=xsrf_token,
                           cookie=cookies)
    api_client.user_agent = 'demisto-py/' + __version__
    mid_client = demisto_api.DefaultApi(api_client)
    second_call = generic_request_func(self=mid_client,
                                       path='/login',
                                       method='POST',
                                       body=body,
                                       accept='application/json',
                                       content_type='application/json')
    updated_cookies = cookies + '; ' + second_call[2]['Set-Cookie']
    mid_api_client = ApiClient(configuration,
                               header_name="X-XSRF-TOKEN",
                               header_value=xsrf_token,
                               cookie=updated_cookies)
    mid_api_client.user_agent = 'demisto-py/' + __version__
    final_api_client = demisto_api.DefaultApi(mid_api_client)

    return final_api_client
Esempio n. 2
0
def configure(base_url=None,
              api_key=None,
              verify_ssl=True,
              proxy=None,
              debug=False):
    """
    This wrapper provides an easier to use method of configuring the API client. The base
    Configuration method is still exposed if you wish to further configure the API Client.

    To avoid hard coding configurations in your code, it is also possible to specify api_key 
    and base_url as the following environment variables (env variables will be used if parameters are not specified):

    * DEMISTO_BASE_URL
    * DEMISTO_API_KEY
    
    :param base_url: str - Base url of your Demisto instance.
    :param api_key: str - API key generated by your instance.
    :param verify_ssl: bool - Indicates if valid SSLs are required for connection.
    :param proxy: dict - Dict object of your proxy settings.
    :param debug: bool - Include verbose logging.
    :return: Returns an API client configuration identical to the Configuration() method.
    """
    configuration = Configuration()
    configuration.api_key['Authorization'] = api_key or os.getenv(
        'DEMISTO_API_KEY', None)
    configuration.host = base_url or os.getenv('DEMISTO_BASE_URL', None)
    configuration.verify_ssl = verify_ssl
    configuration.proxy = proxy
    configuration.debug = debug
    if not configuration.host:
        raise ValueError(
            'You must specify base_url either as a parameter or via env variable: DEMISTO_BASE_URL'
        )
    if not configuration.api_key.get('Authorization', None):
        raise ValueError(
            'You must specify either api_key as parameters or via env variable: DEMISTO_API_KEY'
        )
    api_client = ApiClient(configuration)
    api_client.user_agent = 'demisto-py/' + __version__
    api_instance = demisto_api.DefaultApi(api_client)
    return api_instance
Esempio n. 3
0
def configure(base_url=None,
              api_key=None,
              verify_ssl=None,
              proxy=None,
              username=None,
              password=None,
              ssl_ca_cert=None,
              debug=False):
    """
    This wrapper provides an easier to use method of configuring the API client. The base
    Configuration method is still exposed if you wish to further configure the API Client.

    To avoid hard coding configurations in your code, it is possible to specify configruation params
    as the following environment variables (env variables will be used if parameters are not specified):

    * DEMISTO_BASE_URL
    * DEMISTO_API_KEY
    * DEMISTO_USERNAME
    * DEMISTO_PASSWORD
    * DEMISTO_VERIFY_SSL (true/false. Default: true)
    * SSL_CERT_FILE (specify an alternate certificate bundle)

    :param base_url: str - Base url of your Demisto instance.
    :param api_key: str - API key generated by your instance.
    :param username: str - Username of the user account.
    :param password: str - Password of the user account.
    :param verify_ssl: bool - Indicates if valid SSLs are required for connection. If not specified (None)
        will default to True.
    :param proxy: dict - Dict object of your proxy settings.
    :param ssl_ca_cert: str - specify an alternate certificate bundle
    :param debug: bool - Include verbose logging.
    :return: Returns an API client configuration identical to the Configuration() method.
    """
    if api_key is None:
        api_key = os.getenv('DEMISTO_API_KEY')
    if username is None:
        username = os.getenv('DEMISTO_USERNAME')
    if password is None:
        password = os.getenv('DEMISTO_PASSWORD')
    if ssl_ca_cert is None:
        ssl_ca_cert = os.getenv('SSL_CERT_FILE')
    if verify_ssl is None:
        verify_env = os.getenv('DEMISTO_VERIFY_SSL')
        if verify_env:
            verify_ssl = verify_env.lower() not in ['false', '0', 'no']
        else:
            verify_ssl = True
    configuration = Configuration()
    configuration.api_key['Authorization'] = api_key
    configuration.host = base_url or os.getenv('DEMISTO_BASE_URL', None)
    if isinstance(configuration.host, str):
        configuration.host = configuration.host.rstrip('/')
    configuration.verify_ssl = verify_ssl
    configuration.proxy = proxy
    configuration.debug = debug
    configuration.ssl_ca_cert = ssl_ca_cert

    if not configuration.host:
        raise ValueError(
            'You must specify base_url either as a parameter or via env variable: DEMISTO_BASE_URL'
        )
    if not api_key and not username:
        raise ValueError(
            'You must specify either api_key or username/password either as parameters or use env variables:\n'
            '* DEMISTO_API_KEY\n'
            '* DEMISTO_USERNAME\n'
            '* DEMISTO_PASSWORD')
    if username is None:
        api_client = ApiClient(configuration)
        api_client.user_agent = 'demisto-py/' + __version__
        api_instance = demisto_api.DefaultApi(api_client)
        return api_instance
    else:
        api_instance = login(base_url=base_url,
                             username=username,
                             password=password,
                             verify_ssl=verify_ssl,
                             proxy=proxy,
                             debug=debug)
        return api_instance