Repository navigation
Expand file tree
/
Copy pathsuite_test.go
More file actions
92 lines (79 loc) · 2.83 KB
/
Copy pathsuite_test.go
File metadata and controls
92 lines (79 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Copyright 2025 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package auth_test
import (
"context"
"crypto/tls"
"crypto/x509"
"net/http"
"testing"
"github.com/golang-jwt/jwt/v5"
. "github.com/onsi/gomega"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"github.com/fluxcd/pkg/auth/internal/tokenfile"
)
func newTestEnv(t *testing.T, ctx context.Context) (client.Client, *http.Client) {
t.Helper()
g := NewWithT(t)
// Replace the controller token file with a fake one so that tokens can be
// issued without a ServiceAccount token mounted in the test process.
controllerToken, err := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": "system:serviceaccount:flux-system:controller",
"aud": []string{"https://kubernetes.default.svc.cluster.local"},
}).SignedString([]byte("test-secret"))
g.Expect(err).NotTo(HaveOccurred())
oldReadFile := tokenfile.ReadFile
tokenfile.ReadFile = func(string) ([]byte, error) {
return []byte(controllerToken), nil
}
t.Cleanup(func() { tokenfile.ReadFile = oldReadFile })
// Create test env.
testEnv := &envtest.Environment{}
conf, err := testEnv.Start()
g.Expect(err).NotTo(HaveOccurred())
t.Cleanup(func() { testEnv.Stop() })
kubeClient, err := client.New(conf, client.Options{})
g.Expect(err).NotTo(HaveOccurred())
// Create HTTP client for OIDC verification.
clusterCAPool := x509.NewCertPool()
ok := clusterCAPool.AppendCertsFromPEM(conf.TLSClientConfig.CAData)
g.Expect(ok).To(BeTrue())
oidcClient := &http.Client{}
oidcClient.Transport = http.DefaultTransport.(*http.Transport).Clone()
oidcClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
RootCAs: clusterCAPool,
}
// Grant anonymous access to service account issuer discovery.
err = kubeClient.Create(ctx, &rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "anonymous-service-account-issuer-discovery",
},
Subjects: []rbacv1.Subject{
{
APIGroup: "rbac.authorization.k8s.io",
Kind: "User",
Name: "system:anonymous",
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "ClusterRole",
Name: "system:service-account-issuer-discovery",
},
})
g.Expect(err).NotTo(HaveOccurred())
return kubeClient, oidcClient
}