Skip to content

Commit 0f1187d

Browse files
fix: impresonation regression for service accounts
Signed-off-by: Radek Gruchalski <[email protected]>
1 parent 77b11ad commit 0f1187d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

internal/request/http.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,18 @@ 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

19+
var defaultServiceAccountGroups = []string{
20+
serviceaccount.AllServiceAccountsGroup,
21+
user.AllAuthenticated}
22+
23+
func GetDefaultServiceAccountGroups() []string {
24+
return defaultServiceAccountGroups
25+
}
26+
1827
type http struct {
1928
*h.Request
2029
authTypes []AuthType
@@ -34,6 +43,7 @@ func (h http) GetHTTPRequest() *h.Request {
3443

3544
//nolint:funlen
3645
func (h http) GetUserAndGroups() (username string, groups []string, err error) {
46+
3747
for _, fn := range h.authenticationFns() {
3848
// User authentication data is extracted according to the preferred order:
3949
// in case of first match blocking the iteration
@@ -106,9 +116,10 @@ func (h http) GetUserAndGroups() (username string, groups []string, err error) {
106116
// by appending the expected service account groups:
107117
// - system:serviceaccounts:<namespace>
108118
// - system:serviceaccounts
119+
// - system:authenticated
109120
if namespace, _, err := serviceaccount.SplitUsername(username); err == nil {
110-
groups = append(groups, serviceaccount.AllServiceAccountsGroup)
111121
groups = append(groups, fmt.Sprintf("%s%s", serviceaccount.ServiceAccountGroupPrefix, namespace))
122+
groups = append(groups, defaultServiceAccountGroups...)
112123
}
113124
}()
114125
}

internal/request/http_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
authorizationv1 "k8s.io/api/authorization/v1"
1717
"sigs.k8s.io/controller-runtime/pkg/client"
1818

19+
"k8s.io/apiserver/pkg/authentication/serviceaccount"
20+
1921
"github.com/projectcapsule/capsule-proxy/internal/request"
2022
)
2123

@@ -101,6 +103,37 @@ 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 + "ns:account"},
112+
},
113+
TLS: &tls.ConnectionState{
114+
PeerCertificates: []*x509.Certificate{
115+
{
116+
Subject: pkix.Name{
117+
CommonName: serviceaccount.ServiceAccountUsernamePrefix + "ns:account",
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+
return nil
131+
}),
132+
},
133+
wantUsername: serviceaccount.ServiceAccountUsernamePrefix + "ns:account",
134+
wantGroups: append([]string{fmt.Sprintf("%s%s", serviceaccount.ServiceAccountGroupPrefix, "ns")}, request.GetDefaultServiceAccountGroups()...),
135+
wantErr: false,
136+
},
104137
{
105138
name: "Bearer",
106139
fields: fields{

0 commit comments

Comments
 (0)