@@ -14,6 +14,8 @@ import (
14
14
15
15
authenticationv1 "k8s.io/api/authentication/v1"
16
16
authorizationv1 "k8s.io/api/authorization/v1"
17
+ "k8s.io/apiserver/pkg/authentication/serviceaccount"
18
+ "k8s.io/apiserver/pkg/authentication/user"
17
19
"sigs.k8s.io/controller-runtime/pkg/client"
18
20
19
21
"github.com/projectcapsule/capsule-proxy/internal/request"
@@ -101,6 +103,38 @@ func Test_http_GetUserAndGroups(t *testing.T) {
101
103
wantGroups : []string {"ImpersonatedGroup" },
102
104
wantErr : false ,
103
105
},
106
+ {
107
+ name : "Certificate-ServiceAccount" ,
108
+ fields : fields {
109
+ Request : & http.Request {
110
+ Header : map [string ][]string {
111
+ authenticationv1 .ImpersonateUserHeader : {serviceaccount .ServiceAccountUsernamePrefix + testServiceAccountSuffix },
112
+ },
113
+ TLS : & tls.ConnectionState {
114
+ PeerCertificates : []* x509.Certificate {
115
+ {
116
+ Subject : pkix.Name {
117
+ CommonName : serviceaccount .ServiceAccountUsernamePrefix + testServiceAccountSuffix ,
118
+ },
119
+ },
120
+ },
121
+ },
122
+ },
123
+ authTypes : []request.AuthType {
124
+ request .BearerToken ,
125
+ request .TLSCertificate ,
126
+ },
127
+ client : testClient (func (ctx context.Context , obj client.Object ) error {
128
+ ac := obj .(* authorizationv1.SubjectAccessReview )
129
+ ac .Status .Allowed = true
130
+
131
+ return nil
132
+ }),
133
+ },
134
+ wantUsername : serviceaccount .ServiceAccountUsernamePrefix + testServiceAccountSuffix ,
135
+ wantGroups : []string {fmt .Sprintf ("%s%s" , serviceaccount .ServiceAccountGroupPrefix , "ns" ), serviceaccount .AllServiceAccountsGroup , user .AllAuthenticated },
136
+ wantErr : false ,
137
+ },
104
138
{
105
139
name : "Bearer" ,
106
140
fields : fields {
@@ -187,3 +221,5 @@ func Test_http_GetUserAndGroups(t *testing.T) {
187
221
})
188
222
}
189
223
}
224
+
225
+ const testServiceAccountSuffix = "ns:account"
0 commit comments