You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Metadata API: Provide a way to validate object when serializing to dictionary #1696
This issue is discussed during a couple of conversations with @jku.
We can say we are almost done with Metadata API validation during the initialization of Signed objects as summarized here: #1140 (comment).
What we didn't focus on is validation when serializing the Signed objects to dictionaries through Signed.to_dict().
This could be useful for users of Metadata API that changed the signed portion
I imagine some of the requirements for such validation are:
It's optional. As pointed out by @jku in our conversation the client using Metadata API could provide its own validation on the metadata objects. Additionally, if this kind of validation is mandatory it could slow the work of a big python-tuf client such as Warehouse working on thousands of targets.
It doesn't duplicate validation code. It will be best if we find a way to use the same exact code as the mandatory validation during the initialization without duplicating it.
One thing to consider is doing we want to add a little more complex validation, additional to the one done during initialization.
For example, for Root, it probably could be useful to validate that each of the set of keyids defined in the keys dictionary is all used inside a particular role in roles.
Current behavior:
No validation is done when calling to_dict() from any of the Signed child classes.
Expected behavior:
Provide a way to validate an object before calling to_dict() or when calling to_dict() with an explicit option set.
PS: I didn't include the signatures part from the Metadata objects in this discussion as their validation is a little more complex as you need access to the delegator.
I agree with the basic premise: we'll want to re-evaluate whether our validation is appropriate for the serialization as well (while we've been concentrating on the client case, the focus has been on validating deserialized data).
some quick comments:
the idea of validating when creating a dict is not necessarily the correct one: to_dict() is a helper that json serialization happens to use. There's no guarantee that doing so makes sense for some possible other serialization format. We might want to consider validating during Metadata.to_bytes() if possible
a potential way to 100% prevent incorrect serialization (instead of just checking e.g. specific data types) is during Metadata.to_bytes() to serialize Metadata to bytes, then deserialize back to Metadata, and raise if the two metadata instances are not equal.
This might be too heavy (cpu/memory use) to be a default choice but could be an option
This would mean we can keep focusing on the deserialization path when validating
Metadata does not currently support eq() so this would need a bit of work
I agree with the basic premise: we'll want to re-evaluate whether our validation is appropriate for the serialization as well
I'll rephrase this to be more specific: Ideally we want to guarantee not just that our internal data structures are correct at serialization time, but that our serialization output is valid and that we can succesfully deserialize that output.
a potential way to 100% prevent incorrect serialization (instead of just checking e.g. specific data types) is during Metadata.to_bytes() to serialize Metadata to bytes, then deserialize back to Metadata, and raise if the two metadata instances are not equal.
This might be too heavy (cpu/memory use) to be a default choice but could be an option
This would mean we can keep focusing on the deserialization path when validating
Metadata does not currently support eq() so this would need a bit of work
it will use the same level of validation as initialization. That's useful as if we want to implement more sophisticated checks, like validating that in a root instance each of the set of keyids defined in the keys dictionary is all used inside a particular role in roles, we can implement them separately where they can be called. If we implement them inside the class we can get different results from the two types of validation - during initialization and during serialization
it's easy to understand
So, I like the idea and I can volunteer for the next sprint to implement it.
The only question that remains is do we need to think for an API or a way to do a deeper serialization functionality as this one:
For example, for Root, it probably could be useful to validate that each of the set of keyids defined in the keys dictionary is all used inside a particular role in roles.
or maybe we don't actually as during an update with TrustedMetadataSet we will get an exception if something is bad?
I can volunteer for the next sprint to implement it.
This is unlikely to get implemented in a single sprint but a design doc or prototype or some infrastructure work might be a good idea (I have no idea how much work there is in adding the eq() implementations as an example).
For example, for Root, it probably could be useful to validate that each of the set of keyids defined in the keys dictionary is all used inside a particular role in roles.
Loading (deserializing) validation should fail only if the there is a compliance or safety issue... and I think that's a good rule for serialization as well: deciding that implementations aren't allowed to add keys without using them doesn't clearly improve safety but it could be annoying to the repository implementers who want do it for whatever reasons.
Checks like that would make definitely make sense in repository implementations... and possibly even in a repository library.
As a prototype that's pretty much what I was imagining: definitely good enough to make some sort of a decision
not sure that validate can rely on from_dict/to_dict -- how do you know the serializer will even call to_dict? Could be that this extra validation should be a Serializer.serialize() feature? Not sure about this as serializer would then have to know which Deserializer to use... I suppose that's not out of the question: JSONSerializer could well know to use JSONDeserializer for validation purposes.
the __eq__() implementations are 119 LOC -- not as bad as I feared but obviously makes metadata.py bigger without being useful to client code
Metadata.__eq__() seems complex, I don't understand why
Overall: looks like a reasonable direction to me but let's discuss with others too.
As a prototype that's pretty much what I was imagining: definitely good enough to make some sort of a decision
not sure that validate can rely on from_dict/to_dict -- how do you know the serializer will even call to_dict? Could be that this extra validation should be a Serializer.serialize() feature? Not sure about this as serializer would then have to know which Deserializer to use... I suppose that's not out of the question: JSONSerializer could well know to use JSONDeserializer for validation purposes.
Even if we decide to leave it as a separate API call I think it could be useful.
It's hard for me to be sure about that where we don't have actual interaction with users of Metadata API.
the __eq__() implementations are 119 LOC -- not as bad as I feared but obviously makes metadata.py bigger without being useful to client code
Metadata.__eq__() seems complex, I don't understand why
It's becasue the Metadata.signatures is an OrderedDict and its values are securesytemslib Signature objects which doesn't implement __eq__. I added a comment to clarify that.
Overall: looks like a reasonable direction to me but let's discuss with others too.
not sure that validate can rely on from_dict/to_dict
Thinking a bit further: the prototype ends up calling to_dict() twice when Metadata.to_bytes() is called. The second call is unnecessary if serialize() handled the validation so that seems like the correct direction for the real implementation of this
Thanks for working on this, @MVrachev and @jku! I just caught up on your discussion and read through the prototype and I like it!
I also think that validate should be implemented as part of JSONSerializer.serialize, because that's really what it validates, i.e. "is the json serialize/deserialize loop idempotent?". Then we also don't have to call to_dict twice, and it does not feel odd that we use to_dict/from_dict.
@lukpueh do you think it should be an argument or it's okay to do it during initialization?
Also, do you think it makes sense to add it to the MetadataSerializer as well?
My question towards both of you @jku and @lukpueh is how do you think we should move forward?
Should I propose a pr? Do you think I can/should experiment with alternative solutions?
Do we want to add this API now before 1.0.0?
@lukpueh do you think it should be an argument or it's okay to do it during initialization?
Initialization of what? Regardless, given the computational cost of the validation, I'd make it optional.
Also, do you think it makes sense to add it to the MetadataSerializer as well?
No. I think this is something specific to JSONSerializer.
Should I propose a pr?
Yes.
Do you think I can/should experiment with alternative solutions?
Do you have something on your mind? I think this sounds good for the time being.
Do we want to add this API now before 1.0.0?
It would make 1.0.0 stronger. But I also see no problem in adding this in a subsequent minor release. Let's prioritize things that need to go into 1.0.0.
@lukpueh do you think it should be an argument or it's okay to do it during initialization?
Initialization of what? Regardless, given the computational cost of the validation, I'd make it optional.
There are two options on how to call the validation through the JsonSerializer:
pass a validate argument when creating a JsonSerializer object.
pass a validate argument when calling JSONSerializer.serialize()
I think the first option makes more sense as once a JsonSerializer is created I see no use case when the user would want to change his behavior regarding the validation.
Do you think I can/should experiment with alternative solutions?
Do you have something on your mind? I think this sounds good for the time being.
No, I don't have anything else in pat
Do we want to add this API now before 1.0.0?
It would make 1.0.0 stronger. But I also see no problem in adding this in a subsequent minor release. Let's prioritize things that need to go into 1.0.0.
I think it makes sense to include it in 1.0.0 as we have already discussed it broadly which will make it easier for a review.
Will prepare a pr soon.
There are two options on how to call the validation through the JsonSerializer:
pass a validate argument when creating a JsonSerializer object.
pass a validate argument when calling JSONSerializer.serialize()
I think the first option makes more sense as once a JsonSerializer is created I see no use case when the user would want to change his behavior regarding the validation.
Oh right, I remember. The first option is also consistent with how we configure compact. We only have to ask ourselves, how we want to expose this feature in Metadata.to_bytes:
Given the computational cost of validation I lean towards a default of validate=False and not changing to_bytes for the time being, so that in order to use the feature it a user would have to call e.g.
Activity
MVrachev commentedon Nov 27, 2021
I decided to create one prototype of a possible solution: https://github.com/MVrachev/tuf/tree/serialization-validation.
jku commentedon Nov 30, 2021
I agree with the basic premise: we'll want to re-evaluate whether our validation is appropriate for the serialization as well (while we've been concentrating on the client case, the focus has been on validating deserialized data).
some quick comments:
to_dict()is a helper that json serialization happens to use. There's no guarantee that doing so makes sense for some possible other serialization format. We might want to consider validating duringMetadata.to_bytes()if possibleMetadata.to_bytes()to serialize Metadata to bytes, then deserialize back to Metadata, and raise if the two metadata instances are not equal.jku commentedon Dec 1, 2021
I'll rephrase this to be more specific: Ideally we want to guarantee not just that our internal data structures are correct at serialization time, but that our serialization output is valid and that we can succesfully deserialize that output.
MVrachev commentedon Dec 6, 2021
I see many advantages to that approach:
Metadata.to_bytes()instead of each class separatelySo, I like the idea and I can volunteer for the next sprint to implement it.
The only question that remains is do we need to think for an API or a way to do a deeper serialization functionality as this one:
or maybe we don't actually as during an update with
TrustedMetadataSetwe will get an exception if something is bad?jku commentedon Dec 7, 2021
This is unlikely to get implemented in a single sprint but a design doc or prototype or some infrastructure work might be a good idea (I have no idea how much work there is in adding the eq() implementations as an example).
Loading (deserializing) validation should fail only if the there is a compliance or safety issue... and I think that's a good rule for serialization as well: deciding that implementations aren't allowed to add keys without using them doesn't clearly improve safety but it could be annoying to the repository implementers who want do it for whatever reasons.
Checks like that would make definitely make sense in repository implementations... and possibly even in a repository library.
MVrachev commentedon Dec 8, 2021
We should test how much slower it will get when using this kind of validation.
MVrachev commentedon Dec 21, 2021
I created a prototype in my branch here https://github.com/MVrachev/tuf/tree/validation-during-serialization.
Can you have a look @jku?
jku commentedon Jan 7, 2022
As a prototype that's pretty much what I was imagining: definitely good enough to make some sort of a decision
__eq__()implementations are 119 LOC -- not as bad as I feared but obviously makes metadata.py bigger without being useful to client codeMetadata.__eq__()seems complex, I don't understand whyOverall: looks like a reasonable direction to me but let's discuss with others too.
MVrachev commentedon Jan 7, 2022
Even if we decide to leave it as a separate API call I think it could be useful.
It's hard for me to be sure about that where we don't have actual interaction with users of Metadata API.
It's becasue the
Metadata.signaturesis an OrderedDict and its values are securesytemslibSignatureobjects which doesn't implement__eq__. I added a comment to clarify that.jku commentedon Jan 7, 2022
Thinking a bit further: the prototype ends up calling
to_dict()twice whenMetadata.to_bytes()is called. The second call is unnecessary if serialize() handled the validation so that seems like the correct direction for the real implementation of thisMVrachev commentedon Jan 7, 2022
Added a validation constructor argument in
JsonSerializer.2 remaining items
MVrachev commentedon Jan 14, 2022
@lukpueh do you think it should be an argument or it's okay to do it during initialization?
Also, do you think it makes sense to add it to the
MetadataSerializeras well?My question towards both of you @jku and @lukpueh is how do you think we should move forward?
Should I propose a pr? Do you think I can/should experiment with alternative solutions?
Do we want to add this API now before 1.0.0?
lukpueh commentedon Jan 14, 2022
Initialization of what? Regardless, given the computational cost of the validation, I'd make it optional.
No. I think this is something specific to JSONSerializer.
Yes.
Do you have something on your mind? I think this sounds good for the time being.
It would make 1.0.0 stronger. But I also see no problem in adding this in a subsequent minor release. Let's prioritize things that need to go into 1.0.0.
MVrachev commentedon Jan 14, 2022
There are two options on how to call the validation through the
JsonSerializer:validateargument when creating aJsonSerializerobject.validateargument when callingJSONSerializer.serialize()I think the first option makes more sense as once a
JsonSerializeris created I see no use case when the user would want to change his behavior regarding the validation.No, I don't have anything else in pat
I think it makes sense to include it in
1.0.0as we have already discussed it broadly which will make it easier for a review.Will prepare a pr soon.
lukpueh commentedon Jan 14, 2022
Oh right, I remember. The first option is also consistent with how we configure
compact. We only have to ask ourselves, how we want to expose this feature inMetadata.to_bytes:python-tuf/tuf/api/metadata.py
Lines 242 to 249 in 059bfda
Given the computational cost of validation I lean towards a default of
validate=Falseand not changingto_bytesfor the time being, so that in order to use the feature it a user would have to call e.g.We can discuss this more on the PR.
jku commentedon Jan 14, 2022
Agreed with the discussion above:
Add Signature.__eq__() method
Add Signature.__eq__() method
Add Signature.__eq__() method