Commit graph

133 commits

Author SHA1 Message Date
Lukas Pühringer
a871f648e5
Merge pull request #2378 from jku/move-verify-delegate-v2
Move verify_delegate() to Root/Targets
2023-08-21 13:27:01 +02:00
Jussi Kukkonen
7924f8851d
Merge pull request #2447 from lukpueh/fix-sslibsigner-test
tests: adopt sslib changes in test_sign_failures
2023-08-17 15:51:36 +03:00
Lukas Puehringer
d45d65521b tests: adopt sslib changes in test_sign_failures
fixes #2444

SSlibSigner was changed recently (secure-stystems-lab/securesystemslib#604)
to fail on bad input data (keydict) at init instead of when signing.

The patched test used to trigger expects a Signer.sign error from an
SSlibSigner, which is no longer possible.

To still get the desired error, the test uses a custom signer, which
does raise on sign.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2023-08-16 16:36:27 +02:00
Satvik Vemuganti
ad117d9579 enhancement: Adds from_data() method to MetaFile
Signed-off-by: Satvik Vemuganti <vemugantisesha@iitbhilai.ac.in>
2023-08-16 06:08:34 +05:30
Lukas Puehringer
15dd931609 Metadata API: make new verify_delegate unaware of Metadata
Change new _Delegator.verify_delegate to take payload bytes and
signatures instead of a Metadata object and a payload serializer.

This allows using verify_delegate for payloads that do not come in
a Metadata container, but e.g. in a DSSE envelope (see #2385).

Usage becomes a bit more cumbersome, but still feels reasonable with the
recently added shortcut for default canonical bytes representation of
Metadata.signed.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2023-08-01 15:31:59 +02:00
Lukas Puehringer
fc6c91a711 Metadata API: add Metadata.signed_bytes property
- Add shortcut to canonical json representation of self.signed
- Use in tests and Metadata.sign
- Do not use in _Delegator.verify_delegate (will be updated in subsequent
  commit).

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2023-08-01 15:28:12 +02:00
Jussi Kukkonen
e51c0beee3 tests: Move lint disable to inside block as intended
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-08-01 13:08:13 +02:00
Jussi Kukkonen
a69ddf1f04 ngclient,tests,examples: Use new verify_delegate()
Avoid Metadata.verify_delegate() now that it's deprecated.

Note that this commit does not try to make any code cleanups
that are now possible: this is the minimal change to use the new
API.

Future improvements can make code in TrustedMetadataSet and
Updater slightly easier to read: as an example there's no need for
TrustedMetadataSet to actually store or expose actual Metadata in its
cache -- Signed is all that's needed.

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-08-01 13:08:13 +02:00
Jussi Kukkonen
cddae3b892 Updates to TargetFile.get_prefixed_paths()
* Use the same solution for producing the paths as we already do in ngclient
* Fix linting issues
* Modify the test results according to new code (I believe these are
  correct, although some cases are so edge cases that disagreement may
  exist. Most importantly I think the method should always return as
  many paths as there are hashes listed

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-03-20 16:12:00 +02:00
Kamui
0eef15ad28 fix: parse manually and handle url edge cases
Signed-off-by: Kamui <fin-kamui@pm.me>
2023-03-20 15:48:51 +02:00
Kamui
24b1db6781 feat: generate hash-prefixed path names for target
Signed-off-by: Kamui <fin-kamui@pm.me>
2023-03-20 15:48:51 +02:00
Jussi Kukkonen
b67b8c8ad3 Whitespace changes to make new black linter happy
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-02-06 15:29:28 +02:00
Jussi Kukkonen
41b9b5c60b tests: Remove unnecessary ignores
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-01-17 14:31:22 +02:00
Jussi Kukkonen
ed0ec03399 Metadata API: Fix verify_delegate for new Key API
verify_delegate() unfortunately needs an almost complete rewrite
as the Key.verify_signature() API change affects it quite a bit.

Refactoring the role and key lookup into a separate method makes the
code readable again.

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-01-17 14:31:22 +02:00
Jussi Kukkonen
b55ac25cf5 tests: Fix tests for Key.verify_signature()
Key.verify_signature() API has changed:
* argument is bytes, not metadata
* raised error now comes from securesystemslib

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-01-17 14:31:22 +02:00
Jussi Kukkonen
09971aea16 tests, examples: Stop using Key constructors
New Securesystemslib Keys can now be instantiated in two ways:
* deserialize via Key.from_dict() as before
* generate new keys via implementation specific methods

Fix all cases where we call Key() or Key.from_securesystemslib_key()
and use SSlibKey methods instead. Fix related tests.

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-01-17 14:31:22 +02:00
Martin Vrachev
ddbbcce432 SuccinctRoles.is_delegated_role() add test case
Add a test case when there is a bin name with the desired prefix, but
which cannot be cast to a hexadecimal number.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-06-24 16:48:56 +03:00
Martin Vrachev
28b6917739 Root and Targets key API changes
Here is the list of all breaking API changes:
1) The "role" and "key" arguments in "Root.add_key()" are in reverse
order - "key" becomes first and "role" second.
2) "Root.remove_key()" has been renamed to "Root.revoke_key()".
3) The "role" and "keyid" arguments in "Root.revoke_key()" are in
reverse order - "keyid" becomes first and "role" second.
4) The "role" and "key" arguments in "Targets.add_key()" are in reverse
order - "key" becomes first and "role" second.
5) "Targets.remove_key()" has been renamed to "Targets.revoke_key()".
6) The "role" and "keyid" arguments in "Targets.revoke_key()" are in
reverse order - "keyid" becomes first and "role" second.
7) In both methods "Targets.add_key()" and "Targets.revoke_key()" the
"role" argument becomes an optional with a default value of None.

Those changes are made in an effort to make those methods logical
for both cases when standard roles and succinct_roles are used.
The "Root" API change was done in order to preserve naming and argument
order consistency with "Targets" API.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-06-17 13:33:01 +03:00
Martin Vrachev
15cd42c0d6 Delegations: add support for succinct_roles
This commit contains 2 API changes in "Delegations" class from
tuf/api/metadata.py:
1. roles argment is made optional
2. unrecognized_fields argument becomes the 4-th rather than the 3-rd
as it used to be

In this commit, I add support for succinct_roles roles inside
Delegations class. This change is related to TAP 15 proposal.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-06-17 13:33:01 +03:00
Martin Vrachev
0a92cb91e3 Add helper methods in SuccinctRoles
Add two helper methods in SuccinctRoles.
Those methods proved useful in the testing code, but I believe they have
a potential value for production code as well.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-06-17 13:33:00 +03:00
Ari
d867debb77 Replaced manual path construction with os.path.join
Signed-off-by: Ari <aribasch@umich.edu>
2022-04-18 12:21:44 -04:00
Martin Vrachev
a17ceda4e5 Add "validation" arg in JSONSerializer
If the "validation" argument is set then when
serializing the metadata object will be validated.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-02-28 14:42:06 +02:00
Martin Vrachev
cd34793b0a Move nonunique sigs test to serialization tests
Move the duplicating signatures tests from test_metadata_base function
in test_api.py into test_metadata_serialization.py.
This is a more logical place to store this test case as
test_metadata_base is actually focused on testing
Metadata.signed.is_expired.
That also is the reason why I renamed test_metadata_base to
test_metadata_signed_is_expired.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-02-10 17:30:35 +02:00
Martin Vrachev
8b6566ab3b from_securesystemslib_key() raise ValueError
If a securesystemslib.FormatError is raised inside
Key.from_securesystemslib_key() then reraise ValueError.
This is done so that our users don't have to import securesystemslib
in order to handle the error and because the securesystemslib error
itself is securesystemslib implementation-specific.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-02-08 13:44:12 +02:00
Martin Vrachev
6db3f69b61 Add small missing tests
Add a test triggering the MetaFile version validation and a TargetFile
test accessing custom.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-02-07 17:07:48 +02:00
Martin Vrachev
9533c3f974 Metadata API: add exception tests
Add missing tests testing raising documented
exceptions for "Metadata.sign()",
"Metadata.to_file()" and "Metadata.from_file()".

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-01-27 17:34:00 +02:00
Martin Vrachev
0666520e62 Fix type annotation in test_api
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-01-27 17:05:57 +02:00
Martin Vrachev
32a4545f0b Replace UnsupportedAlgorithmError with ValueError
UnsupportedAlgorithmError is a detailed securesystemslib exception
and there is no need for TUF to redefine it.
Moreover which hash "algorithms" are allowed is work for
securesystemslib not for TUF.

It's only used once inside "Targetfile.from_data()" and there it's used
to denote that there is a problem with the given argument.
That's why this error can be just replaced with "ValueError".

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-01-19 16:52:19 +02:00
Martin Vrachev
4b61be9cf7 Add tuf/api/exceptions.py
Add tuf/api/exceptions.py for exceptions in the new code.
I copied the exceptions from tuf/exceptions.py with a few important
decisions:
1. I only added the exceptions that are used in the new code
2. I removed the general "Error" class as we can directly inherit
Exceptions
3. I tried grouping the exceptions by relevance
4. I removed the second argument "UnsignedMetadataError" as it's only
kept for backward compatibility and is not used
5. I tried following the new code style guidelines and linted the file
with our linters.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-01-19 16:52:19 +02:00
Lukas Puehringer
8620f389a8 Metadata API: Remove Signed.bump_version() method
Remove `bump_version()` method, which is just an alias for "+= 1"
on the version attribute. For a slim low-level API it seems okay to
just directly access/modify the attribute.

