forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
116 lines (95 loc) · 2.86 KB
/
config.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
package http
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/authorizer"
"github.com/influxdata/influxdb/v2/kit/cli"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const prefixConfig = "/api/v2/config"
func errInvalidType(dest interface{}, flag string) error {
return &errors.Error{
Code: errors.EInternal,
Err: fmt.Errorf("unknown destination type %T for %q", dest, flag),
}
}
type parsedOpt map[string]optValue
type optValue []byte
func (o optValue) MarshalJSON() ([]byte, error) { return o, nil }
type ConfigHandler struct {
chi.Router
log *zap.Logger
api *kithttp.API
config parsedOpt
}
// NewConfigHandler creates a handler that will return a JSON object with key/value pairs for the configuration values
// used during the launcher startup. The opts slice provides a list of options names along with a pointer to their
// value.
func NewConfigHandler(log *zap.Logger, opts []cli.Opt) (*ConfigHandler, error) {
h := &ConfigHandler{
log: log,
api: kithttp.NewAPI(kithttp.WithLog(log)),
}
if err := h.parseOptions(opts); err != nil {
return nil, err
}
r := chi.NewRouter()
r.Use(
middleware.Recoverer,
middleware.RequestID,
middleware.RealIP,
h.mwAuthorize,
)
r.Get("/", h.handleGetConfig)
h.Router = r
return h, nil
}
func (h *ConfigHandler) Prefix() string {
return prefixConfig
}
func (h *ConfigHandler) parseOptions(opts []cli.Opt) error {
h.config = make(parsedOpt)
for _, o := range opts {
var b []byte
switch o.DestP.(type) {
// Known types for configuration values. Currently, these can all be encoded directly with json.Marshal.
case *string, *int, *int32, *int64, *bool, *time.Duration, *[]string, *map[string]string, pflag.Value, *platform.ID, *zapcore.Level:
var err error
b, err = json.Marshal(o.DestP)
if err != nil {
return err
}
default:
// Return an error if we don't know how to marshal this type.
return errInvalidType(o.DestP, o.Flag)
}
h.config[o.Flag] = b
}
return nil
}
func (h *ConfigHandler) handleGetConfig(w http.ResponseWriter, r *http.Request) {
h.api.Respond(w, r, http.StatusOK, map[string]parsedOpt{"config": h.config})
}
func (h *ConfigHandler) mwAuthorize(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if err := authorizer.IsAllowedAll(r.Context(), influxdb.OperPermissions()); err != nil {
h.api.Err(w, r, &errors.Error{
Code: errors.EUnauthorized,
Msg: fmt.Sprintf("access to %s requires operator permissions", h.Prefix()),
})
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}