Skip to content

Commit 7ed3de3

Browse files
authored
Merge pull request #3155 from turkenh/impersonate-with-original
✨ Add original user/group as extra to the impersonating client used by virtual workspace
2 parents 4ac09e8 + a6b9309 commit 7ed3de3

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

pkg/virtual/apiexport/builder/build.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ import (
5252
kcpinformers "github.com/kcp-dev/kcp/sdk/client/informers/externalversions"
5353
)
5454

55-
const VirtualWorkspaceName string = "apiexport"
55+
const (
56+
// VirtualWorkspaceName is the name of the virtual workspace.
57+
VirtualWorkspaceName string = "apiexport"
58+
// OriginalUserAnnotationKey is the key used in a user's "extra" to
59+
// specify the original user of the authenticating request.
60+
OriginalUserAnnotationKey = "experimental.authorization.kcp.io/original-username"
61+
// OriginalGroupsAnnotationKey is the key used in a user's "extra" to
62+
// specify the original groups of the authenticating request.
63+
OriginalGroupsAnnotationKey = "experimental.authorization.kcp.io/original-groups"
64+
)
5665

5766
func BuildVirtualWorkspace(
5867
rootPathPrefix string,
@@ -113,6 +122,15 @@ func BuildVirtualWorkspace(
113122
serviceaccount.ClusterNameKey: {cluster.Name.Path().String()},
114123
},
115124
}
125+
126+
if user, ok := genericapirequest.UserFrom(ctx); ok {
127+
// We pass the original user and groups as extra fields to
128+
// the impersonation config so that the receiver can make
129+
// decisions based on the original user/groups.
130+
impersonationConfig.Impersonate.Extra[OriginalUserAnnotationKey] = []string{user.GetName()}
131+
impersonationConfig.Impersonate.Extra[OriginalGroupsAnnotationKey] = user.GetGroups()
132+
}
133+
116134
impersonatedClient, err := kcpdynamic.NewForConfig(impersonationConfig)
117135
if err != nil {
118136
return nil, fmt.Errorf("error generating dynamic client: %w", err)

0 commit comments

Comments
 (0)