Skip to content

Commit

Permalink
Add original user/group as extra in impersonate for virtual workspace
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <[email protected]>
  • Loading branch information
turkenh committed Aug 21, 2024
1 parent 9d45e6d commit dd572dd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/virtual/apiexport/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ import (
kcpinformers "github.com/kcp-dev/kcp/sdk/client/informers/externalversions"
)

const VirtualWorkspaceName string = "apiexport"
const (
// VirtualWorkspaceName is the name of the virtual workspace.
VirtualWorkspaceName string = "apiexport"
// OriginalUserAnnotationKey is the key used in a user's "extra" to
// specify the original user of the authenticating request.
OriginalUserAnnotationKey = "authorization.kcp.io/original-username"
// OriginalGroupsAnnotationKey is the key used in a user's "extra" to
// specify the original groups of the authenticating request.
OriginalGroupsAnnotationKey = "authorization.kcp.io/original-groups"
)

func BuildVirtualWorkspace(
rootPathPrefix string,
Expand Down Expand Up @@ -113,6 +122,15 @@ func BuildVirtualWorkspace(
serviceaccount.ClusterNameKey: {cluster.Name.Path().String()},
},
}

if user, ok := genericapirequest.UserFrom(ctx); ok {
// We pass the original user and groups as extra fields to
// the impersonation config so that the receiver can make
// decisions based on the original user/groups.
impersonationConfig.Impersonate.Extra[OriginalUserAnnotationKey] = []string{user.GetName()}
impersonationConfig.Impersonate.Extra[OriginalGroupsAnnotationKey] = user.GetGroups()
}

impersonatedClient, err := kcpdynamic.NewForConfig(impersonationConfig)
if err != nil {
return nil, fmt.Errorf("error generating dynamic client: %w", err)
Expand Down

0 comments on commit dd572dd

Please sign in to comment.