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

Metadata API: Add Key attributes types validation #1449

Merged
merged 2 commits into from
Jun 16, 2021

Conversation

MVrachev
Copy link
Collaborator

@MVrachev MVrachev commented Jun 15, 2021

Fixes #1438

Description of the changes being introduced by the pull request:

In our discussion with @jku , we come to the conclusion that we want
to verify that all Key attributes contain values in the expected types,
but at the same time, we don't want to focus on validating the semantics
behind them.
The reason is that having a Key instance with invalid attributes is
possible and supported by the spec.
That's why we have a "threshold" for the roles meaning we can have up to
a certain number of invalid Keys until we satisfy
the required threshold.

Also, for deeper semantic validation it's better to be done in
securesystemslib which does the actual work with keys.

Signed-off-by: Martin Vrachev mvrachev@vmware.com

Please verify and check that the pull request fulfills 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

In our discussion with Jussi we come to the conclusion that we want
to verify that all Key attributes contain values in the expected types,
but at the same time, we don't want to focus on validating the semantics
behind them.
The reason is that having a Key instance with invalid attributes is
possible and supported by the spec.
That's why we have a "threshold" for the roles meaning we can have up to
a certain number of invalid Keys until we satisfy
the required threshold.

Also, for deeper semantic validation it's better to be done in
securesystemslib which does the actual work with keys.

For context see: theupdateframework#1438

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
@sechkova
Copy link
Contributor

Should I assume from this PR that we want to implement the validation checks of the various metadata classes in the constructor and not in the deserialization methods (from_dict())?

@MVrachev
Copy link
Collaborator Author

MVrachev commented Jun 15, 2021

Should I assume from this PR that we want to implement the validation checks of the various metadata classes in the constructor and not in the deserialization methods (from_dict())?

From my understanding, yes. The reason is that this way no matter how we are creating Key objects - directly through __init__ like Key() or calling Key.from_dict() which after that will call Key.__init__ we have validation.

Copy link
Member

@jku jku left a comment

Choose a reason for hiding this comment

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

This seems like the approach we have to take: explicit type checking is ewww, but we don't want to / cannot try to understand the semantics of our input at serialization time so it probably makes sense.

The repetition of if-raise could easily be avoided (as the unique error messages are not that important here) so the whole thing would be just 3 lines but I'm fine with this as well, your choice. Left one simplification suggestion though

Comment on lines 403 to 404
public_val = keyval.get("public")
if not public_val or not isinstance(public_val, str):
Copy link
Member

Choose a reason for hiding this comment

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

We're relying on KeyErrors from index access elsewhere in serialization (and this code also relies on TypeError if keyval is not a dict). I think letting index access fail with keyerror is simpler and works just as well:
if not isinstance(keyval["public"], str):

@MVrachev
Copy link
Collaborator Author

MVrachev commented Jun 16, 2021

I experimented with a shorter variation.
In this case, we will receive KeyError when accessing the public key value val = keyval["public"] and the exception will not point to a long if a line with checks and of course the code is shorter and at least to me looks readable :D.

@MVrachev MVrachev force-pushed the key-validation branch 2 times, most recently from 9fe15ff to 9630a10 Compare June 16, 2021 11:56
raise ValueError("keyval doesn't follow the specification format!")
val = keyval["public"]
if not all(isinstance(at, str) for at in [keyid, keytype, scheme, val]):
raise ValueError("Attributes don't follow the type annotations!")
Copy link
Member

@jku jku Jun 16, 2021

Choose a reason for hiding this comment

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

LGTM, I agree getting rid of the repetition outweighs the slightly more complex code.

The message is really not terribly important but maybe something like "Unexpected Key attributes" might be better than referring to annotations?

approved in any case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated with Unexpected Key attributes types! message.

Clarify that we don't semantically validate "Key" instances during
initialization and that this is a responsibility of securesystemslib.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
@jku jku merged commit fa2268d into theupdateframework:develop Jun 16, 2021
MVrachev added a commit to MVrachev/tuf that referenced this pull request Jun 22, 2021
In pr theupdateframework#1449 we introduced
Key validation and there decided to raise "ValueError" if one of
keyid, scheme, keyval or keytype is not a string.
That seems like a mistake given that in the python docs it's written:
"Passing arguments of the wrong type
(e.g. passing a list when an int is expected) should result
in a TypeError, but passing arguments with the wrong value
(e.g. a number outside expected boundaries) should result
in a ValueError."

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
MVrachev added a commit to MVrachev/tuf that referenced this pull request Jun 23, 2021
In pr theupdateframework#1449 we introduced
Key validation and there decided to raise "ValueError" if one of
keyid, scheme, keyval or keytype is not a string.
That seems like a mistake given that in the python docs it's written:
"Passing arguments of the wrong type
(e.g. passing a list when an int is expected) should result
in a TypeError, but passing arguments with the wrong value
(e.g. a number outside expected boundaries) should result
in a ValueError."

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
MVrachev added a commit to MVrachev/tuf that referenced this pull request Jul 5, 2021
In pr theupdateframework#1449 we introduced
Key validation and there decided to raise "ValueError" if one of
keyid, scheme, keyval or keytype is not a string.
That seems like a mistake given that in the python docs it's written:
"Passing arguments of the wrong type
(e.g. passing a list when an int is expected) should result
in a TypeError, but passing arguments with the wrong value
(e.g. a number outside expected boundaries) should result
in a ValueError."

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
MVrachev added a commit to MVrachev/tuf that referenced this pull request Jul 7, 2021
In pr theupdateframework#1449 we introduced
Key validation and there decided to raise "ValueError" if one of
keyid, scheme, keyval or keytype is not a string.
That seems like a mistake given that in the python docs it's written:
"Passing arguments of the wrong type
(e.g. passing a list when an int is expected) should result
in a TypeError, but passing arguments with the wrong value
(e.g. a number outside expected boundaries) should result
in a ValueError."

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
@MVrachev MVrachev deleted the key-validation branch July 19, 2021 13:36
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.

Metadata Attribute research: KEY
3 participants