-
Notifications
You must be signed in to change notification settings - Fork 351
/
response.go
84 lines (67 loc) · 2.2 KB
/
response.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
package openpolicyagent
import (
"bytes"
"io"
"net/http"
"github.com/open-policy-agent/opa-envoy-plugin/envoyauth"
"github.com/opentracing/opentracing-go"
"github.com/zalando/skipper/filters"
)
func (opa *OpenPolicyAgentInstance) ServeInvalidDecisionError(fc filters.FilterContext, span opentracing.Span, result *envoyauth.EvalResult, err error) {
opa.HandleInvalidDecisionError(fc, span, result, err, true)
}
func (opa *OpenPolicyAgentInstance) HandleInvalidDecisionError(fc filters.FilterContext, span opentracing.Span, result *envoyauth.EvalResult, err error, serve bool) {
opa.HandleEvaluationError(fc, span, result, err, serve, http.StatusInternalServerError)
}
func (opa *OpenPolicyAgentInstance) HandleEvaluationError(fc filters.FilterContext, span opentracing.Span, result *envoyauth.EvalResult, err error, serve bool, status int) {
fc.Metrics().IncCounter(opa.MetricsKey("decision.err"))
span.SetTag("error", true)
if result != nil {
span.LogKV(
"event", "error",
"opa.decision_id", result.DecisionID,
"message", err.Error(),
)
opa.Logger().WithFields(map[string]interface{}{
"decision": result.Decision,
"err": err,
"decision_id": result.DecisionID,
}).Info("Rejecting request because of an invalid decision")
} else {
span.LogKV(
"event", "error",
"message", err.Error(),
)
opa.Logger().WithFields(map[string]interface{}{
"err": err,
}).Info("Rejecting request because of an error")
}
if serve {
resp := http.Response{}
resp.StatusCode = status
fc.Serve(&resp)
}
}
func (opa *OpenPolicyAgentInstance) ServeResponse(fc filters.FilterContext, span opentracing.Span, result *envoyauth.EvalResult) {
resp := http.Response{}
var err error
resp.StatusCode, err = result.GetResponseHTTPStatus()
if err != nil {
opa.ServeInvalidDecisionError(fc, span, result, err)
return
}
resp.Header, err = result.GetResponseHTTPHeaders()
if err != nil {
opa.ServeInvalidDecisionError(fc, span, result, err)
return
}
if result.HasResponseBody() {
body, err := result.GetResponseBody()
if err != nil {
opa.ServeInvalidDecisionError(fc, span, result, err)
return
}
resp.Body = io.NopCloser(bytes.NewReader([]byte(body)))
}
fc.Serve(&resp)
}