Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create empty ssh key for git client to avoid creating real deploy keys when using dry-run. #1054

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/services/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (sn SecretName) NamespacedName() types.NamespacedName {
}

type AuthService interface {
CreateGitClient(ctx context.Context, repoUrl gitproviders.RepoURL, targetName string, namespace string) (git.Git, error)
CreateGitClient(ctx context.Context, repoUrl gitproviders.RepoURL, targetName string, namespace string, dryRun bool) (git.Git, error)
GetGitProvider() gitproviders.GitProvider
}

Expand Down Expand Up @@ -93,7 +93,12 @@ func (a *authSvc) GetGitProvider() gitproviders.GitProvider {

// CreateGitClient creates a git.Git client instrumented with existing or generated deploy keys.
// This ensures that git operations are done with stored deploy keys instead of a user's local ssh-agent or equivalent.
func (a *authSvc) CreateGitClient(ctx context.Context, repoUrl gitproviders.RepoURL, targetName string, namespace string) (git.Git, error) {
func (a *authSvc) CreateGitClient(ctx context.Context, repoUrl gitproviders.RepoURL, targetName string, namespace string, dryRun bool) (git.Git, error) {
if dryRun {
d, _ := makePublicKey([]byte(""))
return git.New(d, wrapper.NewGoGit()), nil
}

secretName := SecretName{
Name: app.CreateRepoSecretName(targetName, repoUrl.String()),
Namespace: namespace,
Expand Down
14 changes: 11 additions & 3 deletions pkg/services/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"bytes"
"context"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/weaveworks/weave-gitops/pkg/flux"
Expand Down Expand Up @@ -64,7 +65,7 @@ var _ = Describe("auth", func() {
}
})
It("create and stores a deploy key if none exists", func() {
_, err := as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name)
_, err := as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name, false)
Expect(err).NotTo(HaveOccurred())
sn := SecretName{Name: secretName, Namespace: namespace.Name}
secret := &corev1.Secret{}
Expand All @@ -73,6 +74,13 @@ var _ = Describe("auth", func() {
Expect(secret.StringData["identity"]).NotTo(BeNil())
Expect(secret.StringData["identity.pub"]).NotTo(BeNil())
})
It("doesn't create a deploy key when dry-run is true", func() {
_, err := as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name, true)
Expect(err).NotTo(HaveOccurred())
sn := SecretName{Name: secretName, Namespace: namespace.Name}
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, sn.NamespacedName(), secret)).To(HaveOccurred())
})
It("uses an existing deploy key when present", func() {
gp.DeployKeyExistsReturns(true, nil)
sn := SecretName{Name: secretName, Namespace: namespace.Name}
Expand All @@ -81,7 +89,7 @@ var _ = Describe("auth", func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient.Create(ctx, secret)).To(Succeed())

_, err = as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name)
_, err = as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name, false)
Expect(err).NotTo(HaveOccurred())
// We should NOT have uploaded anything since the key already exists
Expect(gp.UploadDeployKeyCallCount()).To(Equal(0))
Expand All @@ -90,7 +98,7 @@ var _ = Describe("auth", func() {
gp.DeployKeyExistsReturns(true, nil)
sn := SecretName{Name: secretName, Namespace: namespace.Name}

_, err = as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name)
_, err = as.CreateGitClient(ctx, repoUrl, testClustername, namespace.Name, false)
Expect(err).NotTo(HaveOccurred())

newSecret := &corev1.Secret{}
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (f *defaultFactory) GetGitClients(ctx context.Context, gpClient gitprovider

if !params.IsHelmRepository {
// We need to do this even if we have an external config to set up the deploy key for the app repo
appRepoClient, appRepoErr := authSvc.CreateGitClient(ctx, normalizedUrl, targetName, params.Namespace)
appRepoClient, appRepoErr := authSvc.CreateGitClient(ctx, normalizedUrl, targetName, params.Namespace, params.DryRun)
if appRepoErr != nil {
return nil, nil, appRepoErr
}
Expand All @@ -153,7 +153,7 @@ func (f *defaultFactory) GetGitClients(ctx context.Context, gpClient gitprovider
return nil, nil, fmt.Errorf("error normalizing url: %w", err)
}

configRepoClient, configRepoErr := authSvc.CreateGitClient(ctx, normalizedConfigUrl, targetName, params.Namespace)
configRepoClient, configRepoErr := authSvc.CreateGitClient(ctx, normalizedConfigUrl, targetName, params.Namespace, params.DryRun)
if configRepoErr != nil {
return nil, nil, configRepoErr
}
Expand Down