Commit Graph

21 Commits

Author SHA1 Message Date
Paulo Gomes aa3288112e
Implement Managed Transport for libgit2
libgit2 network operations are blocking and do not provide timeout nor context capabilities,
leading for several reports by users of the controllers hanging indefinitely.

By using managed transport, golang primitives such as http.Transport and net.Dial can be used
to ensure timeouts are enforced.

Co-Authored-by: Sunny <darkowlzz@protonmail.com>
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:20 +05:30
Paulo Gomes 8429708997
Upgrade libgit2 to libgit2-1.3.0-2
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-02-16 11:39:11 +00:00
Paulo Gomes f44302eed0
Bump libgit2 to 1.3.0 and git2go to V33.
Downstream breaking changes introduced since git2go@V31:
- git2go.ErrorCode was deprecated in favour of the native error type.
- FetchOptions no longer expects a pointer, but rather the actual value of git2go.FetchOptions.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-02-16 10:17:31 +00:00
Paulo Gomes 9479d04779
Fix host mismatch in libgit2
Depending on libgit2 version or from its dependencies, the hostname may or may not contain ports

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-01-26 17:25:18 +00:00
Hidde Beydals ab089ddb07 libgit2: use `bytes.Equal` instead of `.Compare`
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-23 09:51:44 +01:00
Sunny 65e5c6a686 libgit2: Add more RemoteCallbacks
Add SidebandProgressCallback to be able to cancel the network operation
before any transfer operation.
Add PushTransferProgressCallback to be able to cancel the push transfer
operation.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2021-11-04 16:17:23 +05:30
Sunny d407c824d6 libgit2: Add early return to transferProgressCallback
In transferProgressCallback(), if the received objects is equal to the
total objects, return early with OK.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2021-11-04 16:17:23 +05:30
Hidde Beydals afa82bbc7a libgit2: ensure context timeout cancels transfer
With the information from the refactor still fresh in mind, I continue
to find new paths now I mentally tamed the git2go beast.

`libgit2` seems to assume that a transport will eventually tell by
itself that it has timed out. This also means that at present any
timeout configuration does not seem have an effect. It will continue
to transfer until the remote (or _something_ else) tells it is no
longer transfering.

This commit introduces a simple check (without tests) which was used
to confirm the theory in combination with the tests in
`pkg/git/strategy` (by setting it to a very low timeout and observing
it fail).

A future iteration should probably take the data given to the callback
into account to ensure it doesn't error out if the given data[1]
reports it has successfully received all objects. Another candidate
for this check may be `CompletionCallback`, but one should study the
C code (and likely some Go code as well) before this.

In addition, to ensure the same timeout is taken into account for push
operations, `PushTransferProgressCallback` may require a likewise
helper.

[1]: https://github.com/libgit2/git2go/blob/main/remote.go#L50-L58

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-11-04 16:17:23 +05:30
Sunny 10d262032c libgit2: handle EOF in parseKnownHosts()
parseKnownHosts() uses golang.org/x/crypto/ssh's ParseKnownHosts() for
parsing known hosts. It returns EOF error when the input is not a host
public key, but a valid known_hosts content, like a comment line.

With this fix, lines causing EOF error are skipped and the parsing of
the known_hosts file continues. But invalid lines still cause parsing
failure.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2021-10-28 17:35:46 +05:30
Hidde Beydals 99428f593e libgit2: change credentialsCallback logic
This changes the logic of `credentialsCallback` so that it takes the
`allowedTypes` passed on by `git2go` into account. Reason for this
change is because this prepares it to work with `v33`, but also
because it can provide better guidance when `libgit2` has been
compiled with a different configuration, which e.g. doesn't
allow for "in-memory SSH keys".

Because `AuthOptions#Identity` now gets validated by the callback
and go-git does its own validaiton, the check has been removed
from `Validate` (and now does a simple check if the fields are set).

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-10-27 00:43:31 +05:30
Hidde Beydals 4a23126c6a libgit2: make RemoteCallbacks helper public
Main requirement for this is the image-automation-controller
depending on being able to get a working auth configuration.

Once the package is moved, we should add push logic to it,
so that the controller is able to use that instead.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-10-27 00:43:31 +05:30
Hidde Beydals 0cf0d4e756 git: refactor AuthStrategy into AuthOptions
This commit moves the previous `AuthStrategy` wiring to a more generic
`AuthOptions`, breaking free from implementation specific details in
the `git` package.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-10-27 00:43:31 +05:30
Hidde Beydals d3c27c37f8 Update github.com/libgit2/git2go to v31.6.1
This commit updates `github.com/libgit2/git2go` to `v31.6.1` (with
`libgit2` `1.1.1`), and changes the container image build process so
that it makes use of `ghcr.io/hiddeco/golang-with-libgit2`.

This image provides a pre-build dynamic `libgit2` dependency linked
against OpenSSL and LibSSH2 (without gcrypt), and a set of cross-compile
build tools (see
[rationale](https://github.com/hiddeco/golang-with-libgit2#rationale) and
[usage](https://github.co/hiddeco/golang-with-libgit2#usage) for more
detailed information).

The linked set of dependency should solve most known issues around
unsupport private key types, but does not resolve the issues with ECDSA*
and ED25519 hostkeys yet. Solving this requires a newer version of
`libgit2` (`>=1.2.0`), which currently does not seem to work properly
with `git2go/v32`.

Some small changes have been made to the `libgit2` package to address
(future) deprecations.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-09-30 16:36:20 +02:00
Somtochi Onyekwere d3d1917e5e Add tests for libgit2
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
2021-04-12 16:31:42 +01:00
Somtochi Onyekwere e82c8e8b70 Use password in git method for secret
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
2021-04-12 15:52:51 +01:00
Philip Laine 0a1631dc5a Remove redundant if else
Signed-off-by: Philip Laine <philip.laine@gmail.com>
2021-02-12 11:18:29 +01:00
Philip Laine f9ddeb06e1 Fix hash type switch statement
Signed-off-by: Philip Laine <philip.laine@gmail.com>
2021-02-12 08:43:43 +01:00
Philip Laine 6b3d96ef3b Check hostkey type when validating hostkey
Signed-off-by: Philip Laine <philip.laine@gmail.com>
2021-02-11 23:04:46 +01:00
Hidde Beydals b2f6639c01 libgit2: use provided host to validate public key
The callback from libgit2 only provides a hostname (without the port),
but the `known_hosts` file indexes the public keys based on the full
host (e.g. `[localhost]:123` for a host behind a specific port).

As a result, it was unable to find the correct public key for the
hostname when it was added to the `known_hosts` file with the port.

To work around this, we add the user provided host that includes the
port to the `PublicKeyAuth` strategy, and use this to find the right
entry in the `known_hosts` file, after having validated that the
hostname provided to the callback matches the hostname of the host
provided by the user.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-02-11 12:52:55 +01:00
Hidde Beydals fac1afa2a8 Move `git/common` to `git`
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-02-08 14:05:05 +01:00
Hidde Beydals 7e63ef841c Rename Git packages to implementations
Signed-off-by: Hidde Beydals <hello@hidde.co>
2021-02-08 13:55:23 +01:00