Commit graph

986 commits

Author SHA1 Message Date
Teodora Sechkova
3f1aa10dbd Expose client public classes from __init__.py
Only "Updater" and "FetcherInterface" are considered
public classes of the client. Exposing them in __init__.py
makes usage and access simpler.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-05-21 10:00:00 +03:00
Teodora Sechkova
c29051cc4b Move non-public modules to nglient/_internal
Separate public/private API. Keep modules
containing the piblic classes in the main client
directory and move the rest to _internal.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-05-21 10:00:00 +03:00
Teodora Sechkova
c64a6f92c1 Rename client_rework to ngclient
The current client and the next-gen client should
coexist in the same repository during the ongoing
development of the latter.

Looking for a name which is client-related,
short, meeting PEP8 package names requirements.
Currently "ngclient" seems to fit in until a
better proposal comes.

Rename updater_rework.py to updater.py

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-05-21 10:00:00 +03:00
Teodora Sechkova
3bee15e2e0 Merge branch 'develop' into merge-develop-into-exp-client 2021-05-20 18:00:54 +03:00
Jussi Kukkonen
d35fc27b0a New updater: Update comments
Mostly remove comments that provide little value after all the changes.
Also remove a unused variable from a test.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-20 15:41:24 +03:00
Martin Vrachev
15bf88231d Remove testing "update()" with "version" only
We have tests which make sure we can use `Timestamp.update()` and
`Snapshot.update()` with MetaFile instance storing only version
(because length and hashes are optional).
Those tests were created to make sure that we are actually supporting
optional hashes and length when we call `update` for those classes, but
after we changed the `update()` signature to accept `MetaFile` instance
the tests are obsolete.
The reason is that length and hashes can be optional because of the
MetaFile implementation, no the update function itself and we have
other tests validating creating a MetaFie instance without hashes and
length.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-19 13:21:32 +03:00
Martin Vrachev
37de69050a Change "update()" argument types
Currently, when we call Targets/Snapshot/Timestamp.update() we are
passing all of the necessary values to create MetaFile/Targets File
respectively.
This is not needed, given that one of the reasons we have created
MetaFile and TargetFile is to make the API easier to use.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-19 13:21:32 +03:00
Martin Vrachev
408732f4ff Add MetaFile/TargetFile specific tests
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-19 13:21:32 +03:00
Martin Vrachev
60bbb165a8 New API: Add TargetFile class
In the top-level metadata classes, there are complex attributes such as
"meta" in Targets and Snapshot, "key" and "roles" in Root etc.
We want to represent those complex attributes with a class to allow
easier verification and support for metadata with unrecognized fields.
For more context read ADR 0004 and ADR 0008 in the docs/adr folder.

As written in the spec "targets" in "targets.json" has defined the
"custom" field serving the same purpose as "unrecognized_fields" in the
implementation.
That's why to conform against the spec and support "custom" and allow
"unrecognized_fields" everywhere where it's not sensitive we can define
custom as property which actually access data stored in
unrecognized_fields.
For context read ADR 8 in tuf/docs/adr.

Additionally, after adding the TargetFile class, when we create a
Targets an object we are now calling from dict twice - one for the main
Targets class and one for each of the complex attributes
TargetFile.from_dict() and Delegations.from_dict().
Given that the "from_dict" methods have the side effect of destroying
the given dictionary, we would need to start using deepcopy()
for our tests.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-19 13:21:31 +03:00
Martin Vrachev
3771a77ffe New API: Add MetaFile class
In the top-level metadata classes, there are complex attributes such as
"meta" in Targets and Snapshot, "key" and "roles" in Root etc.
We want to represent those complex attributes with a class to allow
easier verification and support for metadata with unrecognized fields.
For more context read ADR 0004 and ADR 0008 in the docs/adr folder.

