Commit Graph

119 Commits

Author SHA1 Message Date
Sanskar Jaiswal ec45a612b1 enable managed transport for controller tests
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-05-27 14:01:23 +05:30
Sanskar Jaiswal 5152721ae0 factor out unmanaged checkout into its own functions
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-05-27 14:01:23 +05:30
Sanskar Jaiswal 94c50fa3a8 remvoe support for sha1 and md5 hashing for public keys
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-05-27 14:01:23 +05:30
Sanskar Jaiswal 90ec1e230c expand proxy tests to cover managed transport
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
Co-authored-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-27 14:01:23 +05:30
Sanskar Jaiswal 7d2bc64f47 fix panics on unmanaged http and proxy on managed http
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-05-27 14:01:23 +05:30
Sanskar Jaiswal d4beacb6ad Remove dependency on libgit2 credentials callback
Injects transport and auth options at the transport level directly to
bypass the inbuilt credentials callback because of it's several
shortcomings. Moves some of the pre-existing logic from the reconciler
to the checkout implementation.

Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-05-27 14:01:23 +05:30
Paulo Gomes a0d0a6312d
libgit2: enable managed transport by default
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-24 10:46:32 +01:00
Sunny 581695b4d6
gitrepo: Intro contentConfigChecksum & improvement
Introduce contentConfigChecksum in the GitRepository.Status to track the
configurations that affect the content of the artifact. It is used to
detect a change in the configuration that requires rebuilding the whole
artifact. This helps skip the reconciliation early when we find out that
the remote repository has not changed.

Moves fetching the included repositories in reconcileSource() to collect
enough information in reconcileSource() to be able to decide if the full
reconciliation can be skipped. This results in reconcileInclude() to
just copy artifact to the source build directory.

Introduce a gitCheckout() method to perform construction of all the git
checkout options and perform the checkout operation. This helps to
easily perform checkout multiple times when we need it in
reconcileSource(). When we check with the remote repository if there's
an update, and find out that there's no update, we check if any other
configurations that affect the source content has changed, like
includes, ignore rules, etc. If there's a change, we need to perform a
full checkout of the remote repository in order to fetch the complete
source. The git checkout no-op optimization is enabled in this method
based on the presence of an artifact in the storage.

The failure notification handler is modifed to handle the recovery of a
no-op reconcile failure and create a notification message accordingly
with the partial commit.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-05-20 19:52:18 +05:30
Sunny 749068e9c3
pkg/git: introduce concrete and partial commit
Introduce concrete and partial commits. Concrete commits have all the
information from remote including the hash and commit content. Partial
commits are based on locally available copy of a repo, they may only
contain the commit hash and reference.

IsConcreteCommit() can be used to find out if a given commit is based on
local information or full remote repo information.

Update go-git and libgit2 branch/tag clone optimization to return a
partial commit and no error.

Update and simplify the go-git and libgit2 tests for the same.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-05-20 19:52:18 +05:30
Paulo Gomes 8b50367849
libgit2: Add support for hashed known_hosts
Hashed known_hosts was previously only supported when using
go-git. Now both Git implementations benefit from this
features, and the code coverage across them can ensure no
future regression.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-16 16:57:22 +01:00
Paulo Gomes 6a407704a3
libgit2: remove redundant test
Both libgit2 and go-git now have tests that confirms
support for the supported authentication algorithms.
This test was created previously and is no longer
needed.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-13 15:00:10 +01:00
Paulo Gomes ce92881013
libgit2: remove connection caching
Connection caching was a feature created to resolve
upstream issues raised from concurrent ssh connections.
Some scenarios were based on multiple key exchange
operations happening at the same time.

This PR removes the connection caching, and instead:
- Services Session.StdoutPipe() as soon as possible,
  as it is a known source of blocking SSH connections.
- Reuse SSH connection within the same subtransport,
  eliminating the need for new handshakes when talking
  with the same server.
- Simplifies the entire transport logic for better
  maintainability.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-13 11:09:02 +01:00
