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

Stix2.1 wd05 #283

Merged
merged 18 commits into from
Aug 9, 2019
Merged

Stix2.1 wd05 #283

merged 18 commits into from
Aug 9, 2019

Conversation

chisholm
Copy link
Contributor

Major changes include:

  • Updates to all SDO classes for STIX 2.1 WD05
  • Improved the exception class hierarchy
  • Improved how property "cleaning" errors are handled
  • Improved how the workbench monkeypatching works

includes SDO/SRO class updates, but no unit test updates.  The
class updates broke unit tests, so that still needs to be
addressed.
possible library bugs, before I fix the remaining unit tests.
- Removed all plain python base classes (e.g. ValueError, TypeError)
- Renamed InvalidPropertyConfigurationError -> PropertyPresenceError,
  since incorrect values could be considered a property config error, and
  I really just wanted this class to apply to presence (co-)constraint
  violations.
- Added ObjectConfigurationError as a superclass of InvalidValueError,
  PropertyPresenceError, and any other exception that could be raised
  during _STIXBase object init, which is when the spec compliance
  checks happen.  This class is intended to represent general spec
  violations.
- Did some class reordering in exceptions.py, so all the
  ObjectConfigurationError subclasses were together.

Changed how property "cleaning" errors were handled:
- Previous docs said they should all be ValueErrors, but that would require
  extra exception check-and-replace complexity in the property
  implementations, so that requirement is removed.  Doc is changed to just
  say that cleaning problems should cause exceptions to be raised.
  _STIXBase._check_property() now handles most exception types, not just
  ValueError.
- Decided to try chaining the original clean error to the InvalidValueError,
  in case the extra diagnostics would be helpful in the future.  This is
  done via 'six' adapter function and only works on python3.
- A small amount of testing was removed, since it was looking at custom
  exception properties which became unavailable once the exception was
  replaced with InvalidValueError.

Did another pass through unit tests to fix breakage caused by the changed
exception class hierarchy.

Removed unnecessary observable extension handling code from
parse_observable(), since it was all duplicated in ExtensionsProperty.
The redundant code in parse_observable() had different exception behavior
than ExtensionsProperty, which makes the API inconsistent and unit tests
more complicated.  (Problems in ExtensionsProperty get replaced with
InvalidValueError, but extensions problems handled directly in
parse_observable() don't get the same replacement, and so the exception
type is different.)

Redid the workbench monkeypatching.  The old way was impossible to make
work, and had caused ugly ripple effect hackage in other parts of the
codebase.  Now, it replaces the global object maps with factory functions
which behave the same way when called, as real classes.  Had to fix up a
few unit tests to get them all passing with this monkeypatching in place.
Also remove all the xfail markings in the workbench test suite, since all
tests now pass.

Since workbench monkeypatching isn't currently affecting any unit tests,
tox.ini was simplified to remove the special-casing for running the
workbench tests.

Removed the v20 workbench test suite, since the workbench currently only
works with the latest stix object version.
@codecov-io
Copy link

codecov-io commented Jul 19, 2019

Codecov Report

Merging #283 into stix2.1 will increase coverage by 0.35%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           stix2.1     #283      +/-   ##
===========================================
+ Coverage    98.66%   99.01%   +0.35%     
===========================================
  Files          122      121       -1     
  Lines        13060    12907     -153     
===========================================
- Hits         12885    12780     -105     
+ Misses         175      127      -48
Impacted Files Coverage Δ
stix2/test/v21/constants.py 100% <ø> (ø) ⬆️
stix2/v21/common.py 100% <ø> (ø) ⬆️
stix2/test/v21/test_malware_analysis.py 100% <ø> (ø) ⬆️
stix2/properties.py 100% <ø> (ø) ⬆️
stix2/v21/sro.py 94.59% <ø> (ø) ⬆️
stix2/core.py 100% <ø> (ø) ⬆️
stix2/test/v21/test_datastore_filters.py 100% <ø> (ø) ⬆️
stix2/test/v21/test_opinion.py 100% <ø> (ø) ⬆️
stix2/test/v21/conftest.py 100% <ø> (ø) ⬆️
stix2/test/v21/test_datastore_memory.py 100% <ø> (ø) ⬆️
... and 27 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b1fa177...bb4a6b1. Read the comment docs.

monkeypatching algorithm.  It's no longer needed and I forgot to
delete it.
stix2/__init__.py Outdated Show resolved Hide resolved
@rpiazza
Copy link
Contributor

rpiazza commented Jul 22, 2019

Looks good to me...

additionally required:

- Removing the v21 workbench test suite and reinstating the v20
  test suite
- Fixing up a few v20 unit tests to work with the workbench
  monkeypatching.
- I didn't revert the analogous changes I'd previously made to
  the v21 unit tests, because I think they make sense even when
  the workbench monkeypatching isn't happening.
created subclasses, to mention v20 instead of v21.
it doesn't make sense to have a test per STIX version.  The
workbench only uses the latest supported STIX version.  In
order to make this work, the test suite was modified to
dynamically compute some settings like where to get demo data,
based on the value of stix2.DEFAULT_VERSION.

