Skip to content

Commit 5ea7aa0

Browse files
authored
Merge pull request #450 from fluxcd/ssh-host-github
Add token authentication option to bootstrap
2 parents 237d186 + 7792cd6 commit 5ea7aa0

File tree

7 files changed

+125
-72
lines changed

7 files changed

+125
-72
lines changed

cmd/flux/bootstrap.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var (
5757
bootstrapArch = flags.Arch(defaults.Arch)
5858
bootstrapLogLevel = flags.LogLevel(defaults.LogLevel)
5959
bootstrapRequiredComponents = []string{"source-controller", "kustomize-controller"}
60+
bootstrapTokenAuth bool
6061
)
6162

6263
const (
@@ -75,14 +76,16 @@ func init() {
7576
bootstrapCmd.PersistentFlags().Var(&bootstrapArch, "arch", bootstrapArch.Description())
7677
bootstrapCmd.PersistentFlags().StringVar(&bootstrapBranch, "branch", bootstrapDefaultBranch,
7778
"default branch (for GitHub this must match the default branch setting for the organization)")
78-
rootCmd.AddCommand(bootstrapCmd)
7979
bootstrapCmd.PersistentFlags().BoolVar(&bootstrapWatchAllNamespaces, "watch-all-namespaces", true,
8080
"watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed")
8181
bootstrapCmd.PersistentFlags().BoolVar(&bootstrapNetworkPolicy, "network-policy", true,
8282
"deny ingress access to the toolkit controllers from other namespaces using network policies")
83+
bootstrapCmd.PersistentFlags().BoolVar(&bootstrapTokenAuth, "token-auth", false,
84+
"when enabled, the personal access token will be used instead of SSH deploy key")
8385
bootstrapCmd.PersistentFlags().Var(&bootstrapLogLevel, "log-level", bootstrapLogLevel.Description())
8486
bootstrapCmd.PersistentFlags().StringVar(&bootstrapManifestsPath, "manifests", "", "path to the manifest directory")
8587
bootstrapCmd.PersistentFlags().MarkHidden("manifests")
88+
rootCmd.AddCommand(bootstrapCmd)
8689
}
8790

8891
func bootstrapValidate() error {

cmd/flux/bootstrap_github.go

Lines changed: 59 additions & 31 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/flux2/internal/utils"
3133
"github.com/fluxcd/pkg/git"
@@ -35,7 +37,7 @@ var bootstrapGitHubCmd = &cobra.Command{
3537
Use: "github",
3638
Short: "Bootstrap toolkit components in a GitHub repository",
3739
Long: `The bootstrap github command creates the GitHub repository if it doesn't exists and
38-
commits the toolkit components manifests to the master branch.
40+
commits the toolkit components manifests to the main branch.
3941
Then it configures the target cluster to synchronize with the repository.
4042
If the toolkit components are present on the cluster,
4143
the bootstrap command will perform an upgrade if needed.`,
@@ -54,8 +56,11 @@ the bootstrap command will perform an upgrade if needed.`,
5456
# Run bootstrap for a public repository on a personal account
5557
flux bootstrap github --owner=<user> --repository=<repo name> --private=false --personal=true
5658
57-
# Run bootstrap for a private repo hosted on GitHub Enterprise
58-
flux bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain>
59+
# Run bootstrap for a private repo hosted on GitHub Enterprise using SSH auth
60+
flux bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain> --ssh-hostname=<domain>
61+
62+
# Run bootstrap for a private repo hosted on GitHub Enterprise using HTTPS auth
63+
flux bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain> --token-auth
5964
6065
# Run bootstrap for a an existing repository with a branch named main
6166
flux bootstrap github --owner=<organization> --repository=<repo name> --branch=main
@@ -64,15 +69,16 @@ the bootstrap command will perform an upgrade if needed.`,
6469
}
6570

6671
var (
67-
ghOwner string
68-
ghRepository string
69-
ghInterval time.Duration
70-
ghPersonal bool
71-
ghPrivate bool
72-
ghHostname string
73-
ghPath string
74-
ghTeams []string
75-
ghDelete bool
72+
ghOwner string
73+
ghRepository string
74+
ghInterval time.Duration
75+
ghPersonal bool
76+
ghPrivate bool
77+
ghHostname string
78+
ghPath string
79+
ghTeams []string
80+
ghDelete bool
81+
ghSSHHostname string
7682
)
7783

7884
const (
@@ -87,6 +93,7 @@ func init() {
8793
bootstrapGitHubCmd.Flags().BoolVar(&ghPrivate, "private", true, "is private repository")
8894
bootstrapGitHubCmd.Flags().DurationVar(&ghInterval, "interval", time.Minute, "sync interval")
8995
bootstrapGitHubCmd.Flags().StringVar(&ghHostname, "hostname", git.GitHubDefaultHostname, "GitHub hostname")
96+
bootstrapGitHubCmd.Flags().StringVar(&ghSSHHostname, "ssh-hostname", "", "GitHub SSH hostname, to be used when the SSH host differs from the HTTPS one")
9097
bootstrapGitHubCmd.Flags().StringVar(&ghPath, "path", "", "repository path, when specified the cluster sync will be scoped to this path")
9198

9299
bootstrapGitHubCmd.Flags().BoolVar(&ghDelete, "delete", false, "delete repository (used for testing only)")
@@ -110,6 +117,10 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
110117
return err
111118
}
112119

120+
if ghSSHHostname != "" {
121+
repository.SSHHost = ghSSHHostname
122+
}
123+
113124
provider := &git.GithubProvider{
114125
IsPrivate: ghPrivate,
115126
IsPersonal: ghPersonal,
@@ -155,7 +166,7 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
155166
}
156167
}
157168

158-
// clone repository and checkout the master branch
169+
// clone repository and checkout the main branch
159170
if err := repository.Checkout(ctx, bootstrapBranch, tmpDir); err != nil {
160171
return err
161172
}
@@ -201,28 +212,45 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error {
201212
logger.Successf("install completed")
202213
}
203214

204-
// setup SSH deploy key
205-
if shouldCreateDeployKey(ctx, kubeClient, namespace) {
206-
logger.Actionf("configuring deploy key")
207-
u, err := url.Parse(repository.GetSSH())
208-
if err != nil {
209-
return fmt.Errorf("git URL parse failed: %w", err)
215+
if bootstrapTokenAuth {
216+
// setup HTTPS token auth
217+
secret := corev1.Secret{
218+
ObjectMeta: metav1.ObjectMeta{
219+
Name: namespace,
220+
Namespace: namespace,
221+
},
222+
StringData: map[string]string{
223+
"username": "git",
224+
"password": ghToken,
225+
},
210226
}
211-
212-
key, err := generateDeployKey(ctx, kubeClient, u, namespace)
213-
if err != nil {
214-
return fmt.Errorf("generating deploy key failed: %w", err)
227+
if err := upsertSecret(ctx, kubeClient, secret); err != nil {
228+
return err
215229
}
230+
} else {
231+
// setup SSH deploy key
232+
if shouldCreateDeployKey(ctx, kubeClient, namespace) {
233+
logger.Actionf("configuring deploy key")
234+
u, err := url.Parse(repository.GetSSH())
235+
if err != nil {
236+
return fmt.Errorf("git URL parse failed: %w", err)
237+
}
216238

217-
keyName := "flux"
218-
if ghPath != "" {
219-
keyName = fmt.Sprintf("flux-%s", ghPath)
220-
}
239+
key, err := generateDeployKey(ctx, kubeClient, u, namespace)
240+
if err != nil {
241+
return fmt.Errorf("generating deploy key failed: %w", err)
242+
}
221243

222-
if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil {
223-
return err
224-
} else if changed {
225-
logger.Successf("deploy key configured")
244+
keyName := "flux"
245+
if ghPath != "" {
246+
keyName = fmt.Sprintf("flux-%s", ghPath)
247+
}
248+
249+
if changed, err := provider.AddDeployKey(ctx, repository, key, keyName); err != nil {
250+
return err
251+
} else if changed {
252+
logger.Successf("deploy key configured")
253+
}
226254
}
227255
}
228256

cmd/flux/bootstrap_gitlab.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,22 @@ the bootstrap command will perform an upgrade if needed.`,
4545
export GITLAB_TOKEN=<my-token>
4646
4747
# Run bootstrap for a private repo using HTTPS token authentication
48-
flux bootstrap gitlab --owner=<group> --repository=<repo name>
48+
flux bootstrap gitlab --owner=<group> --repository=<repo name> --token-auth
4949
5050
# Run bootstrap for a private repo using SSH authentication
51-
flux bootstrap gitlab --owner=<group> --repository=<repo name> --ssh-hostname=gitlab.com
51+
flux bootstrap gitlab --owner=<group> --repository=<repo name>
5252
5353
# Run bootstrap for a repository path
5454
flux bootstrap gitlab --owner=<group> --repository=<repo name> --path=dev-cluster
5555
5656
# Run bootstrap for a public repository on a personal account
57-
flux bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true
57+
flux bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal --token-auth
5858
5959
# Run bootstrap for a private repo hosted on a GitLab server
60-
flux bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain>
60+
flux bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain> --token-auth
6161
6262
# Run bootstrap for a an existing repository with a branch named main
63-
flux bootstrap gitlab --owner=<organization> --repository=<repo name> --branch=main
63+
flux bootstrap gitlab --owner=<organization> --repository=<repo name> --branch=main --token-auth
6464
`,
6565
RunE: bootstrapGitLabCmdRun,
6666
}
@@ -83,7 +83,7 @@ func init() {
8383
bootstrapGitLabCmd.Flags().BoolVar(&glPrivate, "private", true, "is private repository")
8484
bootstrapGitLabCmd.Flags().DurationVar(&glInterval, "interval", time.Minute, "sync interval")
8585
bootstrapGitLabCmd.Flags().StringVar(&glHostname, "hostname", git.GitLabDefaultHostname, "GitLab hostname")
86-
bootstrapGitLabCmd.Flags().StringVar(&glSSHHostname, "ssh-hostname", "", "GitLab SSH hostname, when specified a deploy key will be added to the repository")
86+
bootstrapGitLabCmd.Flags().StringVar(&glSSHHostname, "ssh-hostname", "", "GitLab SSH hostname, to be used when the SSH host differs from the HTTPS one")
8787
bootstrapGitLabCmd.Flags().StringVar(&glPath, "path", "", "repository path, when specified the cluster sync will be scoped to this path")
8888

8989
bootstrapCmd.AddCommand(bootstrapGitLabCmd)
@@ -180,7 +180,22 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
180180

181181
repoURL := repository.GetURL()
182182

183-
if glSSHHostname != "" {
183+
if bootstrapTokenAuth {
184+
// setup HTTPS token auth
185+
secret := corev1.Secret{
186+
ObjectMeta: metav1.ObjectMeta{
187+
Name: namespace,
188+
Namespace: namespace,
189+
},
190+
StringData: map[string]string{
191+
"username": "git",
192+
"password": glToken,
193+
},
194+
}
195+
if err := upsertSecret(ctx, kubeClient, secret); err != nil {
196+
return err
197+
}
198+
} else {
184199
// setup SSH deploy key
185200
repoURL = repository.GetSSH()
186201
if shouldCreateDeployKey(ctx, kubeClient, namespace) {
@@ -206,21 +221,6 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
206221
logger.Successf("deploy key configured")
207222
}
208223
}
209-
} else {
210-
// setup HTTPS token auth
211-
secret := corev1.Secret{
212-
ObjectMeta: metav1.ObjectMeta{
213-
Name: namespace,
214-
Namespace: namespace,
215-
},
216-
StringData: map[string]string{
217-
"username": "git",
218-
"password": glToken,
219-
},
220-
}
221-
if err := upsertSecret(ctx, kubeClient, secret); err != nil {
222-
return err
223-
}
224224
}
225225

226226
// configure repo synchronization

docs/cmd/flux_bootstrap.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The bootstrap sub-commands bootstrap the toolkit components on the targeted Git
1717
--log-level logLevel log level, available options are: (debug, info, error) (default info)
1818
--network-policy deny ingress access to the toolkit controllers from other namespaces using network policies (default true)
1919
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
20+
--token-auth when enabled, the personal access token will be used instead of SSH deploy key
2021
-v, --version string toolkit version (default "latest")
2122
--watch-all-namespaces watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed (default true)
2223
```

docs/cmd/flux_bootstrap_github.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bootstrap toolkit components in a GitHub repository
55
### Synopsis
66

77
The bootstrap github command creates the GitHub repository if it doesn't exists and
8-
commits the toolkit components manifests to the master branch.
8+
commits the toolkit components manifests to the main branch.
99
Then it configures the target cluster to synchronize with the repository.
1010
If the toolkit components are present on the cluster,
1111
the bootstrap command will perform an upgrade if needed.
@@ -32,8 +32,11 @@ flux bootstrap github [flags]
3232
# Run bootstrap for a public repository on a personal account
3333
flux bootstrap github --owner=<user> --repository=<repo name> --private=false --personal=true
3434
35-
# Run bootstrap for a private repo hosted on GitHub Enterprise
36-
flux bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain>
35+
# Run bootstrap for a private repo hosted on GitHub Enterprise using SSH auth
36+
flux bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain> --ssh-hostname=<domain>
37+
38+
# Run bootstrap for a private repo hosted on GitHub Enterprise using HTTPS auth
39+
flux bootstrap github --owner=<organization> --repository=<repo name> --hostname=<domain> --token-auth
3740
3841
# Run bootstrap for a an existing repository with a branch named main
3942
flux bootstrap github --owner=<organization> --repository=<repo name> --branch=main
@@ -43,15 +46,16 @@ flux bootstrap github [flags]
4346
### Options
4447

4548
```
46-
-h, --help help for github
47-
--hostname string GitHub hostname (default "github.com")
48-
--interval duration sync interval (default 1m0s)
49-
--owner string GitHub user or organization name
50-
--path string repository path, when specified the cluster sync will be scoped to this path
51-
--personal is personal repository
52-
--private is private repository (default true)
53-
--repository string GitHub repository name
54-
--team stringArray GitHub team to be given maintainer access
49+
-h, --help help for github
50+
--hostname string GitHub hostname (default "github.com")
51+
--interval duration sync interval (default 1m0s)
52+
--owner string GitHub user or organization name
53+
--path string repository path, when specified the cluster sync will be scoped to this path
54+
--personal is personal repository
55+
--private is private repository (default true)
56+
--repository string GitHub repository name
57+
--ssh-hostname string GitHub SSH hostname, to be used when the SSH host differs from the HTTPS one
58+
--team stringArray GitHub team to be given maintainer access
5559
```
5660

5761
### Options inherited from parent commands
@@ -68,6 +72,7 @@ flux bootstrap github [flags]
6872
--network-policy deny ingress access to the toolkit controllers from other namespaces using network policies (default true)
6973
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
7074
--timeout duration timeout for this operation (default 5m0s)
75+
--token-auth when enabled, the personal access token will be used instead of SSH deploy key
7176
--verbose print generated objects
7277
-v, --version string toolkit version (default "latest")
7378
--watch-all-namespaces watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed (default true)

docs/cmd/flux_bootstrap_gitlab.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ flux bootstrap gitlab [flags]
2121
export GITLAB_TOKEN=<my-token>
2222
2323
# Run bootstrap for a private repo using HTTPS token authentication
24-
flux bootstrap gitlab --owner=<group> --repository=<repo name>
24+
flux bootstrap gitlab --owner=<group> --repository=<repo name> --token-auth
2525
2626
# Run bootstrap for a private repo using SSH authentication
27-
flux bootstrap gitlab --owner=<group> --repository=<repo name> --ssh-hostname=gitlab.com
27+
flux bootstrap gitlab --owner=<group> --repository=<repo name>
2828
2929
# Run bootstrap for a repository path
3030
flux bootstrap gitlab --owner=<group> --repository=<repo name> --path=dev-cluster
3131
3232
# Run bootstrap for a public repository on a personal account
33-
flux bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true
33+
flux bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal --token-auth
3434
3535
# Run bootstrap for a private repo hosted on a GitLab server
36-
flux bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain>
36+
flux bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain> --token-auth
3737
3838
# Run bootstrap for a an existing repository with a branch named main
39-
flux bootstrap gitlab --owner=<organization> --repository=<repo name> --branch=main
39+
flux bootstrap gitlab --owner=<organization> --repository=<repo name> --branch=main --token-auth
4040
4141
```
4242

@@ -51,7 +51,7 @@ flux bootstrap gitlab [flags]
5151
--personal is personal repository
5252
--private is private repository (default true)
5353
--repository string GitLab repository name
54-
--ssh-hostname string GitLab SSH hostname, when specified a deploy key will be added to the repository
54+
--ssh-hostname string GitLab SSH hostname, to be used when the SSH host differs from the HTTPS one
5555
```
5656

5757
### Options inherited from parent commands
@@ -68,6 +68,7 @@ flux bootstrap gitlab [flags]
6868
--network-policy deny ingress access to the toolkit controllers from other namespaces using network policies (default true)
6969
--registry string container registry where the toolkit images are published (default "ghcr.io/fluxcd")
7070
--timeout duration timeout for this operation (default 5m0s)
71+
--token-auth when enabled, the personal access token will be used instead of SSH deploy key
7172
--verbose print generated objects
7273
-v, --version string toolkit version (default "latest")
7374
--watch-all-namespaces watch for custom resources in all namespaces, if set to false it will only watch the namespace where the toolkit is installed (default true)

0 commit comments

Comments
 (0)