Commit graph

1059 commits

Author SHA1 Message Date
Martin Vrachev
f00295f147 API CHANGE: ValueError in add/remove key in Root
This is an API change to the exceptions thrown in Root.add_key()
and Root.remove_key().
The reason for that change is that in my opinion the correct exceptions
in these cases should be "ValueError" instead of "KeyError" as
the problems are in the given values - role doesn't exist or
key is not used by a particular role.

Additionally, document the thrown exceptions in "Root.add_key" and
add a test which invokes that exception.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-09-21 12:20:09 +03:00
Martin Vrachev
e27070305f Metadata API: Add key helpers in Targets
Root class has the functionality to add and remove keys for delegated
metadata (add_key()/remove_key()) but the other delegator Targets does
not.
It should provide the same/similar functionality.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-09-21 12:20:09 +03:00
Martin Vrachev
1a5912aa7c Remove some unused imports
We can remove the conditional imports from tests as now we support
python versions 3.6+.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-09-21 12:20:09 +03:00
Martin Vrachev
510078b542 Move tests to test_metadata_serialization
Move the Delegation class serialization tests from "test_api.py"
to test_metadata_serialization.py module focused on serialization
testing.

Additionally, a test for empty keys and roles will be added in my
upcomming pr #1511.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-09-21 12:20:09 +03:00
Martin Vrachev
f8620c1992 API CHANGE: enforce role name uniqueness
The spec does not say anything about role name uniqueness in a
delegations object, but I believe we cannot safely allow multiple roles
with the same role name in the roles array of a delegations object.
If we did then the roles could have different keyids, and then we would
end up in a situation where metadata may be both a valid delegation
and an invalid delegation at the same time, depending on how the role
gets chosen and that does not seem like the intention of the design.
There is an issue open in the specification with number 167 about
that issue.

Regardless of the Metadata API, I think we should enforce role name
uniqueness.
I chose to change the data structure containing roles to
OrderedDict, where keys are role names and values are DelegatedRole
instances.
This made sense to me as role names are the unique identifier of a role
and their order is important to the way they are traversed afterward.

Note: we can't use OrderedDict as type annotation until we drop support
for Python 3.6:
https://docs.python.org/3/library/typing.html#typing.OrderedDict
That's why I used quotes around "OrderedDict" annotation, because I
can't import it.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-09-21 12:15:45 +03:00
Martin Vrachev
bf12e7565f Metadata API: change meta type in Timestamp
In Timestamp, the only valid "meta" value is the dictionary representing
meta information for the snapshot file. This makes the API unnecessarily
complicated and requires validation that only information about snapshot
is available inside "meta".
Together with the python-tuf maintainers, we decided that snapshot meta
information will not be represented by a "meta" dictionary but instead
by a MetaFile instance and with this it will diverge from the
specification.
Additionally, to prevent confusion, I will rename the "meta" attribute
to "snapshot_meta" as this attribute will be related only to meta
information about snapshot.

This decision is coherent with ADR9 and the rationale
behind it is to provide easier, safer, and direct access to the
snapshot meta information.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-09-20 14:09:38 +03:00
Jussi Kukkonen
6a5b64295c
Merge pull request #1521 from avelichka/targetfile-from-data
Add an option to create TargetFile from data/file
2021-09-13 09:50:35 +03:00
Jussi Kukkonen
57985a0e30
Merge pull request #1574 from jku/ngclient-persist-metadata-safely
Ngclient: persist metadata safely
2021-09-11 11:57:18 +03:00
Velichka Atanasova
65fd1aaf8a Add an option to create TargetFile from data/file
This is a repository tooling use case but also helpful when testing.
It could be useful when we need to update the targets object.

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
2021-09-10 13:59:33 +03:00
Jussi Kukkonen
ad813a5d0d tests: Add type checks suggested by mypy
also black fixes.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-10 09:59:03 +03:00
Jussi Kukkonen
87c200d014 tests: Add state dumping into RepositorySimulator
if state dumping is enabled with e.g.
    python3 test_updater_with_simulator.py --dump
