Skip to content

Commit e03e539

Browse files
committed
Use the Upstream header for scope impersonation
Signed-off-by: Simo Sorce <[email protected]>
1 parent 21ddabc commit e03e539

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

pkg/cmd/server/origin/handlers.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
2626
configapi "github.com/openshift/origin/pkg/cmd/server/api"
27-
authenticationapi "github.com/openshift/origin/pkg/oauthserver/api"
2827
)
2928

3029
// cacheExcludedPaths is small and simple until the handlers include the cache headers they need
@@ -155,10 +154,12 @@ func (c *MasterConfig) versionSkewFilter(handler http.Handler, contextMapper api
155154
})
156155
}
157156

157+
const legacyImpersonateUserScopeHeader = "Impersonate-User-Scope"
158+
158159
// translateLegacyScopeImpersonation is a filter that will translates user scope impersonation for openshift into the equivalent kube headers.
159160
func translateLegacyScopeImpersonation(handler http.Handler) http.Handler {
160161
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
161-
for _, scope := range req.Header[authenticationapi.ImpersonateUserScopeHeader] {
162+
for _, scope := range req.Header[legacyImpersonateUserScopeHeader] {
162163
req.Header[authenticationv1.ImpersonateUserExtraHeaderPrefix+authorizationapi.ScopesKey] =
163164
append(req.Header[authenticationv1.ImpersonateUserExtraHeaderPrefix+authorizationapi.ScopesKey], scope)
164165
}

pkg/oauthserver/api/types.go

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ const (
1313
// This is useful when the immutable providerUserName is different than the login used to authenticate
1414
// If present, this extra value is used as the preferred username
1515
IdentityPreferredUsernameKey = "preferred_username"
16-
17-
ImpersonateUserScopeHeader = "Impersonate-User-Scope"
1816
)
1917

2018
// UserIdentityInfo contains information about an identity. Identities are distinct from users. An authentication server of

pkg/oauthserver/client/impersonate.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1414

1515
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
16-
authenticationapi "github.com/openshift/origin/pkg/oauthserver/api"
1716
)
1817

18+
const legacyImpersonateUserScopeHeader = "Impersonate-User-Scope"
19+
const impersonateUserExtraPlusScopesHeader = authenticationv1.ImpersonateUserExtraHeaderPrefix + authorizationapi.ScopesKey
20+
1921
type impersonatingRoundTripper struct {
2022
user user.Info
2123
delegate http.RoundTripper
@@ -30,14 +32,16 @@ func (rt *impersonatingRoundTripper) RoundTrip(req *http.Request) (*http.Respons
3032
req = utilnet.CloneRequest(req)
3133
req.Header.Del(authenticationv1.ImpersonateUserHeader)
3234
req.Header.Del(authenticationv1.ImpersonateGroupHeader)
33-
req.Header.Del(authenticationapi.ImpersonateUserScopeHeader)
35+
req.Header.Del(impersonateUserExtraPlusScopesHeader)
36+
// Also delete legacy scope header just in case it was set
37+
req.Header.Del(legacyImpersonateUserScopeHeader)
3438

3539
req.Header.Set(authenticationv1.ImpersonateUserHeader, rt.user.GetName())
3640
for _, group := range rt.user.GetGroups() {
3741
req.Header.Add(authenticationv1.ImpersonateGroupHeader, group)
3842
}
3943
for _, scope := range rt.user.GetExtra()[authorizationapi.ScopesKey] {
40-
req.Header.Add(authenticationapi.ImpersonateUserScopeHeader, scope)
44+
req.Header.Add(impersonateUserExtraPlusScopesHeader, scope)
4145
}
4246
return rt.delegate.RoundTrip(req)
4347
}
@@ -71,7 +75,7 @@ func NewImpersonatingRESTClient(user user.Info, client restclient.Interface) res
7175
func (c impersonatingRESTClient) impersonate(req *restclient.Request) *restclient.Request {
7276
req.SetHeader(authenticationv1.ImpersonateUserHeader, c.user.GetName())
7377
req.SetHeader(authenticationv1.ImpersonateGroupHeader, c.user.GetGroups()...)
74-
req.SetHeader(authenticationapi.ImpersonateUserScopeHeader, c.user.GetExtra()[authorizationapi.ScopesKey]...)
78+
req.SetHeader(impersonateUserExtraPlusScopesHeader, c.user.GetExtra()[authorizationapi.ScopesKey]...)
7579
return req
7680
}
7781

test/integration/scopes_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"k8s.io/client-go/rest"
1111
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
1212

13+
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
1314
"github.com/openshift/origin/pkg/authorization/authorizer/scope"
1415
buildapi "github.com/openshift/origin/pkg/build/apis/build"
1516
buildclient "github.com/openshift/origin/pkg/build/generated/internalclientset"
1617
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
1718
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
18-
authenticationapi "github.com/openshift/origin/pkg/oauthserver/api"
1919
"github.com/openshift/origin/pkg/oauthserver/oauthserver"
2020
userapi "github.com/openshift/origin/pkg/user/apis/user"
2121
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset/typed/user/internalversion"
@@ -107,7 +107,7 @@ func TestScopedImpersonation(t *testing.T) {
107107

108108
err = clusterAdminBuildClient.Build().RESTClient().Get().
109109
SetHeader(authenticationv1.ImpersonateUserHeader, "harold").
110-
SetHeader(authenticationapi.ImpersonateUserScopeHeader, "user:info").
110+
SetHeader(authenticationv1.ImpersonateUserExtraHeaderPrefix+authorizationapi.ScopesKey, "user:info").
111111
Namespace(projectName).Resource("builds").Name("name").Do().Into(&buildapi.Build{})
112112
if !kapierrors.IsForbidden(err) {
113113
t.Fatalf("unexpected error: %v", err)
@@ -116,7 +116,7 @@ func TestScopedImpersonation(t *testing.T) {
116116
user := &userapi.User{}
117117
err = userclient.NewForConfigOrDie(clusterAdminClientConfig).RESTClient().Get().
118118
SetHeader(authenticationv1.ImpersonateUserHeader, "harold").
119-
SetHeader(authenticationapi.ImpersonateUserScopeHeader, "user:info").
119+
SetHeader(authenticationv1.ImpersonateUserExtraHeaderPrefix+authorizationapi.ScopesKey, "user:info").
120120
Resource("users").Name("~").Do().Into(user)
121121
if err != nil {
122122
t.Fatalf("unexpected error: %v", err)

0 commit comments

Comments
 (0)