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

Fix versionability detection #401

Merged
merged 8 commits into from
Jun 10, 2020

Conversation

chisholm
Copy link
Contributor

@chisholm chisholm commented May 28, 2020

Fixes #394 .

Add versionability detection to versioning function (new_version()), and move versioning code out of stix2.utils and into its own module. It eases circular import issues which came up due new imports required for versionability detection, and also due to stix2.utils being imported so widely into other modules. The new module is named "versioning"... so now we have "version" and "versioning". Is that too confusing? There are also some versioning-related bugfixes.

Some details include:

  • Revoking an object unconditionally added the revoked property, whether or not it was supported for the object type (due to hard-coded allow_custom=True). That seemed wrong and is now removed. If the revoked property is not supported, then revocation will fail because of that.

  • Versioning didn't generically work for objects with custom properties. You have to have known it had custom properties in advance, and pass the allow_custom=True kwarg. Otherwise, creation of the new object ignored the allow_custom preference of the old one. This is fixed.

    The function is designed to modify an existing object, so it made sense to have a three-value logic for allow_custom in this case: if True, the resulting object may have custom properties (regardless of whether the original did); if False, it may not; if None, it inherits the preference from the original object. The latter is probably most often what people would intuitively expect, so it is the default. (The _allow_custom attribute is checked, inspired by __deepcopy__.)

    If versioning a dict, there is no previous preference to propagate nor object to propagate it to, so allow_custom doesn't make sense and is ignored.

  • allow_custom is now a separate kwarg, rather than being lumped into the other **kwargs which confuses it with a normal object property. It also prevents that key from winding up in the result when you're versioning dicts (which the old implementation did).

  • The object marking unit tests were trying to change markings on a dict with no "type" property. That doesn't work because a markings change is a versioning change, and versionability checking on dicts requires looking up a registered class based on object type. So I added a type property.

  • Moved the TYPE_REGEX variables out of stix2.utils and into properties.py since that's the only place they're used.

  • When versioning a 2.1 SCO with a UUIDv5 ID, treat ID contributing properties as unchangeable. Changing them would either break the UUIDv5 correspondence, or require changing the ID (assuming the UUIDv5 was created in the spec-prescribed way), which is not allowed across versions.

@codecov-commenter
Copy link

codecov-commenter commented May 28, 2020

Codecov Report

Merging #401 into master will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #401      +/-   ##
==========================================
+ Coverage   98.43%   98.45%   +0.01%     
==========================================
  Files         127      128       +1     
  Lines       14652    14777     +125     
==========================================
+ Hits        14423    14548     +125     
  Misses        229      229              
Impacted Files Coverage Δ
stix2/test/v20/test_object_markings.py 100.00% <ø> (ø)
stix2/test/v21/test_object_markings.py 100.00% <ø> (ø)
stix2/utils.py 95.91% <ø> (-1.30%) ⬇️
stix2/__init__.py 100.00% <100.00%> (ø)
stix2/base.py 97.89% <100.00%> (ø)
stix2/markings/granular_markings.py 100.00% <100.00%> (ø)
stix2/markings/object_markings.py 100.00% <100.00%> (ø)
stix2/properties.py 98.42% <100.00%> (+<0.01%) ⬆️
stix2/test/v20/test_versioning.py 100.00% <100.00%> (ø)
stix2/test/v21/test_versioning.py 100.00% <100.00%> (ø)
... and 2 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 41525f9...c13cb18. Read the comment docs.

chisholm added 8 commits June 5, 2020 14:24
of objects.  Move versioning-related stuff out of stix2.utils
to its own module.  Some misc versioning-related fixes.
was used but not imported there.  Also import sorting from the
pre-commit hook.
use allow_custom=False to force custom properties to be
disallowed.  This is extra insurance against a custom prop
accidentally getting through.
three-value logic for allow_custom, it needed some much better
documentation.
python2, Mapping.keys() returns a list instead of a set!
a list of tuples! (despite what it looks like if you glance at it
quickly.)
properties from being changed, if a UUIDv5 is in use.  Changing
one of those properties would imply an ID change, which is not
allowed across versions.  Also:

- add a trailing comma
- change unchangable_properties to a set instead of a list,
  in case there are dupe props between STIX_UNMOD_PROPERTIES and
  sco_locked_props
- remove var 'properties_to_change' since it's unnecessary
- delete most of remove_custom_stix() since it was unnecessary,
  greatly simplify it
@chisholm chisholm force-pushed the fix_versionable_detection branch from fce4f15 to c13cb18 Compare June 5, 2020 18:29
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.

Looks good for now. We're discussing making further changes to allow_custom offline, but those changes will take place in subsequent pull requests.

@clenk clenk merged commit 9d05c9d into oasis-open:master Jun 10, 2020
@emmanvg emmanvg added this to the 2.0.0 milestone Jun 10, 2020
@chisholm chisholm deleted the fix_versionable_detection branch June 11, 2020 00:34
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.

Some 2.0 Observables can be versioned and revoked
4 participants