[docs]classArtifact:"""API interface for managing SOAR artifacts. This class provides methods to create and manage artifacts within SOAR. Attributes: soar_client (SOARClient): The SOAR client instance for API communication. """
[docs]def__init__(self,soar_client:"SOARClient")->None:"""Initialize the Artifact API interface. Sets up the artifact interface with default artifact properties and initializes internal artifact storage for unauthenticated clients. Args: soar_client (SOARClient): The SOAR client instance for API communication. """self.soar_client:SOARClient=soar_clientself._artifact_common={ph_jsons.APP_JSON_LABEL:ph_consts.APP_DEFAULT_ARTIFACT_LABEL,ph_jsons.APP_JSON_TYPE:ph_consts.APP_DEFAULT_ARTIFACT_TYPE,ph_jsons.APP_JSON_DESCRIPTION:"Artifact added by sdk app",ph_jsons.APP_JSON_RUN_AUTOMATION:False,# Don't run any playbooks, when this artifact is added}self.__artifacts:dict[int,dict]={}
[docs]defcreate(self,artifact:dict)->int:"""Create a new artifact in the SOAR platform. Creates an artifact with the provided data, applying default values for common fields if not specified. For authenticated clients, the artifact is created via the REST API. For unauthenticated clients, the artifact is stored locally for testing purposes. Args: artifact (dict): The artifact data to create. Must be JSON-serializable. Common fields include: - container_id: ID of the container to associate with - cef: Common Event Format data - label: Artifact label (defaults to 'artifact') - type: Artifact type (defaults to 'generic') - description: Human-readable description Returns: int: The ID of the created artifact. Raises: ActionFailure: If the artifact data cannot be serialized to JSON. SoarAPIError: If the API request fails or the artifact cannot be created. For unauthenticated clients, raised if no container_id is provided. Example: >>> artifact_data = { ... "container_id": 123, ... "cef": {"sourceAddress": "192.168.1.1"}, ... "label": "ip", ... "type": "network", ... } >>> artifact_id = artifact_api.create(artifact_data) >>> print(f"Created artifact with ID: {artifact_id}") """artifact.update({k:vfork,vinself._artifact_common.items()if(notartifact.get(k))})try:json.dumps(artifact)exceptTypeErrorase:error_msg=(f"Artifact could not be converted to a JSON string. Error: {e!s}")raiseActionFailure(error_msg)fromeifis_client_authenticated(self.soar_client.client):endpoint="rest/artifact"try:response=self.soar_client.post(endpoint,json=artifact)exceptExceptionase:error_msg=f"Failed to add artifact: {e}"raiseSoarAPIError(error_msg)fromeresp_data=response.json()if"existing_artifact_id"inresp_data:logger.info("Artifact already exists")returnresp_data["existing_artifact_id"]if"id"inresp_data:returnresp_data["id"]msg_cause=resp_data.get("message","NONE_GIVEN")message=f"Artifact addition failed, reason from server: {msg_cause}"raiseSoarAPIError(message)else:if"container_id"notinartifact:message="Artifact addition failed, no container ID given"raiseSoarAPIError(message)next_artifact_id=(max(self.__artifacts.keys())ifself.__artifactselse0)+1self.__artifacts[next_artifact_id]=artifactreturnnext_artifact_id