fleet/server/handler.go

313 lines
6.6 KiB
Go
Raw Normal View History

package server
2016-08-28 03:59:17 +00:00
import (
"net/http"
kitlog "github.com/go-kit/kit/log"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"github.com/kolide/kolide-ose/kolide"
2016-09-03 17:25:16 +00:00
"golang.org/x/net/context"
2016-08-28 03:59:17 +00:00
)
2016-09-04 19:43:12 +00:00
func attachAPIRoutes(router *mux.Router, ctx context.Context, svc kolide.Service, opts []kithttp.ServerOption) {
router.Handle("/api/v1/kolide/login",
kithttp.NewServer(
ctx,
makeLoginEndpoint(svc),
decodeLoginRequest,
encodeResponse,
opts...,
),
).Methods("POST")
router.Handle("/api/v1/kolide/logout",
kithttp.NewServer(
ctx,
makeLogoutEndpoint(svc),
decodeNoParamsRequest,
encodeResponse,
opts...,
),
).Methods("POST")
router.Handle("/api/v1/kolide/forgot_password",
kithttp.NewServer(
ctx,
makeForgotPasswordEndpoint(svc),
decodeForgotPasswordRequest,
encodeResponse,
opts...,
),
).Methods("POST")
router.Handle("/api/v1/kolide/reset_password",
kithttp.NewServer(
ctx,
makeResetPasswordEndpoint(svc),
decodeResetPasswordRequest,
encodeResponse,
opts...,
),
).Methods("POST")
router.Handle("/api/v1/kolide/me",
kithttp.NewServer(
ctx,
authenticated(makeGetSessionUserEndpoint(svc)),
decodeNoParamsRequest,
encodeResponse,
opts...,
),
).Methods("GET")
router.Handle("/api/v1/kolide/users",
kithttp.NewServer(
ctx,
authenticated(canPerformActions(makeListUsersEndpoint(svc))),
decodeNoParamsRequest,
encodeResponse,
opts...,
),
).Methods("GET")
router.Handle("/api/v1/kolide/users",
kithttp.NewServer(
ctx,
authenticated(mustBeAdmin(makeCreateUserEndpoint(svc))),
decodeCreateUserRequest,
encodeResponse,
opts...,
),
).Methods("POST")
router.Handle("/api/v1/kolide/users/{id}",
kithttp.NewServer(
ctx,
authenticated(canReadUser(makeGetUserEndpoint(svc))),
decodeGetUserRequest,
encodeResponse,
opts...,
),
).Methods("GET")
router.Handle("/api/v1/kolide/users/{id}",
kithttp.NewServer(
ctx,
authenticated(validateModifyUserRequest(makeModifyUserEndpoint(svc))),
decodeModifyUserRequest,
encodeResponse,
opts...,
),
).Methods("PATCH")
router.Handle("/api/v1/kolide/users/{id}/sessions",
kithttp.NewServer(
ctx,
authenticated(canReadUser(makeGetInfoAboutSessionsForUserEndpoint(svc))),
decodeGetInfoAboutSessionsForUserRequest,
encodeResponse,
opts...,
),
).Methods("GET")
router.Handle("/api/v1/kolide/users/{id}/sessions",
kithttp.NewServer(
ctx,
authenticated(canModifyUser(makeDeleteSessionsForUserEndpoint(svc))),
decodeDeleteSessionsForUserRequest,
encodeResponse,
opts...,
),
).Methods("DELETE")
router.Handle("/api/v1/kolide/sessions/{id}",
kithttp.NewServer(
ctx,
authenticated(mustBeAdmin(makeGetInfoAboutSessionEndpoint(svc))),
decodeGetInfoAboutSessionRequest,
encodeResponse,
opts...,
),
).Methods("GET")
router.Handle("/api/v1/kolide/sessions/{id}",
kithttp.NewServer(
ctx,
authenticated(mustBeAdmin(makeDeleteSessionEndpoint(svc))),
decodeDeleteSessionRequest,
encodeResponse,
opts...,
),
).Methods("DELETE")
router.Handle("/api/v1/kolide/config",
kithttp.NewServer(
ctx,
authenticated(makeGetAppConfigEndpoint(svc)),
decodeNoParamsRequest,
encodeResponse,
opts...,
),
).Methods("GET")
router.Handle("/api/v1/kolide/config",
kithttp.NewServer(
ctx,
authenticated(mustBeAdmin(makeModifyAppConfigRequest(svc))),
decodeModifyAppConfigRequest,
encodeResponse,
opts...,
),
).Methods("PATCH")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/queries/{id}",
kithttp.NewServer(
ctx,
authenticated(makeGetQueryEndpoint(svc)),
decodeGetQueryRequest,
encodeResponse,
opts...,
),
).Methods("GET")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/queries",
kithttp.NewServer(
ctx,
authenticated(makeGetAllQueriesEndpoint(svc)),
decodeNoParamsRequest,
encodeResponse,
opts...,
),
).Methods("GET")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/queries",
kithttp.NewServer(
ctx,
authenticated(makeCreateQueryEndpoint(svc)),
decodeCreateQueryRequest,
encodeResponse,
opts...,
),
).Methods("POST")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/queries/{id}",
kithttp.NewServer(
ctx,
authenticated(makeModifyQueryEndpoint(svc)),
decodeModifyQueryRequest,
encodeResponse,
opts...,
),
).Methods("PATCH")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/queries/{id}",
kithttp.NewServer(
ctx,
authenticated(makeDeleteQueryEndpoint(svc)),
decodeDeleteQueryRequest,
encodeResponse,
opts...,
),
).Methods("DELETE")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs/{id}",
kithttp.NewServer(
ctx,
authenticated(makeGetPackEndpoint(svc)),
decodeGetPackRequest,
encodeResponse,
opts...,
),
).Methods("GET")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs",
kithttp.NewServer(
ctx,
authenticated(makeGetAllPacksEndpoint(svc)),
decodeNoParamsRequest,
encodeResponse,
opts...,
),
).Methods("GET")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs",
kithttp.NewServer(
ctx,
authenticated(makeCreatePackEndpoint(svc)),
decodeCreatePackRequest,
encodeResponse,
opts...,
),
).Methods("POST")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs/{id}",
kithttp.NewServer(
ctx,
authenticated(makeModifyPackEndpoint(svc)),
decodeModifyPackRequest,
encodeResponse,
opts...,
),
).Methods("PATCH")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs/{id}",
kithttp.NewServer(
ctx,
authenticated(makeDeletePackEndpoint(svc)),
decodeDeletePackRequest,
encodeResponse,
opts...,
),
).Methods("DELETE")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs/{pid}/queries/{qid}",
kithttp.NewServer(
ctx,
authenticated(makeAddQueryToPackEndpoint(svc)),
decodeAddQueryToPackRequest,
encodeResponse,
opts...,
),
).Methods("GET")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs/{id}/queries",
kithttp.NewServer(
ctx,
authenticated(makeGetQueriesInPackEndpoint(svc)),
decodeGetQueriesInPackRequest,
encodeResponse,
opts...,
),
).Methods("GET")
2016-09-04 19:43:12 +00:00
router.Handle("/api/v1/kolide/packs/{pid}/queries/{qid}",
kithttp.NewServer(
ctx,
authenticated(makeDeleteQueryFromPackEndpoint(svc)),
decodeDeleteQueryFromPackRequest,
encodeResponse,
opts...,
),
).Methods("DELETE")
2016-09-04 19:43:12 +00:00
}
2016-09-04 19:43:12 +00:00
// MakeHandler creates an http handler for the Kolide API
func MakeHandler(ctx context.Context, svc kolide.Service, jwtKey string, ds kolide.Datastore, logger kitlog.Logger) http.Handler {
2016-09-04 19:43:12 +00:00
opts := []kithttp.ServerOption{
kithttp.ServerBefore(
setViewerContext(svc, ds, jwtKey, logger),
2016-09-04 19:43:12 +00:00
),
kithttp.ServerErrorLogger(logger),
kithttp.ServerErrorEncoder(encodeError),
2016-09-04 19:43:12 +00:00
kithttp.ServerAfter(
kithttp.SetContentType("application/json; charset=utf-8"),
),
}
2016-08-28 03:59:17 +00:00
2016-09-04 19:43:12 +00:00
r := mux.NewRouter()
attachAPIRoutes(r, ctx, svc, opts)
2016-09-05 14:15:58 +00:00
2016-08-28 03:59:17 +00:00
return r
}