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

Prevents delegate role name as top-level role name #1690

Conversation

kairoaraujo
Copy link
Collaborator

This commit adds the validation in the metadata.Delegations
to prevent that one of the delegate role names given is a top-level
role name.

A ValueError will be raised if one of the roles names in the
the list given to as delegated contains the role name as one of the
top-level roles.

The mirrors role is not covered in this PR.

Signed-off-by: Kairo de Araujo kdearaujo@vmware.com

Please fill in the fields below to submit a pull request. The more information
that is provided, the better.

Fixes #1558

Description of the changes being introduced by the pull request:

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

This commit adds the validation in the ``metadata.Delegations``
to prevent that one of the delegate role names given is a top-level
role name.

A ``ValueError`` will be raised if one of the roles names in the
list given to as delegated contains the role name as one of the
top-level roles.

Signed-off-by: Kairo de Araujo <kdearaujo@vmware.com>
@coveralls
Copy link

coveralls commented Nov 22, 2021

Pull Request Test Coverage Report for Build 1516182630

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.1%) to 97.581%

Files with Coverage Reduction New Missed Lines %
tuf/api/metadata.py 1 98.1%
tuf/ngclient/updater.py 6 95.3%
Totals Coverage Status
Change from base Build 1490568233: 0.1%
Covered Lines: 4032
Relevant Lines: 4116

💛 - Coveralls

Copy link
Collaborator

@MVrachev MVrachev left a comment

Choose a reason for hiding this comment

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

Thank you @kairoaraujo for your pr and effort!
There are a couple of things to fix, but it's a great start.

Please note that at the current stage mirrors won't be implemented in the new implementation. For more reference read the second point from issue #1317.

tests/test_metadata_serialization.py Outdated Show resolved Hide resolved
@@ -360,6 +360,43 @@ def test_delegation_serialization(self, test_case_data: str):
delegation = Delegations.from_dict(copy.deepcopy(case_dict))
self.assertDictEqual(case_dict, delegation.to_dict())

valid_delegations: utils.DataSet = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please use the dataset defined at line 321

invalid_delegations: utils.DataSet = {

and the test function defined in 336
def test_invalid_delegation_serialization(self, test_case_data: str):

instead of defining a valid_delegations and a new test function?

We aim to group all valid tests related to a specific class in one dataset and test function.
Also, the name valid_delegations is not correct.
Those are actually cases of invalid_delegations as when you call Delegations.from_dict(copy.deepcopy(case_dict)) it causes ValueError as you catch it inside your the test function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have a concern about how to make sure that the ValueError is the correct unit we want.
I mean, we test the ValueError raised, but we don't check if the message comes from the part we want to cover.
Unless we don't want to go deep in this level, it is ok.

Copy link
Collaborator

@MVrachev MVrachev Nov 23, 2021

Choose a reason for hiding this comment

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

I think this is a more general question that probably is worth adressing.
I think for sure we want to have all invalid tests for Delegations in one single place, but I understand what you mean.
Maybe we can check the error message and type inside the with?
What do you think @jku?

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, indeed. If agreed in checking the type and error message, we can address it in another issue.

tests/test_metadata_serialization.py Outdated Show resolved Hide resolved
tests/test_metadata_serialization.py Outdated Show resolved Hide resolved
tuf/api/metadata.py Outdated Show resolved Hide resolved
- Reuse the dataset and the existing tests
- Fix the keyids in the tests datasets to be aligned
- Fix the ``ValueError`` message aligned to the existent messages

Signed-off-by: Kairo de Araujo <kdearaujo@vmware.com>
@sechkova
Copy link
Contributor

The issue discussion mentions also preventing delegated roles from being empty strings. Do you mind adding this check as part of this PR? Sorry if it's been done already somewhere and I've missed that.

- Add the check for empty strings in the Delegate Role name
- Remove the comprehensive lists to make the code more readable
- Remove the test for empty file name from
``test_updater_with_simulator``

Signed-off-by: Kairo de Araujo <kdearaujo@vmware.com>
Comment on lines 1217 to 1221
for role in set(roles):
if not role or role in TOP_LEVEL_ROLE_NAMES:
raise ValueError(
"Delegated roles cannot be empty or use top-level role names"
)
Copy link
Collaborator

@MVrachev MVrachev Nov 24, 2021

Choose a reason for hiding this comment

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

You still can write the check as a one-line like this:

Suggested change
for role in set(roles):
if not role or role in TOP_LEVEL_ROLE_NAMES:
raise ValueError(
"Delegated roles cannot be empty or use top-level role names"
)
if any(not role or role in TOP_LEVEL_ROLE_NAMES for role in set(roles)):
raise ValueError(
"Delegated roles cannot be empty or use top-level role names"
)

and I personally prefer it.
@sechkova what do you think?

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, I had a small talk with @jku, and we came out about making it more readable. I'm open about any option 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the check, I agree that separate lines are more readable.

Copy link
Member

Choose a reason for hiding this comment

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

set() does not seem necessary?

@MVrachev
Copy link
Collaborator

@sechkova can you approve running the CI workflows as GitHub says that:
First-time contributors need a maintainer to approve running workflows.

Copy link
Contributor

@sechkova sechkova left a comment

Choose a reason for hiding this comment

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

I don't know if @MVrachev has any more comments, looks ok to me.

@MVrachev
Copy link
Collaborator

Thanks for asking.
I don't have any other comments.
LGTM!

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.

LGTM, but there does seem to be an unnecessary set construction in there

Comment on lines 1217 to 1221
for role in set(roles):
if not role or role in TOP_LEVEL_ROLE_NAMES:
raise ValueError(
"Delegated roles cannot be empty or use top-level role names"
)
Copy link
Member

Choose a reason for hiding this comment

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

set() does not seem necessary?

The set() is not required in the OrderedDict.

Signed-off-by: Kairo de Araujo <kdearaujo@vmware.com>
@jku jku merged commit 2de883a into theupdateframework:develop Nov 29, 2021
@jku
Copy link
Member

jku commented Nov 29, 2021

Thanks!

@lukpueh lukpueh mentioned this pull request Dec 13, 2021
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 API: prevent Delegation role names to be one of top level metadata roles
5 participants