Switched stix2.DEFAULT_VERSION back to "2.0", since I figure it
should be sync'd up with the 'from .vxx import *' import
statement from the top level package.
that turns them on.  These have the potential to affect subsequent
tests.  The side effects include automatically setting
property values, and automatically appending additional values
to list-valued properties.
autogenerated docstrings: v20/v21 is automatically referenced as
appropriate, based on stix2.DEFAULT_VERSION.  To avoid
duplication, I also moved _STIX_VID from test_workbench.py to
workbench.py; the former now imports it from the latter.
@clenk
Copy link
Contributor

clenk commented Jul 25, 2019

This looks great! Can we add some deprecation warnings for Observed Data and for its objects property? I think warnings.warn() is the preferred way to do this.

Can we add a test for threat-actor.last_seen < first_seen? Looks like it's the only thing not covered from the coverage report.

Also, (though I would merge the PR without this one) for the testing that was removed because it looked at extension properties, could we get back at least a little of it by putting 'extensions' in the InvalidValueError.prop_name and checking that? I think this would mean adding an except AtLeastOnePropertyError() to _check_property. The tests in question were the ones changed in test_observed_data.py for both stix versions.

EDIT: We also need to update LanguageContent: object_modified is now optional.

@clenk clenk mentioned this pull request Jul 25, 2019
@chisholm
Copy link
Contributor Author

Can we add some deprecation warnings for Observed Data and for its objects property? I think warnings.warn() is the preferred way to do this.

Seems to me, the warnings module is primarily for the interpreter and perhaps built-in libraries. It interacts with the interpreter's -W commandline option. The provision for "user" warnings is via a special exception type, UserWarning. This gives the impression that ordinary users may use the module, but they shouldn't use the other warning types. Sounds like your idea was to use DeprecationWarning in particular, but I'm not sure that's how it's intended to be used.

Also, for the testing that was removed because it looked at extension properties

You may have misread the commit comment. It said: A small amount of testing was removed, since it was looking at custom exception properties...

So this isn't specifically about extensions. I think those changes were regarding exceptions which had previously been TypeError's, and therefore hadn't been caught and replaced with InvalidValueError. Most of the property presence exception classes fall into that category (at least one, extra, missing, etc).

@clenk
Copy link
Contributor

clenk commented Jul 26, 2019

For warnings, I was reading this. Looks like you can set a filter so they show up without the interpreter option, but I'm not sure yet what best practice is for library authors like us.

Yes I misspoke (and probably misread too) about the removed tests. I was trying to get at stuff like the removed code above this: https://github.com/oasis-open/cti-python-stix2/pull/283/files#diff-5d546fb7e9a4fb90af64f4ae52b50106R799 I was hoping to be able to check a little more precisely that the correct error was encountered.

@chisholm
Copy link
Contributor Author

... I'm not sure yet what best practice is for library authors like us.

Hmmm... I couldn't find anything that described an intention for users (of Python interpreters and core libraries, i.e. normal developers) to only use UserWarning. I found a Deprecation library, which includes a white paper describing ways other Python libraries deal with deprecation warnings, including well-known libraries like Flask and Django. All signs suggest they use all the warning categories. Ok then, guess we can too. I guess that Deprecation library wouldn't really work for us, since we aren't deprecating a function or class (something we can decorate); we're deprecating a virtual "property" whose definition is buried in a data structure... it's more complicated for us.

As far as best practices, I think our libraries should emit the warnings but never set up warning filters. Any commandline tools we create should be free to set filters or give commandline options to allow user control over them.

I was hoping to be able to check a little more precisely that the correct error was encountered.

If you throw out the exception object, you can't test it; that's inherent in the original design. In Python 3, you can chain the original and the new one via a special attribute (__cause__), which is what I chose to do. So on Python 3, you could get the original exception and run some tests. But they would be python version specific.

Another idea is to not test cleaning via object creation, as that triggers the whole property checking, exception replacing, mechanism. If you want to test cleaning, directly call clean() on a property. That will raise the exception directly to you and then you can check whatever you want.

observed-data.  Add a unit test to ensure we get the warning.
@cyvs
Copy link

cyvs commented Jul 31, 2019

Hi, is there a plan to allow setting deterministic uuidv5 ids for stix sdo objects?
It seems this is mentioned in the wd05 draft but there are no changes here to address that.

See:

# This uses the regular expression for a RFC 4122, Version 4 UUID. In the

Or is this something that will be included in future PRs?

@clenk
Copy link
Contributor

clenk commented Jul 31, 2019

@cyvs Yes, this is something we are working on and will be in a future PR.

@chisholm
Copy link
Contributor Author

The beginnings are already in the stix2.1 branch here. uuid5's are allowed, but logic for automatically computing one for an SCO from "ID Contributing Properties" is not implemented yet.

subclass.  Changed the unit test to test for that specific
warning category, instead of any DeprecationWarning.
bug which seems to be related to how it copes with code which
emits warnings.
Copy link
Contributor

@clenk clenk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@clenk clenk merged commit fb834d8 into oasis-open:stix2.1 Aug 9, 2019
@emmanvg emmanvg added this to the 1.2.0 milestone Aug 21, 2019
@chisholm chisholm deleted the stix2.1_wd05 branch February 18, 2020 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants