-
Notifications
You must be signed in to change notification settings - Fork 50
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
Refactor format_metadata_to_key #227
Conversation
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]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4dad937
to
e28d198
Compare
There was a problem hiding this 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).
c4ca76a
to
6796cb9
Compare
6796cb9
to
5b4d843
Compare
There was a problem hiding this 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?
5b4d843
to
19fb72a
Compare
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 |
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 |
Good idea, I like it! |
a8d18d8
to
110c168
Compare
I rebased and addressed Joshua's comments. I changed the following:
|
There was a problem hiding this 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
FORMAT_ERROR_MSG) | ||
|
||
self.assertEqual(None, | ||
securesystemslib.formats.KEY_SCHEMA.check_match(rsakey_dict_from_meta), |
There was a problem hiding this comment.
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.
110c168
to
d64b25c
Compare
There was a problem hiding this 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.
tests/test_keys.py
Outdated
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
d64b25c
to
a3006e0
Compare
There was a problem hiding this 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.
securesystemslib/keys.py
Outdated
@@ -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): |
There was a problem hiding this comment.
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.
tests/test_keys.py
Outdated
|
||
# 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']) |
There was a problem hiding this comment.
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.
f9d3f6a
to
9aab0d1
Compare
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]>
Signed-off-by: Martin Vrachev <[email protected]>
9aab0d1
to
5908bfb
Compare
Thanks for merging that pr @joshuagl! |
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]>
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]>
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]>
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]>
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]>
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: