Commit graph

100 commits

Author SHA1 Message Date
Kapil Sharma
83974c7cab
removing linting contraint arise from noqa:SLF001
Signed-off-by: Kapil Sharma <ks3913688@gmail.com>
2024-05-28 17:45:12 +05:30
h4l0gen
e63ba54906
made variable public
Signed-off-by: h4l0gen <ks3913688@gmail.com>
2024-05-28 17:35:14 +05:30
h4l0gen
d2f9f6dee6
changing useragent
Signed-off-by: h4l0gen <ks3913688@gmail.com>
2024-05-20 21:55:02 +05:30
Jussi Kukkonen
dc5194e9e9 lint: Enable flake8-self
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2024-05-06 09:19:19 +03:00
Jussi Kukkonen
460424a620 lint: Enable flake8-raise
I'm not sure I agree with not using the parens in
   raise SomeError
but being consistent is definitely better than not being consistent.

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2024-05-06 09:19:19 +03:00
Jussi Kukkonen
38f309bbbf WIP: Update to new securesystemslib API
* API changes covered:
  * keys and interface modules removed
  * SSlibSigner removed
  * CryptoSigner added: this replaces the removed functionality
  * DSSE "signatures" container type changed
* Currently pins a securesystemslib main branch commit:
  this shoudl be reverted before merging, when securesystemslib
  has made a release
* tests/generated_data/generate_md.py was simplified
* Encrypted test keys in tests/repository_data/keystore were replaced
  with the unencrypted PEM versions of the same keys
* The public test keys in tests/repository_data/keystore were removed
  as they were not used anymore

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2024-04-25 14:27:54 +03:00
Jussi Kukkonen
f165c76fc2
Merge pull request #2613 from NicholasTanz/enableRulesets
Enable rulesets (BLE and RUF)
2024-04-22 16:19:55 +03:00
E3E
52601e2bd8 add RUF and BLE rulesets; ignore some broad exceptions (BLE001) and RUF012
Signed-off-by: E3E <ntanzill@purdue.edu>
2024-04-21 01:37:40 -04:00
Jussi Kukkonen
fe2068697c Support app-specific user-agents
* application user-agent can be set with UpdaterConfig object
* Setting will affect the default fetcher only
* the application user-agent will be prefixed to the ngclient
  default user-agent

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2024-04-19 17:55:29 +03:00
Jussi Kukkonen
009e1ddbf4 lint: Enable more ruff ulesets
Minor fixes were needed, the only possibly interesting one is
the one in RequestsFetcher (use "yield from").

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2024-04-02 11:02:48 +03:00
Jussi Kukkonen
289950a17c updater: Minor improvements to error handling
In RequestsFetcher it makes sense to raise "from e" but in
updater the error we raise is not related to the original
error: use "from None".

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2024-03-08 15:00:42 +02:00
Nurgaleev_Mansur_908
53ac05d5d3 change utcnow() to now(timezone.utc)
Signed-off-by: Nurgaleev_Mansur_908 <mansur0158@gmail.com>
2024-02-27 21:57:46 +03:00
Lukas Pühringer
1e8820930e
Update tuf/ngclient/_internal/trusted_metadata_set.py
Co-authored-by: Jussi Kukkonen <jku@goto.fi>
Signed-off-by: Lukas Pühringer <luk.puehringer@gmail.com>
2024-02-22 13:43:44 +01:00
Lukas Puehringer
8eb6f08a5c ngclient: replace internal wrapping interface
The internal wrapping interface to case handle deserialization and
verification of traditional metadata vs. simple envelopes inside
trusted metadata set might be a more complicated solution than
necessary.

This removes the interface and instead adds the methods of the interface
implementations as helpers to trusted metadata set, and updates it to
to call one or the other function based on the envelope type
configuration flag.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 17:01:13 +01:00
Lukas Puehringer
1897f9a652 ngclient: change envelope type config to flag
The flag allows adding other envelope types in the future (unlikely),
or parallel support (`METADATA & SIMPLE`) without breaking the API.

