Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 32 additions & 20 deletions pkg/authorization/authorizer/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ import (
policybindingregistry "github.com/openshift/origin/pkg/authorization/registry/policybinding"
)

type openshiftAuthorizer struct {
masterAuthorizationNamespace string
policyRegistry policyregistry.Registry
policyBindingRegistry policybindingregistry.Registry
}
type openshiftAuthorizationAttributes struct {
user authenticationapi.UserInfo
verb string
resourceKind string
namespace string
requestAttributes interface{}
}
type openshiftAuthorizationAttributeBuilder struct {
requestsToUsers *authcontext.RequestContextMap
}

type Authorizer interface {
Authorize(a AuthorizationAttributes) (allowed bool, reason string, err error)
}
Expand All @@ -49,9 +33,28 @@ type AuthorizationAttributes interface {
GetRequestAttributes() interface{}
}

type openshiftAuthorizer struct {
masterAuthorizationNamespace string
policyRegistry policyregistry.Registry
policyBindingRegistry policybindingregistry.Registry
}

func NewAuthorizer(masterAuthorizationNamespace string, policyRuleBindingRegistry policyregistry.Registry, policyBindingRegistry policybindingregistry.Registry) Authorizer {
return &openshiftAuthorizer{masterAuthorizationNamespace, policyRuleBindingRegistry, policyBindingRegistry}
}

type openshiftAuthorizationAttributes struct {
user authenticationapi.UserInfo
verb string
resourceKind string
namespace string
requestAttributes interface{}
}

type openshiftAuthorizationAttributeBuilder struct {
requestsToUsers *authcontext.RequestContextMap
}

