Skip to content

Commit 16deb06

Browse files
fix: impresonation regression for service accounts (#405)
Signed-off-by: Radek Gruchalski <[email protected]>
1 parent 0b04e1b commit 16deb06

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

internal/request/http.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
authenticationv1 "k8s.io/api/authentication/v1"
1313
authorizationv1 "k8s.io/api/authorization/v1"
1414
"k8s.io/apiserver/pkg/authentication/serviceaccount"
15+
"k8s.io/apiserver/pkg/authentication/user"
1516
"sigs.k8s.io/controller-runtime/pkg/client"
1617
)
1718

@@ -106,9 +107,11 @@ func (h http) GetUserAndGroups() (username string, groups []string, err error) {
106107
// by appending the expected service account groups:
107108
// - system:serviceaccounts:<namespace>
108109
// - system:serviceaccounts
110+
// - system:authenticated
109111
if namespace, _, err := serviceaccount.SplitUsername(username); err == nil {
110-
groups = append(groups, serviceaccount.AllServiceAccountsGroup)
111112
groups = append(groups, fmt.Sprintf("%s%s", serviceaccount.ServiceAccountGroupPrefix, namespace))
113+
groups = append(groups, serviceaccount.AllServiceAccountsGroup)
114+
groups = append(groups, user.AllAuthenticated)
112115
}
113116
}()
114117
}

internal/request/http_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
authenticationv1 "k8s.io/api/authentication/v1"
1616
authorizationv1 "k8s.io/api/authorization/v1"
17+
"k8s.io/apiserver/pkg/authentication/serviceaccount"
18+
"k8s.io/apiserver/pkg/authentication/user"
1719
"sigs.k8s.io/controller-runtime/pkg/client"
1820

1921
"github.com/projectcapsule/capsule-proxy/internal/request"
@@ -101,6 +103,38 @@ func Test_http_GetUserAndGroups(t *testing.T) {
101103
wantGroups: []string{"ImpersonatedGroup"},
102104
wantErr: false,
103105
},
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+
},
104138
{
105139
name: "Bearer",
106140
fields: fields{
@@ -187,3 +221,5 @@ func Test_http_GetUserAndGroups(t *testing.T) {
187221
})
188222
}
189223
}
224+
225+
const testServiceAccountSuffix = "ns:account"

0 commit comments

Comments
 (0)