예제 #1
0
    def test_clear_markings(self):
        i = indicator.Indicator()
        api.add_marking(i, MARKING)

        self.assertTrue(api.is_marked(i))
        api.clear_markings(i)
        self.assertEqual(False, api.is_marked(i))
예제 #2
0
    def test_remove_entity(self):
        i = indicator.Indicator()
        api.add_marking(i, MARKING)

        self.assertTrue(api.is_marked(i))
        api.remove_marking(i, MARKING)
        self.assertFalse(api.is_marked(i))
예제 #3
0
    def test_not_found(self):
        i = indicator.Indicator()
        api.add_marking(i, MARKING)

        self.assertRaises(
            KeyError,
            api.remove_marking,
            i,
            data_marking.MarkingSpecification()
        )
예제 #4
0
    def test_add_builtin(self):
        i = indicator.Indicator()
        i.id_ = "foo"

        marked = api.add_marking(i.id_, MARKING)
        self.assertEqual(marked, i.id_)
        self.assertTrue(api.is_marked(marked))
예제 #5
0
    def test_remove_builtin(self):
        i = indicator.Indicator()
        i.id_ = "foo"

        i.id_ = api.add_marking(i.id_, MARKING)
        self.assertTrue(api.is_marked(i.id_))

        api.remove_marking(i.id_, MARKING)
        self.assertEqual(False, api.is_marked(i.id_))
예제 #6
0
def _mark_list(lst):
    for idx, item in enumerate(lst):
        lst[idx] = api.add_marking(item, MARKING)
예제 #7
0
    def add_marking(self, markable, marking, descendants=False):
        """Add the `marking` to the `markable` field/object. If `markable`
        is a built-in immutable Python type, it will be coerced into a
        stixmarx.api.types datatype.

        Note:
            The add_marking() function may not always be able to apply the
            markings in-place. Users should set the input field to the return
            object after calling add_marking().

        Note:
            Use this method to apply null markings. This is, markings that are
            present within the document but, do not apply to any field. The
            `markable` parameter MUST be None.

        Example:
            >>> print type(indicator.title)
            <type 'str'>
            >>> marked_title = add_marking(indicator.title, marking)
            >>> print type(marked_title)
            <class 'stixmarx.api.types.MarkableBytes'>
            >>> indicator.title = marked_title  # set the title to the return value

        Example:
            >>> print type(indicator.timestamp)
            <type 'datetime.datetime'>
            >>> marked_timestamp = add_marking(indicator.timestamp, marking)
            >>> print type(marked_timestamp)
            <class 'stixmarx.api.types.MarkableDateTime'>
            >>> indicator.timestamp = marked_timestamp # set timestamp to the return value

        Example:
            >>> print type(indicator)
            <class 'stix.indicator.indicator.Indicator'>
            >>> marked_indicator = add_marking(indicator, marking, descendants=True) # The equivalent of a component marking
            >>> print type(marked_indicator)
            <class 'stix.indicator.indicator.Indicator'>
            >>> indicator = marked_indicator


        Args:
            markable: An object to mark (e.g., an Indicator.title string).
            marking: A python-stix MarkingSpecification object.
            descendants: If true, add the marking to all descendants
                `markable`.

        Returns:
            The `markable` object with data marking information attached. If
            `markable` is a built-in immutable Python type (e.g., str), it will
            be changed to a stixmarx.api.types datatype.

        Raises:
            UnmarkableError: If `markable` is a STIXPackage object.
            DuplicateMarkingError: If `markable` is already marked by
                `marking`.
            MarkingPathNotEmpty: If `marking` controlled_structure is set.
        """
        utils.check_marking(marking)
        utils.check_empty_marking(marking)

        # Handles null marking case.
        if markable is None:
            if marking not in self._null_markings:
                self._null_markings.append(marking)
                return
            else:
                msg = ("The marking is already present in the null_markings"
                       " internal collection.")

                raise errors.DuplicateMarkingError(message=msg,
                                                   entity=self.package,
                                                   marking=marking)

        self._assert_unique_marking(markable, marking)

        # API call before to avoid duplicates.
        marked = api.add_marking(markable, marking)

        if not utils.is_package(markable):
            if all((marking, descendants) != mark
                   for mark in self._field_markings[marked]):

                if descendants:
                    self._add_descendants(markable, marking)

                # Store marking and descendants option tied.
                self._field_markings[marked].append((marking, descendants))
                return marked

            msg = ("The marking is already present in the field_markings"
                   " internal collection.")

            raise errors.DuplicateMarkingError(message=msg,
                                               entity=self.package,
                                               marking=marking)

        msg = "Cannot mark STIX Package: use add_global()"
        raise errors.UnmarkableError(entity=markable, message=msg)
예제 #8
0
 def _add_descendants(self, markable, marking):
     """Apply marking to `markable` in-place to descendants."""
     for descendant in navigator.iterwalk(markable):
         descendant = api.add_marking(descendant, marking)