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

Refactor format_metadata_to_key #227

Merged

Conversation

MVrachev
Copy link
Collaborator

@MVrachev MVrachev commented Apr 3, 2020

Fixes issue #:

#220

Description of the changes being introduced by the pull request:

Refactor format_metadata_to_key so that it won't depend
on settings.HASH_ALGORITHMS.
There are couple of code snippets in tuf where in order to provide
a custom list of hash algorithms, tuf is temporary changing
settings.HASH_ALGORITHMS and then returning it to its formal state.

This could lead to problems in future and one should
be able to control hash algorithms used for generating keyids in f
ormat_metadata_to_key() without having to change
package level settings in securesystemslib.settings.

Please verify and check that the pull request fulfils the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

@coveralls
Copy link

coveralls commented Apr 3, 2020

Coverage Status

Coverage increased (+0.001%) to 98.944% when pulling 5908bfb on MVrachev:refactor-format-metadata-to-key into df57705 on secure-systems-lab:master.

MVrachev added a commit to MVrachev/tuf that referenced this pull request Apr 3, 2020
This pr enables tuf to use changes made in secure-systems-lab/securesystemslib#227
Don't merge before the above pr is merged!

Because of issue secure-systems-lab/securesystemslib#227
I had to checkout to the last commit which doesn't had that issue and
make the current changes based on it.

Signed-off-by: Martin Vrachev <[email protected]>
Copy link
Collaborator

@joshuagl joshuagl left a comment

Choose a reason for hiding this comment

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

Thanks for the patch @MVrachev. I've made some minor suggestions on the code.

It's worth noting that this PR conflicts with #225

securesystemslib/interface.py Outdated Show resolved Hide resolved
securesystemslib/interface.py Outdated Show resolved Hide resolved
securesystemslib/keys.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from 4dad937 to e28d198 Compare April 6, 2020 10:28
securesystemslib/keys.py Outdated Show resolved Hide resolved
Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

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

Thanks for the patch, @MVrachev! This will make the interface a lot cleaner. Please resolve the git conflict and fix the test (see inline).

