86 Commits

Author SHA1 Message Date
Miguel Pires
79c5ac14b2 many: remove usages of deprecated io/ioutil package (#13768)
* many: remove usages of deprecated io/ioutil package

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* .golangci.yml: remove errcheck ignore rule for io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

* run-checks: prevent new usages of io/ioutil

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>

---------

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2024-04-03 23:23:24 +02:00
Miguel Pires
29c9752d66 many: s/ioutil.WriteFile/os.WriteFile (#13217)
Replace ioutil.WriteFile with os.WriteFile since the former has been
deprecated since go1.16 and simply calls the latter.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-09-26 11:38:46 +01:00
Zeyad Gouda
0cb8e9d29b store: reword panic message and add test to check httputil client has CheckRedirect defined
Signed-off-by: Zeyad Gouda <zeyad.gouda@canonical.com>
2023-08-09 14:24:49 +02:00
Shengjing Zhu
754d9729c2 httputil: fix checking x509 certification error on Go1.20
The error is wrapped in Go1.20 so we need to unwrap first.
2023-02-10 12:57:31 +00:00
Miguel Pires
ee783cc46c client: test SNAPD_CLIENT_DEBUG_HTTP env var
Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-01-04 18:40:08 +01:00
Michael Vogt
f288974120 many: support logging HTTP requests in the client
Add a SNAP_CLIENT_DEBUG_HTTP bitfield environment variable that allows
logging requests, responses and bodies that the client receives from
the REST API. Bodies are never logged in local installs because, since
they contain packaged snaps, their size would make the log unreadable.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-01-04 18:40:08 +01:00
Michael Vogt
d35df2da39 devicestate: retry serial acquire on time based certificate errors (#12403)
This commit adds code to deal with the issue that the time may
we widely off when snapd tries to register the serial. For devices
without a RTC the date maybe so much in the past that the SSL
certificats are not valid yet. To fix this the following changes are
made:

* httputil: add `CertExpiredOrNotValidYet()` helper

This helper can be used to check if the error is that the
certificate is expired or not yet not valid. This is useful
to detect situations like when the time has not yet been
syncronized from a NTP sources.

* devicestate: retry serial acquire on time based certificate errors

When the serial assertion cannot be acquired because the certificate
of the remote system is expired or not yet valid then the most
likely reason for this is that the system clock is off. This case
is now treated in the similar to no network errors, i.e. snapd
will retry to acquire the serial and will only go into the a slow backoff
mode. This helps with the issue that on systems without a RTC
when the device comes up and the NTP sync is slow the serial
is (re)tried 3 times and then it goes into a very long backoff
(as defined in DeviceManager.ensureOperationalShouldBackoff()).

A gradual backoff is still used to not overwhelm the servers and
it is only tried for a bit more than 2048s because that is the maxium
time it takes for timesyncd to wait before trying a NTP sync.
2022-12-15 11:22:17 +01:00
Miguel Pires
0728dfef27 logger: allow logging HTTP traffic enabling all logs
When using SNAPD_DEBUG_HTTP, it was also necessary to set SNAPD_DEBUG
for the HTTP traffic to be logged. So it was impossible to log the HTTP
traffic without logging everything. This commit changes that so that
setting the former logs the HTTP traffic and setting SNAPD_DEBUG
enables the debug logs in the rest of snapd.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2022-12-13 21:30:23 +01:00
Pawel Stolowski
c8122fc15a store: set ResponseHeaderTimeout on the default transport
It appears that ResponseHeaderTimeout needs to be set explicitly, no headers response from the server doesn't seem to be covered by any other timeout.

This should fix the download getting stuck issue.

For more context, see the stacktrace obtained from a system where download got stuck: https://pastebin.canonical.com/p/nZkRMTBbv3/
(the affected goroutine seems to be sitting inside for-select loop of /usr/lib/go-1.10/src/net/http/transport.go:2033)

(to play with it comment out ResponseHeaderTimeout: ... from transport.go and modify test timeout from 5 * time.Second to a huge value and it should be stuck)

* Set ResponseHeaderTimeout on the default transport.

* Use 250ms for mocked ResponseHeaderTimeout.

* Bump ResponseHeaderTimeout to 15s.

* Bump test timeout to avoid potential issues with LP builds.
2021-07-13 11:21:01 +02:00
Samuele Pedroni
610b870bef many: drive-py: fix spelling of PersistentNetworkError 2020-05-25 16:59:55 +02:00
Maciej Borzecki
2c3460bfbc httputil: fix client timeout retry tests
The detection of timeout coupled with context.DeadlineExceeded errors in
http.Client.do() is racy, and boils down to `time.Now().After(deadline)` where
deadline is based on the timeout setting of the client.

This has been partially fixed in 1.14 in
7fc2625ef1 but
feels more reliable if returned via err when reading the response body.

Make the tests expect either the proper `Client.Timeout exceeded while awaiting
headers` or context deadline exceeded error.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-04-15 11:53:09 +02:00
Jamie Strandboge
0c61060113 fix comment string in last commit 2020-04-07 21:52:53 +00:00
Jamie Strandboge
40dab754a3 httputil/client_test.go: account for go-1.6 error string 2020-04-07 21:41:59 +00:00
Jamie Strandboge
831af9cbb0 Merge remote-tracking branch 'refs/remotes/upstream/master' into add-more-httputil-unittests 2020-04-07 21:34:40 +00:00
Michael Vogt
0dec741e85 httputil: increase testRetryStrategy max timelimit to 5s
On the (very slow) armhf builder one unit test failed with:
```
FAIL: retry_test.go:359: retrySuite.TestRetryRequestTimeoutHandling

retry_test.go:411:
    // check that we exhausted all retries (as defined by mocked retry strategy)
    c.Assert(permanentlyBrokenSrvCalls.Count(), Equals, 5)
... obtained int = 4
... expected int = 5
```
This is caused by the retry stragey limit of 1s. The timeout of
the retry to increased to 100ms recently and it looks like this
can sometimes cause the test to go over the 1s retry strategy
limit now. This commit increases this timeout to make this much
less likely.
2020-04-03 17:14:40 +02:00
Michael Vogt
cb2243bcb8 httputil: increase httpclient timeout in TestRetryRequestTimeoutHandling
The test uses a short 25ms connect timeout. Slow (overcommitted)
systems may fail because they are unable to reply in this time.

This commit changes the timeout to 100ms which makes it at least
4x less likely to hit this condition. It also makes the test
slower unfortunately (by ~400ms).
2020-04-03 08:29:42 +02:00
Michael Vogt
70304c3433 travis.yml: run unit tests with go/master as well
* travis.yml: run unit tests with go/master as well

The unit tests of snapd are broken currently for golang-1.14. This
was observed on debian-sid. We did not catch this. To ensure we
get an early warning about failures with the latest go this PR
adds "master" to the go versions to run the unit tests against.

This commit also fixes the broken tests with 1.14
2020-03-19 09:28:01 +01:00
Jamie Strandboge
e3769c2764 httputil/client_test.go: add two TLS version tests 2020-03-18 21:04:12 +00:00
Samuele Pedroni
c30b43760c many: introduce snapdenv and snapdenv.Testing
introduce a new package snapdenv to present the common env options
for snapd components

start with snadpenv.Testing exposing SNAPPY_TESTING

it doesn't make sense for *util packages to use snapdenv directly,
as a consequence move (Set)UserAgent from httputil to snapdenv
2020-03-15 16:40:25 +01:00
Michael Vogt
6d7e4d2bd0 httputils: simplify addLocalSSLCertificates (thanks for zyga) 2020-03-11 10:50:24 +01:00
Michael Vogt
dd0c1dfeb1 many: rename dirs.SnapdExtraSSLCertsDir -> dirs.SnapdStoreSSLCertsDir 2020-03-11 09:21:06 +01:00
Michael Vogt
c7bd3a732a httputil: tweak client error messages (thanks to Samuele) 2020-03-10 17:39:38 +01:00
Michael Vogt
0e37450a3f httputil: fix vet errors in tests 2020-03-10 12:42:52 +01:00
Michael Vogt
24cce83de6 httputil: fix typo (thanks to Maciej) 2020-03-10 12:42:42 +01:00
Michael Vogt
3d1af04e44 httputil: simplify code 2020-03-06 19:40:39 +01:00