-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.go
160 lines (132 loc) · 4.45 KB
/
plugin.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package headers
import (
"fmt"
"net/http"
"regexp"
"strings"
rrcontext "github.com/roadrunner-server/context"
"github.com/roadrunner-server/errors"
"github.com/rs/cors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
jprop "go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
)
// PluginName contains default service name.
const (
RootPluginName string = "http"
PluginName string = "headers"
)
type Configurer interface {
// UnmarshalKey takes a single key and unmarshal it into a Struct.
UnmarshalKey(name string, out any) error
// Has checks if a config section exists.
Has(name string) bool
}
// Plugin serves header files. Potentially convert into middleware?
type Plugin struct {
// server configuration (location, forbidden files etc.)
cfg *Config
prop propagation.TextMapPropagator
cors *cors.Cors
allowedOriginRegex *regexp.Regexp
}
// Init must return configure service and return true if the service has Status=enabled. Must return an error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (p *Plugin) Init(cfg Configurer) error {
const op = errors.Op("headers_plugin_init")
if !cfg.Has(RootPluginName) {
return errors.E(op, errors.Disabled)
}
if !cfg.Has(fmt.Sprintf("%s.%s", RootPluginName, PluginName)) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(fmt.Sprintf("%s.%s", RootPluginName, PluginName), &p.cfg)
if err != nil {
return errors.E(op, err)
}
p.prop = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
// Configure CORS options
if p.cfg.CORS != nil {
opts := cors.Options{
// Keep BC with previous implementation
OptionsSuccessStatus: http.StatusOK,
Debug: p.cfg.CORS.Debug,
}
if p.cfg.CORS.AllowedOrigin != "" {
// trim all spaces
p.cfg.CORS.AllowedOrigin = strings.Trim(p.cfg.CORS.AllowedOrigin, " ")
opts.AllowedOrigins = strings.Split(p.cfg.CORS.AllowedOrigin, ",")
}
// if this option is set, the content of `AllowedOrigins` is ignored
if p.cfg.CORS.AllowedOriginRegex != "" {
var err error
p.allowedOriginRegex, err = regexp.Compile(p.cfg.CORS.AllowedOriginRegex)
if err != nil {
return errors.E(op, err)
}
opts.AllowOriginFunc = func(origin string) bool {
return p.allowedOriginRegex.MatchString(origin)
}
}
if p.cfg.CORS.AllowedMethods != "" {
// trim all spaces
p.cfg.CORS.AllowedMethods = strings.Trim(p.cfg.CORS.AllowedMethods, " ")
opts.AllowedMethods = strings.Split(p.cfg.CORS.AllowedMethods, ",")
}
if p.cfg.CORS.AllowedHeaders != "" {
// trim all spaces
p.cfg.CORS.AllowedHeaders = strings.Trim(p.cfg.CORS.AllowedHeaders, " ")
opts.AllowedHeaders = strings.Split(p.cfg.CORS.AllowedHeaders, ",")
}
if p.cfg.CORS.ExposedHeaders != "" {
// trim all spaces
p.cfg.CORS.ExposedHeaders = strings.Trim(p.cfg.CORS.ExposedHeaders, " ")
opts.ExposedHeaders = strings.Split(p.cfg.CORS.ExposedHeaders, ",")
}
if p.cfg.CORS.MaxAge > 0 {
opts.MaxAge = p.cfg.CORS.MaxAge
}
opts.AllowCredentials = p.cfg.CORS.AllowCredentials
if p.cfg.CORS.OptionsSuccessStatus != 0 {
opts.OptionsSuccessStatus = p.cfg.CORS.OptionsSuccessStatus
}
p.cors = cors.New(opts)
}
return nil
}
// Middleware is HTTP plugin middleware to serve headers
func (p *Plugin) Middleware(next http.Handler) http.Handler {
// Configure CORS handler
if p.cors != nil {
next = p.cors.Handler(next)
}
// Define the http.HandlerFunc
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if val, ok := r.Context().Value(rrcontext.OtelTracerNameKey).(string); ok {
tp := trace.SpanFromContext(r.Context()).TracerProvider()
ctx, span := tp.Tracer(val, trace.WithSchemaURL(semconv.SchemaURL),
trace.WithInstrumentationVersion(otelhttp.Version())).
Start(r.Context(), PluginName, trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
// inject
p.prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
r = r.WithContext(ctx)
}
if p.cfg.Request != nil {
for k, v := range p.cfg.Request {
r.Header.Add(k, v)
}
}
if p.cfg.Response != nil {
for k, v := range p.cfg.Response {
w.Header().Set(k, v)
}
}
next.ServeHTTP(w, r)
})
}
func (p *Plugin) Name() string {
return PluginName
}