Skip to content

Commit 89399ad

Browse files
authored
Merge pull request #2035 from taraspos/taras/aws-codecommit
feat: AWS CodeCommit support
2 parents 380e9de + 923b906 commit 89399ad

6 files changed

Lines changed: 98 additions & 13 deletions

File tree

api/v1/gitrepository_types.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const (
3939
// GitProviderGitHub provides support for authentication to git
4040
// repositories using GitHub App authentication
4141
GitProviderGitHub string = "github"
42+
43+
// GitProviderAWS provides support for authentication to AWS CodeCommit
44+
// repositories using IAM credentials.
45+
GitProviderAWS string = "aws"
4246
)
4347

4448
const (
@@ -77,7 +81,7 @@ const (
7781

7882
// GitRepositorySpec specifies the required configuration to produce an
7983
// Artifact for a Git repository.
80-
// +kubebuilder:validation:XValidation:rule="!has(self.serviceAccountName) || (has(self.provider) && self.provider == 'azure')",message="serviceAccountName can only be set when provider is 'azure'"
84+
// +kubebuilder:validation:XValidation:rule="!has(self.serviceAccountName) || (has(self.provider) && (self.provider == 'azure' || self.provider == 'aws'))",message="serviceAccountName can only be set when provider is 'azure' or 'aws'"
8185
type GitRepositorySpec struct {
8286
// URL specifies the Git repository URL, it can be an HTTP/S or SSH address.
8387
// +kubebuilder:validation:Pattern="^(http|https|ssh)://.*$"
@@ -93,14 +97,14 @@ type GitRepositorySpec struct {
9397
// +optional
9498
SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"`
9599

96-
// Provider used for authentication, can be 'azure', 'github', 'generic'.
100+
// Provider used for authentication, can be 'aws', 'azure', 'github', 'generic'.
97101
// When not specified, defaults to 'generic'.
98-
// +kubebuilder:validation:Enum=generic;azure;github
102+
// +kubebuilder:validation:Enum=generic;aws;azure;github
99103
// +optional
100104
Provider string `json:"provider,omitempty"`
101105

102106
// ServiceAccountName is the name of the Kubernetes ServiceAccount used to
103-
// authenticate to the GitRepository. This field is only supported for 'azure' provider.
107+
// authenticate to the GitRepository. This field is only supported for 'azure' and 'aws' providers.
104108
// +optional
105109
ServiceAccountName string `json:"serviceAccountName,omitempty"`
106110

config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ spec:
105105
type: string
106106
provider:
107107
description: |-
108-
Provider used for authentication, can be 'azure', 'github', 'generic'.
108+
Provider used for authentication, can be 'aws', 'azure', 'github', 'generic'.
109109
When not specified, defaults to 'generic'.
110110
enum:
111111
- generic
112+
- aws
112113
- azure
113114
- github
114115
type: string
@@ -177,7 +178,7 @@ spec:
177178
serviceAccountName:
178179
description: |-
179180
ServiceAccountName is the name of the Kubernetes ServiceAccount used to
180-
authenticate to the GitRepository. This field is only supported for 'azure' provider.
181+
authenticate to the GitRepository. This field is only supported for 'azure' and 'aws' providers.
181182
type: string
182183
sparseCheckout:
183184
description: |-
@@ -242,8 +243,9 @@ spec:
242243
type: object
243244
x-kubernetes-validations:
244245
- message: serviceAccountName can only be set when provider is 'azure'
245-
rule: '!has(self.serviceAccountName) || (has(self.provider) && self.provider
246-
== ''azure'')'
246+
or 'aws'
247+
rule: '!has(self.serviceAccountName) || (has(self.provider) && (self.provider
248+
== ''azure'' || self.provider == ''aws''))'
247249
status:
248250
default:
249251
observedGeneration: -1

docs/api/v1/source.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ string
407407
</td>
408408
<td>
409409
<em>(Optional)</em>
410-
<p>Provider used for authentication, can be &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
410+
<p>Provider used for authentication, can be &lsquo;aws&rsquo;, &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
411411
When not specified, defaults to &lsquo;generic&rsquo;.</p>
412412
</td>
413413
</tr>
@@ -421,7 +421,7 @@ string
421421
<td>
422422
<em>(Optional)</em>
423423
<p>ServiceAccountName is the name of the Kubernetes ServiceAccount used to
424-
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; provider.</p>
424+
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; and &lsquo;aws&rsquo; providers.</p>
425425
</td>
426426
</tr>
427427
<tr>
@@ -2120,7 +2120,7 @@ string
21202120
</td>
21212121
<td>
21222122
<em>(Optional)</em>
2123-
<p>Provider used for authentication, can be &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
2123+
<p>Provider used for authentication, can be &lsquo;aws&rsquo;, &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
21242124
When not specified, defaults to &lsquo;generic&rsquo;.</p>
21252125
</td>
21262126
</tr>
@@ -2134,7 +2134,7 @@ string
21342134
<td>
21352135
<em>(Optional)</em>
21362136
<p>ServiceAccountName is the name of the Kubernetes ServiceAccount used to
2137-
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; provider.</p>
2137+
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; and &lsquo;aws&rsquo; providers.</p>
21382138
</td>
21392139
</tr>
21402140
<tr>

docs/spec/v1/gitrepositories.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ used for authentication purposes.
245245
Supported options are:
246246

247247
- `generic`
248+
- `aws`
248249
- `azure`
249250
- `github`
250251

@@ -254,6 +255,65 @@ mechanisms using `spec.secretRef` are used for authentication.
254255
For a complete guide on how to set up authentication for cloud providers,
255256
see the integration [docs](/flux/integrations/).
256257

258+
#### AWS
259+
260+
The `aws` provider can be used to authenticate to
261+
[AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)
262+
repositories using IAM credentials sourced from the controller runtime identity.
263+
264+
**Note:** When the `aws` provider is used, `.spec.url` must be an AWS
265+
CodeCommit HTTPS endpoint in the format
266+
`https://git-codecommit.<region>.amazonaws.com/v1/repos/<repository-name>`.
267+
For a full list of available regions and their endpoints, see the
268+
[AWS CodeCommit regions documentation](https://docs.aws.amazon.com/codecommit/latest/userguide/regions.html#regions-git).
269+
270+
##### Pre-requisites
271+
272+
- An EKS cluster with either
273+
[EKS Pod Identity](https://fluxcd.io/flux/integrations/aws/#with-eks-pod-identity)
274+
or
275+
[IAM Roles for Service Accounts (IRSA)](https://fluxcd.io/flux/integrations/aws/#with-oidc-federation)
276+
configured.
277+
- An IAM role with the `codecommit:GitPull` permission for the target
278+
repository.
279+
280+
##### Configure Flux controller
281+
282+
1. Configure authentication using your preferred [method](https://fluxcd.io/flux/integrations/aws/#authentication).
283+
284+
Example IAM role policy (see [docs](https://fluxcd.io/flux/integrations/aws/#for-amazon-codecommit) for more details):
285+
286+
```json
287+
{
288+
"Version": "2012-10-17",
289+
"Statement": [
290+
{
291+
"Effect": "Allow",
292+
"Action": "codecommit:GitPull",
293+
"Resource": "arn:aws:codecommit:<region>:<account-id>:<repository-name>"
294+
}
295+
]
296+
}
297+
```
298+
299+
2. Configure AWS CodeCommit `GitRepository`
300+
301+
```yaml
302+
---
303+
apiVersion: source.toolkit.fluxcd.io/v1
304+
kind: GitRepository
305+
metadata:
306+
name: codecommit-repository
307+
namespace: flux-system
308+
spec:
309+
interval: 5m0s
310+
provider: aws
311+
serviceAccountName: my-tenant # optional (used for object-level workload identity)
312+
url: https://git-codecommit.<region>.amazonaws.com/v1/repos/<repository-name>
313+
ref:
314+
branch: main
315+
```
316+
257317
#### Azure
258318

259319
The `azure` provider can be used to authenticate to Azure DevOps repositories

internal/controller/gitrepository_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,11 +670,13 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
670670
// Configure provider authentication if specified.
671671
var getCreds func() (*authutils.GitCredentials, error)
672672
switch provider := obj.GetProvider(); provider {
673-
case sourcev1.GitProviderAzure: // If AWS or GCP are added in the future they can be added here separated by a comma.
673+
// If other providers (GCP, etc.) are added in the future they can be added here separated by a comma.
674+
case sourcev1.GitProviderAzure, sourcev1.GitProviderAWS:
674675
getCreds = func() (*authutils.GitCredentials, error) {
675676
opts := []auth.Option{
676677
auth.WithClient(r.Client),
677678
auth.WithServiceAccountNamespace(obj.GetNamespace()),
679+
auth.WithGitURL(u),
678680
}
679681

680682
if obj.Spec.ServiceAccountName != "" {

internal/controller/gitrepository_controller_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,23 @@ func TestGitRepositoryReconciler_getAuthOpts_provider(t *testing.T) {
929929
},
930930
wantErr: auth.FeatureGateObjectLevelWorkloadIdentity,
931931
},
932+
{
933+
name: "aws provider with non codecommit URL",
934+
url: "https://github.com/org/repo.git",
935+
beforeFunc: func(obj *sourcev1.GitRepository) {
936+
obj.Spec.Provider = sourcev1.GitProviderAWS
937+
},
938+
wantErr: "failed to create provider access token for the controller",
939+
},
940+
{
941+
name: "aws provider with service account and feature gate for object-level identity disabled",
942+
url: "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo",
943+
beforeFunc: func(obj *sourcev1.GitRepository) {
944+
obj.Spec.Provider = sourcev1.GitProviderAWS
945+
obj.Spec.ServiceAccountName = "aws-sa"
946+
},
947+
wantErr: auth.FeatureGateObjectLevelWorkloadIdentity,
948+
},
932949
{
933950
name: "github provider with no secret ref",
934951
url: "https://github.com/org/repo.git",

0 commit comments

Comments
 (0)