Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions api/v1/gitrepository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
// GitProviderGitHub provides support for authentication to git
// repositories using GitHub App authentication
GitProviderGitHub string = "github"

// GitProviderAWS provides support for authentication to AWS CodeCommit
// repositories using IAM credentials.
GitProviderAWS string = "aws"
)

const (
Expand Down Expand Up @@ -77,7 +81,7 @@ const (

// GitRepositorySpec specifies the required configuration to produce an
// Artifact for a Git repository.
// +kubebuilder:validation:XValidation:rule="!has(self.serviceAccountName) || (has(self.provider) && self.provider == 'azure')",message="serviceAccountName can only be set when provider is 'azure'"
// +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'"
type GitRepositorySpec struct {
// URL specifies the Git repository URL, it can be an HTTP/S or SSH address.
// +kubebuilder:validation:Pattern="^(http|https|ssh)://.*$"
Expand All @@ -93,14 +97,14 @@ type GitRepositorySpec struct {
// +optional
SecretRef *meta.LocalObjectReference `json:"secretRef,omitempty"`

// Provider used for authentication, can be 'azure', 'github', 'generic'.
// Provider used for authentication, can be 'aws', 'azure', 'github', 'generic'.
// When not specified, defaults to 'generic'.
// +kubebuilder:validation:Enum=generic;azure;github
// +kubebuilder:validation:Enum=generic;aws;azure;github
// +optional
Provider string `json:"provider,omitempty"`

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

Expand Down
10 changes: 6 additions & 4 deletions config/crd/bases/source.toolkit.fluxcd.io_gitrepositories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ spec:
type: string
provider:
description: |-
Provider used for authentication, can be 'azure', 'github', 'generic'.
Provider used for authentication, can be 'aws', 'azure', 'github', 'generic'.
When not specified, defaults to 'generic'.
enum:
- generic
- aws
- azure
- github
type: string
Expand Down Expand Up @@ -177,7 +178,7 @@ spec:
serviceAccountName:
description: |-
ServiceAccountName is the name of the Kubernetes ServiceAccount used to
authenticate to the GitRepository. This field is only supported for 'azure' provider.
authenticate to the GitRepository. This field is only supported for 'azure' and 'aws' providers.
type: string
sparseCheckout:
description: |-
Expand Down Expand Up @@ -242,8 +243,9 @@ spec:
type: object
x-kubernetes-validations:
- message: serviceAccountName can only be set when provider is 'azure'
rule: '!has(self.serviceAccountName) || (has(self.provider) && self.provider
== ''azure'')'
or 'aws'
rule: '!has(self.serviceAccountName) || (has(self.provider) && (self.provider
== ''azure'' || self.provider == ''aws''))'
status:
default:
observedGeneration: -1
Expand Down
8 changes: 4 additions & 4 deletions docs/api/v1/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ string
</td>
<td>
<em>(Optional)</em>
<p>Provider used for authentication, can be &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
<p>Provider used for authentication, can be &lsquo;aws&rsquo;, &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
When not specified, defaults to &lsquo;generic&rsquo;.</p>
</td>
</tr>
Expand All @@ -421,7 +421,7 @@ string
<td>
<em>(Optional)</em>
<p>ServiceAccountName is the name of the Kubernetes ServiceAccount used to
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; provider.</p>
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; and &lsquo;aws&rsquo; providers.</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -2120,7 +2120,7 @@ string
</td>
<td>
<em>(Optional)</em>
<p>Provider used for authentication, can be &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
<p>Provider used for authentication, can be &lsquo;aws&rsquo;, &lsquo;azure&rsquo;, &lsquo;github&rsquo;, &lsquo;generic&rsquo;.
When not specified, defaults to &lsquo;generic&rsquo;.</p>
</td>
</tr>
Expand All @@ -2134,7 +2134,7 @@ string
<td>
<em>(Optional)</em>
<p>ServiceAccountName is the name of the Kubernetes ServiceAccount used to
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; provider.</p>
authenticate to the GitRepository. This field is only supported for &lsquo;azure&rsquo; and &lsquo;aws&rsquo; providers.</p>
</td>
</tr>
<tr>
Expand Down
60 changes: 60 additions & 0 deletions docs/spec/v1/gitrepositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ used for authentication purposes.
Supported options are:

- `generic`
- `aws`
- `azure`
- `github`

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

#### AWS
Comment thread
matheuscscp marked this conversation as resolved.

The `aws` provider can be used to authenticate to
[AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)
repositories using IAM credentials sourced from the controller runtime identity.

**Note:** When the `aws` provider is used, `.spec.url` must be an AWS
CodeCommit HTTPS endpoint in the format
`https://git-codecommit.<region>.amazonaws.com/v1/repos/<repository-name>`.
For a full list of available regions and their endpoints, see the
[AWS CodeCommit regions documentation](https://docs.aws.amazon.com/codecommit/latest/userguide/regions.html#regions-git).

##### Pre-requisites

- An EKS cluster with either
[EKS Pod Identity](https://fluxcd.io/flux/integrations/aws/#with-eks-pod-identity)
or
[IAM Roles for Service Accounts (IRSA)](https://fluxcd.io/flux/integrations/aws/#with-oidc-federation)
configured.
- An IAM role with the `codecommit:GitPull` permission for the target
repository.

##### Configure Flux controller

1. Configure authentication using your preferred [method](https://fluxcd.io/flux/integrations/aws/#authentication).

Example IAM role policy (see [docs](https://fluxcd.io/flux/integrations/aws/#for-amazon-codecommit) for more details):

```json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "codecommit:GitPull",
"Resource": "arn:aws:codecommit:<region>:<account-id>:<repository-name>"
}
]
}
```

2. Configure AWS CodeCommit `GitRepository`

```yaml
---
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
name: codecommit-repository
namespace: flux-system
spec:
interval: 5m0s
provider: aws
serviceAccountName: my-tenant # optional (used for object-level workload identity)
url: https://git-codecommit.<region>.amazonaws.com/v1/repos/<repository-name>
Comment thread
taraspos marked this conversation as resolved.
ref:
branch: main
```

#### Azure

The `azure` provider can be used to authenticate to Azure DevOps repositories
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,13 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
// Configure provider authentication if specified.
var getCreds func() (*authutils.GitCredentials, error)
switch provider := obj.GetProvider(); provider {
case sourcev1.GitProviderAzure: // If AWS or GCP are added in the future they can be added here separated by a comma.
// If other providers (GCP, etc.) are added in the future they can be added here separated by a comma.
case sourcev1.GitProviderAzure, sourcev1.GitProviderAWS:
getCreds = func() (*authutils.GitCredentials, error) {
opts := []auth.Option{
auth.WithClient(r.Client),
auth.WithServiceAccountNamespace(obj.GetNamespace()),
auth.WithGitURL(u),
Comment thread
taraspos marked this conversation as resolved.
}

if obj.Spec.ServiceAccountName != "" {
Expand Down
17 changes: 17 additions & 0 deletions internal/controller/gitrepository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,23 @@ func TestGitRepositoryReconciler_getAuthOpts_provider(t *testing.T) {
},
wantErr: auth.FeatureGateObjectLevelWorkloadIdentity,
},
{
name: "aws provider with non codecommit URL",
url: "https://github.com/org/repo.git",
beforeFunc: func(obj *sourcev1.GitRepository) {
obj.Spec.Provider = sourcev1.GitProviderAWS
},
wantErr: "failed to create provider access token for the controller",
},
{
name: "aws provider with service account and feature gate for object-level identity disabled",
url: "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo",
beforeFunc: func(obj *sourcev1.GitRepository) {
obj.Spec.Provider = sourcev1.GitProviderAWS
obj.Spec.ServiceAccountName = "aws-sa"
},
wantErr: auth.FeatureGateObjectLevelWorkloadIdentity,
},
{
name: "github provider with no secret ref",
url: "https://github.com/org/repo.git",
Expand Down
Loading