Skip to content

Commit bba3179

Browse files
committed
gitrepo: add support for specifying CA data via ca.crt
Check the auth secret for the `ca.crt` key for CA certificate data. `ca.crt` takes precdence over `caFile`. Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 69e7be8 commit bba3179

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

docs/spec/v1/gitrepositories.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ data:
161161
#### HTTPS Certificate Authority
162162

163163
To provide a Certificate Authority to trust while connecting with a Git
164-
repository over HTTPS, the referenced Secret can contain a `.data.caFile`
165-
value.
164+
repository over HTTPS, the referenced Secret's `.data` can contain a `ca.crt`
165+
or `caFile` key. `ca.crt` takes precedence over `caFile`, i.e. if both keys
166+
are present, the value of `ca.crt` will be taken into consideration.
166167

167168
```yaml
168169
---
@@ -173,7 +174,7 @@ metadata:
173174
namespace: default
174175
type: Opaque
175176
data:
176-
caFile: <BASE64>
177+
ca.crt: <BASE64>
177178
```
178179

179180
#### SSH authentication

internal/controller/gitrepository_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,13 @@ func (r *GitRepositoryReconciler) getAuthOpts(ctx context.Context, obj *sourcev1
646646
if err != nil {
647647
return nil, err
648648
}
649+
650+
// `git.NewAuthOptions()` populates the CA cert data by checking for the `caFile` key.
651+
// Since, `ca.crt` takes precedence, check for its presence here and override the CA
652+
// certificate, if found.
653+
if ca, ok := authData["ca.crt"]; ok {
654+
authOpts.CAFile = ca
655+
}
649656
return authOpts, nil
650657
}
651658

internal/controller/gitrepository_controller_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,32 @@ func TestGitRepositoryReconciler_reconcileSource_authStrategy(t *testing.T) {
386386
*conditions.UnknownCondition(meta.ReadyCondition, meta.ProgressingReason, "building artifact: new upstream revision 'master@sha1:<commit>'"),
387387
},
388388
},
389+
{
390+
name: "HTTPS with CAFile secret with both ca.crt and caFile keys makes Reconciling=True and ignores caFile",
391+
protocol: "https",
392+
server: options{
393+
publicKey: tlsPublicKey,
394+
privateKey: tlsPrivateKey,
395+
ca: tlsCA,
396+
},
397+
secret: &corev1.Secret{
398+
ObjectMeta: metav1.ObjectMeta{
399+
Name: "ca-file",
400+
},
401+
Data: map[string][]byte{
402+
"ca.crt": tlsCA,
403+
"caFile": []byte("invalid"),
404+
},
405+
},
406+
beforeFunc: func(obj *sourcev1.GitRepository) {
407+
obj.Spec.SecretRef = &meta.LocalObjectReference{Name: "ca-file"}
408+
},
409+
want: sreconcile.ResultSuccess,
410+
assertConditions: []metav1.Condition{
411+
*conditions.TrueCondition(meta.ReconcilingCondition, meta.ProgressingReason, "building artifact: new upstream revision 'master@sha1:<commit>'"),
412+
*conditions.UnknownCondition(meta.ReadyCondition, meta.ProgressingReason, "building artifact: new upstream revision 'master@sha1:<commit>'"),
413+
},
414+
},
389415
{
390416
name: "HTTPS with invalid CAFile secret makes CheckoutFailed=True and returns error",
391417
protocol: "https",

0 commit comments

Comments
 (0)