Also, be aware that default values in functions are only evaluated once when the function definition is evaluated. This has bitten me before (see #202). As a consequence, constructs like these won't be possible anymore.

# The default arg is evaluated together with the function definition on first import:
# format_metadata_to_key(key_metadata, hash_algorithms=['sha256', 'sha512'])
import securesystemslib.keys
import securesystemslib.settings

securesystemslib.settings.HASH_ALGORITHMS = ["blake2"]

format_metadata_to_key(key_metadata)
# the default for hash_algorithms still is ['sha256', 'sha512'] and not ["blake2"]

We shouldn't use constructs like these anyway and you already have a fix for TUF in the queue (theupdateframework/python-tuf#1016, thanks!), but given that this interface was used like this before, we maybe want to mention that backwards incompatibility in the CHANGELOG for the next release (mostly a note to myself).

tests/test_keys.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from c4ca76a to 6796cb9 Compare April 14, 2020 14:19
securesystemslib/keys.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from 6796cb9 to 5b4d843 Compare April 16, 2020 11:23
Copy link
Collaborator

@joshuagl joshuagl left a comment

Choose a reason for hiding this comment

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

Thanks for making the suggested changes, Martin. Could you address the remaining minor nit?

securesystemslib/keys.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from 5b4d843 to 19fb72a Compare April 27, 2020 14:16
@joshuagl
Copy link
Collaborator

This change looks good, thank you.

I would like to hold off on merging it until we have a solid release out to support tuf changes related to the Warehouse work for pep 458, because I anticipate that this will be the first of several API changes as we work to remove the settings module (re: #219) and I would like us to include those in one release rather than spread them over multiple releases.

We just had the 0.15.0 release with abstract files & directories support. Next we'll want a release with the HSM support in #229. Then we'll have all of the features required of PEP 458 and can start landing changes like this towards #219.

It would be really nice if we tested format_metadata_to_key with non-default parameters too, do you think you could work on a patch for that in this PR @MVrachev ?

@joshuagl
Copy link
Collaborator

Also, be aware that default values in functions are only evaluated once when the function definition is evaluated. This has bitten me before (see #202). As a consequence, constructs like these won't be possible anymore.

# The default arg is evaluated together with the function definition on first import:
# format_metadata_to_key(key_metadata, hash_algorithms=['sha256', 'sha512'])
import securesystemslib.keys
import securesystemslib.settings

securesystemslib.settings.HASH_ALGORITHMS = ["blake2"]

format_metadata_to_key(key_metadata)
# the default for hash_algorithms still is ['sha256', 'sha512'] and not ["blake2"]

I've been wondering what the right pattern for this is. Maybe something like:

def format_metadata_to_key(key_metadata, default_keyid=None,
  keyid_hash_algorithms=None):
  # docstrings and stuff ...
  if keyid_hash_algorithms is None:
    keyid_hash_algorithms = securesystemslib.settings.HASH_ALGORITHMS
  # ... rest of the function

That way we don't bind the default value at function evaluation and we allow users to continue with the current pattern of assigning securesystemslib.settings.HASH_ALGORITHMS, making this change less of an API break. We're also a little bit cleaner in our intent to deprecate the settings module as we don't encode it in the interface (by using a variable from the module as a default value).

@trishankatdatadog
Copy link
Contributor

I've been wondering what the right pattern for this is. Maybe something like:

Good idea, I like it!

@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch 2 times, most recently from a8d18d8 to 110c168 Compare May 18, 2020 10:41
@MVrachev
Copy link
Collaborator Author

MVrachev commented May 18, 2020

I rebased and addressed Joshua's comments. I changed the following:

  1. Renamed has_algorithms to keyid_hash_algorithms

  2. Use the approach proposed from Joshua Lock about handling the default value of keyid_hash_algorithms

  3. Added a new commit adding unit tests using nondefault hash algorithms.

Copy link
Collaborator

@joshuagl joshuagl left a comment

Choose a reason for hiding this comment

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

Thanks @MVrachev. This is looking good. I made some minor style and layout suggestions for the tests to keep them readable and maintainable, would you be able to address?

tests/test_keys.py Outdated Show resolved Hide resolved
tests/test_keys.py Outdated Show resolved Hide resolved
tests/test_keys.py Outdated Show resolved Hide resolved
FORMAT_ERROR_MSG)

self.assertEqual(None,
securesystemslib.formats.KEY_SCHEMA.check_match(rsakey_dict_from_meta),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question on matches() vs check_match() here.

tests/test_keys.py Outdated Show resolved Hide resolved
@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from 110c168 to d64b25c Compare May 21, 2020 16:55
Copy link
Collaborator

@joshuagl joshuagl left a comment

Choose a reason for hiding this comment

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

This looks good, thanks @MVrachev. One minor question.

del self.rsakey_dict['keyid']
# Copying self.rsakey_dict so that rsakey_dict remains
# unchanged during and after this test execution.
test_rsakey_dict = copy.deepcopy(self.rsakey_dict)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I think copy would suffice here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are right.

I overdo it a little to make sure the right values were copied but forgot to try with copy.copy.

I made the change and amended.

@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from d64b25c to a3006e0 Compare June 2, 2020 17:39
Copy link
Member

@lukpueh lukpueh left a comment

Choose a reason for hiding this comment

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

Thanks, @MVrachev! Please consider addressing my two minor style comments. Otherwise this looks good to me.

@@ -474,7 +474,8 @@ def format_keyval_to_metadata(keytype, scheme, key_value, private=False):



def format_metadata_to_key(key_metadata, default_keyid=None):
def format_metadata_to_key(key_metadata, default_keyid=None,
keyid_hash_algorithms=None):
Copy link
Member

Choose a reason for hiding this comment

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

Minor nit: Please use double-indentation (4 spaces) for line continuation.


# Call format_metadata_to_key by using custom value for keyid_hash_algorithms
rsakey_dict_from_meta_custom, junk = KEYS.format_metadata_to_key(test_rsakey_dict,
keyid_hash_algorithms=['sha384'])
Copy link
Member

Choose a reason for hiding this comment

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

See comment above about line continuation.

@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch 2 times, most recently from f9d3f6a to 9aab0d1 Compare June 8, 2020 15:43
@MVrachev
Copy link
Collaborator Author

MVrachev commented Jun 8, 2020

Fixed the indentation.

Refactor format_metadata_to_key so that it won't depend
on settings.HASH_ALGORITHMS.
There are couple of code snippets in tuf where in order to provide
a custom list of hash algorithms, tuf is temporary changing
settings.HASH_ALGORITHMS and then returning it to its formal state.

This could lead to problems in future and one should
be able to control hash algorithms used for generating keyids in f
ormat_metadata_to_key() without having to change
package level settings in securesystemslib.settings.

Closes: secure-systems-lab#220

Signed-off-by: Martin Vrachev <[email protected]>
@MVrachev MVrachev force-pushed the refactor-format-metadata-to-key branch from 9aab0d1 to 5908bfb Compare June 25, 2020 10:23
@joshuagl joshuagl merged commit 7531747 into secure-systems-lab:master Jun 25, 2020
@MVrachev
Copy link
Collaborator Author

MVrachev commented Jun 26, 2020

Thanks for merging that pr @joshuagl!
I will update the corresponding pr theupdateframework/python-tuf#1016 in TUF next week.

@MVrachev MVrachev deleted the refactor-format-metadata-to-key branch June 30, 2020 16:07
MVrachev added a commit to MVrachev/tuf that referenced this pull request Jul 7, 2020
In commit b7a15fdee7dee899c098b01fe64d604635b2b132
or pr secure-systems-lab/securesystemslib#227
in securesystemslib I change the function arguments of the
format_metadata_to_key function in securesystemslib/keys.py
to add the opportunity to use custom keyid hash algorithms without
chainging the securesystemslib.settings.HASH_ALGORITHMS variable.

With this commit, I make use of the above changes in tuf.

Signed-off-by: Martin Vrachev <[email protected]>
MVrachev added a commit to MVrachev/tuf that referenced this pull request Aug 17, 2020
In commit b7a15fdee7dee899c098b01fe64d604635b2b132
or pr secure-systems-lab/securesystemslib#227
in securesystemslib I change the function arguments of the
format_metadata_to_key function in securesystemslib/keys.py
to add the opportunity to use custom keyid hash algorithms without
chainging the securesystemslib.settings.HASH_ALGORITHMS variable.

With this commit, I make use of the above changes in tuf.

Signed-off-by: Martin Vrachev <[email protected]>
MVrachev added a commit to MVrachev/tuf that referenced this pull request Aug 18, 2020
In commit b7a15fdee7dee899c098b01fe64d604635b2b132
or pr secure-systems-lab/securesystemslib#227
in securesystemslib I change the function arguments of the
format_metadata_to_key function in securesystemslib/keys.py
to add the opportunity to use custom keyid hash algorithms without
chainging the securesystemslib.settings.HASH_ALGORITHMS variable.

With this commit, I make use of the above changes in tuf.

Signed-off-by: Martin Vrachev <[email protected]>
mnm678 pushed a commit to mnm678/tuf that referenced this pull request Sep 8, 2020
In commit b7a15fdee7dee899c098b01fe64d604635b2b132
or pr secure-systems-lab/securesystemslib#227
in securesystemslib I change the function arguments of the
format_metadata_to_key function in securesystemslib/keys.py
to add the opportunity to use custom keyid hash algorithms without
chainging the securesystemslib.settings.HASH_ALGORITHMS variable.

With this commit, I make use of the above changes in tuf.

Signed-off-by: Martin Vrachev <[email protected]>
MVrachev added a commit to MVrachev/tuf that referenced this pull request Sep 17, 2020
In commit b7a15fdee7dee899c098b01fe64d604635b2b132
or pr secure-systems-lab/securesystemslib#227
in securesystemslib I change the function arguments of the
format_metadata_to_key function in securesystemslib/keys.py
to add the opportunity to use custom keyid hash algorithms without
chainging the securesystemslib.settings.HASH_ALGORITHMS variable.

With this commit, I make use of the above changes in tuf.

Signed-off-by: Martin Vrachev <[email protected]>
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.

5 participants