The repository state can be dumped at will.

Modify the test so it dumps the state on every updater refresh if
--dump is set.

Add a root modifying case to test_refresh()

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-10 09:59:03 +03:00
Jussi Kukkonen
d64730b1e6 tests: RepositorySimulator: Add special handling for roots
We need to store past versions of root: that means an explicit
publish step (publish_root()) is required. It stores a serialization of
current root as a new version: fetch() then serves only these
serialized root versions.

Add a few tests demonstrating how to create root versions and change
signatures.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-10 09:59:03 +03:00
Jussi Kukkonen
4e5980e89d tests: Start testing ngclient with repo simulator
Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-10 09:59:03 +03:00
Jussi Kukkonen
d018279e21 ngclient: Fix rollback checks
The rollback checks themselves work, but they create a situation
where Updater does not realize that it needs to download e.g. a new
snapshot because the local snapshot is valid as _intermediate_ snapshot
(that can be used for rollback protection but nothing else), but is not
valid as final snapshot.

Raise in the end of update_snapshot and update_timestamp if the files
are not valid final metadata: this way the intermediate metadata does
get loaded but Updater also knows it is not the final metadata.

This modifies the existing tests but does not yet test the situation
described in the first paragraph.

Fixes #1563

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-10 09:59:03 +03:00
Jussi Kukkonen
bc05a1071e tests: Make sure ngclient stores the metadata we expect
Do not add similar assertions for test_refresh_on_consistent_targets():
The test is broken and can't actually update metadata (#1573).

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-08 16:03:03 +03:00
Jussi Kukkonen
ea9acf2bfd
Merge pull request #1564 from sechkova/improve-coverage
Add ngclient to coverage report
2021-09-08 13:07:01 +03:00
Teodora Sechkova
6a178f4c96
Apply black to test_updater_ng.py
Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-09-07 14:46:24 +03:00
Teodora Sechkova
867c2b39f0
Improve ngclient/updater.py coverage
Add tests covering missing branches of the Updater
code. Inlcude ngclient in the total coverage report.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-09-07 14:42:34 +03:00
Teodora Sechkova
61e8f40346
Update tests/test_trusted_metadata_set.py
Modify root tests to cover both loading inital
root metadata and updating it.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-09-07 14:41:08 +03:00
Teodora Sechkova
e6b41d582a
Merge pull request #1553 from jku/metadata-missing-tests
tests: Add some missing coverage
2021-09-02 14:24:55 +03:00
Joshua Lock
84004604c1
Merge pull request #1557 from joshuagl/joshuagl/python-tuf
Update links to account for repository rename
2021-09-02 10:04:29 +01:00
Teodora Sechkova
40082094df
Test SlowretreivalError in RequestsFetcher
Add test_read_timeout to test for
urllib3.ReadTimeoutError -> tuf.SlowRetrievalError.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-09-01 18:09:45 +03:00
Jussi Kukkonen
deec2eaaa0
Merge pull request #1519 from sechkova/fetcher-max-length
Remove max_length parameter from fetch
2021-09-01 17:27:12 +03:00
Jussi Kukkonen
017425e497 tests: Improve DelegatedRole coverage
Test basic cases of DelegatedRole.is_delegated_path()

This is not trying to be an extensive test of possible methods of delegation:
this gives us basic coverage for all code paths.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-01 13:34:50 +03:00
Jussi Kukkonen
c127b6b5a3 tests: Improve add_key/remove_key API tests
Make sure key is not removed from root.signed.keys on remove_key()
if another role is still using the key.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-09-01 13:34:50 +03:00
Joshua Lock
3dc5594242 Update links to account for repository rename
We have renamed the repository from tuf->python-tuf

