2017-12-13 01:06:57 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-12-13 17:28:39 +00:00
|
|
|
"encoding/json"
|
2017-12-13 01:06:57 +00:00
|
|
|
"net/http"
|
|
|
|
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/chronograf"
|
2017-12-13 01:06:57 +00:00
|
|
|
)
|
|
|
|
|
2018-07-10 23:13:37 +00:00
|
|
|
type configLinks struct {
|
|
|
|
Self string `json:"self"` // Self link mapping to this resource
|
|
|
|
Auth string `json:"auth"` // Auth link to the auth config endpoint
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:06:57 +00:00
|
|
|
type configResponse struct {
|
2018-07-10 23:13:37 +00:00
|
|
|
Links configLinks `json:"links"`
|
2017-12-13 01:06:57 +00:00
|
|
|
chronograf.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigResponse(config chronograf.Config) *configResponse {
|
|
|
|
return &configResponse{
|
2018-07-10 23:13:37 +00:00
|
|
|
Links: configLinks{
|
2017-12-13 01:06:57 +00:00
|
|
|
Self: "/chronograf/v1/config",
|
2018-07-10 23:13:37 +00:00
|
|
|
Auth: "/chronograf/v1/config/auth",
|
2017-12-13 01:06:57 +00:00
|
|
|
},
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:04:44 +00:00
|
|
|
type authConfigResponse struct {
|
|
|
|
Links selfLinks `json:"links"`
|
|
|
|
chronograf.AuthConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAuthConfigResponse(config chronograf.Config) *authConfigResponse {
|
|
|
|
return &authConfigResponse{
|
|
|
|
Links: selfLinks{
|
|
|
|
Self: "/chronograf/v1/config/auth",
|
|
|
|
},
|
|
|
|
AuthConfig: config.Auth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:06:57 +00:00
|
|
|
// Config retrieves the global application configuration
|
|
|
|
func (s *Service) Config(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res := newConfigResponse(*config)
|
2018-07-03 18:06:14 +00:00
|
|
|
|
2017-12-13 01:06:57 +00:00
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
2017-12-13 17:04:44 +00:00
|
|
|
|
2018-07-02 23:17:23 +00:00
|
|
|
// AuthConfig retrieves the auth section of the global application configuration
|
|
|
|
func (s *Service) AuthConfig(w http.ResponseWriter, r *http.Request) {
|
2017-12-13 17:28:39 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-03 18:06:14 +00:00
|
|
|
res := newAuthConfigResponse(*config)
|
2018-07-02 23:17:23 +00:00
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceAuthConfig replaces the auth section of the global application configuration
|
|
|
|
func (s *Service) ReplaceAuthConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
2018-07-03 18:06:14 +00:00
|
|
|
var authConfig chronograf.AuthConfig
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&authConfig); err != nil {
|
|
|
|
invalidJSON(w, s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-02 23:17:23 +00:00
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config.Auth = authConfig
|
2017-12-13 17:28:39 +00:00
|
|
|
|
2018-07-03 18:06:14 +00:00
|
|
|
res := newAuthConfigResponse(*config)
|
2017-12-13 17:28:39 +00:00
|
|
|
if err := s.Store.Config(ctx).Update(ctx, config); err != nil {
|
|
|
|
unknownErrorWithMessage(w, err, s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|