Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions tests/test_metadata_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ def test_invalid_delegated_role_serialization(self, test_case_data: str):
{"keyids": ["keyid2"], "name": "a", "paths": ["fn3"], "terminating": false, "threshold": 2} \
] \
}',
"using empty string role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ \
{"keyids": ["keyid1"], "name": "", "terminating": true, "paths": ["fn1"], "threshold": 3}] \
}',
"using root as delegate role name": '{"keys": { \
"keyid1" : {"keytype": "rsa", "scheme": "rsassa-pss-sha256", "keyval": {"public": "foo"}}}, \
"roles": [ \
Expand Down
1 change: 0 additions & 1 deletion tests/test_updater_with_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def test_targets(self, test_case_data: Tuple[str, bytes, str]):
def test_fishy_rolenames(self):
roles_to_filenames = {
"../a": "..%2Fa.json",
"": ".json",
".": "..json",
"/": "%2F.json",
"ö": "%C3%B6.json",
Expand Down
8 changes: 6 additions & 2 deletions tuf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,12 @@ def __init__(
unrecognized_fields: Optional[Mapping[str, Any]] = None,
):
self.keys = keys
if [role for role in set(roles) if role in TOP_LEVEL_ROLE_NAMES]:
raise ValueError("Delegated roles cannot use top-level role names")

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"
)

@MVrachev MVrachev Nov 24, 2021

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
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?


self.roles = roles
self.unrecognized_fields = unrecognized_fields or {}
Expand Down