-
Notifications
You must be signed in to change notification settings - Fork 10
/
filter.go
119 lines (105 loc) · 3.14 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright The HTNN Authors.
//
// 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 ext_auth
import (
"bytes"
"io"
"net/http"
"net/url"
"mosn.io/htnn/pkg/filtermanager/api"
)
func factory(c interface{}, callbacks api.FilterCallbackHandler) api.Filter {
return &filter{
callbacks: callbacks,
config: c.(*config),
}
}
type filter struct {
api.PassThroughFilter
callbacks api.FilterCallbackHandler
config *config
}
func (f *filter) check(headers api.RequestHeaderMap, data api.BufferInstance) api.ResultAction {
hs := f.config.GetHttpService()
uri := hs.GetUrl()
path, err := url.JoinPath(uri, headers.Path())
if err != nil {
api.LogWarnf("failed to join path: %v", err)
return &api.LocalResponse{Code: 503}
}
req, err := http.NewRequest(headers.Method(), path, bytes.NewReader([]byte{}))
if err != nil {
api.LogWarnf("failed to new request to ext authz server: %v", err)
return &api.LocalResponse{Code: 503}
}
req.Host = headers.Host()
authz, ok := headers.Get("authorization")
if ok {
req.Header.Set("authorization", authz)
}
headersToAdd := hs.GetAuthorizationRequest().GetHeadersToAdd()
for _, h := range headersToAdd {
// Envoy doesn't support adding multiple same name headers here,
// so we don't support it until it's needed
req.Header.Set(h.Key, h.Value)
}
if data != nil {
req.Body = io.NopCloser(bytes.NewReader(data.Bytes()))
}
rsp, err := f.config.client.Do(req)
if err != nil || rsp.StatusCode >= 500 {
if err != nil {
api.LogWarnf("failed to call ext authz server: %v", err)
} else {
api.LogWarnf("failed to call ext authz server: %s", rsp.Status)
}
code := int(hs.GetStatusOnError())
if code == 0 {
code = 403
}
return &api.LocalResponse{Code: code}
}
rsp.Body.Close()
if rsp.StatusCode != 200 {
rspHdr := rsp.Header
if f.config.headerToClientMatcher != nil {
rspHdr = http.Header{}
for k, v := range rsp.Header {
if f.config.headerToClientMatcher.Match(k) {
for _, vv := range v {
rspHdr.Add(k, vv)
}
}
}
}
return &api.LocalResponse{Code: rsp.StatusCode, Header: rspHdr}
}
if f.config.headerToUpstreamMatcher != nil {
for k, v := range rsp.Header {
if f.config.headerToUpstreamMatcher.Match(k) {
headers.Set(k, v[len(v)-1])
}
}
}
return api.Continue
}
func (f *filter) DecodeHeaders(headers api.RequestHeaderMap, endStream bool) api.ResultAction {
if f.config.GetHttpService().GetWithRequestBody() {
return api.WaitAllData
}
return f.check(headers, nil)
}
func (f *filter) DecodeRequest(headers api.RequestHeaderMap, data api.BufferInstance, trailers api.RequestTrailerMap) api.ResultAction {
return f.check(headers, data)
}