2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-08-28 03:59:17 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2021-11-22 14:13:26 +00:00
|
|
|
"errors"
|
2016-08-28 03:59:17 +00:00
|
|
|
"net/http"
|
2017-01-12 00:40:58 +00:00
|
|
|
"strings"
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
2021-08-02 22:06:27 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/service/middleware/authzcheck"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/service/middleware/ratelimit"
|
2016-09-26 17:14:39 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2016-08-28 03:59:17 +00:00
|
|
|
kitlog "github.com/go-kit/kit/log"
|
2021-02-10 20:13:11 +00:00
|
|
|
"github.com/go-kit/kit/log/level"
|
2016-08-28 03:59:17 +00:00
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
|
|
"github.com/gorilla/mux"
|
2016-12-22 17:39:44 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-12-20 14:20:58 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2021-03-26 18:23:29 +00:00
|
|
|
"github.com/throttled/throttled/v2"
|
2016-08-28 03:59:17 +00:00
|
|
|
)
|
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
// FleetEndpoints is a collection of RPC endpoints implemented by the Fleet API.
|
|
|
|
type FleetEndpoints struct {
|
2022-01-31 21:35:22 +00:00
|
|
|
Login endpoint.Endpoint
|
|
|
|
Logout endpoint.Endpoint
|
|
|
|
ForgotPassword endpoint.Endpoint
|
|
|
|
ResetPassword endpoint.Endpoint
|
|
|
|
CreateUserWithInvite endpoint.Endpoint
|
|
|
|
PerformRequiredPasswordReset endpoint.Endpoint
|
|
|
|
CreateInvite endpoint.Endpoint
|
|
|
|
ListInvites endpoint.Endpoint
|
|
|
|
DeleteInvite endpoint.Endpoint
|
|
|
|
VerifyInvite endpoint.Endpoint
|
|
|
|
EnrollAgent endpoint.Endpoint
|
|
|
|
GetClientConfig endpoint.Endpoint
|
|
|
|
GetDistributedQueries endpoint.Endpoint
|
|
|
|
SubmitDistributedQueryResults endpoint.Endpoint
|
|
|
|
SubmitLogs endpoint.Endpoint
|
|
|
|
CarveBegin endpoint.Endpoint
|
|
|
|
CarveBlock endpoint.Endpoint
|
|
|
|
SearchTargets endpoint.Endpoint
|
|
|
|
ChangeEmail endpoint.Endpoint
|
|
|
|
InitiateSSO endpoint.Endpoint
|
|
|
|
CallbackSSO endpoint.Endpoint
|
|
|
|
SSOSettings endpoint.Endpoint
|
|
|
|
StatusResultStore endpoint.Endpoint
|
|
|
|
StatusLiveQuery endpoint.Endpoint
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
// MakeFleetServerEndpoints creates the Fleet API endpoints.
|
2021-09-10 17:48:33 +00:00
|
|
|
func MakeFleetServerEndpoints(svc fleet.Service, urlPrefix string, limitStore throttled.GCRAStore, logger kitlog.Logger) FleetEndpoints {
|
2021-03-26 18:23:29 +00:00
|
|
|
limiter := ratelimit.NewMiddleware(limitStore)
|
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
return FleetEndpoints{
|
2021-06-03 23:24:15 +00:00
|
|
|
Login: limiter.Limit(
|
|
|
|
throttled.RateQuota{MaxRate: throttled.PerMin(10), MaxBurst: 9})(
|
2021-03-26 18:23:29 +00:00
|
|
|
makeLoginEndpoint(svc),
|
|
|
|
),
|
2021-08-23 22:40:00 +00:00
|
|
|
Logout: logged(makeLogoutEndpoint(svc)),
|
2021-03-26 18:23:29 +00:00
|
|
|
ForgotPassword: limiter.Limit(
|
|
|
|
throttled.RateQuota{MaxRate: throttled.PerHour(10), MaxBurst: 9})(
|
2021-08-23 22:40:00 +00:00
|
|
|
logged(makeForgotPasswordEndpoint(svc)),
|
2021-03-26 18:23:29 +00:00
|
|
|
),
|
2021-08-23 22:40:00 +00:00
|
|
|
ResetPassword: logged(makeResetPasswordEndpoint(svc)),
|
|
|
|
CreateUserWithInvite: logged(makeCreateUserFromInviteEndpoint(svc)),
|
|
|
|
VerifyInvite: logged(makeVerifyInviteEndpoint(svc)),
|
|
|
|
InitiateSSO: logged(makeInitiateSSOEndpoint(svc)),
|
|
|
|
CallbackSSO: logged(makeCallbackSSOEndpoint(svc, urlPrefix)),
|
|
|
|
SSOSettings: logged(makeSSOSettingsEndpoint(svc)),
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2017-01-10 04:42:50 +00:00
|
|
|
// PerformRequiredPasswordReset needs only to authenticate the
|
|
|
|
// logged in user
|
2021-08-23 22:40:00 +00:00
|
|
|
PerformRequiredPasswordReset: logged(canPerformPasswordReset(makePerformRequiredPasswordResetEndpoint(svc))),
|
2021-06-16 17:55:41 +00:00
|
|
|
|
|
|
|
// Standard user authentication routes
|
2022-01-25 14:34:00 +00:00
|
|
|
CreateInvite: authenticatedUser(svc, makeCreateInviteEndpoint(svc)),
|
|
|
|
ListInvites: authenticatedUser(svc, makeListInvitesEndpoint(svc)),
|
|
|
|
DeleteInvite: authenticatedUser(svc, makeDeleteInviteEndpoint(svc)),
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
SearchTargets: authenticatedUser(svc, makeSearchTargetsEndpoint(svc)),
|
|
|
|
ChangeEmail: authenticatedUser(svc, makeChangeEmailEndpoint(svc)),
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2019-08-13 16:42:58 +00:00
|
|
|
// Authenticated status endpoints
|
2021-06-07 01:10:58 +00:00
|
|
|
StatusResultStore: authenticatedUser(svc, makeStatusResultStoreEndpoint(svc)),
|
|
|
|
StatusLiveQuery: authenticatedUser(svc, makeStatusLiveQueryEndpoint(svc)),
|
2019-08-13 16:42:58 +00:00
|
|
|
|
2016-09-29 04:21:39 +00:00
|
|
|
// Osquery endpoints
|
2021-08-23 22:40:00 +00:00
|
|
|
EnrollAgent: logged(makeEnrollAgentEndpoint(svc)),
|
2021-06-16 17:55:41 +00:00
|
|
|
// Authenticated osquery endpoints
|
2021-09-10 17:48:33 +00:00
|
|
|
GetClientConfig: authenticatedHost(svc, logger, makeGetClientConfigEndpoint(svc)),
|
|
|
|
GetDistributedQueries: authenticatedHost(svc, logger, makeGetDistributedQueriesEndpoint(svc)),
|
|
|
|
SubmitDistributedQueryResults: authenticatedHost(svc, logger, makeSubmitDistributedQueryResultsEndpoint(svc)),
|
|
|
|
SubmitLogs: authenticatedHost(svc, logger, makeSubmitLogsEndpoint(svc)),
|
|
|
|
CarveBegin: authenticatedHost(svc, logger, makeCarveBeginEndpoint(svc)),
|
2020-11-05 04:45:16 +00:00
|
|
|
// For some reason osquery does not provide a node key with the block
|
|
|
|
// data. Instead the carve session ID should be verified in the service
|
|
|
|
// method.
|
2021-08-23 22:40:00 +00:00
|
|
|
CarveBlock: logged(makeCarveBlockEndpoint(svc)),
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
type fleetHandlers struct {
|
2022-01-31 21:35:22 +00:00
|
|
|
Login http.Handler
|
|
|
|
Logout http.Handler
|
|
|
|
ForgotPassword http.Handler
|
|
|
|
ResetPassword http.Handler
|
|
|
|
CreateUserWithInvite http.Handler
|
|
|
|
PerformRequiredPasswordReset http.Handler
|
|
|
|
CreateInvite http.Handler
|
|
|
|
ListInvites http.Handler
|
|
|
|
DeleteInvite http.Handler
|
|
|
|
VerifyInvite http.Handler
|
|
|
|
EnrollAgent http.Handler
|
|
|
|
GetClientConfig http.Handler
|
|
|
|
GetDistributedQueries http.Handler
|
|
|
|
SubmitDistributedQueryResults http.Handler
|
|
|
|
SubmitLogs http.Handler
|
|
|
|
CarveBegin http.Handler
|
|
|
|
CarveBlock http.Handler
|
|
|
|
SearchTargets http.Handler
|
|
|
|
ChangeEmail http.Handler
|
|
|
|
InitiateSSO http.Handler
|
|
|
|
CallbackSSO http.Handler
|
|
|
|
SettingsSSO http.Handler
|
|
|
|
StatusResultStore http.Handler
|
|
|
|
StatusLiveQuery http.Handler
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
func makeKitHandlers(e FleetEndpoints, opts []kithttp.ServerOption) *fleetHandlers {
|
2016-12-22 17:39:44 +00:00
|
|
|
newServer := func(e endpoint.Endpoint, decodeFn kithttp.DecodeRequestFunc) http.Handler {
|
2021-06-03 23:24:15 +00:00
|
|
|
e = authzcheck.NewMiddleware().AuthzCheck()(e)
|
2017-03-15 15:55:30 +00:00
|
|
|
return kithttp.NewServer(e, decodeFn, encodeResponse, opts...)
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2021-06-04 23:51:18 +00:00
|
|
|
return &fleetHandlers{
|
2022-01-31 21:35:22 +00:00
|
|
|
Login: newServer(e.Login, decodeLoginRequest),
|
|
|
|
Logout: newServer(e.Logout, decodeNoParamsRequest),
|
|
|
|
ForgotPassword: newServer(e.ForgotPassword, decodeForgotPasswordRequest),
|
|
|
|
ResetPassword: newServer(e.ResetPassword, decodeResetPasswordRequest),
|
|
|
|
CreateUserWithInvite: newServer(e.CreateUserWithInvite, decodeCreateUserRequest),
|
|
|
|
PerformRequiredPasswordReset: newServer(e.PerformRequiredPasswordReset, decodePerformRequiredPasswordResetRequest),
|
|
|
|
CreateInvite: newServer(e.CreateInvite, decodeCreateInviteRequest),
|
|
|
|
ListInvites: newServer(e.ListInvites, decodeListInvitesRequest),
|
|
|
|
DeleteInvite: newServer(e.DeleteInvite, decodeDeleteInviteRequest),
|
|
|
|
VerifyInvite: newServer(e.VerifyInvite, decodeVerifyInviteRequest),
|
|
|
|
EnrollAgent: newServer(e.EnrollAgent, decodeEnrollAgentRequest),
|
|
|
|
GetClientConfig: newServer(e.GetClientConfig, decodeGetClientConfigRequest),
|
|
|
|
GetDistributedQueries: newServer(e.GetDistributedQueries, decodeGetDistributedQueriesRequest),
|
|
|
|
SubmitDistributedQueryResults: newServer(e.SubmitDistributedQueryResults, decodeSubmitDistributedQueryResultsRequest),
|
|
|
|
SubmitLogs: newServer(e.SubmitLogs, decodeSubmitLogsRequest),
|
|
|
|
CarveBegin: newServer(e.CarveBegin, decodeCarveBeginRequest),
|
|
|
|
CarveBlock: newServer(e.CarveBlock, decodeCarveBlockRequest),
|
|
|
|
SearchTargets: newServer(e.SearchTargets, decodeSearchTargetsRequest),
|
|
|
|
ChangeEmail: newServer(e.ChangeEmail, decodeChangeEmailRequest),
|
|
|
|
InitiateSSO: newServer(e.InitiateSSO, decodeInitiateSSORequest),
|
|
|
|
CallbackSSO: newServer(e.CallbackSSO, decodeCallbackSSORequest),
|
|
|
|
SettingsSSO: newServer(e.SSOSettings, decodeNoParamsRequest),
|
|
|
|
StatusResultStore: newServer(e.StatusResultStore, decodeNoParamsRequest),
|
|
|
|
StatusLiveQuery: newServer(e.StatusLiveQuery, decodeNoParamsRequest),
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-09-04 19:43:12 +00:00
|
|
|
}
|
2016-09-01 04:51:38 +00:00
|
|
|
|
2021-03-26 18:23:29 +00:00
|
|
|
type errorHandler struct {
|
|
|
|
logger kitlog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *errorHandler) Handle(ctx context.Context, err error) {
|
|
|
|
// get the request path
|
|
|
|
path, _ := ctx.Value(kithttp.ContextKeyRequestPath).(string)
|
|
|
|
logger := level.Info(kitlog.With(h.logger, "path", path))
|
|
|
|
|
2021-11-15 14:11:38 +00:00
|
|
|
var ewi fleet.ErrWithInternal
|
|
|
|
if errors.As(err, &ewi) {
|
|
|
|
logger = kitlog.With(logger, "internal", ewi.Internal())
|
2021-06-03 23:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 14:11:38 +00:00
|
|
|
var ewlf fleet.ErrWithLogFields
|
|
|
|
if errors.As(err, &ewlf) {
|
|
|
|
logger = kitlog.With(logger, ewlf.LogFields()...)
|
2021-06-05 13:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 14:11:38 +00:00
|
|
|
var rle ratelimit.Error
|
|
|
|
if errors.As(err, &rle) {
|
|
|
|
res := rle.Result()
|
2021-03-26 18:23:29 +00:00
|
|
|
logger.Log("err", "limit exceeded", "retry_after", res.RetryAfter)
|
2021-11-15 14:11:38 +00:00
|
|
|
} else {
|
2021-03-26 18:23:29 +00:00
|
|
|
logger.Log("err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:06:27 +00:00
|
|
|
func logRequestEnd(logger kitlog.Logger) func(context.Context, http.ResponseWriter) context.Context {
|
|
|
|
return func(ctx context.Context, w http.ResponseWriter) context.Context {
|
|
|
|
logCtx, ok := logging.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
logCtx.Log(ctx, logger)
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:28:53 +00:00
|
|
|
func checkLicenseExpiration(svc fleet.Service) func(context.Context, http.ResponseWriter) context.Context {
|
|
|
|
return func(ctx context.Context, w http.ResponseWriter) context.Context {
|
|
|
|
license, err := svc.License(ctx)
|
|
|
|
if err != nil || license == nil {
|
|
|
|
return ctx
|
|
|
|
}
|
2021-09-03 16:05:23 +00:00
|
|
|
if license.IsPremium() && license.IsExpired() {
|
2021-08-26 13:28:53 +00:00
|
|
|
w.Header().Set(fleet.HeaderLicenseKey, fleet.HeaderLicenseValueExpired)
|
|
|
|
}
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
// MakeHandler creates an HTTP handler for the Fleet server endpoints.
|
2021-06-06 22:07:29 +00:00
|
|
|
func MakeHandler(svc fleet.Service, config config.FleetConfig, logger kitlog.Logger, limitStore throttled.GCRAStore) http.Handler {
|
2021-06-04 23:51:18 +00:00
|
|
|
fleetAPIOptions := []kithttp.ServerOption{
|
2016-09-04 19:43:12 +00:00
|
|
|
kithttp.ServerBefore(
|
2017-12-01 00:52:23 +00:00
|
|
|
kithttp.PopulateRequestContext, // populate the request context with common fields
|
2021-06-07 01:10:58 +00:00
|
|
|
setRequestsContexts(svc),
|
2016-09-04 19:43:12 +00:00
|
|
|
),
|
2021-03-26 18:23:29 +00:00
|
|
|
kithttp.ServerErrorHandler(&errorHandler{logger}),
|
2022-01-20 19:41:02 +00:00
|
|
|
kithttp.ServerErrorEncoder(encodeErrorAndTrySentry(config.Sentry.Dsn != "")),
|
2016-09-04 19:43:12 +00:00
|
|
|
kithttp.ServerAfter(
|
|
|
|
kithttp.SetContentType("application/json; charset=utf-8"),
|
2021-08-02 22:06:27 +00:00
|
|
|
logRequestEnd(logger),
|
2021-08-26 13:28:53 +00:00
|
|
|
checkLicenseExpiration(svc),
|
2016-09-04 19:43:12 +00:00
|
|
|
),
|
|
|
|
}
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2021-09-10 17:48:33 +00:00
|
|
|
fleetEndpoints := MakeFleetServerEndpoints(svc, config.Server.URLPrefix, limitStore, logger)
|
2021-06-04 23:51:18 +00:00
|
|
|
fleetHandlers := makeKitHandlers(fleetEndpoints, fleetAPIOptions)
|
2016-09-26 17:14:39 +00:00
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
r := mux.NewRouter()
|
2021-02-10 20:13:11 +00:00
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
attachFleetAPIRoutes(r, fleetHandlers)
|
2021-07-16 18:28:13 +00:00
|
|
|
attachNewStyleFleetAPIRoutes(r, svc, fleetAPIOptions)
|
2021-02-10 20:13:11 +00:00
|
|
|
|
2020-11-13 03:06:56 +00:00
|
|
|
// Results endpoint is handled different due to websockets use
|
2021-02-10 20:13:11 +00:00
|
|
|
r.PathPrefix("/api/v1/fleet/results/").
|
2021-06-07 01:10:58 +00:00
|
|
|
Handler(makeStreamDistributedQueryCampaignResultsHandler(svc, logger)).
|
2021-02-10 20:13:11 +00:00
|
|
|
Name("distributed_query_results")
|
2017-03-01 21:14:26 +00:00
|
|
|
|
2020-11-13 03:06:56 +00:00
|
|
|
addMetrics(r)
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
return r
|
|
|
|
}
|
2016-09-26 17:14:39 +00:00
|
|
|
|
2021-12-20 14:20:58 +00:00
|
|
|
// InstrumentHandler wraps the provided handler with prometheus metrics
|
|
|
|
// middleware and returns the resulting handler that should be mounted for that
|
|
|
|
// route.
|
|
|
|
func InstrumentHandler(name string, handler http.Handler) http.Handler {
|
|
|
|
reg := prometheus.DefaultRegisterer
|
|
|
|
registerOrExisting := func(coll prometheus.Collector) prometheus.Collector {
|
|
|
|
if err := reg.Register(coll); err != nil {
|
|
|
|
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
|
|
|
|
return are.ExistingCollector
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return coll
|
|
|
|
}
|
|
|
|
|
|
|
|
// this configuration is to keep prometheus metrics as close as possible to
|
|
|
|
// what the v0.9.3 (that we used to use) provided via the now-deprecated
|
|
|
|
// prometheus.InstrumentHandler.
|
|
|
|
|
|
|
|
reqCnt := registerOrExisting(prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Subsystem: "http",
|
|
|
|
Name: "requests_total",
|
|
|
|
Help: "Total number of HTTP requests made.",
|
|
|
|
ConstLabels: prometheus.Labels{"handler": name},
|
|
|
|
},
|
|
|
|
[]string{"method", "code"},
|
|
|
|
)).(*prometheus.CounterVec)
|
|
|
|
|
|
|
|
reqDur := registerOrExisting(prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Subsystem: "http",
|
|
|
|
Name: "request_duration_seconds",
|
|
|
|
Help: "The HTTP request latencies in seconds.",
|
|
|
|
ConstLabels: prometheus.Labels{"handler": name},
|
|
|
|
// Use default buckets, as they are suited for durations.
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)).(*prometheus.HistogramVec)
|
|
|
|
|
|
|
|
// 1KB, 100KB, 1MB, 100MB, 1GB
|
|
|
|
sizeBuckets := []float64{1024, 100 * 1024, 1024 * 1024, 100 * 1024 * 1024, 1024 * 1024 * 1024}
|
|
|
|
|
|
|
|
resSz := registerOrExisting(prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Subsystem: "http",
|
|
|
|
Name: "response_size_bytes",
|
|
|
|
Help: "The HTTP response sizes in bytes.",
|
|
|
|
ConstLabels: prometheus.Labels{"handler": name},
|
|
|
|
Buckets: sizeBuckets,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)).(*prometheus.HistogramVec)
|
|
|
|
|
|
|
|
reqSz := registerOrExisting(prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Subsystem: "http",
|
|
|
|
Name: "request_size_bytes",
|
|
|
|
Help: "The HTTP request sizes in bytes.",
|
|
|
|
ConstLabels: prometheus.Labels{"handler": name},
|
|
|
|
Buckets: sizeBuckets,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)).(*prometheus.HistogramVec)
|
|
|
|
|
|
|
|
return promhttp.InstrumentHandlerDuration(reqDur,
|
|
|
|
promhttp.InstrumentHandlerCounter(reqCnt,
|
|
|
|
promhttp.InstrumentHandlerResponseSize(resSz,
|
|
|
|
promhttp.InstrumentHandlerRequestSize(reqSz, handler))))
|
|
|
|
}
|
|
|
|
|
|
|
|
// addMetrics decorates each handler with prometheus instrumentation
|
2016-12-22 17:39:44 +00:00
|
|
|
func addMetrics(r *mux.Router) {
|
|
|
|
walkFn := func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
2021-12-20 14:20:58 +00:00
|
|
|
route.Handler(InstrumentHandler(route.GetName(), route.GetHandler()))
|
2016-12-22 17:39:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.Walk(walkFn)
|
|
|
|
}
|
|
|
|
|
2021-06-04 23:51:18 +00:00
|
|
|
func attachFleetAPIRoutes(r *mux.Router, h *fleetHandlers) {
|
2021-02-10 20:13:11 +00:00
|
|
|
r.Handle("/api/v1/fleet/login", h.Login).Methods("POST").Name("login")
|
|
|
|
r.Handle("/api/v1/fleet/logout", h.Logout).Methods("POST").Name("logout")
|
|
|
|
r.Handle("/api/v1/fleet/forgot_password", h.ForgotPassword).Methods("POST").Name("forgot_password")
|
|
|
|
r.Handle("/api/v1/fleet/reset_password", h.ResetPassword).Methods("POST").Name("reset_password")
|
|
|
|
r.Handle("/api/v1/fleet/perform_required_password_reset", h.PerformRequiredPasswordReset).Methods("POST").Name("perform_required_password_reset")
|
|
|
|
r.Handle("/api/v1/fleet/sso", h.InitiateSSO).Methods("POST").Name("intiate_sso")
|
|
|
|
r.Handle("/api/v1/fleet/sso", h.SettingsSSO).Methods("GET").Name("sso_config")
|
|
|
|
r.Handle("/api/v1/fleet/sso/callback", h.CallbackSSO).Methods("POST").Name("callback_sso")
|
2022-01-10 19:43:39 +00:00
|
|
|
|
2021-02-10 20:13:11 +00:00
|
|
|
r.Handle("/api/v1/fleet/users", h.CreateUserWithInvite).Methods("POST").Name("create_user_with_invite")
|
|
|
|
|
|
|
|
r.Handle("/api/v1/fleet/invites", h.CreateInvite).Methods("POST").Name("create_invite")
|
|
|
|
r.Handle("/api/v1/fleet/invites", h.ListInvites).Methods("GET").Name("list_invites")
|
2021-10-26 14:33:31 +00:00
|
|
|
r.Handle("/api/v1/fleet/invites/{id:[0-9]+}", h.DeleteInvite).Methods("DELETE").Name("delete_invite")
|
2021-02-10 20:13:11 +00:00
|
|
|
r.Handle("/api/v1/fleet/invites/{token}", h.VerifyInvite).Methods("GET").Name("verify_invite")
|
|
|
|
|
|
|
|
r.Handle("/api/v1/fleet/email/change/{token}", h.ChangeEmail).Methods("GET").Name("change_email")
|
|
|
|
|
|
|
|
r.Handle("/api/v1/fleet/targets", h.SearchTargets).Methods("POST").Name("search_targets")
|
|
|
|
|
|
|
|
r.Handle("/api/v1/fleet/status/result_store", h.StatusResultStore).Methods("GET").Name("status_result_store")
|
|
|
|
r.Handle("/api/v1/fleet/status/live_query", h.StatusLiveQuery).Methods("GET").Name("status_live_query")
|
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/osquery/enroll", h.EnrollAgent).Methods("POST").Name("enroll_agent")
|
|
|
|
r.Handle("/api/v1/osquery/config", h.GetClientConfig).Methods("POST").Name("get_client_config")
|
|
|
|
r.Handle("/api/v1/osquery/distributed/read", h.GetDistributedQueries).Methods("POST").Name("get_distributed_queries")
|
|
|
|
r.Handle("/api/v1/osquery/distributed/write", h.SubmitDistributedQueryResults).Methods("POST").Name("submit_distributed_query_results")
|
|
|
|
r.Handle("/api/v1/osquery/log", h.SubmitLogs).Methods("POST").Name("submit_logs")
|
2020-11-05 04:45:16 +00:00
|
|
|
r.Handle("/api/v1/osquery/carve/begin", h.CarveBegin).Methods("POST").Name("carve_begin")
|
|
|
|
r.Handle("/api/v1/osquery/carve/block", h.CarveBlock).Methods("POST").Name("carve_block")
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-11-09 17:19:07 +00:00
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
func attachNewStyleFleetAPIRoutes(r *mux.Router, svc fleet.Service, opts []kithttp.ServerOption) {
|
2021-12-21 15:23:12 +00:00
|
|
|
e := NewUserAuthenticatedEndpointer(svc, opts, r, "v1")
|
|
|
|
|
2022-01-25 14:34:00 +00:00
|
|
|
e.GET("/api/_version_/fleet/me", meEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/sessions/{id:[0-9]+}", getInfoAboutSessionEndpoint, getInfoAboutSessionRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/sessions/{id:[0-9]+}", deleteSessionEndpoint, deleteSessionRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/config/certificate", getCertificateEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/config", getAppConfigEndpoint, nil)
|
|
|
|
e.PATCH("/api/_version_/fleet/config", modifyAppConfigEndpoint, modifyAppConfigRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/spec/enroll_secret", applyEnrollSecretSpecEndpoint, applyEnrollSecretSpecRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/spec/enroll_secret", getEnrollSecretSpecEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/version", versionEndpoint, nil)
|
|
|
|
|
2021-12-21 15:23:12 +00:00
|
|
|
e.POST("/api/_version_/fleet/users/roles/spec", applyUserRoleSpecsEndpoint, applyUserRoleSpecsRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/translate", translatorEndpoint, translatorRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/spec/teams", applyTeamSpecsEndpoint, applyTeamSpecsRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/teams/{team_id:[0-9]+}/secrets", modifyTeamEnrollSecretsEndpoint, modifyTeamEnrollSecretsRequest{})
|
2022-01-19 15:52:14 +00:00
|
|
|
e.POST("/api/_version_/fleet/teams", createTeamEndpoint, createTeamRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/teams", listTeamsEndpoint, listTeamsRequest{})
|
2022-02-04 17:33:22 +00:00
|
|
|
e.GET("/api/_version_/fleet/teams/{id:[0-9]+}", getTeamEndpoint, getTeamRequest{})
|
2022-01-19 15:52:14 +00:00
|
|
|
e.PATCH("/api/_version_/fleet/teams/{id:[0-9]+}", modifyTeamEndpoint, modifyTeamRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/teams/{id:[0-9]+}", deleteTeamEndpoint, deleteTeamRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/teams/{id:[0-9]+}/agent_options", modifyTeamAgentOptionsEndpoint, modifyTeamAgentOptionsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/teams/{id:[0-9]+}/users", listTeamUsersEndpoint, listTeamUsersRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/teams/{id:[0-9]+}/users", addTeamUsersEndpoint, modifyTeamUsersRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/teams/{id:[0-9]+}/users", deleteTeamUsersEndpoint, modifyTeamUsersRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/teams/{id:[0-9]+}/secrets", teamEnrollSecretsEndpoint, teamEnrollSecretsRequest{})
|
2021-12-21 15:23:12 +00:00
|
|
|
|
|
|
|
// Alias /api/_version_/fleet/team/ -> /api/_version_/fleet/teams/
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/schedule").GET("/api/_version_/fleet/teams/{team_id}/schedule", getTeamScheduleEndpoint, getTeamScheduleRequest{})
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/schedule").POST("/api/_version_/fleet/teams/{team_id}/schedule", teamScheduleQueryEndpoint, teamScheduleQueryRequest{})
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/schedule/{scheduled_query_id}").PATCH("/api/_version_/fleet/teams/{team_id}/schedule/{scheduled_query_id}", modifyTeamScheduleEndpoint, modifyTeamScheduleRequest{})
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/schedule/{scheduled_query_id}").DELETE("/api/_version_/fleet/teams/{team_id}/schedule/{scheduled_query_id}", deleteTeamScheduleEndpoint, deleteTeamScheduleRequest{})
|
|
|
|
|
2022-01-10 19:43:39 +00:00
|
|
|
e.GET("/api/_version_/fleet/users", listUsersEndpoint, listUsersRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/users/admin", createUserEndpoint, createUserRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/users/{id:[0-9]+}", getUserEndpoint, getUserRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/users/{id:[0-9]+}", modifyUserEndpoint, modifyUserRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/users/{id:[0-9]+}", deleteUserEndpoint, deleteUserRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/users/{id:[0-9]+}/require_password_reset", requirePasswordResetEndpoint, requirePasswordResetRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/users/{id:[0-9]+}/sessions", getInfoAboutSessionsForUserEndpoint, getInfoAboutSessionsForUserRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/users/{id:[0-9]+}/sessions", deleteSessionsForUserEndpoint, deleteSessionsForUserRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/change_password", changePasswordEndpoint, changePasswordRequest{})
|
|
|
|
|
2021-12-21 15:23:12 +00:00
|
|
|
e.POST("/api/_version_/fleet/global/policies", globalPolicyEndpoint, globalPolicyRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/global/policies", listGlobalPoliciesEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/global/policies/{policy_id}", getPolicyByIDEndpoint, getPolicyByIDRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/global/policies/delete", deleteGlobalPoliciesEndpoint, deleteGlobalPoliciesRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/global/policies/{policy_id}", modifyGlobalPolicyEndpoint, modifyGlobalPolicyRequest{})
|
|
|
|
|
|
|
|
// Alias /api/_version_/fleet/team/ -> /api/_version_/fleet/teams/
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/policies").POST("/api/_version_/fleet/teams/{team_id}/policies", teamPolicyEndpoint, teamPolicyRequest{})
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/policies").GET("/api/_version_/fleet/teams/{team_id}/policies", listTeamPoliciesEndpoint, listTeamPoliciesRequest{})
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/policies/{policy_id}").GET("/api/_version_/fleet/teams/{team_id}/policies/{policy_id}", getTeamPolicyByIDEndpoint, getTeamPolicyByIDRequest{})
|
|
|
|
e.WithAltPaths("/api/_version_/fleet/team/{team_id}/policies/delete").POST("/api/_version_/fleet/teams/{team_id}/policies/delete", deleteTeamPoliciesEndpoint, deleteTeamPoliciesRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/teams/{team_id}/policies/{policy_id}", modifyTeamPolicyEndpoint, modifyTeamPolicyRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/spec/policies", applyPolicySpecsEndpoint, applyPolicySpecsRequest{})
|
|
|
|
|
2022-01-31 21:35:22 +00:00
|
|
|
e.GET("/api/_version_/fleet/queries/{id:[0-9]+}", getQueryEndpoint, getQueryRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/queries", listQueriesEndpoint, listQueriesRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/queries", createQueryEndpoint, createQueryRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/queries/{id:[0-9]+}", modifyQueryEndpoint, modifyQueryRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/queries/{name}", deleteQueryEndpoint, deleteQueryRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/queries/id/{id:[0-9]+}", deleteQueryByIDEndpoint, deleteQueryByIDRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/queries/delete", deleteQueriesEndpoint, deleteQueriesRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/spec/queries", applyQuerySpecsEndpoint, applyQuerySpecsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/spec/queries", getQuerySpecsEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/spec/queries/{name}", getQuerySpecEndpoint, getGenericSpecRequest{})
|
|
|
|
|
2021-12-21 15:23:12 +00:00
|
|
|
e.GET("/api/_version_/fleet/packs/{id:[0-9]+}/scheduled", getScheduledQueriesInPackEndpoint, getScheduledQueriesInPackRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/schedule", scheduleQueryEndpoint, scheduleQueryRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/schedule/{id:[0-9]+}", getScheduledQueryEndpoint, getScheduledQueryRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/schedule/{id:[0-9]+}", modifyScheduledQueryEndpoint, modifyScheduledQueryRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/schedule/{id:[0-9]+}", deleteScheduledQueryEndpoint, deleteScheduledQueryRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/packs/{id:[0-9]+}", getPackEndpoint, getPackRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/packs", createPackEndpoint, createPackRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/packs/{id:[0-9]+}", modifyPackEndpoint, modifyPackRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/packs", listPacksEndpoint, listPacksRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/packs/{name}", deletePackEndpoint, deletePackRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/packs/id/{id:[0-9]+}", deletePackByIDEndpoint, deletePackByIDRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/spec/packs", applyPackSpecsEndpoint, applyPackSpecsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/spec/packs", getPackSpecsEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/spec/packs/{name}", getPackSpecEndpoint, getGenericSpecRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/software", listSoftwareEndpoint, listSoftwareRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/software/count", countSoftwareEndpoint, countSoftwareRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/host_summary", getHostSummaryEndpoint, getHostSummaryRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/hosts", listHostsEndpoint, listHostsRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/hosts/delete", deleteHostsEndpoint, deleteHostsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/hosts/{id:[0-9]+}", getHostEndpoint, getHostRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/hosts/count", countHostsEndpoint, countHostsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/hosts/identifier/{identifier}", hostByIdentifierEndpoint, hostByIdentifierRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/hosts/{id:[0-9]+}", deleteHostEndpoint, deleteHostRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/hosts/transfer", addHostsToTeamEndpoint, addHostsToTeamRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/hosts/transfer/filter", addHostsToTeamByFilterEndpoint, addHostsToTeamByFilterRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/hosts/{id:[0-9]+}/refetch", refetchHostEndpoint, refetchHostRequest{})
|
2021-12-21 20:36:19 +00:00
|
|
|
e.GET("/api/_version_/fleet/hosts/{id:[0-9]+}/device_mapping", listHostDeviceMappingEndpoint, listHostDeviceMappingRequest{})
|
2021-12-21 15:23:12 +00:00
|
|
|
|
|
|
|
e.POST("/api/_version_/fleet/labels", createLabelEndpoint, createLabelRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/labels/{id:[0-9]+}", modifyLabelEndpoint, modifyLabelRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/labels/{id:[0-9]+}", getLabelEndpoint, getLabelRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/labels", listLabelsEndpoint, listLabelsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/labels/{id:[0-9]+}/hosts", listHostsInLabelEndpoint, listHostsInLabelRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/labels/{name}", deleteLabelEndpoint, deleteLabelRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/labels/id/{id:[0-9]+}", deleteLabelByIDEndpoint, deleteLabelByIDRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/spec/labels", applyLabelSpecsEndpoint, applyLabelSpecsRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/spec/labels", getLabelSpecsEndpoint, nil)
|
|
|
|
e.GET("/api/_version_/fleet/spec/labels/{name}", getLabelSpecEndpoint, getGenericSpecRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/queries/run", runLiveQueryEndpoint, runLiveQueryRequest{})
|
2022-01-31 21:35:22 +00:00
|
|
|
e.POST("/api/_version_/fleet/queries/run", createDistributedQueryCampaignEndpoint, createDistributedQueryCampaignRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/queries/run_by_names", createDistributedQueryCampaignByNamesEndpoint, createDistributedQueryCampaignByNamesRequest{})
|
2021-12-21 15:23:12 +00:00
|
|
|
|
|
|
|
e.PATCH("/api/_version_/fleet/invites/{id:[0-9]+}", updateInviteEndpoint, updateInviteRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/activities", listActivitiesEndpoint, listActivitiesRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/global/schedule", getGlobalScheduleEndpoint, getGlobalScheduleRequest{})
|
|
|
|
e.POST("/api/_version_/fleet/global/schedule", globalScheduleQueryEndpoint, globalScheduleQueryRequest{})
|
|
|
|
e.PATCH("/api/_version_/fleet/global/schedule/{id:[0-9]+}", modifyGlobalScheduleEndpoint, modifyGlobalScheduleRequest{})
|
|
|
|
e.DELETE("/api/_version_/fleet/global/schedule/{id:[0-9]+}", deleteGlobalScheduleEndpoint, deleteGlobalScheduleRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/carves", listCarvesEndpoint, listCarvesRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/carves/{id:[0-9]+}", getCarveEndpoint, getCarveRequest{})
|
|
|
|
e.GET("/api/_version_/fleet/carves/{id:[0-9]+}/block/{block_id}", getCarveBlockEndpoint, getCarveBlockRequest{})
|
|
|
|
|
|
|
|
e.GET("/api/_version_/fleet/hosts/{id:[0-9]+}/macadmins", getMacadminsDataEndpoint, getMacadminsDataRequest{})
|
2022-01-26 20:55:07 +00:00
|
|
|
e.GET("/api/_version_/fleet/macadmins", getAggregatedMacadminsDataEndpoint, getAggregatedMacadminsDataRequest{})
|
2021-07-16 18:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this duplicates the one in makeKitHandler
|
|
|
|
func newServer(e endpoint.Endpoint, decodeFn kithttp.DecodeRequestFunc, opts []kithttp.ServerOption) http.Handler {
|
|
|
|
e = authzcheck.NewMiddleware().AuthzCheck()(e)
|
|
|
|
return kithttp.NewServer(e, decodeFn, encodeResponse, opts...)
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:10:55 +00:00
|
|
|
// WithSetup is an http middleware that checks if setup procedures have been completed.
|
2017-02-09 18:43:45 +00:00
|
|
|
// If setup hasn't been completed it serves the API with a setup middleware.
|
2016-11-09 17:19:07 +00:00
|
|
|
// If the server is already configured, the default API handler is exposed.
|
2021-06-06 22:07:29 +00:00
|
|
|
func WithSetup(svc fleet.Service, logger kitlog.Logger, next http.Handler) http.HandlerFunc {
|
2016-12-02 18:46:31 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
configRouter := http.NewServeMux()
|
|
|
|
configRouter.Handle("/api/v1/setup", kithttp.NewServer(
|
|
|
|
makeSetupEndpoint(svc),
|
|
|
|
decodeSetupRequest,
|
|
|
|
encodeResponse,
|
|
|
|
))
|
2017-01-12 00:40:58 +00:00
|
|
|
// whitelist osqueryd endpoints
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v1/osquery") {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2021-06-03 23:24:15 +00:00
|
|
|
requireSetup, err := svc.SetupRequired(context.Background())
|
2017-02-09 18:43:45 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Log("msg", "fetching setup info from db", "err", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if requireSetup {
|
2016-12-02 18:46:31 +00:00
|
|
|
configRouter.ServeHTTP(w, r)
|
2017-02-09 18:43:45 +00:00
|
|
|
return
|
2016-12-02 18:46:31 +00:00
|
|
|
}
|
2017-02-09 18:43:45 +00:00
|
|
|
next.ServeHTTP(w, r)
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 23:36:36 +00:00
|
|
|
// RedirectLoginToSetup detects if the setup endpoint should be used. If setup is required it redirect all
|
|
|
|
// frontend urls to /setup, otherwise the frontend router is used.
|
2021-06-06 22:07:29 +00:00
|
|
|
func RedirectLoginToSetup(svc fleet.Service, logger kitlog.Logger, next http.Handler, urlPrefix string) http.HandlerFunc {
|
2016-12-29 23:36:36 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
redirect := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-09-01 16:42:46 +00:00
|
|
|
if r.URL.Path == "/setup" {
|
2016-12-29 23:36:36 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newURL := r.URL
|
2019-10-16 23:40:45 +00:00
|
|
|
newURL.Path = urlPrefix + "/setup"
|
2016-12-29 23:36:36 +00:00
|
|
|
http.Redirect(w, r, newURL.String(), http.StatusTemporaryRedirect)
|
|
|
|
})
|
2017-02-09 18:43:45 +00:00
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
setupRequired, err := svc.SetupRequired(context.Background())
|
2017-02-09 18:43:45 +00:00
|
|
|
if err != nil {
|
2017-09-01 16:42:46 +00:00
|
|
|
logger.Log("msg", "fetching setupinfo from db", "err", err)
|
2017-02-09 18:43:45 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if setupRequired {
|
2016-12-29 23:36:36 +00:00
|
|
|
redirect.ServeHTTP(w, r)
|
2017-02-09 18:43:45 +00:00
|
|
|
return
|
2016-12-29 23:36:36 +00:00
|
|
|
}
|
2019-10-16 23:40:45 +00:00
|
|
|
RedirectSetupToLogin(svc, logger, next, urlPrefix).ServeHTTP(w, r)
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:42:46 +00:00
|
|
|
// RedirectSetupToLogin forces the /setup path to be redirected to login. This middleware is used after
|
2017-01-11 19:05:07 +00:00
|
|
|
// the app has been setup.
|
2021-06-06 22:07:29 +00:00
|
|
|
func RedirectSetupToLogin(svc fleet.Service, logger kitlog.Logger, next http.Handler, urlPrefix string) http.HandlerFunc {
|
2017-01-11 19:05:07 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/setup" {
|
|
|
|
newURL := r.URL
|
2019-10-16 23:40:45 +00:00
|
|
|
newURL.Path = urlPrefix + "/login"
|
2017-01-11 19:05:07 +00:00
|
|
|
http.Redirect(w, r, newURL.String(), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|