Signed-off-by: Joshua Lock <jlock@vmware.com>
2021-09-01 11:15:33 +01:00
Jussi Kukkonen
e1ec782f19
Merge pull request #1520 from avelichka/sslib-key
Add Key.from_securesystemslib_key
2021-08-31 09:42:39 +03:00
Jussi Kukkonen
7d77eeec31
Merge pull request #1512 from MVrachev/glob-pattern-matching
Implement glob-like pattern matching
2021-08-31 09:42:27 +03:00
Teodora Sechkova
35ef056ce6
Add tests for ngclient/fetcher
Add tests for the file/data download.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-08-30 16:21:37 +03:00
Jussi Kukkonen
7731738590
Merge pull request #1514 from MVrachev/filename-in-targetfile
Metadata API: include target target name in TargetFile
2021-08-27 11:02:49 +03:00
Martin Vrachev
b18176db9b Implement glob-like pattern matching
According to the recently updated version of the specification the shell
style wildcard matching is glob-like (see https://github.com/theupdateframework/specification/pull/174),
and therefore a path separator in a path should not be matched by a
wildcard in the PATHPATTERN.

That's not what happens with `fnmatch.fnmatch()` which doesn't
see "/" separator as a special symbol.
For example: fnmatch.fnmatch("targets/foo.tgz", "*.tgz") will return
True which is not what glob-like implementation will do.

We should make sure that target_path and the pathpattern contain the
same number of directories and because each part of the pathpattern
could include a glob pattern we should check that fnmatch.fnmatch() is
true on each target and pathpattern directory fragment separated by "/".

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-26 19:57:52 +03:00
Martin Vrachev
9229a405e3 Remove filename argument from Targets.update()
After the addition of "path" argument in the TargetFile class the
filename argument in Targets.update() became redundant.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-26 19:48:46 +03:00
Martin Vrachev
91b0c59602 Metadata API: include target path in targetfile
Currently, TargetFile instances do not contain the path relative URL of
the file they represent. The API itself does not need it but it could be
useful for users of the API.

As an example, the current client returns a dict for
get_one_valid_targetinfo(): that dict contains a filepath field and
a targetinfo field (essentially TargetFile).
We would like to keep a similar API, but avoid hand-crafted dicts.
It would be much nicer to return a TargetFile that would contain the
full "metadata" of the targetfile.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-26 19:48:45 +03:00
Velichka Atanasova
c875b7ed04 Add Key.from_securesystemslib_key
The securesystemslib key dictionary representation includes
the private key in keyval. TUF key doesn't handle it in any way,
but considering that we allow unrecognized symbols in the format,
we should exclude the private key otherwise this could lead to
misuse.
A call to securesystemslib.keys.format_keyval_to_metadata
with the default private=False would do exactly that.

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
2021-08-26 15:37:25 +03:00
Jussi Kukkonen
66aac38a4a
Merge pull request #1511 from MVrachev/test-containers
new API: test containers for zero or more elements
2021-08-25 20:09:38 +03:00
Martin Vrachev
45743444b1 Remove duplicate serialization tests from test_api
I was looking at how can we simplify or split test_api.py when I noticed
that the test cases covered by those two test functions are already
covered in the test_metadata_serialization.py module in the
"invalid_keys" and "invalid_roles" datasets.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-25 16:03:03 +03:00
Martin Vrachev
4c3fd95cb1 Additional tests for containers with 0 or >1 items
Those tests are needed to cover use cases when syntatcticly as
standalone objects the metadata classes and their helper classes defined
in tuf/api/metadata.py are valid even if they cannot be verified.

An example where an object is valid, but cannot be verified is
if we have a Role instance with an empty list of "keyids".
This instance is valid and can be created, but cannot be verified
because there is a requirement that the threshold should be above
1, meaning that there should be at least 1 element inside the "keyids"
list to complete successful threshold verification.

The situation is the same for the rest of the tests I am adding to this
commit:
- Root object without keys
- Root object without roles
- DelegationRole object with empty "keyids"
- DelegationRole object with an empty list of "paths"
- DelegationRole object with an empty list of "path_hash_prefixes"
all of these objects can be instantiated, but cannot complete
successfully threshold verification.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-20 17:12:44 +03:00
Martin Vrachev
df6a399b08 new API: test containers for zero or more elements
Test metadata (de)serialization with input data containing containers
with zero or more elements.

Here is the status for the different use cases:
Root keys:
- many keys: added
Root roles:
- many roles: added
Root role keyids:
- many keids: already added in https://github.com/theupdateframework/tuf/pull/1481
MetaFile hashes:
- many hashes: already tested
- zero hashes: added. Testing as invalid test case.
Timestamp meta:
- zero elements: already tested
- many elements: added
Snapshot meta:
- zero items: added
- many items: added
Delegation keys:
- many keys: added
Delegation role keyids:
- many keyids: added
Delegation role paths:
- many paths: already tested
Delegation role path_hash_prefixes:
- many path_hash_path_prefixes: already tested
Delegation roles:
- zero roles: added
- multiple roles: added
Targets targets:
- zero items: already tested
- multiple items: added

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-20 17:12:43 +03:00
Jussi Kukkonen
c8696d905e tests: Remove unhelpful helper
Doing the work inline is less code and easier to understand.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-19 11:22:01 +03:00
Jussi Kukkonen
e3b789c306 ngclient: allow limited use of wrong snapshot version
Spec does not explicitly say so but the intent is that a snapshot
metadata can be trusted for rollback protection checks of newer
snapshots even if current snapshot version does not match the version
in current timestamp meta.

Only do the snapshot version check for the "final" snapshot by doing it
when targets is updated.

Improve test names and comments.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-19 11:22:01 +03:00
Jussi Kukkonen
b515997701 ngclient: Allow limited use of expired timestamp/snapshot
While this is not explicitly said in the spec, the intention is that
expired timestamp and snapshot should be used for rollback protection
checks on newer timestamp/snapshot (but not for anything else).

Move the expiry checks to the "next" metadata update: timestamp expiry
is checked when snapshot is loaded, and snapshot expiry is checked
when targets is loaded.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-19 11:22:01 +03:00
Jussi Kukkonen
091197288f ngclient: Remove root_update_finished()
The usefulness was debatable to begin with, and now that it has become
clear that rollback protection requires a second "final verification"
step for all three root, timestamp and snapshot it is clear that
root_update_finished() is not good design.

update_root() still accepts expired root metadata but now the final
root expiry is checked when the "next" metadata (timestamp) is loaded.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-19 11:09:12 +03:00
Joshua Lock
e9106b59cd
Merge pull request #1522 from jku/more-trusted-set-tests
More trusted set tests
2021-08-19 08:31:52 +01:00
Jussi Kukkonen
d3441f056a
Merge pull request #1457 from jku/use-generics-to-improve-signed-typing
Improve signed typing
2021-08-18 12:01:33 +03:00
Jussi Kukkonen
7901687017
Merge pull request #1501 from MVrachev/ngclient-support-consistent-snapshot
ng client: support for prefix_targets_with_hash when downloading targets
2021-08-18 09:20:11 +03:00
Jussi Kukkonen
f02fed2502 tests: Test TrustedMetadataSet iteration
Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-17 15:50:01 +03:00
Jussi Kukkonen
85656a56eb tests: Add missing test to TrustedMetadataSet
Test failing length/hash test for snapshot

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-17 15:40:13 +03:00
Jussi Kukkonen
efb5653016 tests: Improve TrustedSet coverage
* test for succesful root update
* fix test for snapshot version rollback in timestamp update

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-08-17 13:54:18 +03:00
Jussi Kukkonen
25993d6a2d
Merge pull request #1477 from MVrachev/ng-client-improve-metadata-set-testing
ng client improve TrustedMetadataSet testing
2021-08-17 11:00:43 +03:00
Martin Vrachev
617e87eb26 Annotations and use extensively modify_metadata
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-08-16 18:13:01 +03:00