Skip to content

Commit b4f13e1

Browse files
authored
Merge pull request #351 from fluxcd/gitlab-https-auth
Add GitLab HTTPS auth to bootstrap options
2 parents 0ab814f + d0eb55f commit b4f13e1

File tree

3 files changed

+69
-25
lines changed

3 files changed

+69
-25
lines changed

cmd/gotk/bootstrap_gitlab.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"time"
2727

2828
"github.com/spf13/cobra"
29+
corev1 "k8s.io/api/core/v1"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2931

3032
"github.com/fluxcd/pkg/git"
3133
)
@@ -41,14 +43,17 @@ the bootstrap command will perform an upgrade if needed.`,
4143
Example: ` # Create a GitLab API token and export it as an env var
4244
export GITLAB_TOKEN=<my-token>
4345
44-
# Run bootstrap for a private repo owned by a GitLab group
46+
# Run bootstrap for a private repo using HTTPS token authentication
4547
gotk bootstrap gitlab --owner=<group> --repository=<repo name>
4648
49+
# Run bootstrap for a private repo using SSH authentication
50+
gotk bootstrap gitlab --owner=<group> --repository=<repo name> --ssh-hostname=gitlab.com
51+
4752
# Run bootstrap for a repository path
4853
gotk bootstrap gitlab --owner=<group> --repository=<repo name> --path=dev-cluster
4954
5055
# Run bootstrap for a public repository on a personal account
51-
gotk bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true
56+
gotk bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true
5257
5358
# Run bootstrap for a private repo hosted on a GitLab server
5459
gotk bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain>
@@ -77,7 +82,7 @@ func init() {
7782
bootstrapGitLabCmd.Flags().BoolVar(&glPrivate, "private", true, "is private repository")
7883
bootstrapGitLabCmd.Flags().DurationVar(&glInterval, "interval", time.Minute, "sync interval")
7984
bootstrapGitLabCmd.Flags().StringVar(&glHostname, "hostname", git.GitLabDefaultHostname, "GitLab hostname")
80-
bootstrapGitLabCmd.Flags().StringVar(&glSSHHostname, "ssh-hostname", "", "GitLab SSH hostname, defaults to hostname if not specified")
85+
bootstrapGitLabCmd.Flags().StringVar(&glSSHHostname, "ssh-hostname", "", "GitLab SSH hostname, when specified a deploy key will be added to the repository")
8186
bootstrapGitLabCmd.Flags().StringVar(&glPath, "path", "", "repository path, when specified the cluster sync will be scoped to this path")
8287

8388
bootstrapCmd.AddCommand(bootstrapGitLabCmd)
@@ -172,34 +177,54 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
172177
logger.Successf("install completed")
173178
}
174179

175-
// setup SSH deploy key
176-
if shouldCreateDeployKey(ctx, kubeClient, namespace) {
177-
logger.Actionf("configuring deploy key")
178-
u, err := url.Parse(repository.GetSSH())
179-
if err != nil {
180-
return fmt.Errorf("git URL parse failed: %w", err)
181-
}
180+
repoURL := repository.GetURL()
182181

183-
key, err := generateDeployKey(ctx, kubeClient, u, namespace)
184-
if err != nil {
185-
return fmt.Errorf("generating deploy key failed: %w", err)
182+
if glSSHHostname != "" {
183+
// setup SSH deploy key
184+
repoURL = repository.GetSSH()
185+
if shouldCreateDeployKey(ctx, kubeClient, namespace) {
186+
logger.Actionf("configuring deploy key")
187+
u, err := url.Parse(repoURL)
188+
if err != nil {
189+
return fmt.Errorf("git URL parse failed: %w", err)
190+
}
191+
192+
key, err := generateDeployKey(ctx, kubeClient, u, namespace)
193+
if err != nil {
194+
return fmt.Errorf("generating deploy key failed: %w", err)
195+
}
196+
197+
keyName := "gotk"
198+
if glPath != "" {
199+
keyName = fmt.Sprintf("gotk-%s", glPath)
200+
}
201+
202+
if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil {
203+
return err
204+
} else if changed {
205+
logger.Successf("deploy key configured")
206+
}
186207
}
187-
188-
keyName := "gotk"
189-
if glPath != "" {
190-
keyName = fmt.Sprintf("gotk-%s", glPath)
208+
} else {
209+
// setup HTTPS token auth
210+
secret := corev1.Secret{
211+
ObjectMeta: metav1.ObjectMeta{
212+
Name: namespace,
213+
Namespace: namespace,
214+
},
215+
StringData: map[string]string{
216+
"username": "git",
217+
"password": glToken,
218+
},
191219
}
192-
193-
if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil {
220+
if err := upsertSecret(ctx, kubeClient, secret); err != nil {
194221
return err
195-
} else if changed {
196-
logger.Successf("deploy key configured")
197222
}
198223
}
199224

200225
// configure repo synchronization
201226
logger.Actionf("generating sync manifests")
202-
if err := generateSyncManifests(repository.GetSSH(), bootstrapBranch, namespace, namespace, glPath, tmpDir, glInterval); err != nil {
227+
if err := generateSyncManifests(repoURL, bootstrapBranch, namespace, namespace, glPath, tmpDir, glInterval); err != nil {
203228
return err
204229
}
205230

docs/cmd/gotk_bootstrap_gitlab.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ gotk bootstrap gitlab [flags]
2020
# Create a GitLab API token and export it as an env var
2121
export GITLAB_TOKEN=<my-token>
2222
23-
# Run bootstrap for a private repo owned by a GitLab group
23+
# Run bootstrap for a private repo using HTTPS token authentication
2424
gotk bootstrap gitlab --owner=<group> --repository=<repo name>
2525
26+
# Run bootstrap for a private repo using SSH authentication
27+
gotk bootstrap gitlab --owner=<group> --repository=<repo name> --ssh-hostname=gitlab.com
28+
2629
# Run bootstrap for a repository path
2730
gotk bootstrap gitlab --owner=<group> --repository=<repo name> --path=dev-cluster
2831
2932
# Run bootstrap for a public repository on a personal account
30-
gotk bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true
33+
gotk bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true
3134
3235
# Run bootstrap for a private repo hosted on a GitLab server
3336
gotk bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain>
@@ -48,7 +51,7 @@ gotk bootstrap gitlab [flags]
4851
--personal is personal repository
4952
--private is private repository (default true)
5053
--repository string GitLab repository name
51-
--ssh-hostname string GitLab SSH hostname, defaults to hostname if not specified
54+
--ssh-hostname string GitLab SSH hostname, when specified a deploy key will be added to the repository
5255
```
5356

5457
### Options inherited from parent commands

docs/guides/installation.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ gotk bootstrap gitlab \
154154
--personal
155155
```
156156

157+
To run the bootstrap for a repository using deploy keys for authentication, you have to specify the SSH hostname:
158+
159+
```sh
160+
gotk bootstrap gitlab \
161+
--ssh-hostname=gitlab.com \
162+
--owner=my-gitlab-username \
163+
--repository=my-repository \
164+
--branch=master \
165+
--path=my-cluster
166+
```
167+
168+
!!! hint "Authentication"
169+
When providing the `--ssh-hostname`, a read-only (SSH) deploy key will be added
170+
to your repository, otherwise your GitLab personal token will be used to
171+
authenticate against the HTTPS endpoint instead.
172+
157173
Run the bootstrap for a repository owned by a GitLab group:
158174

159175
```sh

0 commit comments

Comments
 (0)