Paulo Gomes 7f315f9807
gogit: use rem.ListContext instead of rem.List
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-11 16:36:55 +01:00
Paulo Gomes 90ef278797
libgit2: return func to help callers free git2go objects
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-11 13:02:27 +01:00
Paulo Gomes 44e166e576
libgit2: dispose git2go objects when error != nil
getBlankRepoAndRemote's callers are responsible for the disposal
of the returned objects. However, the caller does not expect to
need to dispose objects when err != nil, which may result to memory
leaks.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-11 11:40:09 +01:00
Paulo Gomes 54e07d8783
libgit2: recover from panic in short-circuited clones
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-11 11:40:08 +01:00
Somtochi Onyekwere 2bb3a1fea9
Implements no-op before CheckoutTag in gogit
Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
2022-05-11 11:40:07 +01:00
Somtochiama 8a3df9da42
gogit: check if revision changed before cloning in checkout branch (#694)
* Check if revision has changed in gogit CheckoutBranch

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
2022-05-11 11:40:06 +01:00
Sanskar Jaiswal 6a793a55f6
Optimize libgit2 checkout tag strategy through condition no-ops
Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-05-11 11:40:05 +01:00
Paulo Gomes 860d7051da
libgit2: optimise checkout branch clones
No-op reconciliations are very inefficient, as they carry out
a full clone operation of the target repository even when
no changes have taken place.

This change will execute a remote-ls operation, and cancel
the clone operation if the remote tip commit is still the same
as the one observed on the last reconcilation. In such cases,
an git.NoChangesError is returned.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-11 11:40:04 +01:00
Paulo Gomes 2b59150fbe
tests: algorithms test coverage for go-git and libgit2
Assures support for:
- Authentication Key Types
  - rsa
  - ecdsa P256
  - ecdsa P384
  - ecdsa P521
  - ed25519
- Key Exchange Algoritms:
  - diffie-hellman-group14-sha1
  - diffie-hellman-group14-sha256
  - curve25519-sha256
  - ecdh-sha2-nistp256
  - ecdh-sha2-nistp384
  - ecdh-sha2-nistp521
  - curve25519-sha256@libssh.org
- HostKey Algoritms:
  - ssh-rsa
  - rsa-sha2-256
  - rsa-sha2-512
  - ecdsa-sha2-nistp256
  - ecdsa-sha2-nistp384
  - ecdsa-sha2-nistp521
  - ssh-ed25519

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-09 13:31:54 +01:00
Paulo Gomes 4e3e62923b
git: Add git.HostKeyAlgos
Enables the setting of HostKey algorithms to be used from
a client perspective. This implementation supports go-git
and libgit2 when in ManagedTransport.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-06 17:58:09 +01:00
Paulo Gomes c0c5046e5f
libgit2: recover from git2go panic
Some specific scenarios may lead libgit2 or git2go to panic.
Adding a recovery logic, ensures a predictable execution path
for callers, and safeguards the controller's stability.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-05-06 08:39:00 +01:00
Eng Zer Jun 10b92da433
test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-04-29 23:01:39 +08:00
Paulo Gomes 68eece48fb
libgit2: optimise mutex on cached connections
Previously the mutex.Lock was acquired before creating
a new connection. The lock would then hold until the
process was finished, and all network latency would be
absorbed by other goroutines trying to establish a new
connection.

Now the lock is acquired after the connection has been
created. The downside of this approach is that concurrent
goroutine may be trying to open a connection to the same
target. The loser in the race will then have to Close the
connection and use the winner's instead.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 19:10:54 +01:00
Paulo Gomes b264a3513d
libgit2: refactor max length values into constants
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 19:10:50 +01:00
Paulo Gomes 3ab95a4bf0
libgit2: close discarded connections
Cached connections can be shared across concurrent
operations, and their disposal must take that into
account to avoid closing a connection that is stale for
one goroutine, but is still valid for another.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 18:37:40 +01:00
Paulo Gomes add07745f3
libgit2: restrict smart creds to Type SSH Memory
Avoid asking for SSH credential in files, as they won't be
used. The cacheKeyAndConfig func already enforces this
behaviour.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 18:37:39 +01:00
Paulo Gomes d86f0a280a
libgit2: validate URL max length
The major Git SaaS providers have repository URLs
for both HTTP and SSH that tops around 250
characters in length.

The limits chosen were a lot higher to align with use
cases in which users may have on-premise servers with
long domain names and paths.

For SSH the validation is around path length only,
which is now limited to 4096 characters, which is
at the higher end of the range in Linux.

For HTTP the validation is around the full URL
provided by the caller.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 18:37:38 +01:00
Paulo Gomes 54d0794d19
libgit2: handle the closing of stale connections
Internal and upstream calls to sshSmartSubtransport.Close()
when dealing with an stale connection, may lead to misleading
errors.

Focus should instead be redirected to ensuring that Close()
releases resources and ensures that a new SubTransport can be
created, so new operations can succeed.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 18:37:38 +01:00
Paulo Gomes 69c3f00172
libgit2: retry on stale connections
SSH servers that block the reuse of SSH connections for
multiple SSH sessions may lead to EOF when a new session
is being created.

This fixes the issue of long-running connections resulting
in EOF for GitLab servers.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-04-07 18:37:37 +01:00
Sanskar Jaiswal 5c84ea7e96 add flag to allow configuration of SSH kex algos
Adds a flag `ssh-kex-algos` which configures the gogit and libgit2
managed clients to use the specified list of kex algos for ssh. If not
used the default list in `golang/x/crypto/ssh` is used.

Signed-off-by: Sanskar Jaiswal <jaiswalsanskar078@gmail.com>
2022-04-07 16:43:15 +05:30
Paulo Gomes 36fcdeeb5e
libgit2: fix access to nil t.stdin and improve observability
All errors that were previously not handled are now logged through
traceLog, to further help during transport investigations.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-30 14:44:41 +01:00
Paulo Gomes c84179088c Fixes regression accessing GitLab public repositories
Some git servers are more accommodating than others. Gitlab will try to
validate credentials when they are provided, even if they are empty and
the target repository is public, leading to a failed authentication error.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-30 10:50:40 +02:00
Paulo Gomes 92ad1f813b
Cache SSH connections
The underlying SSH connections are kept open and are reused
across several SSH sessions. This is due to upstream issues in
which concurrent/parallel SSH connections may lead to instability.

https://github.com/golang/go/issues/51926
https://github.com/golang/go/issues/27140
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-28 11:58:10 +01:00
Paulo Gomes 017707a71c
Improve managed transport observability
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-25 19:08:54 +00:00
Paulo Gomes 5091b69ad5
Force ssh.Dial timeout
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-25 19:08:52 +00:00
Paulo Gomes a860ebee04
Reuse HTTP connections in managed transport
Ensure all requests are completely processed and closed,
to prove odds of the underlying connections to be reused.

The transport now is pooled and reused whenever possible.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-25 19:08:51 +00:00
Paulo Gomes 45641b17c7 Fix dpanic issue when logging odd number of args
Info was receiving odd number of arguments leading to:
odd number of arguments passed as key-value pairs for logging

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-25 14:27:04 +01:00
Paulo Gomes 115040e9ea
Support redirects for libgit2 managed transport
For backwards compatibility, support for HTTP redirection is enabled when targeting
the same host, and no TLS downgrade took place.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
Paulo Gomes 43661dd15e
Enforce effective URL on error messages
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
Paulo Gomes d1a7e5d609
Fix race condition on httpSmartSubTransport
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
Paulo Gomes 822788b79e
Fix word casing
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
Paulo Gomes 4ed54bc359
Optimise basic auth for libgit2 managed transport
The initial implementation was based off upstream, which cause
an initial request to fail, and only then the credentials would
be added into the request.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
Paulo Gomes 24bc95e1d8
Add license headers
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
Paulo Gomes a000d8b859
Add tests for experimental libgit2 transport
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
2022-03-16 16:22:21 +05:30
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
Sunny b814070bc2 Fixes to gitrepo reconciler tests
NOTE: This should be amended with the previous commit which has
commented out tests.

Update reconcileSource() to work with the test case where no secret is
set. A minimal auth options is created and used for git checkout.

Update TestGitRepositoryReconciler_verifyCommitSignature() to use the
new git.Commit type.

Update TestGitRepositoryReconciler_reconcileSource_checkoutStrategy to
add skipForImplementation for branch commit test case.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
2022-02-23 12:35:30 +01:00
Hidde Beydals eff40e22e9 git/libgit2: assert proper test of default branch
If there is no configuration set for `init.defaultBranch`, it does not
return an error but an empty string. We now take this into account so
we do not overwrite the default, and make the default `master` to match
with libgit2 defaults.

In addition, some comments have been added to not get confused about
what commits we are checking against.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-22 16:31:56 +01:00
Hidde Beydals 15c064abdf git/libgit2: set CheckoutForce on branch strategy
In the recent update from libgit2 1.1.x to 1.3.x, something seems to
have changed upstream. Resulting in the clone of a branch ending up
with a semi-bare file system state (in other words: without any files
present in the directory).

This commit patches the clone behavior to set the `CheckoutForce`
strategy as `CheckoutOption`, which mitigates the issue.

In addition, test cases have been added to ensure we do not run into
this again by asserting the state of the branch after cloning.

Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-02-22 16:31:56 +01:00