Skip to content

Metadata API: validate root role names - #1630

Merged
jku merged 3 commits into
theupdateframework:developfrom
MVrachev:validate-role
Oct 27, 2021
Merged

Metadata API: validate root role names#1630
jku merged 3 commits into
theupdateframework:developfrom
MVrachev:validate-role

Conversation

@MVrachev

Copy link
Copy Markdown
Collaborator

Fixes#1516

Description of the changes being introduced by the pull request:

Validate that root role names are 4 and that they are exactly
"root", "snapshot", "targets" and "timestamp" as described in
the spec:
https://theupdateframework.github.io/specification/latest/#root-role

Additionally, fix the valid_roots dataset, so each of the cases contains
the top metadata role names inside the roles dictionary.

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

@MVrachev

Copy link
Copy Markdown
CollaboratorAuthor

The CI failures are fixed in #1631.

Comment threadtuf/api/metadata.py Outdated
Comment threadtuf/api/metadata.py Outdated
@coveralls

coveralls commented Oct 25, 2021

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 1381179026

  • 3 of 3(100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 97.43%

TotalsCoverage Status
Change from base Build 1380118579:0.002%
Covered Lines:3946
Relevant Lines:4030

💛 - Coveralls

@lukpuehlukpueh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice patch, @MVrachev, and thanks for considering my comments from earlier! I have two more comments/questions. Other than that this lgtm.

roles = {role_name: Role([], 1) for role_name in TOP_LEVEL_ROLE_NAMES}
root = Root(1, SPEC_VER, self.safe_expiry, {}, roles, True)

for role in TOP_LEVEL_ROLE_NAMES:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that sets are unordered. If the original order, i.e. ["root", "timestamp", "snapshot", "targets"], needs to be guaranteed, you can't use TOP_LEVEL_ROLE_NAMES here.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Good note. I don't think the order matters here, am I right @jku?

Also, I decided to use a set, instead of a list for TOP_LEVEL_ROLE_NAMES because of this check:
if set(roles) != TOP_LEVEL_ROLE_NAMES: in tuf/api/metadata.py in Root.init() will result as false even if roles have the same content as TOP_LEVEL_ROLE_NAMES. The reason is that one is a list and the other is a set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think using a set makes sense. If it turns out that there are many use cases that require the same particular order of the top-level role names, we can still add another global that is a list, or make this one an ordered set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

WFM

Comment threadtuf/api/metadata.py
self.keys = keys
if set(roles) != TOP_LEVEL_ROLE_NAMES:
raise ValueError("Role names must be the top-level metadata roles")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be helpful to include the received, i.e. roles and expected, i.e. TOP_LEVEL_ROLE_NAMES value in the error message. I can't remember, do we have a general recommendation for error messages in that regard?

@jkujkuOct 25, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd avoid any complexity. We're preparing for one of two options:

  • deserialization: error message won't be useful
  • construction of new Root: this is code that we hopefully write once in a repository component and that's it

complex error message would have very limited value in both cases

Comment threadtuf/api/metadata.py Outdated
Comment threadtests/test_metadata_serialization.py Outdated
Martin Vrachev added 3 commits October 25, 2021 15:32
This constant can be used across tuf without defining it each time.
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
Validate that root role names are 4 and that they are exactly
"root", "snapshot", "targets" and "timestamp" as described in
the spec:
https://theupdateframework.github.io/specification/latest/#root-role
Additionally, fix the valid_roots dataset, so each of the cases contains
the top metadata role names inside the roles dictionary.
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>

@lukpuehlukpueh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

roles = {role_name: Role([], 1) for role_name in TOP_LEVEL_ROLE_NAMES}
root = Root(1, SPEC_VER, self.safe_expiry, {}, roles, True)

for role in TOP_LEVEL_ROLE_NAMES:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think using a set makes sense. If it turns out that there are many use cases that require the same particular order of the top-level role names, we can still add another global that is a list, or make this one an ordered set.

jku
jku approved these changes Oct 26, 2021

@jkujku left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@jku
jku merged commit 1d115b5 into theupdateframework:developOct 27, 2021
@MVrachev
MVrachev deleted the validate-role branch November 5, 2021 15:50
@lukpuehlukpueh mentioned this pull request Dec 13, 2021
@coveralls

coveralls commented Dec 23, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 1380621788

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 2 of 2(100.0%) changed or added relevant lines in 1 file are covered.
  • 6 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+1.1%) to 98.478%

Files with Coverage ReductionNew Missed Lines%
tuf/api/metadata.py697.97%
TotalsCoverage Status
Change from base Build 1380118579:1.1%
Covered Lines:3785
Relevant Lines:3811

💛 - Coveralls

Sign up for freeto 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 API validation: validate root roles dictionary content

4 participants

@MVrachev@coveralls@jku@lukpueh