Commit graph

4230 commits

Author SHA1 Message Date
Jussi Kukkonen
cfc7638ebe
Merge pull request #1451 from sechkova/hash-validation
api/metadata input validation: length and hashes
2021-06-22 09:54:09 +03:00
Teodora Sechkova
328f637264
Remove trailing comma from test data
A trailing comma makes any element a one-item tuple.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-06-17 14:38:02 +03:00
Teodora Sechkova
e30faa89be
Remove empty hash dict check from MetaFile
The check for an empty hash dictionary is now part
of the hash validation function.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-06-17 14:38:02 +03:00
Teodora Sechkova
03f39b01e7
Add hash and length validation
- valid length: greater than zero
- valid hashes: a non-empty dictionary of type Dict[str, str]

Checking the validity of hash algorithms is not part
of the metadata input validation and is done by
securesystemslib during  hash verification.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-06-17 14:38:01 +03:00
Jussi Kukkonen
39ed706d72
Merge pull request #1437 from sechkova/hash-verification
Add hash and length verification to MetaFile and TargetFile
2021-06-16 22:18:03 +03:00
Jussi Kukkonen
51c26b7b7a
Merge pull request #1428 from theupdateframework/dependabot/pip/certifi-2021.5.30
build(deps): bump certifi from 2020.12.5 to 2021.5.30
2021-06-16 20:30:22 +03:00
Jussi Kukkonen
6c4e2be196
Merge pull request #1450 from MVrachev/threshold-validation
Metadata API: Add simple threshold validation
2021-06-16 19:56:26 +03:00
Jussi Kukkonen
fa2268df5a
Merge pull request #1449 from MVrachev/key-validation
Metadata API: Add Key attributes types validation
2021-06-16 19:55:34 +03:00
Jussi Kukkonen
25e5f304ee
Merge pull request #1430 from MVrachev/spec_version-validation
New metadata API: spec_version attribute validation
2021-06-16 19:54:36 +03:00
Martin Vrachev
a9dc24adea Metadata API: Add a comment for Key validation
Clarify that we don't semantically validate "Key" instances during
initialization and that this is a responsibility of securesystemslib.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-06-16 16:37:48 +03:00
Martin Vrachev
6c5d970799 Metadata API: Add simple threshold validation
Probably there could be future API calls that modify "threshold"
to a new value, but the problem is we don't have a clear idea
if they would exist and what exactly they will do.
That's why it makes sense to validate against the potential problems
we can imagine - in this case, is passing a threshold below 1.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-06-15 18:06:31 +03:00
Martin Vrachev
f20664d2fc Metadata API: Add Key attributes types validation
In our discussion with Jussi we come to the conclusion that we want
to verify that all Key attributes contain values in the expected types,
but at the same time, we don't want to focus on validating the semantics
behind them.
The reason is that having a Key instance with invalid attributes is
possible and supported by the spec.
That's why we have a "threshold" for the roles meaning we can have up to
a certain number of invalid Keys until we satisfy
the required threshold.

Also, for deeper semantic validation it's better to be done in
securesystemslib which does the actual work with keys.

For context see: https://github.com/theupdateframework/tuf/issues/1438

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-06-15 17:35:02 +03:00
Martin Vrachev
41afb1e134 Validate spec_version during initialization
According to point 2 in the semver specification:
"A normal version number MUST take the form X.Y.Z where X, Y, and Z are
non-negative integers...". See: https://semver.org/#spec-item-2
Also, even though version strings like "2.0.0-rc.2" or "1.0.0-beta" are
valid strings in semantic versioning format, in TUF we never needed
to add letters for our specification number.
That's why I validate that: spec_version is a . separated string
and when split it has a length of 3 and that each of the
three elements is a number.

The modules under the tuf/api folder in TUF are an alternative TUF
implementation. That's why they should use their own constant for
SPECIFICATION_VERSION in tuf/metadata/api.

This time, I used a list for the SPECIFICATION_VERSION constant in order
to retrieve major and minor versions easier.

I use the SPECIFICATION_VERSION to check that the given spec_version is
supported against the tuf code spec version.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-06-14 17:54:14 +03:00
Jussi Kukkonen
2a5bfb9cac
Merge pull request #1424 from jku/enforce-unique-sigs
Metadata API: Store signatures as dict
2021-06-11 13:55:20 +03:00
Teodora Sechkova
dcdd33287c
Add basic input validation to {Meta,Target}File
Add basic checks for allowed input values during
objects' serialization.

Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
2021-06-11 11:59:13 +03:00
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
146eb105c1 Metadata API: Be more explicit when appending sigs
Clearing the OrderedDict makes it easier to see what happens and
avoids having to call OrderedDict() again.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-06-11 10:47:18 +03:00
Martin Vrachev
15eb0d93c4 Remove question about version validation place
In issue #1418 in this comment:
https://github.com/theupdateframework/tuf/issues/1418#issuecomment-852147689
I summarized the discussion we had with the participants in this issue.
In summary: no additional changes are needed for "version" validation
considering there is "bump_version()" function for that.

If we won't be adding "version" validation elsewhere we can keep it
the way it is.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-06-09 13:53:17 +03:00
Jussi Kukkonen
1ce36d0cd6 Metadata API: Use OrderedDict for signatures
Dict ordering is part of regular Dict from Python 3.7: Use OrderedDict
for signatures to make sure signatures are serialized in a reproducible
order even on 3.6.

The added benefit is that reader will immediately understand that the
order has some significance.

The actual type annotations are a bit convoluted because:
* typing does not include OrderedDict before 3.7 so can't use that
* Annotating inner types does not work for collections.OrderedDict
  in older pythons (so have to use the "stringified annotations")

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-06-09 11:21:38 +03:00
Jussi Kukkonen
e3190fca9d Metadata API: Store signatures as dict
store signatures in a Dict of keyid to Signature. This ensures
signature uniqueness. Raise in from_dict() if input contains multiple
different signatures for a keyid.

This changes Metadata object API, and makes it slightly different from
the file format: this is justified by making the API safer to use and
easier to validate.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2021-06-09 11:21:38 +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
dependabot[bot]
8cfbfa5626
build(deps): bump certifi from 2020.12.5 to 2021.5.30
Bumps [certifi](https://github.com/certifi/python-certifi) from 2020.12.5 to 2021.5.30.
- [Release notes](https://github.com/certifi/python-certifi/releases)
- [Commits](https://github.com/certifi/python-certifi/compare/2020.12.05...2021.05.30)

Signed-off-by: dependabot[bot] <support@github.com>
2021-05-31 10:05:24 +00: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