Internally, the flag is now just passed on to TrustedMetadataSet as
mandatory parameter. (Optional parameters make less sense when we
control all the invocations.)

This change requires updating all invocations of TrustedMetadataSet,
including the duplication of a test function.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 16:59:55 +01:00
Lukas Puehringer
8544bbd6f1 Envelope API: rename, add docs, add alias
* Rename Envelope to SimpleEnvelope:
  Envelope should be the generic term in this context for something that
  contains a payload and signatures. SimpleEnvelope is the specific
  DSSE implementation (just like Metadata is the specific traditional
  canonical JSON -based TUF envelope implementation).

* Add SimpleEnvelope class and method docstrings.

* Add convenience alias for ``self.signatures`` mapped to keyids for
  compatibility with Metadata.signatures.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 16:59:55 +01:00
Lukas Puehringer
5fab6355ab ngclient: support dsse in Updater
* Add `use_dsse` updater config parameter which indicates, if an updater
  instance expects metadata to come in a DSSE envelope.

* Update TrustedMetadataSet to take an Unwrapper instance.

* Update Updater, to pass an EnvelopeUnwrapper to TrustedMetadataSet, if
  configured with `use_dsse`.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 16:59:41 +01:00
Lukas Puehringer
d859bac7f8 ngclient: add EnvelopeUnwrapper implementation
Add Unwrapper implementation for DSSE Envelope. The order of
deserialization and signature verification differs from traditional
Metadata.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 16:35:20 +01:00
Lukas Puehringer
cb9aa4a362 ngclient: use Unwrapper in TrustedMetadataSet
Change TrustedMetadataSet to load and verify metadata using a
MetadataUnwrapper instance.

IMPORTANT NOTES:
* Requires changing the TrustedMetadataSet to store payloads only, which
  is okay, because signatures are no longer needed, after being verified
  at load time.

* Includes a minor re-ordering of validation steps.  That is, the
  version increment for root metadata is now checked after signature
  verification. Preserving the order would require including the check in
  the Unwrapper interface, which is feasible but does not seem correct wrt
  separation of responsibility.

Changes are adopted in updater, tests and _localrepo.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 16:24:29 +01:00
Lukas Puehringer
48af058953 ngclient: add Unwrapper interface and implementation
Add internal payload unwrapper interface and implementation for payloads
wrapped in Metadata.

This is an abstraction over behavior --  load signature wrapper, verify
signatures over payload, return deserialized payload -- which is common
for relevant signature wrappers (Metadata, DSSE Envelope), but performed
differently / in different order.

Signed-off-by: Lukas Puehringer <lukas.puehringer@nyu.edu>
2024-02-21 16:21:02 +01:00
E3E
206c9424f1 Add to linting Configuration:
- adpot changes in dependabot.yml and remove --diff from ruff check.
- select pydocstyle, isort, pyflakes, pep8-naming, pycodestyle for ruff and ignore some small issues / add inline comments.
- adjust docstring length to 80 in various files

