Skip to content

Commit

Permalink
Add basic Authorization.
Browse files Browse the repository at this point in the history
Added basic interface for authorizer implementations.
Added default "authorize everything" and "authorize nothing
implementations.
Added authorization check immediately after authentication check.
Added an integration test of authorization at the HTTP level of
abstraction.
erictune committed Oct 31, 2014
1 parent 893291d commit 55c2d6b
Showing 9 changed files with 433 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/apiserver/apiserver.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ import (
"net"
"net/http"
"strconv"
"strings"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
@@ -63,6 +64,7 @@ var (
healthCheckMinions = flag.Bool("health_check_minions", true, "If true, health check minions and filter unhealthy ones. Default true.")
eventTTL = flag.Duration("event_ttl", 48*time.Hour, "Amount of time to retain events. Default 2 days.")
tokenAuthFile = flag.String("token_auth_file", "", "If set, the file that will be used to secure the API server via token authentication.")
authorizationMode = flag.String("authorization_mode", "AlwaysAllow", "Selects how to do authorization. One of: "+strings.Join(apiserver.AuthorizationModeChoices, ","))
etcdServerList util.StringList
etcdConfigFile = flag.String("etcd_config", "", "The config file for the etcd client. Mutually exclusive with -etcd_servers.")
corsAllowedOriginList util.StringList
@@ -159,6 +161,7 @@ func main() {
ReadOnlyPort: *readOnlyPort,
ReadWritePort: *port,
PublicAddress: *publicAddressOverride,
AuthorizationMode: *authorizationMode,
}
m := master.New(config)

1 change: 1 addition & 0 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
@@ -146,6 +146,7 @@ func startComponents(manifestURL string) (apiServerURL string) {
KubeletClient: fakeKubeletClient{},
EnableLogsSupport: false,
APIPrefix: "/api",
AuthorizationMode: "AlwaysAllow",

ReadWritePort: portNumber,
ReadOnlyPort: portNumber,
2 changes: 1 addition & 1 deletion hack/test-integration.sh
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ start_etcd
echo ""
echo "Integration test cases..."
echo ""
GOFLAGS="-tags 'integration no-docker'" \
GOFLAGS="-tags 'integration no-docker' -test.v" \
"${KUBE_ROOT}/hack/test-go.sh" test/integration

echo ""
68 changes: 68 additions & 0 deletions pkg/apiserver/authz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apiserver

import (
"errors"

"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/authorizer"
)

// Attributes implements authorizer.Attributes interface.
type Attributes struct {
// TODO: add fields and methods when authorizer.Attributes is completed.
}

// alwaysAllowAuthorizer is an implementation of authorizer.Attributes
// which always says yes to an authorization request.
// It is useful in tests and when using kubernetes in an open manner.
type alwaysAllowAuthorizer struct{}

func (alwaysAllowAuthorizer) Authorize(a authorizer.Attributes) (err error) {
return nil
}

// alwaysDenyAuthorizer is an implementation of authorizer.Attributes
// which always says no to an authorization request.
// It is useful in unit tests to force an operation to be forbidden.
type alwaysDenyAuthorizer struct{}

func (alwaysDenyAuthorizer) Authorize(a authorizer.Attributes) (err error) {
return errors.New("Everything is forbidden.")
}

const (
ModeAlwaysAllow string = "AlwaysAllow"
ModeAlwaysDeny string = "AlwaysDeny"
)

// Keep this list in sync with constant list above.
var AuthorizationModeChoices = []string{ModeAlwaysAllow, ModeAlwaysDeny}

// NewAuthorizerFromAuthorizationConfig returns the right sort of authorizer.Authorizer
// based on the authorizationMode xor an error. authorizationMode should be one of AuthorizationModeChoices.
func NewAuthorizerFromAuthorizationConfig(authorizationMode string) (authorizer.Authorizer, error) {
// Keep cases in sync with constant list above.
switch authorizationMode {
case ModeAlwaysAllow:
return new(alwaysAllowAuthorizer), nil
case ModeAlwaysDeny:
return new(alwaysDenyAuthorizer), nil
default:
return nil, errors.New("Unknown authorization mode")
}
}
22 changes: 22 additions & 0 deletions pkg/apiserver/handlers.go
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ import (
"runtime/debug"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/authorizer"
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/golang/glog"
@@ -118,3 +119,24 @@ func CORS(handler http.Handler, allowedOriginPatterns []*regexp.Regexp, allowedM
handler.ServeHTTP(w, req)
})
}

// RequestAttributeGetter is a function that extracts authorizer.Attributes from an http.Request
type RequestAttributeGetter func(req *http.Request) (attribs authorizer.Attributes)

// BasicAttributeGetter gets authorizer.Attributes from an http.Request.
func BasicAttributeGetter(req *http.Request) (attribs authorizer.Attributes) {
// TODO: fill in attributes once attributes are defined.
return
}

// WithAuthorizationCheck passes all authorized requests on to handler, and returns a forbidden error otherwise.
func WithAuthorizationCheck(handler http.Handler, getAttribs RequestAttributeGetter, a authorizer.Authorizer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
err := a.Authorize(getAttribs(req))
if err == nil {
handler.ServeHTTP(w, req)
return
}
forbidden(w, req)
})
}
30 changes: 30 additions & 0 deletions pkg/auth/authorizer/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package authorizer

// Attributes is an interface used by an Authorizer to get information about a request
// that is used to make an authorization decision.
type Attributes interface {
// TODO: add attribute getter functions, e.g. GetUserName(), per #1430.
}

// Authorizer makes an authorization decision based on information gained by making
// zero or more calls to methods of the Attributes interface. It returns nil when an action is
// authorized, otherwise it returns an error.
type Authorizer interface {
Authorize(a Attributes) (err error)
}
18 changes: 15 additions & 3 deletions pkg/master/master.go
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ type Config struct {
APIPrefix string
CorsAllowedOriginList util.StringList
TokenAuthFile string
AuthorizationMode string

// Number of masters running; all masters must be started with the
// same value for this field. (Numbers > 1 currently untested.)
@@ -101,6 +102,7 @@ type Master struct {
apiPrefix string
corsAllowedOriginList util.StringList
tokenAuthFile string
authorizationzMode string
masterCount int

// "Outputs"
@@ -220,9 +222,11 @@ func New(c *Config) *Master {
apiPrefix: c.APIPrefix,
corsAllowedOriginList: c.CorsAllowedOriginList,
tokenAuthFile: c.TokenAuthFile,
masterCount: c.MasterCount,
readOnlyServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadOnlyPort))),
readWriteServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadWritePort))),
authorizationzMode: c.AuthorizationMode,

masterCount: c.MasterCount,
readOnlyServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadOnlyPort))),
readWriteServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadWritePort))),
}
m.masterServices = util.NewRunner(m.serviceWriterLoop, m.roServiceWriterLoop)
m.init(c)
@@ -310,6 +314,14 @@ func (m *Master) init(c *Config) {
handler = apiserver.CORS(handler, allowedOriginRegexps, nil, nil, "true")
}

// Install Authorizer
authorizer, err := apiserver.NewAuthorizerFromAuthorizationConfig(m.authorizationzMode)
if err != nil {
glog.Fatal(err)
}
handler = apiserver.WithAuthorizationCheck(handler, apiserver.BasicAttributeGetter, authorizer)

// Install Authenticator
if authenticator != nil {
handler = handlers.NewRequestAuthenticator(userContexts, authenticator, handlers.Unauthorized, handler)
}
Loading
Oops, something went wrong.

0 comments on commit 55c2d6b

Please sign in to comment.