Skip to content

Commit

Permalink
restructure authentication package, add noop authn
Browse files Browse the repository at this point in the history
  • Loading branch information
ideahitme committed May 29, 2017
1 parent b799213 commit 2e8d74e
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Library for quickly bootstrapping authentication/authorisation webhook for Kuber

- [x] Move `main.go` to `cmd/main.go`
- [ ] Add full integration step-by-step tutorial with casbin
- [x] Think how to restructure packages
- [x] Restructure packages
- [ ] Provide detailed readme how to extend it
- [x] Write authorization module
- [x] Complete tests
Expand Down
11 changes: 11 additions & 0 deletions authn/authenticator/noop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package authenticator

import "github.com/ideahitme/k8s-api-webhook/authn/unversioned"

// Noop is the default authenticator which only returns empty user
type Noop struct{}

// Authenticate return empty userinfo
func (n Noop) Authenticate(token string) (*unversioned.UserInfo, error) {
return nil, nil
}
10 changes: 10 additions & 0 deletions authn/authenticator/noop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package authenticator

import "testing"
import "github.com/stretchr/testify/assert"

func TestNoop(t *testing.T) {
user, err := Noop{}.Authenticate("1234")
assert.Nil(t, user, "Noop authenticator returns empty user")
assert.NoError(t, err, "Noop authenticate should not return any error")
}
27 changes: 17 additions & 10 deletions authn/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@ import (
"net/http"

"github.com/ideahitme/k8s-api-webhook/authn/authenticator"
"github.com/ideahitme/k8s-api-webhook/authn/v1beta1"
"github.com/ideahitme/k8s-api-webhook/authn/versioned"
"github.com/ideahitme/k8s-api-webhook/authn/versioned/v1beta1"
)

// AuthenticationHandler implements the webhook handler
type AuthenticationHandler struct {
authProvider authenticator.Authenticator
resConstructor ResponseConstructor
reqParser RequestParser
authenticator authenticator.Authenticator
resConstructor versioned.ResponseConstructor
reqParser versioned.RequestParser
}

// NewAuthenticationHandler returns authentication http handler
func NewAuthenticationHandler(p authenticator.Authenticator) *AuthenticationHandler {
// CreateAuthenticationHandler returns default authentication http handler
func CreateAuthenticationHandler() *AuthenticationHandler {
h := &AuthenticationHandler{
authProvider: p,
authenticator: authenticator.Noop{},
resConstructor: v1beta1.ResponseConstructor{},
reqParser: v1beta1.RequestParser{},
}

return h
}

// WithAuthenticator adds authenticator to overwrite default noop authenticator
func (h *AuthenticationHandler) WithAuthenticator(p authenticator.Authenticator) *AuthenticationHandler {
h.authenticator = p
return h
}

// WithAPIVersion specify API version to use for handling authentication requests
func (h *AuthenticationHandler) WithAPIVersion(apiVersion APIVersion) *AuthenticationHandler {
if apiVersion == V1Beta1 {
func (h *AuthenticationHandler) WithAPIVersion(apiVersion versioned.APIVersion) *AuthenticationHandler {
if apiVersion == versioned.V1Beta1 {
h.resConstructor = v1beta1.ResponseConstructor{}
h.reqParser = v1beta1.RequestParser{}
}
Expand All @@ -42,7 +49,7 @@ func (h *AuthenticationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
return
}

user, err := h.authProvider.Authenticate(token)
user, err := h.authenticator.Authenticate(token)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write(h.resConstructor.NewFailResponse())
Expand Down
16 changes: 10 additions & 6 deletions authn/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ import (

"github.com/ideahitme/k8s-api-webhook/authn/authenticator"
"github.com/ideahitme/k8s-api-webhook/authn/unversioned"
"github.com/ideahitme/k8s-api-webhook/authn/v1beta1"
"github.com/ideahitme/k8s-api-webhook/authn/versioned"
"github.com/ideahitme/k8s-api-webhook/authn/versioned/v1beta1"
"github.com/ideahitme/k8s-api-webhook/internal/testutils"
)

func TestNewAuthenticationHandler(t *testing.T) {
h := NewAuthenticationHandler(authenticator.Static{}).WithAPIVersion(V1Beta1)
assert.Equal(t, h.authProvider, authenticator.Static{})
func TestCreateAuthenticationHandler(t *testing.T) {
h := CreateAuthenticationHandler()
assert.IsType(t, h.authenticator, authenticator.Noop{})

h = h.WithAuthenticator(authenticator.Static{}).WithAPIVersion(versioned.V1Beta1)
assert.Equal(t, h.authenticator, authenticator.Static{})
}

func TestWithAPIVersion(t *testing.T) {
h := (&AuthenticationHandler{}).WithAPIVersion(V1Beta1)
h := (&AuthenticationHandler{}).WithAPIVersion(versioned.V1Beta1)
assert.Equal(t, h.reqParser, v1beta1.RequestParser{})
assert.Equal(t, h.resConstructor, v1beta1.ResponseConstructor{})

Expand All @@ -47,7 +51,7 @@ func TestServeHTTP(t *testing.T) {
assert.Nil(t, err)
failAuthenticator := errAuthenticator{invalidToken: "cause-error"}

handler := NewAuthenticationHandler(authenticator.NewAggregator(staticAuthenticator, failAuthenticator))
handler := CreateAuthenticationHandler().WithAuthenticator(authenticator.NewAggregator(staticAuthenticator, failAuthenticator))
mockServer := httptest.NewServer(handler)
defer mockServer.Close()

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion authn/definitions.go → authn/versioned/versioned.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authn
package versioned

import (
"io"
Expand Down

0 comments on commit 2e8d74e

Please sign in to comment.