Ejemplo n.º 1
0
    def update_docker_document(self, docker_document_type, docker_document_id,
                               update, docker_server_id):
        """
        Updates a docker document in the DB.

        Args:
            docker_document_type (str): document type, eg. "Container", "Image"
            docker_document_id (str): the ID of the docker document.
            update (dict): a dictionary that represents which values needs to be updated.
            docker_server_id (str): docker server ID.

        update examples:
            {
                "Containers.$.State": "stopped"
            }

            means update the State key to a "stopped" state.
        """
        amazon_document = self.get_amazon_document(
            resource_id=docker_server_id)

        try:
            self.collection_type.update_one(
                filter={
                    global_constants.ID:
                    amazon_document[global_constants.ID],
                    f"{docker_document_type}s.{global_constants.ID}":
                    docker_document_id
                },
                update={global_constants.SET: update})
        except Exception as error:
            raise UpdateDatabaseError(document=amazon_document,
                                      error_msg=str(error))
Ejemplo n.º 2
0
    def add_metasploit_document(self, amazon_resource_id, metasploit_document):
        """
        Adds a new metasploit document to the DB.

        Args:
            amazon_resource_id (str): amazon resource ID.
            metasploit_document (str): new metasploit document.

        Raises:
            UpdateDatabaseError: in case updating the DB with a new metasploit document fails.
        """
        amazon_document = self.get_amazon_document(
            resource_id=amazon_resource_id)

        try:
            self.collection_type.update_one(filter={
                global_constants.ID:
                amazon_document[global_constants.ID]
            },
                                            update={
                                                global_constants.ADD_TO_SET: {
                                                    "Metasploit":
                                                    metasploit_document
                                                }
                                            })
        except Exception as error:
            raise UpdateDatabaseError(document=amazon_document,
                                      error_msg=str(error))
Ejemplo n.º 3
0
    def delete_docker_document(self, amazon_resource_id, docker_resource_id,
                               docker_document_type):
        """
        Deletes a docker document from DB.

        Args:
            amazon_resource_id (str): amazon resource ID.
            docker_resource_id (str): docker resource ID.
            docker_document_type (str): document type, eg. "Container", "Network", "Image"

        update example:

            test.update_one(
                filter={
                    "_id": "i-086d96f9de57d095b"
                },
                update={
                "$pull": {
                    "Containers": {
                        "_id": 2
                    }
                }
            }
        )

        Remove container document that its ID is 2.


        Raises:
            UpdateDatabaseError: in case pulling docker document from the array has failed.

        """
        docker_document = self.get_docker_document(
            amazon_resource_id=amazon_resource_id,
            docker_resource_id=docker_resource_id,
            type=docker_document_type)

        try:
            self.collection_type.update_one(
                filter={global_constants.ID: amazon_resource_id},
                update={
                    global_constants.PULL: {
                        f"{docker_document_type}s": {
                            global_constants.ID: docker_resource_id
                        }
                    }
                })
        except Exception as error:
            raise UpdateDatabaseError(document=docker_document,
                                      error_msg=str(error))
Ejemplo n.º 4
0
    def add_docker_document(self, amazon_resource_id, docker_document_type,
                            new_docker_document):
        """
        Adds a docker document to the DB.

        Args:
            amazon_resource_id (str): amazon resource ID.
            docker_document_type (str): document type, eg. "Container", "Network", "Image"
            new_docker_document (dict): the new docker document.

        new_docker_document example:
            {
                "_id": 1,
                "image": "metasploit_image",
                "name": "Awesome",
                "status": "running",
                "ports": [55555, 55556]
            }

        Raises:
            UpdateDatabaseError: in case updating the DB with a new docker document fails.
        """
        amazon_document = self.get_amazon_document(
            resource_id=amazon_resource_id)

        try:
            self.collection_type.update_one(filter={
                global_constants.ID:
                amazon_document[global_constants.ID]
            },
                                            update={
                                                global_constants.ADD_TO_SET: {
                                                    f"{docker_document_type}s":
                                                    new_docker_document
                                                }
                                            })
        except Exception as error:
            raise UpdateDatabaseError(document=amazon_document,
                                      error_msg=str(error))
Ejemplo n.º 5
0
    def update_amazon_document(self, amazon_resource_id, update):
        """
        Updates the amazon document

        Args:
            amazon_resource_id (str): amazon resource ID.
            update (dict): a dictionary that represents which values needs to be updated.

        update examples:

            update = {"IpPermissionsInbound": ip_permissions}

            means update IpInboundPermission of a security group to a new ip permissions
        """
        amazon_document = self.get_amazon_document(
            resource_id=amazon_resource_id)

        try:
            self.collection_type.update_one(
                filter={global_constants.ID: amazon_resource_id},
                update={global_constants.SET: update})
        except Exception as error:
            raise UpdateDatabaseError(document=amazon_document,
                                      error_msg=str(error))