func NewAuthorizationAttributeBuilder(requestsToUsers *authcontext.RequestContextMap) AuthorizationAttributeBuilder {
return &openshiftAuthorizationAttributeBuilder{requestsToUsers}
}
Expand Down Expand Up @@ -283,15 +286,19 @@ func (a openshiftAuthorizationAttributes) kindMatches(rule authorizationapi.Poli
func (a openshiftAuthorizationAttributes) GetUserInfo() authenticationapi.UserInfo {
return a.user
}

func (a openshiftAuthorizationAttributes) GetVerb() string {
return a.verb
}

func (a openshiftAuthorizationAttributes) GetResourceKind() string {
return a.resourceKind
}

func (a openshiftAuthorizationAttributes) GetNamespace() string {
return a.namespace
}

func (a openshiftAuthorizationAttributes) GetRequestAttributes() interface{} {
return a.requestAttributes
}
Expand Down Expand Up @@ -328,9 +335,14 @@ var specialVerbs = map[string]bool{
"watch": true,
}

var ErrNoStandardParts = errors.New("the provided URL does not match the standard API form")

// VerbAndKindAndNamespace returns verb, kind, namespace, remaining parts, error
func VerbAndKindAndNamespace(req *http.Request) (string, string, string, []string, error) {
parts := splitPath(req.URL.Path)
if len(parts) == 0 {
return "", "", "", nil, ErrNoStandardParts
}

verb := ""
switch req.Method {
Expand All @@ -348,7 +360,7 @@ func VerbAndKindAndNamespace(req *http.Request) (string, string, string, []strin
if len(parts) > 2 {
parts = parts[2:]
} else {
return "", "", "", nil, fmt.Errorf("Unable to determine kind and namespace from url, %v", req.URL)
return "", "", "", nil, ErrNoStandardParts
}
}

Expand All @@ -358,7 +370,7 @@ func VerbAndKindAndNamespace(req *http.Request) (string, string, string, []strin
if len(parts) > 2 {
parts = parts[2:]
} else {
return "", "", "", parts, fmt.Errorf("Unable to determine kind and namespace from url, %v", req.URL)
return "", "", "", parts, ErrNoStandardParts
}
}

Expand All @@ -368,7 +380,7 @@ func VerbAndKindAndNamespace(req *http.Request) (string, string, string, []strin
if len(parts) > 1 {
parts = parts[1:]
} else {
return "", "", "", parts, fmt.Errorf("Unable to determine kind and namespace from url, %v", req.URL)
return "", "", "", parts, ErrNoStandardParts
}
}

Expand All @@ -377,7 +389,7 @@ func VerbAndKindAndNamespace(req *http.Request) (string, string, string, []strin
if len(parts) < 3 {
return "", "", "", parts, fmt.Errorf("ResourceTypeAndNamespace expects a path of form /ns/{namespace}/*")
}
return verb, parts[1], parts[2], parts[2:], fmt.Errorf("Unable to determine kind and namespace from url, %v", req.URL)
return verb, parts[1], parts[2], parts[2:], ErrNoStandardParts
}

// URL forms: /{kind}/*
Expand Down
29 changes: 29 additions & 0 deletions pkg/cmd/server/kubernetes/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kubernetes

import (
"net/http/httputil"
"net/url"

kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
restful "github.com/emicklei/go-restful"
"github.com/golang/glog"
)

type ProxyConfig struct {
KubernetesAddr *url.URL
ClientConfig *kclient.Config
}

func (c *ProxyConfig) InstallAPI(container *restful.Container) []string {
transport, err := kclient.TransportFor(c.ClientConfig)
if err != nil {
glog.Fatalf("Unable to initialize the Kubernetes proxy: %v", err)
}
proxy := httputil.NewSingleHostReverseProxy(c.KubernetesAddr)
proxy.Transport = transport
container.Handle("/api/", proxy)

return []string{
"Started Kubernetes proxy at %s/api/",
}
}
47 changes: 47 additions & 0 deletions pkg/cmd/server/origin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/openshift/origin/pkg/auth/authenticator/request/headerrequest"
"github.com/openshift/origin/pkg/auth/authenticator/request/unionrequest"
"github.com/openshift/origin/pkg/auth/authenticator/token/filetoken"
authcontext "github.com/openshift/origin/pkg/auth/context"
"github.com/openshift/origin/pkg/auth/oauth/external"
"github.com/openshift/origin/pkg/auth/oauth/external/github"
"github.com/openshift/origin/pkg/auth/oauth/external/google"
Expand All @@ -38,6 +39,7 @@ import (
"github.com/openshift/origin/pkg/auth/server/login"
"github.com/openshift/origin/pkg/auth/server/session"
"github.com/openshift/origin/pkg/auth/server/tokenrequest"
userregistry "github.com/openshift/origin/pkg/user/registry/user"

"github.com/openshift/origin/pkg/auth/userregistry/identitymapper"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
Expand Down Expand Up @@ -560,3 +562,48 @@ func (redirectSuccessHandler) AuthenticationSucceeded(user api.UserInfo, then st
http.Redirect(w, req, then, http.StatusFound)
return true, nil
}

// currentUserContextFilter replaces the the last segment of the provided URL with the current user's name
func currentUserContextFilter(requestsToUsers *authcontext.RequestContextMap) restful.FilterFunction {
return func(req *restful.Request, res *restful.Response, chain *restful.FilterChain) {
name := path.Base(req.Request.URL.Path)
if name != "~" {
chain.ProcessFilter(req, res)
return
}

val, found := requestsToUsers.Get(req.Request)
if !found {
http.Error(res.ResponseWriter, "Need to be authenticated to access this method", http.StatusUnauthorized)
return
}
user, ok := val.(userregistry.Info)
if !ok {
http.Error(res.ResponseWriter, "Unable to convert internal object", http.StatusInternalServerError)
return
}

base := path.Dir(req.Request.URL.Path)
req.Request.URL.Path = path.Join(base, user.GetName())

chain.ProcessFilter(req, res)
}
}

// authenticationHandlerFilter creates a filter object that will enforce authentication directly
func authenticationHandlerFilter(handler http.Handler, authenticator authenticator.Request, requestsToUsers *authcontext.RequestContextMap) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
user, ok, err := authenticator.AuthenticateRequest(req)
if err != nil || !ok {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
return
}
glog.V(4).Infof("user %v -> %v", user, req.URL)

requestsToUsers.Set(req, user)
defer requestsToUsers.Remove(req)

handler.ServeHTTP(w, req)
})
}
Loading