def _create_parser(self, services): parser = ALCliArgsParser( services, f"alcli/{alcli_version} Python/{platform.python_version()}" f" almdrlib/{almdrlib_version}" f" alsdkdefs/{alsdkdefs_version}", "Alert Logic CLI Utility", prog="alcli") # Add Global Options parser.add_argument('--access_key_id', dest='access_key_id', default=None) parser.add_argument('--secret_key', dest='secret_key', default=None) parser.add_argument('--profile', dest='profile', default=None) parser.add_argument('--residency', dest='residency', default=None, choices=Residency.list_residencies()) parser.add_argument('--global_endpoint', dest='global_endpoint', default=None, choices=Region.list_endpoints()) parser.add_argument('--query', dest='query', default=None) parser.add_argument('--debug', dest='debug', default=False, action="store_true") return parser
def __init__(self, access_key_id=None, secret_key=None, aims_token=None, account_id=None, profile=None, global_endpoint=None, residency="us", raise_for_status=True): """ :param region: a Region object :param access_key_id: your Alert Logic ActiveWatchaccess_key_id or username :param secret_key: your Alert Logic ActiveWatchsecret_key or password :param aims_token: aims_token to be used for authentication. If aims_token is specified, access_key_id and secret_key paramters are ignored : param account_id: Alert Logic Account ID to initialize a session for. Unless account_id is provided explicitly during service connection initialization, this account id is used. If this parameter isn't specified, the account id of the access_key_id is used. :param: profile: name of the profile section of the configuration file :param: global_endpoint: Name of the global endpoint. 'production' or 'integration' are the only valid values :param residency: Data residency name to perform data residency dependend actions. Currently, 'default', 'us' and 'emea' are the only valid entries :param raise_for_status: Raise an exception for failed http requests instead of returning response object """ self._config = Config(access_key_id=access_key_id, secret_key=secret_key, account_id=account_id, profile=profile, global_endpoint=global_endpoint, residency=residency) self._token = None self._defaults = None self._account_id = self._config.account_id self._residency = self._config.residency self._global_endpoint = self._config.global_endpoint self._global_endpoint_url = Region.get_global_endpoint( self._global_endpoint) self._raise_for_status = raise_for_status # Setup session object self._session = requests.Session() retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503]) self._session.mount('https://', HTTPAdapter(max_retries=retries)) if aims_token: self._token = aims_token else: self._access_key_id, self._secret_key = self._config.get_auth()
def get_url(self, service_name, account_id=None): try: response = self._session.get( Region.get_endpoint_url(self._global_endpoint_url, service_name, account_id or self.account_id, self.residency)) response.raise_for_status() except requests.exceptions.HTTPError as e: raise AuthenticationException( f"invalid http response from endpoints service {e}") return "https://{}".format(response.json()[service_name])
def get_url(self, service_name, account_id=None): if self._global_endpoint == 'aesolo': # Does not support residency return self._global_endpoint_url if self._global_endpoint == "map": return self.get_mapped_url(service_name, account_id) try: response = self.request( 'get', Region.get_endpoint_url(self._global_endpoint_url, service_name, account_id or self.account_id, self.residency), ) response.raise_for_status() except requests.exceptions.HTTPError as e: raise AuthenticationException( f"invalid http response from endpoints service {e}") return "https://{}".format(response.json()[service_name])
def _init_session(self, *args, **kwargs): """ Initialize session object based on the kwargs provided """ if not len(kwargs): return access_key_id = kwargs.get('access_key_id') secret_key = kwargs.get('secret_key') account_id = kwargs.get('account_id') profile = kwargs.get('profile') global_endpoint = kwargs.get('global_endpoint') residency = kwargs.get('residency', 'default') aims_token = kwargs.get('aims_token') self._config = Config( access_key_id=access_key_id, secret_key=secret_key, account_id=account_id, profile=profile, global_endpoint=global_endpoint, residency=residency ) self._account_id = self._config.account_id self._residency = self._config.residency self._global_endpoint = self._config.global_endpoint self._endpoint_map = self._config.endpoint_map self._global_endpoint_url = Region.get_global_endpoint( self._global_endpoint) self._raise_for_status = kwargs.get('raise_for_status') if aims_token: self._token = aims_token else: self._access_key_id, self._secret_key = self._config.get_auth() logger.debug( "Initialized session. " f"access_key_id={self._access_key_id}, " f"account_id={self._account_id}, " f"profile={profile}, " f"global_endpoint={self._global_endpoint}, " f"residency={self._residency}" )
def make_options(self): yield f'{self.bold("OPTIONS")}' yield f'\t{self.bold("--debug")} (boolean)' yield '' yield '\tTurn on debug logging.' yield '' yield f'\t{self.bold("--query")} (string)' yield '' yield '\tA JMESPath query to use in filtering the response data.' yield '' yield f'\t{self.bold("--global_endpoint")} (string)' yield '' yield f'\tUse specific Alert Logic backend.' yield '' for endpoint in Region.list_endpoints(): yield f'\to {endpoint}' yield '' yield f'\t{self.bold("--residency")} (string)' yield '' yield f'\tUse a specific data residency.' yield '' for residency in Residency.list_residencies(): yield f'\to {residency}' yield '' yield f'\t{self.bold("--profile")} (string)' yield '' yield f'\tUse a specific profile from your configuration file.' yield '' yield f'\t{self.bold("--version")} (string)' yield '' yield '\tDisplay the version of alcli.' yield ''