2016-09-05 20:03:58 +00:00
|
|
|
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) {
|
2016-09-08 01:24:11 +00:00
|
|
|
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...,
|
|
|
|
),
|
2016-09-08 02:58:25 +00:00
|
|
|
).Methods("POST")
|
2016-09-08 01:24:11 +00:00
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
router.Handle("/api/v1/kolide/forgot_password",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-15 14:52:17 +00:00
|
|
|
makeForgotPasswordEndpoint(svc),
|
|
|
|
decodeForgotPasswordRequest,
|
2016-09-04 05:13:42 +00:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("POST")
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
router.Handle("/api/v1/kolide/reset_password",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-15 14:52:17 +00:00
|
|
|
makeResetPasswordEndpoint(svc),
|
|
|
|
decodeResetPasswordRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("POST")
|
|
|
|
|
|
|
|
router.Handle("/api/v1/kolide/users",
|
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
|
|
|
authenticated(canPerformActions(makeListUsersEndpoint(svc))),
|
|
|
|
decodeNoParamsRequest,
|
2016-09-04 05:13:42 +00:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
router.Handle("/api/v1/kolide/users",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-15 14:52:17 +00:00
|
|
|
authenticated(mustBeAdmin(makeCreateUserEndpoint(svc))),
|
|
|
|
decodeCreateUserRequest,
|
2016-09-04 05:13:42 +00:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("POST")
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
router.Handle("/api/v1/kolide/users/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-15 14:52:17 +00:00
|
|
|
authenticated(canReadUser(makeGetUserEndpoint(svc))),
|
|
|
|
decodeGetUserRequest,
|
2016-09-04 05:13:42 +00:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
2016-09-15 14:52:17 +00:00
|
|
|
).Methods("GET")
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
router.Handle("/api/v1/kolide/users/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-15 14:52:17 +00:00
|
|
|
authenticated(validateModifyUserRequest(makeModifyUserEndpoint(svc))),
|
|
|
|
decodeModifyUserRequest,
|
2016-09-04 05:13:42 +00:00
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
2016-09-15 14:52:17 +00:00
|
|
|
).Methods("PATCH")
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-05 19:50:57 +00:00
|
|
|
router.Handle("/api/v1/kolide/users/{id}/sessions",
|
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(canReadUser(makeGetInfoAboutSessionsForUserEndpoint(svc))),
|
2016-09-05 19:50:57 +00:00
|
|
|
decodeGetInfoAboutSessionsForUserRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
router.Handle("/api/v1/kolide/users/{id}/sessions",
|
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(canModifyUser(makeDeleteSessionsForUserEndpoint(svc))),
|
2016-09-05 19:50:57 +00:00
|
|
|
decodeDeleteSessionsForUserRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("DELETE")
|
|
|
|
|
|
|
|
router.Handle("/api/v1/kolide/sessions/{id}",
|
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(mustBeAdmin(makeGetInfoAboutSessionEndpoint(svc))),
|
2016-09-05 19:50:57 +00:00
|
|
|
decodeGetInfoAboutSessionRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
|
|
|
router.Handle("/api/v1/kolide/sessions/{id}",
|
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(mustBeAdmin(makeDeleteSessionEndpoint(svc))),
|
2016-09-05 19:50:57 +00:00
|
|
|
decodeDeleteSessionRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("DELETE")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/queries/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeGetQueryEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeGetQueryRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/queries",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeGetAllQueriesEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeNoParamsRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/queries",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeCreateQueryEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeCreateQueryRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("POST")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/queries/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeModifyQueryEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeModifyQueryRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("PATCH")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/queries/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeDeleteQueryEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeDeleteQueryRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("DELETE")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeGetPackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeGetPackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeGetAllPacksEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeNoParamsRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeCreatePackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeCreatePackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("POST")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeModifyPackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeModifyPackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("PATCH")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs/{id}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeDeletePackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeDeletePackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("DELETE")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs/{pid}/queries/{qid}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeAddQueryToPackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeAddQueryToPackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs/{id}/queries",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeGetQueriesInPackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeGetQueriesInPackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("GET")
|
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
router.Handle("/api/v1/kolide/packs/{pid}/queries/{qid}",
|
2016-09-04 05:13:42 +00:00
|
|
|
kithttp.NewServer(
|
|
|
|
ctx,
|
2016-09-08 01:24:11 +00:00
|
|
|
authenticated(makeDeleteQueryFromPackEndpoint(svc)),
|
2016-09-04 05:13:42 +00:00
|
|
|
decodeDeleteQueryFromPackRequest,
|
|
|
|
encodeResponse,
|
|
|
|
opts...,
|
|
|
|
),
|
|
|
|
).Methods("DELETE")
|
2016-09-04 19:43:12 +00:00
|
|
|
}
|
2016-09-01 04:51:38 +00:00
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
// MakeHandler creates an http handler for the Kolide API
|
2016-09-08 01:24:11 +00:00
|
|
|
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(
|
2016-09-08 01:24:11 +00:00
|
|
|
setViewerContext(svc, ds, jwtKey, logger),
|
2016-09-04 19:43:12 +00:00
|
|
|
),
|
|
|
|
kithttp.ServerErrorLogger(logger),
|
|
|
|
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()
|
2016-09-08 01:24:11 +00:00
|
|
|
attachAPIRoutes(r, ctx, svc, opts)
|
2016-09-05 14:15:58 +00:00
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
return r
|
|
|
|
}
|