Skip to content

Commit 7e017a3

Browse files
committed
feat: add support of aws codecommit bootstrap
Signed-off-by: Taras <9948629+taraspos@users.noreply.github.com>
1 parent 4e78a9d commit 7e017a3

6 files changed

Lines changed: 38 additions & 12 deletions

File tree

cmd/flux/bootstrap_git.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
"github.com/spf13/cobra"
2929
corev1 "k8s.io/api/core/v1"
3030

31+
"github.com/fluxcd/pkg/auth"
32+
"github.com/fluxcd/pkg/auth/aws"
33+
authutils "github.com/fluxcd/pkg/auth/utils"
3134
"github.com/fluxcd/pkg/git"
3235
"github.com/fluxcd/pkg/git/gogit"
3336

@@ -62,9 +65,12 @@ command will perform an upgrade if needed.`,
6265
# Run bootstrap for a Git repository with a private key and password
6366
flux bootstrap git --url=ssh://git@example.com/repository.git --private-key-file=<path/to/private.key> --password=<password> --path=clusters/my-cluster
6467
65-
# Run bootstrap for a Git repository on AWS CodeCommit
68+
# Run bootstrap for a Git repository on AWS CodeCommit using SSH
6669
flux bootstrap git --url=ssh://<SSH-Key-ID>@git-codecommit.<region>.amazonaws.com/v1/repos/<repository> --private-key-file=<path/to/private.key> --password=<SSH-passphrase> --path=clusters/my-cluster
6770
71+
# Run bootstrap for a Git repository on AWS CodeCommit using HTTPS with IAM credentials
72+
flux bootstrap git --url=https://git-codecommit.<region>.amazonaws.com/v1/repos/<repository> --path=clusters/my-cluster
73+
6874
# Run bootstrap for a Git repository on Azure Devops
6975
flux bootstrap git --url=ssh://git@ssh.dev.azure.com/v3/<org>/<project>/<repository> --private-key-file=<path/to/rsa-sha2-private.key> --ssh-hostkey-algos=rsa-sha2-512,rsa-sha2-256 --path=clusters/my-cluster
7076
@@ -109,6 +115,7 @@ func bootstrapGitCmdRun(cmd *cobra.Command, args []string) error {
109115
bootstrapArgs.tokenAuth = true
110116
}
111117

118+
var gitProvider string
112119
gitPassword := os.Getenv(gitPasswordEnvVar)
113120
if gitPassword != "" && gitArgs.password == "" {
114121
gitArgs.password = gitPassword
@@ -131,8 +138,12 @@ func bootstrapGitCmdRun(cmd *cobra.Command, args []string) error {
131138
return err
132139
}
133140

141+
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
142+
defer cancel()
143+
134144
if strings.Contains(repositoryURL.Hostname(), "git-codecommit") && strings.Contains(repositoryURL.Hostname(), "amazonaws.com") {
135-
if repositoryURL.Scheme == string(git.SSH) {
145+
// https://docs.aws.amazon.com/codecommit/latest/userguide/auth-and-access-control.html
146+
if repositoryURL.Scheme == string(git.SSH) { // IAM user + SSH
136147
if repositoryURL.User == nil {
137148
return fmt.Errorf("invalid AWS CodeCommit url: ssh username should be specified in the url")
138149
}
@@ -142,14 +153,18 @@ func bootstrapGitCmdRun(cmd *cobra.Command, args []string) error {
142153
if bootstrapArgs.privateKeyFile == "" {
143154
return fmt.Errorf("private key file is required for bootstrapping against AWS CodeCommit using ssh")
144155
}
156+
} else if repositoryURL.Scheme == string(git.HTTPS) && !bootstrapArgs.tokenAuth { // IAM role + HTTPS
157+
creds, err := authutils.GetGitCredentials(ctx, "aws", auth.WithGitURL(*repositoryURL))
158+
if err != nil {
159+
return fmt.Errorf("failed to get AWS CodeCommit IAM git credentials: %w", err)
160+
}
161+
gitArgs.username = creds.Username
162+
gitArgs.password = creds.Password
163+
bootstrapArgs.tokenAuth = true
164+
gitProvider = aws.ProviderName
145165
}
146-
if repositoryURL.Scheme == string(git.HTTPS) && !bootstrapArgs.tokenAuth {
147-
return fmt.Errorf("--token-auth=true must be specified for using an HTTPS AWS CodeCommit url")
148-
}
149-
}
150166

151-
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
152-
defer cancel()
167+
}
153168

154169
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
155170
if err != nil {
@@ -297,6 +312,9 @@ func bootstrapGitCmdRun(cmd *cobra.Command, args []string) error {
297312
ManifestFile: sync.MakeDefaultOptions().ManifestFile,
298313
RecurseSubmodules: bootstrapArgs.recurseSubmodules,
299314
}
315+
if gitProvider != "" {
316+
syncOpts.Provider = gitProvider
317+
}
300318

301319
entityList, err := bootstrap.LoadEntityListFromPath(bootstrapArgs.gpgKeyRingPath)
302320
if err != nil {

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/fluxcd/notification-controller/api v1.8.4
2121
github.com/fluxcd/pkg/apis/event v0.25.0
2222
github.com/fluxcd/pkg/apis/meta v1.26.0
23-
github.com/fluxcd/pkg/auth v0.40.0
23+
github.com/fluxcd/pkg/auth v0.41.0
2424
github.com/fluxcd/pkg/chartutil v1.23.0
2525
github.com/fluxcd/pkg/envsubst v1.5.0
2626
github.com/fluxcd/pkg/git v0.46.0
@@ -101,6 +101,7 @@ require (
101101
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.35.13 // indirect
102102
github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 // indirect
103103
github.com/aws/smithy-go v1.24.0 // indirect
104+
github.com/aws/smithy-go/aws-http-auth v1.1.3 // indirect
104105
github.com/beorn7/perks v1.0.1 // indirect
105106
github.com/blang/semver/v4 v4.0.0 // indirect
106107
github.com/bshuster-repo/logrus-logstash-hook v1.1.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.41.6 h1:5fFjR/ToSOzB2OQ/XqWpZBmNvmP/
8787
github.com/aws/aws-sdk-go-v2/service/sts v1.41.6/go.mod h1:qgFDZQSD/Kys7nJnVqYlWKnh0SSdMjAi0uSwON4wgYQ=
8888
github.com/aws/smithy-go v1.24.0 h1:LpilSUItNPFr1eY85RYgTIg5eIEPtvFbskaFcmmIUnk=
8989
github.com/aws/smithy-go v1.24.0/go.mod h1:LEj2LM3rBRQJxPZTB4KuzZkaZYnZPnvgIhb4pu07mx0=
90+
github.com/aws/smithy-go/aws-http-auth v1.1.3 h1:8/T7/2n8x+x9sIAmi5h5mDKS8v7/u2GEpF6T6RrGMrc=
91+
github.com/aws/smithy-go/aws-http-auth v1.1.3/go.mod h1:KL46VTjVK9De3jurMqDLBkXCP9vrAvD03zQrmyzyrQ0=
9092
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
9193
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
9294
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -196,8 +198,8 @@ github.com/fluxcd/pkg/apis/kustomize v1.16.0 h1:PhWXEhqQqsisIpwp1/wHvTvo+MO+GGzs
196198
github.com/fluxcd/pkg/apis/kustomize v1.16.0/go.mod h1:IZOy4CCtR/hxMGb7erK1RfbGnczVv4/dRBoVD37AywI=
197199
github.com/fluxcd/pkg/apis/meta v1.26.0 h1:dxP1FfBpTCYso6odzRcltVnnRuBb2VyhhgV0VX9YbUE=
198200
github.com/fluxcd/pkg/apis/meta v1.26.0/go.mod h1:c7o6mJGLCMvNrfdinGZehkrdZuFT9vZdZNrn66DtVD0=
199-
github.com/fluxcd/pkg/auth v0.40.0 h1:p6Kw6KH+z8oRqngKhmTt8ILKD/rC+8tP87a//kLZhi8=
200-
github.com/fluxcd/pkg/auth v0.40.0/go.mod h1:Oq/hIEKUMTbL2bv5blf+EhC/jXXJLsOjIMtJj/AtG3Y=
201+
github.com/fluxcd/pkg/auth v0.41.0 h1:7NaaPN03ginRUUA928n7hiRJoBoMrF/Prl0AtDlLXBQ=
202+
github.com/fluxcd/pkg/auth v0.41.0/go.mod h1:U9xNHUyxOdPhxRnSW7dwloEF9EMeITxt84g8CD8YB3Q=
201203
github.com/fluxcd/pkg/cache v0.13.0 h1:MqtlgOwIVcGKKgV422e39O+KFSVMWuExKeRaMDBjJlk=
202204
github.com/fluxcd/pkg/cache v0.13.0/go.mod h1:0xRZ1hitrIFQ6pl68ke2wZLbIqA2VLzY78HpDo9DVxs=
203205
github.com/fluxcd/pkg/chartutil v1.23.0 h1:ohstQEVnrBIbN85FGu83hnmAohLl0PdOoPlsM6+cjyI=

internal/flags/source_git_provider.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
"strings"
2222

2323
"github.com/fluxcd/flux2/v2/internal/utils"
24+
"github.com/fluxcd/pkg/auth/aws"
25+
"github.com/fluxcd/pkg/auth/azure"
2426
sourcev1 "github.com/fluxcd/source-controller/api/v1"
2527
)
2628

2729
var supportedSourceGitProviders = []string{
2830
sourcev1.GitProviderGeneric,
29-
sourcev1.GitProviderAzure,
3031
sourcev1.GitProviderGitHub,
32+
aws.ProviderName,
33+
azure.ProviderName,
3134
}
3235

3336
type SourceGitProvider string

pkg/manifestgen/sync/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Options struct {
3333
TargetPath string
3434
ManifestFile string
3535
RecurseSubmodules bool
36+
Provider string
3637
}
3738

3839
func MakeDefaultOptions() Options {

pkg/manifestgen/sync/sync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func Generate(options Options) (*manifestgen.Manifest, error) {
6868
Name: options.Secret,
6969
},
7070
RecurseSubmodules: options.RecurseSubmodules,
71+
Provider: options.Provider,
7172
},
7273
}
7374

0 commit comments

Comments
 (0)