Skip to content

Commit

Permalink
context for access controls errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Koch committed Mar 18, 2021
1 parent cf5fd7e commit 3396db5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions config/request/context_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type ContextKey uint8

const (
UID ContextKey = iota
AccessControl
AccessControls
BackendName
Endpoint
Expand Down
3 changes: 2 additions & 1 deletion config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/avenga/couper/errors"
"github.com/avenga/couper/eval"
"github.com/avenga/couper/handler"
hac "github.com/avenga/couper/handler/ac"
"github.com/avenga/couper/handler/middleware"
"github.com/avenga/couper/handler/producer"
"github.com/avenga/couper/handler/transport"
Expand Down Expand Up @@ -550,7 +551,7 @@ func configureProtectedHandler(m ac.Map, errTpl *errors.Template, parentAC, hand
acList = append(acList, m[acName])
}
if len(acList) > 0 {
return handler.NewAccessControl(h, errTpl, acList...)
return hac.NewAccessControl(h, errTpl, acList...)
}
return h
}
Expand Down
6 changes: 5 additions & 1 deletion handler/access_control.go → handler/ac/access_control.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package handler
package ac

import (
"net/http"

ac "github.com/avenga/couper/accesscontrol"
"github.com/avenga/couper/config/request"
"github.com/avenga/couper/errors"
)

Expand Down Expand Up @@ -47,6 +48,9 @@ func (a *AccessControl) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
default:
code = errors.AuthorizationFailed
}
if ctx, ok := req.Context().Value(request.AccessControl).(*AccessControlContext); ok {
ctx.errors = append(ctx.errors, err)
}
}
a.errorTpl.ServeError(code).ServeHTTP(rw, req)
return
Expand Down
27 changes: 27 additions & 0 deletions handler/ac/access_control_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ac

import (
"context"

"github.com/avenga/couper/config/request"
)

type AccessControlContext struct {
errors []error
}

func NewWithContext(ctx context.Context) (context.Context, *AccessControlContext) {
octx := &AccessControlContext{}
return context.WithValue(ctx, request.AccessControl, octx), octx
}

func (o *AccessControlContext) Errors() []string {
if len(o.errors) == 0 {
return nil
}
var result []string
for _, e := range o.errors {
result = append(result, e.Error())
}
return result
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package ac

import (
"fmt"
Expand Down
8 changes: 8 additions & 0 deletions logging/access_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/avenga/couper/config/request"
"github.com/avenga/couper/errors"
"github.com/avenga/couper/handler/ac"
)

type RoundtripHandlerFunc http.HandlerFunc
Expand All @@ -35,13 +36,20 @@ func (log *AccessLog) ServeHTTP(rw http.ResponseWriter, req *http.Request, nextH
statusRecorder := NewStatusRecorder(rw)
rw = statusRecorder

oCtx, acContext := ac.NewWithContext(req.Context())
*req = *req.WithContext(oCtx)

nextHandler.ServeHTTP(rw, req)
serveDone := time.Now()

fields := Fields{
"proto": req.Proto,
}

if acErrors := acContext.Errors(); len(acErrors) > 0 {
fields["access_control"] = acErrors
}

backendName, _ := req.Context().Value(request.BackendName).(string)
if backendName == "" {
endpointName, _ := req.Context().Value(request.Endpoint).(string)
Expand Down

0 comments on commit 3396db5

Please sign in to comment.