Skip to content

Commit 47a6b0a

Browse files
authored
Merge pull request #309 from Kuadrant/raw-http-authz-attrs
HTTP request attributes passed in the call for the raw HTTP authorization check
2 parents fb3835a + b413f1d commit 47a6b0a

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

pkg/service/auth.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ func (a *AuthService) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
9191
return
9292
}
9393

94-
if req.Header.Get("Content-Type") != "application/json" {
95-
closeWithStatus(envoy_type.StatusCode_BadRequest, resp, ctx, nil)
96-
return
97-
}
98-
9994
var payload []byte
10095
var err error
10196

@@ -110,13 +105,25 @@ func (a *AuthService) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
110105
}
111106

112107
metrics.ReportTimedMetric(httpServerDuration, func() {
108+
headers := make(map[string]string)
109+
for key, values := range req.Header {
110+
headers[strings.ToLower(key)] = strings.Join(values, " ")
111+
}
112+
113113
checkRequest := &envoy_auth.CheckRequest{
114114
Attributes: &envoy_auth.AttributeContext{
115115
Request: &envoy_auth.AttributeContext_Request{
116116
Http: &envoy_auth.AttributeContext_HttpRequest{
117-
Id: requestId,
118-
Host: req.Host,
119-
Body: string(payload),
117+
Id: requestId,
118+
Method: req.Method,
119+
Headers: headers,
120+
Path: path,
121+
Host: req.Host,
122+
Scheme: req.URL.Scheme,
123+
Query: req.URL.Query().Encode(),
124+
Fragment: req.URL.Fragment,
125+
Protocol: req.Proto,
126+
Body: string(payload),
120127
},
121128
},
122129
},

pkg/service/auth_test.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/kuadrant/authorino/pkg/evaluators"
1818
"github.com/kuadrant/authorino/pkg/evaluators/authorization"
1919
"github.com/kuadrant/authorino/pkg/evaluators/identity"
20+
"github.com/kuadrant/authorino/pkg/evaluators/response"
21+
"github.com/kuadrant/authorino/pkg/json"
2022

2123
envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2224
envoy_auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
@@ -200,18 +202,6 @@ func TestAuthServiceRawHTTPAuthorization_WithQueryString(t *testing.T) {
200202
assert.Equal(t, response.Code, 200)
201203
}
202204

203-
func TestAuthServiceRawHTTPAuthorization_UnsupportedContentType(t *testing.T) {
204-
mockController := gomock.NewController(t)
205-
defer mockController.Finish()
206-
cacheMock := mock_cache.NewMockCache(mockController)
207-
authService := &AuthService{Cache: cacheMock}
208-
request, _ := http.NewRequest("POST", "http://myapp.io/check", bytes.NewReader([]byte(`{}`)))
209-
request.Header = map[string][]string{"Content-Type": {"text/plain"}}
210-
response := gohttptest.NewRecorder()
211-
authService.ServeHTTP(response, request)
212-
assert.Equal(t, response.Code, 400)
213-
}
214-
215205
type notReadable struct{}
216206

217207
func (n *notReadable) Read(_ []byte) (int, error) {
@@ -230,6 +220,30 @@ func TestAuthServiceRawHTTPAuthorization_UnreadableBody(t *testing.T) {
230220
assert.Equal(t, response.Code, 400)
231221
}
232222

223+
func TestAuthServiceRawHTTPAuthorization_WithHeaders(t *testing.T) {
224+
mockController := gomock.NewController(t)
225+
defer mockController.Finish()
226+
227+
authConfig := mockAnonymousAccessAuthConfig()
228+
authConfig.ResponseConfigs = []auth.AuthConfigEvaluator{&evaluators.ResponseConfig{
229+
Name: "x-auth-data",
230+
Wrapper: "httpHeader",
231+
WrapperKey: "x-auth-data",
232+
DynamicJSON: &response.DynamicJSON{
233+
Properties: []json.JSONProperty{{Name: "headers", Value: json.JSONValue{Pattern: "context.request.http.headers"}}},
234+
},
235+
}}
236+
cacheMock := mock_cache.NewMockCache(mockController)
237+
cacheMock.EXPECT().Get("myapp.io").Return(authConfig)
238+
authService := &AuthService{Cache: cacheMock}
239+
request, _ := http.NewRequest("POST", "http://myapp.io/check", bytes.NewReader([]byte(`{}`)))
240+
request.Header = map[string][]string{"Content-Type": {"application/json"}, "Authorization": {"Bearer secret"}}
241+
response := gohttptest.NewRecorder()
242+
authService.ServeHTTP(response, request)
243+
assert.Equal(t, response.Code, 200)
244+
assert.Equal(t, response.Header().Get("X-Auth-Data"), `{"headers":{"authorization":"Bearer secret","content-type":"application/json"}}`)
245+
}
246+
233247
func TestAuthServiceRawHTTPAuthorization_K8sAdmissionReviewAuthorized(t *testing.T) {
234248
mockController := gomock.NewController(t)
235249
defer mockController.Finish()

0 commit comments

Comments
 (0)