Skip to content

Commit 1355dbb

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 d9db01d commit 1355dbb

File tree

5 files changed

+118
-11
lines changed

5 files changed

+118
-11
lines changed

docs/spec/v1beta2/imageupdateautomations.md

+7
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

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,5 @@ require (
162162
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
163163
sigs.k8s.io/yaml v1.4.0 // indirect
164164
)
165+
166+
replace github.com/fluxcd/source-controller/api => github.com/dipti-pai/source-controller/api v0.0.0-20241209192934-58ebb1b51f43

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
5656
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5757
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
5858
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
59+
github.com/dipti-pai/source-controller/api v0.0.0-20241209192934-58ebb1b51f43 h1:f/wiXS42JPs6ibWkh3SfJvrr7UEKRdI7bbftYx5tyPo=
60+
github.com/dipti-pai/source-controller/api v0.0.0-20241209192934-58ebb1b51f43/go.mod h1:0lo5XmaerQ3tncMQKSqWwNOJo75AeXqaBCL1Qx55gX4=
5961
github.com/elazarl/goproxy v0.0.0-20240909085733-6741dbfc16a1 h1:g7YUigN4dW2+zpdusdTTghZ+5Py3BaUMAStvL8Nk+FY=
6062
github.com/elazarl/goproxy v0.0.0-20240909085733-6741dbfc16a1/go.mod h1:thX175TtLTzLj3p7N/Q9IiKZ7NF+p72cvL91emV0hzo=
6163
github.com/emicklei/go-restful/v3 v3.12.1 h1:PJMDIM/ak7btuL8Ex0iYET9hxM3CI2sjZtzpL63nKAU=
@@ -96,8 +98,6 @@ github.com/fluxcd/pkg/ssh v0.15.0 h1:RSjoFlrPPXR8ac9roV4BnfbhaqjcoJeywQ+0BO8kf6s
9698
github.com/fluxcd/pkg/ssh v0.15.0/go.mod h1:ulKcj8psZyUNJrvZZ3J748Ygh7EiS2HfdSzqoORDM4k=
9799
github.com/fluxcd/pkg/version v0.5.0 h1:td9PR7Um3CLP9ke+PgNex4yqMCRmUZv6dMK+oquD654=
98100
github.com/fluxcd/pkg/version v0.5.0/go.mod h1:ASkoZ+vz1Ob+5vb81ptUXPPNf3jjX/UQyfAeg+bnFfk=
99-
github.com/fluxcd/source-controller/api v1.4.1 h1:zV01D7xzHOXWbYXr36lXHWWYS7POARsjLt61Nbh3kVY=
100-
github.com/fluxcd/source-controller/api v1.4.1/go.mod h1:gSjg57T+IG66SsBR0aquv+DFrm4YyBNpKIJVDnu3Ya8=
101101
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
102102
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
103103
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=

internal/source/git.go

+19-1
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

+88-8
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)