Additionally, after adding the MetaFile class, when we create an object
we are now calling from dict twice - one for the main class (Timestamp,
Snapshot) and one for the pacticular complex attribute -
MetaFile.from_dict(). Given that the "from_dict" methods have the
side effect of destroying the given dictionary, we would need to
start using deepcopy() for our tests.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-19 13:04:24 +03:00
Jussi Kukkonen
8348523b77
Merge pull request #1394 from MVrachev/optional-consistent-snapshot
Metadata API: Make consistent_snapshot optional
2021-05-18 21:11:57 +03:00
Jussi Kukkonen
2bbf5bc178 tests: Cleanup in new Updater tests
Add test for a refresh with just a local root.json.

Remove unused code. Add docstrings for raised exceptions, add TODOs for
the missing exception handling.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-18 20:56:10 +03:00
Jussi Kukkonen
1d45c2aa2c New Updater: use the MetadataBundle
Use the MetadataBundle to verify metadata validity.
* Updater now handles reading metadata files (from filesystem as
  well as network
* Updater feeds bytes to MetadataBundle for verification
* Updater persists data on disk after it had been verified

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-18 20:56:02 +03:00
Martin Vrachev
de2644f3d0 Breaking: new API: consistent_snapshot optional
NOTE: making consistent_snapshot optional requires using a default value
for the argument in __init__ in Root and thus consistent_snapshot should
be rearranged in the end.
Read more: https://github.com/theupdateframework/tuf/pull/1394#issuecomment-842134961

From chapter 7 in the spec (version 1.0.17)
"Finally, the root metadata should write the Boolean
"consistent_snapshot" attribute at the root level of its keys of
attributes.
If consistent snapshots are not written by the repository,
then the attribute may either be left unspecified or be set to the
False value. Otherwise, it must be set to the True value."

We want to make sure we support repositories
without consistent_snapshot set.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-17 11:41:52 +03:00
Jussi Kukkonen
f2cff951a6 MetadataBundle: Don't do any file IO
Remove file IO from MetadataBundle:
* This make the bundle API very clear and easy to understand
* This means caller must now read from and persist data to disk
  but initial prototypes suggest this won't make Updater too
  complex

This change is something we can still back out from if it turns out to
be the wrong decision: the file-persisting MetadataBundle has been tested
and works fine.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 12:30:06 +03:00
Jussi Kukkonen
b86d1f733f MetadataBundle: Raise instead of returning bool
The bundle should now raise
* derivatives of RepositoryError on failures that are likely a result
  of server error or a malicious server
* RuntimeErrors if calls were made when they are not possible
* ValueErrors if arguments are invalid

last two are callers errors and avoidable.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 10:48:59 +03:00
Jussi Kukkonen
eb648d19bc MetadataBundle: Save original files on disk
Don't use the serialized format as that won't match any hashes in
"meta".

Add basic tests for updating metadata.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 10:48:59 +03:00
Jussi Kukkonen
2d155faae6 MetadataBundle: Change ValueErrors to RuntimeErrors
As the metadata type is no longer an argument, these are not
ValueErrors.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 10:48:59 +03:00
Jussi Kukkonen
f8b714d167 Metadata API: Don't do equality comparisons on containers
Use either "if X is not None:" or a try-except instead of a "if X:".

I believe Targets.from_dict() was not really broken with previous code
but it looks suspicious and did fail the added test with a strange
exception: I expect the from_dict() methods to mainly fail with
KeyErrors, ValueErrors or AttributeErrors if file format structure
is incorrect.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 10:48:59 +03:00
Jussi Kukkonen
112b333bba Metadata API: Fix DelegatedRole serialization issue
A DelegatedRole with paths=[] fails to serialize correctly (paths is not
included in the output json).

Fix the issue, modify tests to notice a regression.

Fixes #1389

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 10:48:59 +03:00
Jussi Kukkonen
0bbfe038cf tests: Add minimal test case for Bundle
Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-16 10:48:59 +03:00
Jussi Kukkonen
14f5957eee Metadata API: Don't do equality comparisons on containers
Use either "if X is not None:" or a try-except instead of a "if X:".

I believe Targets.from_dict() was not really broken with previous code
but it looks suspicious and did fail the added test with a strange
exception: I expect the from_dict() methods to mainly fail with
KeyErrors, ValueErrors or AttributeErrors if file format structure
is incorrect.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-14 16:12:50 +03:00
Jussi Kukkonen
aa480b1280 Metadata API: Fix DelegatedRole serialization issue
A DelegatedRole with paths=[] fails to serialize correctly (paths is not
included in the output json).

Fix the issue, modify tests to notice a regression.

Fixes #1389

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-14 14:02:29 +03:00
Teodora Sechkova
167e1793d0 Fix Root.add_key() argument's type
After the implementation of a Key class representing
the public portion of a key, the method add_key() should
take an argument of type Key, instead of a dictionary.

Test cases are updated accordingly.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-05-13 11:23:15 +03:00
Teodora Sechkova
10a02ddd32 Merge branch 'develop' into experimental-client 2021-05-12 17:07:22 +03:00
Jussi Kukkonen
ec4c5ce00d tests: Don't use os.path.join() for URLS
The test has issues like this alsready but let's not add more...

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-12 15:42:53 +03:00
Martin Vrachev
42b3269ac1 Remove forgotten debug line from a test
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-11 16:04:25 +03:00
Martin Vrachev
f98f4f750a Add Delegation/DelegatednRole specific tests
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-11 16:04:25 +03:00
Martin Vrachev
8e4d3b9a84 Add DelegatedRole and Delegations
In the top level metadata classes, there are complex attributes such as
"meta" in Targets and Snapshot, "key" and "roles" in Root etc.
We want to represent those complex attributes with a class to allow
easier verification and support for metadata with unrecognized fields.
For more context read ADR 0004 and ADR 0008 in the docs/adr folder.

DelegatedRole shares a couple of fields with the Role class and that's
why it inherits it.
I decided to use a separate Delegations class because I thought it will
make it easier to read, verify and add additional helper functions.
Also, I tried to make sure that I test each level of the delegations
representation for support of storing unrecognized fields.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-11 16:03:56 +03:00
Jussi Kukkonen
17121bad28 Metadata API: remove_key() improvements
We should not do multiple lookups through data structures if one is
enough (here we have extra lookups on both roles and keyids).

Also in this case raising on missing key seems like the preferable
alternative so even a try-except is not needed.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-10 17:02:12 +03:00
Martin Vrachev
139bfc0ea9 BREAKING CHANGE: Make delegations optional
According to the spec, delegations in targets are marked as optional:
https://theupdateframework.github.io/specification/latest/#file-formats-targets
and a pr, clarifying that even more, is approved:
https://github.com/theupdateframework/specification/pull/157.

This is a possible breaking change.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-10 15:27:37 +03:00
Martin Vrachev
71c4992cea Make length and hashes optional in Timestamp
As per the specification (v1.0.1) length and hashes fields
in timestamp and snapshot metadata are optional.
We have implement this in the older API
(see https://github.com/theupdateframework/tuf/pull/1031) and we should
implement it in the new API.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-10 15:27:37 +03:00
Martin Vrachev
c0d72d460c New API: Add root use case in couple of tests
Add a use case for the root class to be tested in test_generic_read
and test_read_write_read_compare tests in test_apy.py

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-10 15:27:37 +03:00
Martin Vrachev
1ce94b95cb keyid: verify adding an existing key is ignored
Verify that adding an already existing key to keyid for a particular
role in Root won't create duplicate key.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-04-29 12:26:40 +03:00
Teodora Sechkova
576d055cd4 Drop mirrors support
Updater now uses only a single url for metadata download.
Target files download use either a default url or an
optional one for each file passed by the caller.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-04-29 12:14:51 +03:00
Jussi Kukkonen
d0438e3de9 Merge branch 'develop' into merge-develop-to-experimental-client
Merging to longer living working branch to get linter fixes from develop
2021-04-28 10:07:36 +03:00
Martin Vrachev
54a535e4c3 New API: Add Key/Role specific tests
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-04-27 15:26:41 +03:00
Martin Vrachev
eab8c318f0 Add a Role class and integrate it into Root
In the top level metadata classes, there are complex attributes such as
"meta" in Targets and Snapshot, "key" and "roles" in Root etc.
We want to represent those complex attributes with a class to allow
easier verification and support for metadata with unrecognized fields.
For more context read ADR 0004 and ADR 0008 in the docs/adr folder.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-04-27 15:13:40 +03:00
Martin Vrachev
11e0a87bde Add Key class and integrate it into Root
In the top level metadata classes, there are complex attributes such as
"meta" in Targets and Snapshot, "key" and "roles" in Root etc.
We want to represent those complex attributes with a class to allow
easier verification and support for metadata with unrecognized fields.
For more context read ADR 0004 and ADR 0008 in the docs/adr folder.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-04-27 15:09:48 +03:00
Jussi Kukkonen
feb340f8da
Merge pull request #1345 from MVrachev/implement-adr-8
New metadata API: add support for ADR 0008
2021-04-22 21:54:32 +03:00
Martin Vrachev
79391f1d85 New API: accept metadata with unrecognized fields
In order to support ADR 0008 we would want to accept unrecognized
fields in all metadata classes.
Input that contains unknown fields in the 'signed' dictionary should
successfully deserialize into a Metadata object, and that object should
successfully serialize with the unknown fields intact.

Also, we should test that we support unrecognized fields when adding
new classes or modifying existing ones to make sure we support
ADR 0008.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-04-22 17:11:05 +03:00
Jussi Kukkonen
df1a8885b3 Merge remote-tracking branch 'origin/develop' into merge-master-to-experiemental-client 2021-04-21 15:46:19 +03:00
Jussi Kukkonen
4e8738ffa5 api: Add Metadata.from_bytes()
This is essentially short-hand for
    JSONDeserializer().deserialize(data)
but seems much easier for the API user so may be worth it.

Metadata.from_file() now uses Metadata.from_bytes() internally.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-04-21 09:36:34 +03:00
Jussi Kukkonen
7bf41c953e
Merge pull request #1347 from avelichka/develop
Add a method of Signed metadata class returning information about metadata expiration
2021-04-16 08:49:58 +03:00
Velichka Atanasova
1eaef0093b Add is_expired method to the Signed class
Checks metadata expiration against a reference time (a naive datetime in UTC).
If not provided, checks against the current UTC date and time.
Returns True if expiration time is less than the reference time.

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
2021-04-14 13:28:36 +03:00
Philippe Coval
f00f89328e tests: Use current python interpreter for sub tests
Can be useful to run tests using distro runtimes,
(like python3 on Debian).

Relate-to: https://github.com/theupdateframework/tuf/issues/263
Origin: https://salsa.debian.org/rzr/python-tuf/-/tree/debian/review/master
Forwarded: https://github.com/theupdateframework/tuf/pull/1337
Signed-off-by: Philippe Coval <rzr@users.sf.net>
2021-04-12 23:34:31 +02:00
Jussi Kukkonen
1902975d0e tests: Kill processes before deleting temp files
These tests seem to try to remove temp files before the processes
using those files had stopped. This likely lead to an error (and
dangling temp files) on Windows, but Modified_Testcase hides the error

Make sure temp directories are removed as the last thing in teardown.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-04-12 11:55:37 +03:00
Jussi Kukkonen
441a4fa343 tests: Remove temp directories after other cleanup
Call the parent (Modified_Testcase) tearDown as the last thing in
tearDown(). This is good practice anyway and in practice may prevent
bugs where the instance needs to cleanup something before
Modified_Testcase removes the temp dir.

In practice there does not seem to be visible bugs in these tests
(as the all have top level temp directory handling in tearDownClass())

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-04-12 11:55:37 +03:00
Jussi Kukkonen
1b7fca4910 tests: Fix the order of rmdir and process kill
Make sure test server processes are killed before the temporary
directories are removed.

Let Modified_Testcase handle the top-level temporary directory.
Don't let Modified_testcase handle any subdirectories because:
 * teardown will try to remove them in the wrong order
 * removing the top level is enough

Fixes #1344

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-04-12 11:55:31 +03:00
Kainaat Singh
ec68bd9316 Remove future module #1297
Signed-off-by: Kainaat Singh <kainaatsingh93@gmail.com>

remove unwanted lines
2021-04-11 11:37:45 +02:00