Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/raw data #165

Merged
merged 9 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions axonius_api_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from .auth import ApiKey
from .connect import Connect
from .http import Http
from .features import Features
except Exception: # pragma: no cover
raise

Expand Down Expand Up @@ -96,6 +97,7 @@
"WizardText",
"ActivityLogs",
"ApiEndpoints",
"Features",
# modules
"api",
"auth",
Expand Down
17 changes: 3 additions & 14 deletions axonius_api_client/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,9 @@
from .api_endpoints import ApiEndpoints
from .assets import Devices, Users
from .enforcements import Enforcements
from .system import (
ActivityLogs,
Dashboard,
Instances,
Meta,
RemoteSupport,
SettingsGlobal,
SettingsGui,
SettingsIdentityProviders,
SettingsLifecycle,
Signup,
SystemRoles,
SystemUsers,
)
from .system import (ActivityLogs, Dashboard, Instances, Meta, RemoteSupport,
SettingsGlobal, SettingsGui, SettingsIdentityProviders,
SettingsLifecycle, Signup, SystemRoles, SystemUsers)
from .wizards import Wizard, WizardCsv, WizardText

__all__ = (
Expand Down
36 changes: 17 additions & 19 deletions axonius_api_client/api/adapters/cnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@
from typing import Callable, List, Optional, Union

from ...constants.adapters import CNX_SANE_DEFAULTS
from ...exceptions import (
CnxAddError,
CnxGoneError,
CnxTestError,
CnxUpdateError,
ConfigInvalidValue,
ConfigRequired,
NotFoundError,
)
from ...parsers.config import (
config_build,
config_default,
config_empty,
config_info,
config_required,
config_unchanged,
config_unknown,
)
from ...exceptions import (CnxAddError, CnxGoneError, CnxTestError,
CnxUpdateError, ConfigInvalidValue, ConfigRequired,
NotFoundError)
from ...parsers.config import (config_build, config_default, config_empty,
config_info, config_required, config_unchanged,
config_unknown)
from ...parsers.tables import tablize_cnxs, tablize_schemas
from ...tools import json_dump, json_load, listify, pathlib
from .. import json_api
Expand Down Expand Up @@ -869,11 +857,21 @@ def hook(http, response, **kwargs):
response_json = json_dump(obj=data, error=False)
response_txt = f"Connectivty test failure, response:\n{response_json}"

status = data.get("status")
etype = data.get("type")
emsg = data.get("message")
errors = listify(data.get("errors"))

exc = None
pre = ""

if status == "error":
exc = ConfigRequired
pre = f"Generic error of type {etype!r} with message: {emsg}"
msg = f"{pre} {config_txt}"
err = tablize_schemas(schemas=cnx_schemas, err=msg)
raise exc(f"{response_txt}\n{err}")

# PBUG: this nasty footwork should be server side
for error in errors:
if not isinstance(error, dict):
Expand All @@ -893,7 +891,7 @@ def hook(http, response, **kwargs):
if not exc and data.get("meta") == 400:
pre = "Invalid/missing domain or other connection parameter"
exc = ConfigRequired
# PBUG: with domain=None, {'data': null, 'meta': 400}
# PBUG: sometimes returns {'data': null, 'meta': 400}

if exc:
msg = f"{pre} {config_txt}"
Expand Down
39 changes: 39 additions & 0 deletions axonius_api_client/api/api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ class SavedQueries(BaseData):
response_model_cls=json_api.generic.Metadata,
)

delete_4_3: ApiEndpoint = ApiEndpoint(
method="delete",
path="api/V4.0/{asset_type}/views/view/{uuid}",
request_schema_cls=json_api.saved_queries.SavedQueryDeleteSchema,
request_model_cls=json_api.saved_queries.SavedQueryDelete,
response_schema_cls=json_api.generic.MetadataSchema,
response_model_cls=json_api.generic.Metadata,
)


@dataclasses.dataclass
class Instances(BaseData):
Expand Down Expand Up @@ -191,6 +200,36 @@ class Instances(BaseData):
response_model_cls=json_api.instances.FactoryReset,
)

admin_script_upload_start: ApiEndpoint = ApiEndpoint(
method="put",
path="api/V4.0/settings/configuration/upload_file",
request_schema_cls=None,
request_model_cls=None,
response_schema_cls=None,
response_model_cls=None,
response_as_text=True,
)

admin_script_upload_chunk: ApiEndpoint = ApiEndpoint(
method="patch",
path="api/V4.0/settings/configuration/upload_file?patch={uuid}",
request_schema_cls=None,
request_model_cls=None,
response_schema_cls=None,
response_model_cls=None,
response_as_text=True,
)

admin_script_execute: ApiEndpoint = ApiEndpoint(
method="put",
path="api/V4.0/settings/configuration/execute/{uuid}",
request_schema_cls=None,
request_model_cls=None,
response_schema_cls=None,
response_model_cls=None,
response_as_text=True,
)