The extra level of abstraction of "bumping a version" is more
appropriate for a repository library (see #1136).

This patch also removes a related unit test and updates another one
to directly do `(...).version +=`.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2022-01-11 10:56:41 +01:00
Lukas Puehringer
9f2c593813 Metadata API: Remove Signed.bump_expiration()
Remove `bump_expiration()` method, which is unlikely to be used as
is, i.e.  bump to "current expiration date plus delta". A more
realistic use case is to bump to "now plus delta" (see #1727 for
details).

Moreover, bump_expiration can either way easily be replaced by a
one-liner expression using the 'datetime' module. A corresponding
code snippet is added to the `expires` property's docstring.  Note:
`expires` became a property with a millisec-removing setter (for
spec conformance) in  #1712, which further reduces the need for a
convenience bump_expiration method.

This patch also removes a related unit test and updates another
one.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2021-12-22 11:40:36 +01:00
Lukas Puehringer
f22f357934 Metadata API: Remove 3 'update' methods + tests
Remove ambiguous, unspecific, opinionated and trivial 'update'
methods, which can be replaced by feasible one-liners that assign
values directly to the object attribute to be *updated*. (see #1627
for details).

Reasons to have these methods would be increased usability in terms of
- reduced work
- immediate feedback on invalid assignments

However, given above described issues, the reasons against the
methods as they are now seem to outweigh the reasons for them.
Furthermore, it seems easier to re-add similar methods, which
addressed these issues, after the upcoming 1.0.0 release than to
remove or modify them.

This patch also removes the corresponding tests as they become
irrelevant (there is no need to test object assignment).  In the
case of the timestamp test, the removal also includes redundant
test logic, which is already tested in `test_metadata_base`.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2021-12-21 09:48:31 +01:00
Martin Vrachev
181b1a7e6d Metadata API: remove dateutil requirement
I added "dateutil" as a possible argument type for
`Metadata.bump_expiration()` as we are already testing for this and
implying it should be supported.
The problem is that "dateutil" is not added as a nontest requirement
and after a discussion, we decided we don't want to add it as well.

That's why we decided to remove "dateutil" mentions from the code
and not confuse our users we support it.

We will create a separate issue discussing the validity of
`Metadata.bump_expiration()`.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-12-14 11:23:40 +02:00
Martin Vrachev
a728717eda Fix more black warnings on test files
The changes are automatic linting fixes from black.
The target files are only those who test the new code.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-12-09 15:52:40 +02:00
Ivana Atanasova
00589f08e3 Apply top-level rolenames constants in tests
This applies the use of constants of top-level rolenames in the
tests instead of the previously hardcoded strings.
Fixes #1648

Signed-off-by: Ivana Atanasova <iyovcheva@iyovcheva-a02.vmware.com>
2021-12-02 12:25:48 +02:00
Martin Vrachev
e2deff3148 Address mypy warnings
This commit includes manual fixes for a lot of mypy warnings.
When there were warnings that we are calling non-annotated function
in annotated context I decided to add annotations instead of ignoring
those warnings.
That's how I end up adding annotations in the whole tests/utils.py
module.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-24 20:50:10 +02:00
Martin Vrachev
29f936b76d Tests: address new pylint warnings
After the recent changes there are a couple of new pylint warnings that
appeared.
They are caused by the new test file that was added
test_updater_top_level_update.py and the limit of public functions was
reached in the TestMetadata class in test_api.py
The warnings should be addressed before enabling all of the linters
on the tests files.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-12 17:10:06 +02:00
Jussi Kukkonen
fa7990c354
Merge pull request #1670 from MVrachev/assert-raises-fix
Tests: self.assertRaises -> with self.assertRaises
2021-11-11 18:51:51 +02:00
Martin Vrachev
d4187f3186 Address pylint warnings on tests for the new code
Address or disable pylint warnings raised on all test files inside
the "tests/" directory testing the code of the new implementation.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-11 15:39:56 +02:00
Martin Vrachev
8c6e157519 Tests: test_api split test_sign_verify()
test_sign_verify() is testing too many cases and after the recent
pylint warning about the usage of too many local variables it became
clear it's time to split this test function.

I decided to split it logically as half of the function was about
failures connected with verify.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-10 15:55:34 +02:00
Martin Vrachev
3c80c5bcd0 Tests: self.assertRaises -> with self.assertRaises
Change the instances of "self.assertRaises" to "with self.assertRaises"
as using "self.assertRaises" can lead to long statements separated to
multiline expressions as pointed out by Jussi here:
https://github.com/theupdateframework/python-tuf/pull/1658#discussion_r741725382

On another hand "with self.assertRaises()" looks a lot better:
589ed9e0d4/tests/test_api.py (L131)

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-10 15:27:03 +02:00
Martin Vrachev
6fe36a00c4 Rename & simplify a couple of tests in test_api.py
Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-02 19:31:46 +02:00
Martin Vrachev
2e9ef79762 Apply isort on the tests of the new code
All of the changes included are a result of applying isort
on our tests on the new code.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-02 19:31:25 +02:00
Martin Vrachev
c98b429643 Apply black on the tests of the new code
All of the changes included are a result of applying black
on our tests on the new code.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2021-11-02 19:30:35 +02:00
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
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
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
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
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