Signed-off-by: E3E <ntanzill@purdue.edu>
2024-02-20 00:34:47 -05:00
Nicholas Tanzillo
af4beb1cb3
increase default network timeout (#2542)
* Increase default network timeout
* trying to defend against slow retrieval attacks in a generic library is impossible
but too low timeouts mean failures in high latency systems (like tests running
on CI).

Signed-off-by: E3E <ntanzill@purdue.edu>
2024-02-01 22:06:26 +02:00
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
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
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
Fridolin Pokorny
744f75c1e7
Fix pydocstyle D209
Fix multi-line docstring closing quotes should be on a separate line

Signed-off-by: Fridolin Pokorny <fridolin.pokorny@datadoghq.com>
2023-01-26 16:36:13 +01:00
Fridolin Pokorny
5d347b8707
Fix pydocstyle D400: first line should end with a period
Signed-off-by: Fridolin Pokorny <fridolin.pokorny@datadoghq.com>
2023-01-26 15:33:09 +01:00
Jussi Kukkonen
889b218e7a Add comment explaining public status to the module itself
Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-01-26 09:33:44 +02:00
Jussi Kukkonen
f8a7881c87 Make RequestsFetcher public
This is useful for those who want to use the default fetcher
but modify some attributes

The file itself could be moved to tuf/ngclient/ but this is not done yet
as sigstore-python is using this internal module. Move can be done once
sigstore-python 1.0 is no longer relevant.

Fixes #2268

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2023-01-25 15:55:11 +02:00
Jussi Kukkonen
e1d15887a8 ngclient: Remove "Optional" from helper props
The properties in TrustedMetadataSet are a bit difficult to use
with static typing since they return Optional but in many cases
we know the "None"-case is impossible.

Remove None from annotation: the idea is that calling the property
getter too early is a programming error: it will result in KeyError
which is consistent:
 * trusted_set["timestamp"] raises KeyError if timestamp is not set
 * trusted_set.timestamp raises KeyError if timestamp is not set

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2022-12-27 16:56:32 +02:00
Jussi Kukkonen
ba9ea5b88c
Merge pull request #2230 from fridex/pydocstyle-imperative-mood
Fix pydocstyle D401: first line should be in imperative mood
2022-12-27 11:47:46 +02:00
William Woodruff
2876bfd764
tuf: move INFO logs to DEBUG or WARNING
Signed-off-by: William Woodruff <william@trailofbits.com>
2022-12-20 16:36:04 -05:00
Fridolin Pokorny
c4e1479003
Fix pydocstyle D401: first line should be in imperative mood
Signed-off-by: Fridolin Pokorny <fridolin.pokorny@datadoghq.com>
2022-12-18 13:13:35 +01:00
Jussi Kukkonen
35a7dd58c5 RequestsFetcher: satisfy mypy with small fix
The typeshed annotations for requests say that the hostname could be None:
I think this is untrue but let's keep mypy happy.

Signed-off-by: Jussi Kukkonen <jkukkonen@google.com>
2022-10-31 21:25:02 +03:00
KOLANICH
0c9c494261 Supported schemes without netloc.
Signed-off-by: KOLANICH <KOLANICH@users.noreply.github.com>
2022-10-31 21:23:56 +03:00
Martin Vrachev
5fd3ddccbc ngclient: pick old timestamp if new.ver is equal
In the spec version 1.0.30, a new change has been added considering what
should happen if there is a new timestamp with the same version.
It says the following:
"In case they [versions] are equal, discard the new
timestamp metadata and abort the update cycle.
This is normal and it shouldn't raise any error."

In other words, if there is a new timestamp with the same version, then
stop the update process and use the old timestamp.

Those changes reflect these latest specification modifications.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-06-21 14:52:31 +03:00
Kairo de Araujo
b5e42c1c92 import requests.exceptions is not necessary
All calls use requests.* and importing requests.exceptions is not
necessary.

Signed-off-by: Kairo de Araujo <kdearaujo@vmware.com>
2022-03-31 10:58:53 +02:00
Ivana Atanasova
d8d0486514 Fix expired metadata tests
This change fixes the expired metadata tests to mock `datetime`
as previously they mocked `time` incorrectly, which did not affect
update methods, as they use `datetime.datetime.utcnow()` to
calculate now

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
2022-03-18 19:53:50 +02:00
Ivana Atanasova
a0d31854bc Fix line lengths in ngclient docstrings
This change shortens line lengths that exceed the requiremets and
adds more clarification on methods where the short message is not
complete enough

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
2022-02-11 15:33:48 +02:00
Ivana Atanasova
43080e1228 Unify wording of docstrings language in ngclient
This change unifies common wording in the docstrings library of
ngclient, like "Args" vs. "Arguments"

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
2022-02-11 14:51:51 +02:00
Ivana Atanasova
d399ad5d1b Unify article in ngclient docstrings
This change updates the docstrings library of ngclient with no
article for all Args in order to be unified amongst all python-tuf
docstrings

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
2022-02-11 14:51:51 +02:00
Ivana Atanasova
d8f60e1baf Unify quoting in ngclient docstrings
This change updates the docstrings library of ngclient with
unified double backtick quoting for better readability

Signed-off-by: Ivana Atanasova <iyovcheva@vmware.com>
2022-02-11 14:51:28 +02:00
Jussi Kukkonen
6b079eefec ngclient: Add missing f to an f-string
Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2022-02-04 11:03:12 +02:00
Jussi Kukkonen
17f2ddff02 exceptions: rename FetcherHTTPError
I've not supported many renames but I'm suggesting this one:
FetcherHTTPError was created because we needed to signal 403/404
from the fetcher to updater. At that time the download error hierarchy
in general was not thought out.

Now we have a couple of different errors all derived from
DownloadError. I believe it does not make sense to point out "Fetcher"
in one of their names: DownloadHTTPError makes it clearer this is a
specific type of DownloadError.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2022-02-04 11:03:12 +02:00
Jussi Kukkonen
4efd9496dc ngclient: Make DownloadErrors consistent
Fetcher interface should only raise DownloadErrors,
regardless of the implementation.
 * Make sure fetch() wraps non-DownloadError errors in a DownloadError
 * Make the abstract function private _fetch()
 * Try to be more consistent in doscstrings

This now makes the example client more sensible (when server does not
respond):
    $ ./client_example.py download qwerty
    ...
    Failed to download target qwerty: Failed to download url http://127.0.0.1:8000/metadata/2.root.json

(here the latter part of the error string comes from DownloadError
raised by FetcherInterface.fetch())

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2022-02-04 11:03:12 +02:00
Jussi Kukkonen
d3ca8fd2fe ngclient: Remove obsolete TODOs
First TODO is now handled (or we have PRs already).
Second TODO is moved to issue #1804.

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2022-01-28 09:53:03 +02:00
lukpueh
d95ead6280
Merge pull request #1787 from MVrachev/ngclient-exceptions
Review and document ngclient exceptions
2022-01-27 14:28:13 +01:00
Martin Vrachev
9f7866db0a Additions to the client exceptions
I made a review on all files inside tuf/ngclient to see which of them
needs additions or changes in their function docstrings regarding
exceptions.

I didn't find any changes required inside the request_fetcher.py
and of course inside the config module.
Other than that multiple additions had to be made.

For trusted_metadata_set we had a discussion with Jussi that there is
no need to list each of the specific RepositoryErrors one by one as
this is an internal module and this will only create a bigger
maintenance burden.

For updater.py we had discussions with Jussi and Lukas that we want to
document only those exceptions that could be potentially handled.
This means there is no point in documenting each of the RepositoryErrors
or DownloadErrors separately.

Finally, I added a little documentation for download_bytes() inside
fetcher.py, as it's naming, suggests it's not an internal function.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-01-27 14:39:24 +02:00
Jussi Kukkonen
b8759a9937 ngclient: allow compression in HTTP responses
This commit tries to deal with two interests:
* metadata is highly repetitive and compressible: allowing compression
  would be good
* there may be broken web servers (see
  404838abcc/src/pip/_internal/download.py (L842))
  that have problems with compression on already compressed target files

We can make things better for that first interest while we have no real
data for the second interest -- our current workarounds to avoid
compression are based on hearsay, not testing.

Now that individual fetchers are possible I suggest we simplify
ngclient and allow compression. As an example the pip Fetcher
could still use the pip response chunking code with all their
workarounds -- pip certainly has better capability to maintain
a mountain of workarounds and also has endless amounts of real-world
testing compared to python-tuf.

Details:
* Stop modifying Accept-Encoding (Requests default includes gzip)
* Don't use response.raw in RequestsFetcher as there is no need:
  This was a workaround for false "Content-encoding: gzip" inserted by
  a broken server -- and the workaround was only possible because we
  knew we never asked for compression
* Fix issue in test_session_get_timeout(): it's not mocking the error
  that requests really raises in this case

Fixes #1251

Signed-off-by: Jussi Kukkonen <jkukkonen@vmware.com>
2022-01-25 15:03:35 +02:00
Martin Vrachev
7bb916f962 Document DownloadError for RequestsFetcher.fetch()
We should document that "DownloadError" is thrown inside
RequestsFetcher.fetch().

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
2022-01-19 20:24:02 +02:00