Skip to content

Commit f03a53d

Browse files
committed
[RFC-007] Implement GitHub app authentication for git repositories in IAC
- Controller change to use the GitHub authentication information specified in Git Repository's `.spec.secretRef` to create the auth options to authenticate to git repositories when the `provider` field is set to `github`, - Tests for new `github` provider field in IAC - Updated docs to use GitHub Apps for authentication in image-automation-controller. Signed-off-by: Dipti Pai <[email protected]>
1 parent 4bdd80f commit f03a53d

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

docs/spec/v1beta2/imageupdateautomations.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ patches:
257257
azure.workload.identity/use: "true"
258258
```
259259

260+
##### GitHub
261+
262+
If the provider is set to `github`, make sure the GitHub App is registered and
263+
installed with the necessary permissions and the github app secret is configured
264+
as described
265+
[here](https://fluxcd.io/flux/components/source/gitrepositories/#github).
266+
260267
### Git specification
261268

262269
`.spec.git` is a required field to specify Git configurations related to source

internal/source/git.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/client"
3333

3434
"github.com/fluxcd/pkg/auth/azure"
35+
"github.com/fluxcd/pkg/auth/github"
3536
"github.com/fluxcd/pkg/git"
3637
"github.com/fluxcd/pkg/git/gogit"
3738
sourcev1 "github.com/fluxcd/source-controller/api/v1"
@@ -181,13 +182,30 @@ func getAuthOpts(ctx context.Context, c client.Client, repo *sourcev1.GitReposit
181182
return nil, fmt.Errorf("failed to configure authentication options: %w", err)
182183
}
183184

184-
if repo.GetProvider() == sourcev1.GitProviderAzure {
185+
switch repo.GetProvider() {
186+
case sourcev1.GitProviderAzure:
185187
opts.ProviderOpts = &git.ProviderOptions{
186188
Name: sourcev1.GitProviderAzure,
187189
AzureOpts: []azure.OptFunc{
188190
azure.WithAzureDevOpsScope(),
189191
},
190192
}
193+
case sourcev1.GitProviderGitHub:
194+
// if provider is github, but secret ref is not specified
195+
if repo.Spec.SecretRef == nil {
196+
return nil, fmt.Errorf("secretRef with github app data must be specified when provider is set to github: %w", ErrInvalidSourceConfiguration)
197+
}
198+
opts.ProviderOpts = &git.ProviderOptions{
199+
Name: sourcev1.GitProviderGitHub,
200+
GitHubOpts: []github.OptFunc{
201+
github.WithAppData(data),
202+
},
203+
}
204+
default:
205+
// analyze secret, if it has github app data, perhaps provider should have been github.
206+
if appID := data[github.AppIDKey]; len(appID) != 0 {
207+
return nil, fmt.Errorf("secretRef '%s/%s' has github app data but provider is not set to github: %w", repo.GetNamespace(), repo.Spec.SecretRef.Name, ErrInvalidSourceConfiguration)
208+
}
191209
}
192210

193211
return opts, nil

internal/source/git_test.go

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package source
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"testing"
2324
"time"
@@ -34,6 +35,7 @@ import (
3435
imagev1 "github.com/fluxcd/image-automation-controller/api/v1beta2"
3536
"github.com/fluxcd/image-automation-controller/internal/testutil"
3637
"github.com/fluxcd/pkg/apis/meta"
38+
"github.com/fluxcd/pkg/auth/github"
3739
"github.com/fluxcd/pkg/git"
3840
sourcev1 "github.com/fluxcd/source-controller/api/v1"
3941
)
@@ -141,49 +143,127 @@ func Test_getAuthOpts(t *testing.T) {
141143
func Test_getAuthOpts_providerAuth(t *testing.T) {
142144
tests := []struct {
143145
name string
146+
url string
147+
secret *corev1.Secret
144148
beforeFunc func(obj *sourcev1.GitRepository)
145149
wantProviderOptsName string
150+
wantErr error
146151
}{
147152
{
148153
name: "azure provider",
154+
url: "https://dev.azure.com/foo/bar/_git/baz",
149155
beforeFunc: func(obj *sourcev1.GitRepository) {
150156
obj.Spec.Provider = sourcev1.GitProviderAzure
151157
},
152158
wantProviderOptsName: sourcev1.GitProviderAzure,
153159
},
160+
{
161+
name: "github provider with no secret ref",
162+
url: "https://github.com/org/repo.git",
163+
beforeFunc: func(obj *sourcev1.GitRepository) {
164+
obj.Spec.Provider = sourcev1.GitProviderGitHub
165+
},
166+
wantProviderOptsName: sourcev1.GitProviderGitHub,
167+
wantErr: errors.New("secretRef with github app data must be specified when provider is set to github: invalid source configuration"),
168+
},
169+
{
170+
name: "github provider with secret ref that does not exist",
171+
url: "https://github.com/org/repo.git",
172+
beforeFunc: func(obj *sourcev1.GitRepository) {
173+
obj.Spec.Provider = sourcev1.GitProviderGitHub
174+
obj.Spec.SecretRef = &meta.LocalObjectReference{
175+
Name: "githubAppSecret",
176+
}
177+
},
178+
wantErr: errors.New("failed to get auth secret '/githubAppSecret': secrets \"githubAppSecret\" not found"),
179+
},
180+
{
181+
name: "github provider with github app data in secret",
182+
url: "https://example.com/org/repo",
183+
secret: &corev1.Secret{
184+
ObjectMeta: metav1.ObjectMeta{
185+
Name: "githubAppSecret",
186+
},
187+
Data: map[string][]byte{
188+
github.AppIDKey: []byte("123"),
189+
github.AppInstallationIDKey: []byte("456"),
190+
github.AppPrivateKey: []byte("abc"),
191+
},
192+
},
193+
beforeFunc: func(obj *sourcev1.GitRepository) {
194+
obj.Spec.Provider = sourcev1.GitProviderGitHub
195+
obj.Spec.SecretRef = &meta.LocalObjectReference{
196+
Name: "githubAppSecret",
197+
}
198+
},
199+
wantProviderOptsName: sourcev1.GitProviderGitHub,
200+
},
201+
{
202+
name: "generic provider with github app data in secret",
203+
url: "https://example.com/org/repo",
204+
secret: &corev1.Secret{
205+
ObjectMeta: metav1.ObjectMeta{
206+
Name: "githubAppSecret",
207+
},
208+
Data: map[string][]byte{
209+
github.AppIDKey: []byte("123"),
210+
},
211+
},
212+
beforeFunc: func(obj *sourcev1.GitRepository) {
213+
obj.Spec.Provider = sourcev1.GitProviderGeneric
214+
obj.Spec.SecretRef = &meta.LocalObjectReference{
215+
Name: "githubAppSecret",
216+
}
217+
},
218+
wantErr: errors.New("secretRef '/githubAppSecret' has github app data but provider is not set to github: invalid source configuration"),
219+
},
154220
{
155221
name: "generic provider",
222+
url: "https://example.com/org/repo",
156223
beforeFunc: func(obj *sourcev1.GitRepository) {
157224
obj.Spec.Provider = sourcev1.GitProviderGeneric
158225
},
159226
},
160227
{
161228
name: "no provider",
229+
url: "https://example.com/org/repo",
162230
},
163231
}
164232

165233
for _, tt := range tests {
166234
t.Run(tt.name, func(t *testing.T) {
167235
g := NewWithT(t)
236+
clientBuilder := fakeclient.NewClientBuilder().
237+
WithScheme(scheme.Scheme).
238+
WithStatusSubresource(&sourcev1.GitRepository{})
168239

240+
if tt.secret != nil {
241+
clientBuilder.WithObjects(tt.secret)
242+
}
243+
c := clientBuilder.Build()
169244
obj := &sourcev1.GitRepository{
170245
Spec: sourcev1.GitRepositorySpec{
171-
URL: "https://dev.azure.com/foo/bar/_git/baz",
246+
URL: tt.url,
172247
},
173248
}
174249

175250
if tt.beforeFunc != nil {
176251
tt.beforeFunc(obj)
177252
}
178-
opts, err := getAuthOpts(context.TODO(), nil, obj)
253+
opts, err := getAuthOpts(context.TODO(), c, obj)
179254

180-
g.Expect(err).ToNot(HaveOccurred())
181-
g.Expect(opts).ToNot(BeNil())
182-
if tt.wantProviderOptsName != "" {
183-
g.Expect(opts.ProviderOpts).ToNot(BeNil())
184-
g.Expect(opts.ProviderOpts.Name).To(Equal(tt.wantProviderOptsName))
255+
if tt.wantErr != nil {
256+
g.Expect(err).To(HaveOccurred())
257+
g.Expect(err.Error()).To(ContainSubstring(tt.wantErr.Error()))
185258
} else {
186-
g.Expect(opts.ProviderOpts).To(BeNil())
259+
g.Expect(err).ToNot(HaveOccurred())
260+
g.Expect(opts).ToNot(BeNil())
261+
if tt.wantProviderOptsName != "" {
262+
g.Expect(opts.ProviderOpts).ToNot(BeNil())
263+
g.Expect(opts.ProviderOpts.Name).To(Equal(tt.wantProviderOptsName))
264+
} else {
265+
g.Expect(opts.ProviderOpts).To(BeNil())
266+
}
187267
}
188268
})
189269
}

0 commit comments

Comments
 (0)