Commit graph

4210 commits

Author SHA1 Message Date
Teodora Sechkova
22facb7753
Update test_api
Add tests for hash and length verification.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-06-11 11:59:09 +03:00
Teodora Sechkova
8710f8d71a
Add hash and length verification
Extend MetaFile and TargetFile classes with methods
for length and hash verification.  The common functionality
is implemented as static methods of the base class while
MetaFile and TargetFile implement the user API based on it.

Define LengthOrHasheMismathError.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-06-11 11:52:54 +03:00
Jussi Kukkonen
de78251b00
Merge pull request #1423 from jku/verify-in-key
Metadata API: implement sig verification in Key, store id in key
2021-06-09 10:14:45 +03:00
Jussi Kukkonen
414dfc829f Metadata API: Move signature verification to Key
This is likely not needed by users of the API (as they are interested
in the higher level functionality "verify delegate metadata with
threshold of signatures").

Moving verify to Key makes the API cleaner because including both
"verify myself" and "verify a delegate with threshold" can look awkward
in Metadata, and because the ugly Securesystemslib integration is now
Key class implementation detail (see Key.to_securesystemslib_key()).

Also raise on verify failure instead of returning false: this was found
to confuse API users (and was arguably not a pythonic way to handle it).

* Name the function verify_signature() to make it clear what is being
  verified.
* Assume only one signature per keyid exists: see #1422
* Raise only UnsignedMetadataError (when no signatures or verify failure),
  the remaining lower level errors will be handled in #1351
* Stop using a "keystore" in tests for the public keys: everything we
  need is in metadata already

This changes API, but also should not be something API users want to
call in the future when "verify a delegate with threshold" exists.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-06-07 13:20:26 +03:00
Jussi Kukkonen
41a6daca75 Metadata API: Add id to Key
This simplifies life for API users as usually a key needs its
identifier: this is already visible in how update() becomes simpler
in the API.

The downside is that 'from_dict()' now has two arguments (so arguably
the name is not great anymore but it still does _mostly_ the same job
as other from_dicts).

This is an API change, if a minor one.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-06-07 13:20:22 +03:00
Jussi Kukkonen
7f3b15e125
Merge pull request #1432 from avelichka/develop
Stop using formats.py in metadata.py
2021-06-04 09:43:44 +03:00
Velichka Atanasova
4e8a0fe140 Stop using formats.py in metadata.py
Currently we have one use of tuf/formats.py in tuf/api/metadata.py.
If we do the conversion of the expires string in metadata.py,
we can keep the two implementations separate.

Signed-off-by: Velichka Atanasova <avelichka@vmware.com>
2021-06-03 13:26:35 +03:00
Jussi Kukkonen
0b54cafa94
Merge pull request #1415 from MVrachev/targets-optional
new API: make sure targets in Targets can be empty
2021-06-02 11:19:43 +03:00
Jussi Kukkonen
d9a928e626
Merge pull request #1395 from jku/initial-mypy-integration
Build: Initial mypy integration
2021-06-01 14:33:47 +03:00
Philippe Coval
f496c83e78 docs: Remove academics files
Duplication is not needed since files are hosted in website project:

https://github.com/theupdateframework/theupdateframework.io/tree/master/static/papers

Those generated files are not explicitly under Apache-2.0 licence
and AFAIK they can not be regenerated from missing (latex?) sources.

To avoid licence mixup.
It would help to have those files published elsewhere.
Meanwhile online (Github) links are used.

Debian had to repack the source package to make tarball compliant with DFSG
despite debian tools are known to be trustworthy,
this extra step would add weakess in the chain of trust

Cleanup done upstream would make distribution safer.

Bug: https://github.com/theupdateframework/tuf/issues/1161
Bug-Debian: https://salsa.debian.org/python-team/packages/tuf/-/merge_requests/11
Relate-to: https://github.com/theupdateframework/tuf/issues/263#issuecomment-835239415
Forwarded: https://github.com/theupdateframework/tuf/pull/1380
Relate-to: https://github.com/theupdateframework/specification/pull/160
Signed-off-by: Philippe Coval <rzr@users.sf.net>
2021-05-28 17:07:14 -04:00
Jussi Kukkonen
edeb8afaa7
Merge pull request #1421 from theupdateframework/dependabot/pip/urllib3-1.26.5
build(deps): bump urllib3 from 1.26.4 to 1.26.5
2021-05-27 19:58:47 +03:00
dependabot[bot]
85ca1da00e
build(deps): bump urllib3 from 1.26.4 to 1.26.5
Bumps [urllib3](https://github.com/urllib3/urllib3) from 1.26.4 to 1.26.5.
- [Release notes](https://github.com/urllib3/urllib3/releases)
- [Changelog](https://github.com/urllib3/urllib3/blob/main/CHANGES.rst)
- [Commits](https://github.com/urllib3/urllib3/compare/1.26.4...1.26.5)

Signed-off-by: dependabot[bot] <support@github.com>
2021-05-27 10:04:16 +00:00
Martin Vrachev
737c249067 new API: make sure targets in Targets can be empty
This change is relevant to the new metadata class Targets.

In the specification, when describing the Targets metadata file format
and more precisely "TARGETPATH" (or targets containing the actual
target files) it's said:
"It is allowed to have a TARGETS object with no TARGETPATH elements.
This can be used to indicate that no target files are available."

If there is no "TARGETPATH" keys for the dictionary "targets", this
would mean that "Targets.targets" is {}.
Make sure we test for that.

See: https://theupdateframework.github.io/specification/latest/#targetpath

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-25 13:15:00 +03:00
Jussi Kukkonen
b643e5bec7 Metadata API: Add type annotation to help mypy
Without this mypy figures the dict is Dict[str, str] and then promptly
fails when int value is inserted

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:42:18 +03:00
Jussi Kukkonen
2e3eb40cf9 Metadata API: Fix DelegatedRole.from_dict() return type
Also mark the argument as Dict as we will pop() it.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:29:42 +03:00
Jussi Kukkonen
510f224e3f tox: Run pylint in parallel
pylint on the legacy code is by far the slowest part of linting (to
the extent that parallelizing the tox env itself doesn't really help):
pylint can fortunately parallelize itself.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:29:42 +03:00
Jussi Kukkonen
ca5f2ddd9c Add initial mypy configuration
This is an initial setup: By default check only tuf/api/,
and ignore securesystemslib imports.

Change lint working directory to source root: This saves repeating a lot
of {toxinidir} in the command lines.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:29:42 +03:00
Jussi Kukkonen
43f13a0565 Metadata: _common_fields_from_dict() should return Tuple
This allows mypy to track the argument types through the constructor
calls.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:29:42 +03:00
Jussi Kukkonen
dfe6a6619b Metadata API: Type hint signed in Metadata.from_dict()
Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:29:42 +03:00
Jussi Kukkonen
fe23a95cb2 Metadata API: Mark Signed as Abstract Base Class
Also define from_dict()/to_dict() as abstract: this helps mypy keep
track of things. Rename derived argument *_dict in the derived classes
to keep the linter happy.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:29:39 +03:00
Jussi Kukkonen
f4d008cd14 Metadata API: Improve type hint for _signed_type
Use ClassVar for extra protection, set default value to a string so type
checking is ok with it

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:26:55 +03:00
Jussi Kukkonen
1b9aec291c Metadata API: Use TYPE_CHECKING for cyclic import
The import is useful for mypy so it can check the types.
Add a pylint disable just like json.py does in the same situation.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-19 14:26:55 +03:00
Jussi Kukkonen
f935ea33da
Merge pull request #1329 from MVrachev/new-api-classes
New metadata API: add MetaFile and TargetFile classes
2021-05-19 14:23:52 +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
aaa5bb4fc0 Disable "C0302" for tuf/api/metadata.py
Disable the "C0302: Too many lines in module" warning which warns for modules
with more 1000 lines, because all of the code here is logically connected
and currently, we are above 1000 lines by a small margin.

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
d002dcdb8f
Merge pull request #1390 from jku/no-falsy-tests-for-none
Avoid using falsy tests for None
2021-05-18 20:32:59 +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
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
Jussi Kukkonen
9a397b9eff
Merge pull request #1386 from sechkova/root_add_key
Fix Root.add_key() argument's type
2021-05-14 09:27:17 +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
Jussi Kukkonen
2ef8546afb
Merge pull request #1370 from MVrachev/delegation-classes
New API: Add DelegationRole and Delegations classes
2021-05-12 08:52:13 +03:00
Martin Vrachev
b2cde9bbbf Don't use Mapping for mutable dicts
Stop using Mapping where we actually mean Dict:
Mapping means "we only need a read-only dict" and most of the time
this is not really the case.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-11 16:04:25 +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
a261d49253
Merge pull request #1379 from jku/metadata-minor-fixes
Metadata minor fixes
2021-05-11 15:18:24 +03:00
Jussi Kukkonen
d3a1d43569
Merge pull request #1383 from jku/old-style-logging
Metadata API/pylintrc: Use old style logging
2021-05-11 14:23:25 +03:00
Jussi Kukkonen
efd412aa90 Metadata API/pylintrc: Use old style logging
This is suggested by the Google style guide: the old style logging
(%-format) allows the log strings to be lazily formatted so there's less
need to think about performance when forming debug messages.

No actual code changes are needed because the metadata API does not yet
log anything.

Fixes #1334

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-11 11:47:38 +03:00
Joshua Lock
d5deba2185
Merge pull request #1376 from jku/duplicate-type-property
Metadata API: Provide type as well as _type
2021-05-10 21:27:24 +01:00
Jussi Kukkonen
084df969a4 Metadata API: Fix type hints
* Define missing argument type hints
* Stop using Mapping where we actually mean Dict:
  Mapping means "we only need a read-only dict" and most of the
  time this is not really the case.
* Use List, not list (latter only works from Python 3.9)
* Update Metadata.signatures documentation

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-05-10 21:27:24 +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
Jussi Kukkonen
b6558c2070
Merge pull request #1367 from MVrachev/improvements
Make hashes, length and delegations optional + improvements
2021-05-10 16:06:36 +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
0604f7026e NEW API: Fix documentation indentation
From the reST/sphinx docs:
https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#literal-blocks

I added new lines and an identation where it was missed.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-05-10 15:27:37 +03:00