@dataclasses.dataclass
class CentralCore(BaseData):
Expand Down
2 changes: 2 additions & 0 deletions axonius_api_client/api/assets/asset_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def get_generator(
fields_fuzzy: Optional[Union[List[str], str]] = None,
fields_default: bool = True,
fields_root: Optional[str] = None,
fields_error: bool = True,
max_rows: Optional[int] = None,
max_pages: Optional[int] = None,
row_start: int = 0,
Expand Down Expand Up @@ -273,6 +274,7 @@ def get_generator(
fields_default=fields_default,
fields_root=fields_root,
fields_fuzzy=fields_fuzzy,
fields_error=fields_error,
)

if sort_field:
Expand Down
30 changes: 17 additions & 13 deletions axonius_api_client/api/assets/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
from cachetools import TTLCache, cached
from fuzzyfinder import fuzzyfinder

from ...constants.fields import (
AGG_ADAPTER_ALTS,
AGG_ADAPTER_NAME,
FUZZY_SCHEMAS_KEYS,
GET_SCHEMA_KEYS,
GET_SCHEMAS_KEYS,
PRETTY_SCHEMA_TMPL,
)
from ...constants.fields import (AGG_ADAPTER_ALTS, AGG_ADAPTER_NAME,
FUZZY_SCHEMAS_KEYS, GET_SCHEMA_KEYS,
GET_SCHEMAS_KEYS, PRETTY_SCHEMA_TMPL)
from ...exceptions import ApiError, NotFoundError
from ...parsers.fields import parse_fields
from ...parsers.fields import parse_fields, schema_custom
from ...tools import listify, split_str, strip_right
from .. import json_api
from ..api_endpoints import ApiEndpoints
Expand Down Expand Up @@ -71,6 +66,7 @@ def validate(
fields_manual: Optional[Union[List[str], str]] = None,
fields_fuzzy: Optional[Union[List[str], str]] = None,
fields_default: bool = True,
fields_error: bool = True,
fields_root: Optional[str] = None,
) -> List[dict]:
"""Get the fully qualified field names for getting asset data.
Expand Down Expand Up @@ -120,11 +116,11 @@ def add(items):
add(self.get_field_names_root(adapter=fields_root))

add(fields_manual)
add(self.get_field_names_eq(value=fields))
add(self.get_field_names_eq(value=fields, fields_error=fields_error))
add(self.get_field_names_re(value=fields_regex))
add(self.get_field_names_fuzzy(value=fields_fuzzy))

if not selected:
if fields_error and not selected:
raise ApiError("No fields supplied, must supply at least one field")

return selected
Expand Down Expand Up @@ -251,7 +247,9 @@ def get_field_names_re(self, value: Union[str, List[str]], key: str = "name_qual
matches += [x for x in names if x not in matches]
return matches

def get_field_names_eq(self, value: Union[str, List[str]], key: str = "name_qual") -> List[str]:
def get_field_names_eq(
self, value: Union[str, List[str]], key: str = "name_qual", fields_error: bool = True
) -> List[str]:
"""Get field names that equal a value.

Examples:
Expand Down Expand Up @@ -287,7 +285,13 @@ def get_field_names_eq(self, value: Union[str, List[str]], key: str = "name_qual
adapter = self.get_adapter_name(value=adapter_name)
for name in names:
schemas = fields[adapter]
schema = self.get_field_schema(value=name, schemas=schemas)
try:
schema = self.get_field_schema(value=name, schemas=schemas)
except Exception:
if fields_error:
raise
schema = schema_custom(name=name)

match = schema[key] if key else schema
if match not in matches:
matches.append(match)
Expand Down
32 changes: 23 additions & 9 deletions axonius_api_client/api/assets/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from typing import Generator, List, Optional, Union

from ...constants.api import MAX_PAGE_SIZE
from ...exceptions import NotFoundError
from ...exceptions import NotFoundError, ResponseError
# from ...features import Features
from ...parsers.tables import tablize_sqs
from ...tools import check_gui_page_size, listify
from .. import json_api
Expand Down Expand Up @@ -365,14 +366,27 @@ def _delete(self, uuid: str) -> json_api.generic.Metadata:
Args:
ids: list of uuid's to delete
"""
api_endpoint = ApiEndpoints.saved_queries.delete
request_obj = api_endpoint.load_request()
return api_endpoint.perform_request(
http=self.auth.http,
request_obj=request_obj,
asset_type=self.parent.ASSET_TYPE,
uuid=uuid,
)
# NEW_IN: 05/31/21 cortex/develop
try:
api_endpoint = ApiEndpoints.saved_queries.delete
request_obj = api_endpoint.load_request()
return api_endpoint.perform_request(
http=self.auth.http,
request_obj=request_obj,
asset_type=self.parent.ASSET_TYPE,
uuid=uuid,
)
except ResponseError as exc:
if exc.is_incorrect_type:
api_endpoint = ApiEndpoints.saved_queries.delete_4_3
request_obj = api_endpoint.load_request()
return api_endpoint.perform_request(
http=self.auth.http,
request_obj=request_obj,
asset_type=self.parent.ASSET_TYPE,
uuid=uuid,
)
raise

def _get(
self, limit: int = MAX_PAGE_SIZE, offset: int = 0
Expand Down
3 changes: 2 additions & 1 deletion axonius_api_client/api/json_api/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from ...parsers.config import parse_schema
from ...tools import listify, longest_str, strip_right
from .base import BaseModel, BaseSchema, BaseSchemaJson
from .custom_fields import SchemaBool, SchemaDatetime, dump_date, get_field_dc_mm
from .custom_fields import (SchemaBool, SchemaDatetime, dump_date,
get_field_dc_mm)
from .generic import Metadata, MetadataSchema
from .system_settings import SystemSettingsUpdateSchema

Expand Down
Loading