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"
|
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
|
|
|
|
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"
|
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
|
|
|
"github.com/gorilla/mux"
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-12-22 17:39:44 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-08-28 03:59:17 +00:00
|
|
|
)
|
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
// KolideEndpoints is a collection of RPC endpoints implemented by the Kolide API.
|
|
|
|
type KolideEndpoints struct {
|
2016-11-16 21:07:50 +00:00
|
|
|
Login endpoint.Endpoint
|
|
|
|
Logout endpoint.Endpoint
|
|
|
|
ForgotPassword endpoint.Endpoint
|
|
|
|
ResetPassword endpoint.Endpoint
|
|
|
|
Me endpoint.Endpoint
|
2016-12-14 18:11:43 +00:00
|
|
|
ChangePassword endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
CreateUser endpoint.Endpoint
|
|
|
|
GetUser endpoint.Endpoint
|
|
|
|
ListUsers endpoint.Endpoint
|
|
|
|
ModifyUser endpoint.Endpoint
|
2017-01-17 16:43:59 +00:00
|
|
|
AdminUser endpoint.Endpoint
|
|
|
|
EnableUser endpoint.Endpoint
|
2017-01-06 22:38:39 +00:00
|
|
|
RequirePasswordReset endpoint.Endpoint
|
2017-01-10 04:42:50 +00:00
|
|
|
PerformRequiredPasswordReset endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
GetSessionsForUserInfo endpoint.Endpoint
|
|
|
|
DeleteSessionsForUser endpoint.Endpoint
|
|
|
|
GetSessionInfo endpoint.Endpoint
|
|
|
|
DeleteSession endpoint.Endpoint
|
|
|
|
GetAppConfig endpoint.Endpoint
|
|
|
|
ModifyAppConfig endpoint.Endpoint
|
|
|
|
CreateInvite endpoint.Endpoint
|
|
|
|
ListInvites endpoint.Endpoint
|
|
|
|
DeleteInvite endpoint.Endpoint
|
2016-12-30 01:58:12 +00:00
|
|
|
VerifyInvite endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
GetQuery endpoint.Endpoint
|
|
|
|
ListQueries endpoint.Endpoint
|
|
|
|
CreateQuery endpoint.Endpoint
|
|
|
|
ModifyQuery endpoint.Endpoint
|
|
|
|
DeleteQuery endpoint.Endpoint
|
2016-12-09 17:12:45 +00:00
|
|
|
DeleteQueries endpoint.Endpoint
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyQuerySpecs endpoint.Endpoint
|
|
|
|
GetQuerySpecs endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
CreateDistributedQueryCampaign endpoint.Endpoint
|
|
|
|
GetPack endpoint.Endpoint
|
|
|
|
ListPacks endpoint.Endpoint
|
|
|
|
DeletePack endpoint.Endpoint
|
2016-12-13 22:22:05 +00:00
|
|
|
GetScheduledQueriesInPack endpoint.Endpoint
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyPackSpecs endpoint.Endpoint
|
|
|
|
GetPackSpecs endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
EnrollAgent endpoint.Endpoint
|
|
|
|
GetClientConfig endpoint.Endpoint
|
|
|
|
GetDistributedQueries endpoint.Endpoint
|
|
|
|
SubmitDistributedQueryResults endpoint.Endpoint
|
|
|
|
SubmitLogs endpoint.Endpoint
|
|
|
|
GetLabel endpoint.Endpoint
|
|
|
|
ListLabels endpoint.Endpoint
|
|
|
|
DeleteLabel endpoint.Endpoint
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyLabelSpecs endpoint.Endpoint
|
|
|
|
GetLabelSpecs endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
GetHost endpoint.Endpoint
|
|
|
|
DeleteHost endpoint.Endpoint
|
|
|
|
ListHosts endpoint.Endpoint
|
2017-01-04 21:16:17 +00:00
|
|
|
GetHostSummary endpoint.Endpoint
|
2016-11-16 21:07:50 +00:00
|
|
|
SearchTargets endpoint.Endpoint
|
2016-12-29 18:32:28 +00:00
|
|
|
GetOptions endpoint.Endpoint
|
|
|
|
ModifyOptions endpoint.Endpoint
|
2017-03-30 23:56:11 +00:00
|
|
|
ResetOptions endpoint.Endpoint
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyOsqueryOptionsSpec endpoint.Endpoint
|
|
|
|
GetOsqueryOptionsSpec endpoint.Endpoint
|
2017-01-20 19:32:10 +00:00
|
|
|
GetCertificate endpoint.Endpoint
|
2017-01-27 13:35:58 +00:00
|
|
|
ChangeEmail endpoint.Endpoint
|
2017-05-09 00:43:48 +00:00
|
|
|
InitiateSSO endpoint.Endpoint
|
|
|
|
CallbackSSO endpoint.Endpoint
|
2017-05-17 15:58:40 +00:00
|
|
|
SSOSettings endpoint.Endpoint
|
2017-08-18 15:37:33 +00:00
|
|
|
GetFIM endpoint.Endpoint
|
|
|
|
ModifyFIM endpoint.Endpoint
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
// MakeKolideServerEndpoints creates the Kolide API endpoints.
|
|
|
|
func MakeKolideServerEndpoints(svc kolide.Service, jwtKey string) KolideEndpoints {
|
|
|
|
return KolideEndpoints{
|
2016-09-29 04:21:39 +00:00
|
|
|
Login: makeLoginEndpoint(svc),
|
|
|
|
Logout: makeLogoutEndpoint(svc),
|
|
|
|
ForgotPassword: makeForgotPasswordEndpoint(svc),
|
|
|
|
ResetPassword: makeResetPasswordEndpoint(svc),
|
|
|
|
CreateUser: makeCreateUserEndpoint(svc),
|
2016-12-30 01:58:12 +00:00
|
|
|
VerifyInvite: makeVerifyInviteEndpoint(svc),
|
2017-05-09 00:43:48 +00:00
|
|
|
InitiateSSO: makeInitiateSSOEndpoint(svc),
|
|
|
|
CallbackSSO: makeCallbackSSOEndpoint(svc),
|
2017-05-17 15:58:40 +00:00
|
|
|
SSOSettings: makeSSOSettingsEndpoint(svc),
|
2016-09-29 04:21:39 +00:00
|
|
|
|
|
|
|
// Authenticated user endpoints
|
2016-12-13 16:57:49 +00:00
|
|
|
// Each of these endpoints should have exactly one
|
|
|
|
// authorization check around the make.*Endpoint method. At a
|
|
|
|
// minimum, canPerformActions. Some endpoints use
|
|
|
|
// stricter/different checks and should NOT also use
|
|
|
|
// canPerformActions (these other checks should also call
|
|
|
|
// canPerformActions if that is appropriate).
|
2017-01-10 04:42:50 +00:00
|
|
|
Me: authenticatedUser(jwtKey, svc, canPerformActions(makeGetSessionUserEndpoint(svc))),
|
|
|
|
ChangePassword: authenticatedUser(jwtKey, svc, canPerformActions(makeChangePasswordEndpoint(svc))),
|
|
|
|
GetUser: authenticatedUser(jwtKey, svc, canReadUser(makeGetUserEndpoint(svc))),
|
|
|
|
ListUsers: authenticatedUser(jwtKey, svc, canPerformActions(makeListUsersEndpoint(svc))),
|
2017-01-17 16:43:59 +00:00
|
|
|
ModifyUser: authenticatedUser(jwtKey, svc, canPerformActions(makeModifyUserEndpoint(svc))),
|
|
|
|
AdminUser: authenticatedUser(jwtKey, svc, mustBeAdmin(makeAdminUserEndpoint(svc))),
|
|
|
|
EnableUser: authenticatedUser(jwtKey, svc, mustBeAdmin(makeEnableUserEndpoint(svc))),
|
2017-01-10 04:42:50 +00:00
|
|
|
RequirePasswordReset: authenticatedUser(jwtKey, svc, mustBeAdmin(makeRequirePasswordResetEndpoint(svc))),
|
|
|
|
// PerformRequiredPasswordReset needs only to authenticate the
|
|
|
|
// logged in user
|
|
|
|
PerformRequiredPasswordReset: authenticatedUser(jwtKey, svc, makePerformRequiredPasswordResetEndpoint(svc)),
|
2016-11-16 21:07:50 +00:00
|
|
|
GetSessionsForUserInfo: authenticatedUser(jwtKey, svc, canReadUser(makeGetInfoAboutSessionsForUserEndpoint(svc))),
|
|
|
|
DeleteSessionsForUser: authenticatedUser(jwtKey, svc, canModifyUser(makeDeleteSessionsForUserEndpoint(svc))),
|
|
|
|
GetSessionInfo: authenticatedUser(jwtKey, svc, mustBeAdmin(makeGetInfoAboutSessionEndpoint(svc))),
|
|
|
|
DeleteSession: authenticatedUser(jwtKey, svc, mustBeAdmin(makeDeleteSessionEndpoint(svc))),
|
2016-12-13 16:57:49 +00:00
|
|
|
GetAppConfig: authenticatedUser(jwtKey, svc, canPerformActions(makeGetAppConfigEndpoint(svc))),
|
2017-01-18 15:05:09 +00:00
|
|
|
ModifyAppConfig: authenticatedUser(jwtKey, svc, mustBeAdmin(makeModifyAppConfigEndpoint(svc))),
|
2016-11-16 21:07:50 +00:00
|
|
|
CreateInvite: authenticatedUser(jwtKey, svc, mustBeAdmin(makeCreateInviteEndpoint(svc))),
|
|
|
|
ListInvites: authenticatedUser(jwtKey, svc, mustBeAdmin(makeListInvitesEndpoint(svc))),
|
|
|
|
DeleteInvite: authenticatedUser(jwtKey, svc, mustBeAdmin(makeDeleteInviteEndpoint(svc))),
|
2016-12-13 22:22:05 +00:00
|
|
|
GetQuery: authenticatedUser(jwtKey, svc, makeGetQueryEndpoint(svc)),
|
|
|
|
ListQueries: authenticatedUser(jwtKey, svc, makeListQueriesEndpoint(svc)),
|
|
|
|
CreateQuery: authenticatedUser(jwtKey, svc, makeCreateQueryEndpoint(svc)),
|
|
|
|
ModifyQuery: authenticatedUser(jwtKey, svc, makeModifyQueryEndpoint(svc)),
|
|
|
|
DeleteQuery: authenticatedUser(jwtKey, svc, makeDeleteQueryEndpoint(svc)),
|
|
|
|
DeleteQueries: authenticatedUser(jwtKey, svc, makeDeleteQueriesEndpoint(svc)),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyQuerySpecs: authenticatedUser(jwtKey, svc, makeApplyQuerySpecsEndpoint(svc)),
|
|
|
|
GetQuerySpecs: authenticatedUser(jwtKey, svc, makeGetQuerySpecsEndpoint(svc)),
|
2016-12-13 22:22:05 +00:00
|
|
|
CreateDistributedQueryCampaign: authenticatedUser(jwtKey, svc, makeCreateDistributedQueryCampaignEndpoint(svc)),
|
|
|
|
GetPack: authenticatedUser(jwtKey, svc, makeGetPackEndpoint(svc)),
|
|
|
|
ListPacks: authenticatedUser(jwtKey, svc, makeListPacksEndpoint(svc)),
|
|
|
|
DeletePack: authenticatedUser(jwtKey, svc, makeDeletePackEndpoint(svc)),
|
|
|
|
GetScheduledQueriesInPack: authenticatedUser(jwtKey, svc, makeGetScheduledQueriesInPackEndpoint(svc)),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyPackSpecs: authenticatedUser(jwtKey, svc, makeApplyPackSpecsEndpoint(svc)),
|
|
|
|
GetPackSpecs: authenticatedUser(jwtKey, svc, makeGetPackSpecsEndpoint(svc)),
|
2016-12-13 22:22:05 +00:00
|
|
|
GetHost: authenticatedUser(jwtKey, svc, makeGetHostEndpoint(svc)),
|
|
|
|
ListHosts: authenticatedUser(jwtKey, svc, makeListHostsEndpoint(svc)),
|
2017-01-04 21:16:17 +00:00
|
|
|
GetHostSummary: authenticatedUser(jwtKey, svc, makeGetHostSummaryEndpoint(svc)),
|
2016-12-13 22:22:05 +00:00
|
|
|
DeleteHost: authenticatedUser(jwtKey, svc, makeDeleteHostEndpoint(svc)),
|
|
|
|
GetLabel: authenticatedUser(jwtKey, svc, makeGetLabelEndpoint(svc)),
|
|
|
|
ListLabels: authenticatedUser(jwtKey, svc, makeListLabelsEndpoint(svc)),
|
|
|
|
DeleteLabel: authenticatedUser(jwtKey, svc, makeDeleteLabelEndpoint(svc)),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyLabelSpecs: authenticatedUser(jwtKey, svc, makeApplyLabelSpecsEndpoint(svc)),
|
|
|
|
GetLabelSpecs: authenticatedUser(jwtKey, svc, makeGetLabelSpecsEndpoint(svc)),
|
2016-12-13 22:22:05 +00:00
|
|
|
SearchTargets: authenticatedUser(jwtKey, svc, makeSearchTargetsEndpoint(svc)),
|
2016-12-29 18:32:28 +00:00
|
|
|
GetOptions: authenticatedUser(jwtKey, svc, mustBeAdmin(makeGetOptionsEndpoint(svc))),
|
|
|
|
ModifyOptions: authenticatedUser(jwtKey, svc, mustBeAdmin(makeModifyOptionsEndpoint(svc))),
|
2017-03-30 23:56:11 +00:00
|
|
|
ResetOptions: authenticatedUser(jwtKey, svc, mustBeAdmin(makeResetOptionsEndpoint(svc))),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyOsqueryOptionsSpec: authenticatedUser(jwtKey, svc, makeApplyOsqueryOptionsSpecEndpoint(svc)),
|
|
|
|
GetOsqueryOptionsSpec: authenticatedUser(jwtKey, svc, makeGetOsqueryOptionsSpecEndpoint(svc)),
|
2017-01-20 19:32:10 +00:00
|
|
|
GetCertificate: authenticatedUser(jwtKey, svc, makeCertificateEndpoint(svc)),
|
2017-01-27 13:35:58 +00:00
|
|
|
ChangeEmail: authenticatedUser(jwtKey, svc, makeChangeEmailEndpoint(svc)),
|
2017-08-18 15:37:33 +00:00
|
|
|
GetFIM: authenticatedUser(jwtKey, svc, makeGetFIMEndpoint(svc)),
|
|
|
|
ModifyFIM: authenticatedUser(jwtKey, svc, makeModifyFIMEndpoint(svc)),
|
2016-09-29 04:21:39 +00:00
|
|
|
|
|
|
|
// Osquery endpoints
|
|
|
|
EnrollAgent: makeEnrollAgentEndpoint(svc),
|
|
|
|
GetClientConfig: authenticatedHost(svc, makeGetClientConfigEndpoint(svc)),
|
|
|
|
GetDistributedQueries: authenticatedHost(svc, makeGetDistributedQueriesEndpoint(svc)),
|
|
|
|
SubmitDistributedQueryResults: authenticatedHost(svc, makeSubmitDistributedQueryResultsEndpoint(svc)),
|
|
|
|
SubmitLogs: authenticatedHost(svc, makeSubmitLogsEndpoint(svc)),
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
type kolideHandlers struct {
|
2016-12-22 17:39:44 +00:00
|
|
|
Login http.Handler
|
|
|
|
Logout http.Handler
|
|
|
|
ForgotPassword http.Handler
|
|
|
|
ResetPassword http.Handler
|
|
|
|
Me http.Handler
|
|
|
|
ChangePassword http.Handler
|
|
|
|
CreateUser http.Handler
|
|
|
|
GetUser http.Handler
|
|
|
|
ListUsers http.Handler
|
|
|
|
ModifyUser http.Handler
|
2017-01-17 16:43:59 +00:00
|
|
|
AdminUser http.Handler
|
|
|
|
EnableUser http.Handler
|
2017-01-06 22:38:39 +00:00
|
|
|
RequirePasswordReset http.Handler
|
2017-01-10 04:42:50 +00:00
|
|
|
PerformRequiredPasswordReset http.Handler
|
2016-12-22 17:39:44 +00:00
|
|
|
GetSessionsForUserInfo http.Handler
|
|
|
|
DeleteSessionsForUser http.Handler
|
|
|
|
GetSessionInfo http.Handler
|
|
|
|
DeleteSession http.Handler
|
|
|
|
GetAppConfig http.Handler
|
|
|
|
ModifyAppConfig http.Handler
|
|
|
|
CreateInvite http.Handler
|
|
|
|
ListInvites http.Handler
|
|
|
|
DeleteInvite http.Handler
|
2016-12-30 01:58:12 +00:00
|
|
|
VerifyInvite http.Handler
|
2016-12-22 17:39:44 +00:00
|
|
|
GetQuery http.Handler
|
|
|
|
ListQueries http.Handler
|
|
|
|
CreateQuery http.Handler
|
|
|
|
ModifyQuery http.Handler
|
|
|
|
DeleteQuery http.Handler
|
|
|
|
DeleteQueries http.Handler
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyQuerySpecs http.Handler
|
|
|
|
GetQuerySpecs http.Handler
|
2016-12-22 17:39:44 +00:00
|
|
|
CreateDistributedQueryCampaign http.Handler
|
|
|
|
GetPack http.Handler
|
|
|
|
ListPacks http.Handler
|
|
|
|
DeletePack http.Handler
|
|
|
|
GetScheduledQueriesInPack http.Handler
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyPackSpecs http.Handler
|
|
|
|
GetPackSpecs http.Handler
|
2016-12-22 17:39:44 +00:00
|
|
|
EnrollAgent http.Handler
|
|
|
|
GetClientConfig http.Handler
|
|
|
|
GetDistributedQueries http.Handler
|
|
|
|
SubmitDistributedQueryResults http.Handler
|
|
|
|
SubmitLogs http.Handler
|
|
|
|
GetLabel http.Handler
|
|
|
|
ListLabels http.Handler
|
|
|
|
DeleteLabel http.Handler
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyLabelSpecs http.Handler
|
|
|
|
GetLabelSpecs http.Handler
|
2016-12-22 17:39:44 +00:00
|
|
|
GetHost http.Handler
|
|
|
|
DeleteHost http.Handler
|
|
|
|
ListHosts http.Handler
|
2017-01-04 21:16:17 +00:00
|
|
|
GetHostSummary http.Handler
|
2016-12-22 17:39:44 +00:00
|
|
|
SearchTargets http.Handler
|
2016-12-29 18:32:28 +00:00
|
|
|
GetOptions http.Handler
|
|
|
|
ModifyOptions http.Handler
|
2017-03-30 23:56:11 +00:00
|
|
|
ResetOptions http.Handler
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyOsqueryOptionsspec http.Handler
|
|
|
|
GetOsqueryOptionsspec http.Handler
|
2017-01-20 19:32:10 +00:00
|
|
|
GetCertificate http.Handler
|
2017-01-27 13:35:58 +00:00
|
|
|
ChangeEmail http.Handler
|
2017-05-09 00:43:48 +00:00
|
|
|
InitiateSSO http.Handler
|
|
|
|
CallbackSSO http.Handler
|
2017-05-17 15:58:40 +00:00
|
|
|
SettingsSSO http.Handler
|
2017-08-18 15:37:33 +00:00
|
|
|
ModifyFIM http.Handler
|
|
|
|
GetFIM http.Handler
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2017-03-15 15:55:30 +00:00
|
|
|
func makeKolideKitHandlers(e KolideEndpoints, opts []kithttp.ServerOption) *kolideHandlers {
|
2016-12-22 17:39:44 +00:00
|
|
|
newServer := func(e endpoint.Endpoint, decodeFn kithttp.DecodeRequestFunc) http.Handler {
|
2017-03-15 15:55:30 +00:00
|
|
|
return kithttp.NewServer(e, decodeFn, encodeResponse, opts...)
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-12-22 17:39:44 +00:00
|
|
|
return &kolideHandlers{
|
2016-11-16 21:07:50 +00:00
|
|
|
Login: newServer(e.Login, decodeLoginRequest),
|
|
|
|
Logout: newServer(e.Logout, decodeNoParamsRequest),
|
|
|
|
ForgotPassword: newServer(e.ForgotPassword, decodeForgotPasswordRequest),
|
|
|
|
ResetPassword: newServer(e.ResetPassword, decodeResetPasswordRequest),
|
|
|
|
Me: newServer(e.Me, decodeNoParamsRequest),
|
2016-12-14 18:11:43 +00:00
|
|
|
ChangePassword: newServer(e.ChangePassword, decodeChangePasswordRequest),
|
2016-11-16 21:07:50 +00:00
|
|
|
CreateUser: newServer(e.CreateUser, decodeCreateUserRequest),
|
|
|
|
GetUser: newServer(e.GetUser, decodeGetUserRequest),
|
|
|
|
ListUsers: newServer(e.ListUsers, decodeListUsersRequest),
|
|
|
|
ModifyUser: newServer(e.ModifyUser, decodeModifyUserRequest),
|
2017-01-06 22:38:39 +00:00
|
|
|
RequirePasswordReset: newServer(e.RequirePasswordReset, decodeRequirePasswordResetRequest),
|
2017-01-10 04:42:50 +00:00
|
|
|
PerformRequiredPasswordReset: newServer(e.PerformRequiredPasswordReset, decodePerformRequiredPasswordResetRequest),
|
2017-01-17 16:43:59 +00:00
|
|
|
EnableUser: newServer(e.EnableUser, decodeEnableUserRequest),
|
|
|
|
AdminUser: newServer(e.AdminUser, decodeAdminUserRequest),
|
2016-11-16 21:07:50 +00:00
|
|
|
GetSessionsForUserInfo: newServer(e.GetSessionsForUserInfo, decodeGetInfoAboutSessionsForUserRequest),
|
|
|
|
DeleteSessionsForUser: newServer(e.DeleteSessionsForUser, decodeDeleteSessionsForUserRequest),
|
|
|
|
GetSessionInfo: newServer(e.GetSessionInfo, decodeGetInfoAboutSessionRequest),
|
|
|
|
DeleteSession: newServer(e.DeleteSession, decodeDeleteSessionRequest),
|
|
|
|
GetAppConfig: newServer(e.GetAppConfig, decodeNoParamsRequest),
|
|
|
|
ModifyAppConfig: newServer(e.ModifyAppConfig, decodeModifyAppConfigRequest),
|
|
|
|
CreateInvite: newServer(e.CreateInvite, decodeCreateInviteRequest),
|
|
|
|
ListInvites: newServer(e.ListInvites, decodeListInvitesRequest),
|
|
|
|
DeleteInvite: newServer(e.DeleteInvite, decodeDeleteInviteRequest),
|
2016-12-30 01:58:12 +00:00
|
|
|
VerifyInvite: newServer(e.VerifyInvite, decodeVerifyInviteRequest),
|
2016-11-16 21:07:50 +00:00
|
|
|
GetQuery: newServer(e.GetQuery, decodeGetQueryRequest),
|
|
|
|
ListQueries: newServer(e.ListQueries, decodeListQueriesRequest),
|
|
|
|
CreateQuery: newServer(e.CreateQuery, decodeCreateQueryRequest),
|
|
|
|
ModifyQuery: newServer(e.ModifyQuery, decodeModifyQueryRequest),
|
|
|
|
DeleteQuery: newServer(e.DeleteQuery, decodeDeleteQueryRequest),
|
2016-12-09 17:12:45 +00:00
|
|
|
DeleteQueries: newServer(e.DeleteQueries, decodeDeleteQueriesRequest),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyQuerySpecs: newServer(e.ApplyQuerySpecs, decodeApplyQuerySpecsRequest),
|
|
|
|
GetQuerySpecs: newServer(e.GetQuerySpecs, decodeNoParamsRequest),
|
2016-11-16 21:07:50 +00:00
|
|
|
CreateDistributedQueryCampaign: newServer(e.CreateDistributedQueryCampaign, decodeCreateDistributedQueryCampaignRequest),
|
2016-09-29 04:21:39 +00:00
|
|
|
GetPack: newServer(e.GetPack, decodeGetPackRequest),
|
2016-10-13 18:21:47 +00:00
|
|
|
ListPacks: newServer(e.ListPacks, decodeListPacksRequest),
|
2016-09-29 04:21:39 +00:00
|
|
|
DeletePack: newServer(e.DeletePack, decodeDeletePackRequest),
|
2016-12-13 22:22:05 +00:00
|
|
|
GetScheduledQueriesInPack: newServer(e.GetScheduledQueriesInPack, decodeGetScheduledQueriesInPackRequest),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyPackSpecs: newServer(e.ApplyPackSpecs, decodeApplyPackSpecsRequest),
|
|
|
|
GetPackSpecs: newServer(e.GetPackSpecs, decodeNoParamsRequest),
|
2016-09-29 04:21:39 +00:00
|
|
|
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),
|
2016-10-03 03:14:35 +00:00
|
|
|
GetLabel: newServer(e.GetLabel, decodeGetLabelRequest),
|
2016-10-13 18:21:47 +00:00
|
|
|
ListLabels: newServer(e.ListLabels, decodeListLabelsRequest),
|
2016-10-03 03:14:35 +00:00
|
|
|
DeleteLabel: newServer(e.DeleteLabel, decodeDeleteLabelRequest),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyLabelSpecs: newServer(e.ApplyLabelSpecs, decodeApplyLabelSpecsRequest),
|
|
|
|
GetLabelSpecs: newServer(e.GetLabelSpecs, decodeNoParamsRequest),
|
2016-10-06 00:10:44 +00:00
|
|
|
GetHost: newServer(e.GetHost, decodeGetHostRequest),
|
|
|
|
DeleteHost: newServer(e.DeleteHost, decodeDeleteHostRequest),
|
2016-10-13 18:21:47 +00:00
|
|
|
ListHosts: newServer(e.ListHosts, decodeListHostsRequest),
|
2017-01-04 21:16:17 +00:00
|
|
|
GetHostSummary: newServer(e.GetHostSummary, decodeNoParamsRequest),
|
2016-11-02 14:59:53 +00:00
|
|
|
SearchTargets: newServer(e.SearchTargets, decodeSearchTargetsRequest),
|
2016-12-29 18:32:28 +00:00
|
|
|
GetOptions: newServer(e.GetOptions, decodeNoParamsRequest),
|
|
|
|
ModifyOptions: newServer(e.ModifyOptions, decodeModifyOptionsRequest),
|
2017-03-30 23:56:11 +00:00
|
|
|
ResetOptions: newServer(e.ResetOptions, decodeNoParamsRequest),
|
2018-05-04 01:01:57 +00:00
|
|
|
ApplyOsqueryOptionsspec: newServer(e.ApplyOsqueryOptionsSpec, decodeApplyOsqueryOptionsSpecRequest),
|
|
|
|
GetOsqueryOptionsspec: newServer(e.GetOsqueryOptionsSpec, decodeNoParamsRequest),
|
2017-01-20 19:32:10 +00:00
|
|
|
GetCertificate: newServer(e.GetCertificate, decodeNoParamsRequest),
|
2017-01-27 13:35:58 +00:00
|
|
|
ChangeEmail: newServer(e.ChangeEmail, decodeChangeEmailRequest),
|
2017-05-09 00:43:48 +00:00
|
|
|
InitiateSSO: newServer(e.InitiateSSO, decodeInitiateSSORequest),
|
|
|
|
CallbackSSO: newServer(e.CallbackSSO, decodeCallbackSSORequest),
|
2017-05-17 15:58:40 +00:00
|
|
|
SettingsSSO: newServer(e.SSOSettings, decodeNoParamsRequest),
|
2017-08-18 15:37:33 +00:00
|
|
|
ModifyFIM: newServer(e.ModifyFIM, decodeModifyFIMRequest),
|
|
|
|
GetFIM: newServer(e.GetFIM, 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
|
|
|
|
2016-09-26 17:14:39 +00:00
|
|
|
// MakeHandler creates an HTTP handler for the Kolide server endpoints.
|
2017-03-15 15:55:30 +00:00
|
|
|
func MakeHandler(svc kolide.Service, jwtKey string, logger kitlog.Logger) http.Handler {
|
2016-09-26 17:14:39 +00:00
|
|
|
kolideAPIOptions := []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
|
2016-09-26 17:14:39 +00:00
|
|
|
setRequestsContexts(svc, jwtKey),
|
2016-09-04 19:43:12 +00:00
|
|
|
),
|
|
|
|
kithttp.ServerErrorLogger(logger),
|
2016-09-23 02:41:58 +00:00
|
|
|
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-26 17:14:39 +00:00
|
|
|
kolideEndpoints := MakeKolideServerEndpoints(svc, jwtKey)
|
2017-03-15 15:55:30 +00:00
|
|
|
kolideHandlers := makeKolideKitHandlers(kolideEndpoints, kolideAPIOptions)
|
2016-09-26 17:14:39 +00:00
|
|
|
|
2016-09-04 19:43:12 +00:00
|
|
|
r := mux.NewRouter()
|
2016-09-26 17:14:39 +00:00
|
|
|
attachKolideAPIRoutes(r, kolideHandlers)
|
2016-12-22 17:39:44 +00:00
|
|
|
addMetrics(r)
|
2016-09-05 14:15:58 +00:00
|
|
|
|
2017-03-01 21:14:26 +00:00
|
|
|
r.PathPrefix("/api/v1/kolide/results/").
|
|
|
|
Handler(makeStreamDistributedQueryCampaignResultsHandler(svc, jwtKey, logger)).
|
|
|
|
Name("distributed_query_results")
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
return r
|
|
|
|
}
|
2016-09-26 17:14:39 +00:00
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
// addMetrics decorates each hander with prometheus instrumentation
|
|
|
|
func addMetrics(r *mux.Router) {
|
|
|
|
walkFn := func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
|
|
|
|
route.Handler(prometheus.InstrumentHandler(route.GetName(), route.GetHandler()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.Walk(walkFn)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func attachKolideAPIRoutes(r *mux.Router, h *kolideHandlers) {
|
|
|
|
r.Handle("/api/v1/kolide/login", h.Login).Methods("POST").Name("login")
|
|
|
|
r.Handle("/api/v1/kolide/logout", h.Logout).Methods("POST").Name("logout")
|
|
|
|
r.Handle("/api/v1/kolide/forgot_password", h.ForgotPassword).Methods("POST").Name("forgot_password")
|
|
|
|
r.Handle("/api/v1/kolide/reset_password", h.ResetPassword).Methods("POST").Name("reset_password")
|
|
|
|
r.Handle("/api/v1/kolide/me", h.Me).Methods("GET").Name("me")
|
|
|
|
r.Handle("/api/v1/kolide/change_password", h.ChangePassword).Methods("POST").Name("change_password")
|
2017-01-10 04:42:50 +00:00
|
|
|
r.Handle("/api/v1/kolide/perform_required_password_reset", h.PerformRequiredPasswordReset).Methods("POST").Name("perform_required_password_reset")
|
2017-05-09 00:43:48 +00:00
|
|
|
r.Handle("/api/v1/kolide/sso", h.InitiateSSO).Methods("POST").Name("intiate_sso")
|
2017-05-17 15:58:40 +00:00
|
|
|
r.Handle("/api/v1/kolide/sso", h.SettingsSSO).Methods("GET").Name("sso_config")
|
2017-05-09 00:43:48 +00:00
|
|
|
r.Handle("/api/v1/kolide/sso/callback", h.CallbackSSO).Methods("POST").Name("callback_sso")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/users", h.ListUsers).Methods("GET").Name("list_users")
|
|
|
|
r.Handle("/api/v1/kolide/users", h.CreateUser).Methods("POST").Name("create_user")
|
|
|
|
r.Handle("/api/v1/kolide/users/{id}", h.GetUser).Methods("GET").Name("get_user")
|
|
|
|
r.Handle("/api/v1/kolide/users/{id}", h.ModifyUser).Methods("PATCH").Name("modify_user")
|
2017-01-17 16:43:59 +00:00
|
|
|
r.Handle("/api/v1/kolide/users/{id}/enable", h.EnableUser).Methods("POST").Name("enable_user")
|
|
|
|
r.Handle("/api/v1/kolide/users/{id}/admin", h.AdminUser).Methods("POST").Name("admin_user")
|
2017-01-06 22:38:39 +00:00
|
|
|
r.Handle("/api/v1/kolide/users/{id}/require_password_reset", h.RequirePasswordReset).Methods("POST").Name("require_password_reset")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/users/{id}/sessions", h.GetSessionsForUserInfo).Methods("GET").Name("get_session_for_user")
|
|
|
|
r.Handle("/api/v1/kolide/users/{id}/sessions", h.DeleteSessionsForUser).Methods("DELETE").Name("delete_session_for_user")
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/sessions/{id}", h.GetSessionInfo).Methods("GET").Name("get_session_info")
|
|
|
|
r.Handle("/api/v1/kolide/sessions/{id}", h.DeleteSession).Methods("DELETE").Name("delete_session")
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2017-01-20 19:32:10 +00:00
|
|
|
r.Handle("/api/v1/kolide/config/certificate", h.GetCertificate).Methods("GET").Name("get_certificate")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/config", h.GetAppConfig).Methods("GET").Name("get_app_config")
|
|
|
|
r.Handle("/api/v1/kolide/config", h.ModifyAppConfig).Methods("PATCH").Name("modify_app_config")
|
|
|
|
r.Handle("/api/v1/kolide/invites", h.CreateInvite).Methods("POST").Name("create_invite")
|
|
|
|
r.Handle("/api/v1/kolide/invites", h.ListInvites).Methods("GET").Name("list_invites")
|
|
|
|
r.Handle("/api/v1/kolide/invites/{id}", h.DeleteInvite).Methods("DELETE").Name("delete_invite")
|
2016-12-30 01:58:12 +00:00
|
|
|
r.Handle("/api/v1/kolide/invites/{token}", h.VerifyInvite).Methods("GET").Name("verify_invite")
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2017-01-27 13:35:58 +00:00
|
|
|
r.Handle("/api/v1/kolide/email/change/{token}", h.ChangeEmail).Methods("GET").Name("change_email")
|
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/queries/{id}", h.GetQuery).Methods("GET").Name("get_query")
|
|
|
|
r.Handle("/api/v1/kolide/queries", h.ListQueries).Methods("GET").Name("list_queries")
|
|
|
|
r.Handle("/api/v1/kolide/queries", h.CreateQuery).Methods("POST").Name("create_query")
|
|
|
|
r.Handle("/api/v1/kolide/queries/{id}", h.ModifyQuery).Methods("PATCH").Name("modify_query")
|
2018-05-04 18:05:55 +00:00
|
|
|
r.Handle("/api/v1/kolide/queries/{name}", h.DeleteQuery).Methods("DELETE").Name("delete_query")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/queries/delete", h.DeleteQueries).Methods("POST").Name("delete_queries")
|
2018-05-07 19:44:40 +00:00
|
|
|
r.Handle("/api/v1/kolide/spec/queries", h.ApplyQuerySpecs).Methods("POST").Name("apply_query_specs")
|
|
|
|
r.Handle("/api/v1/kolide/spec/queries", h.GetQuerySpecs).Methods("GET").Name("get_query_specs")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/queries/run", h.CreateDistributedQueryCampaign).Methods("POST").Name("create_distributed_query_campaign")
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/packs/{id}", h.GetPack).Methods("GET").Name("get_pack")
|
|
|
|
r.Handle("/api/v1/kolide/packs", h.ListPacks).Methods("GET").Name("list_packs")
|
2018-05-04 18:05:55 +00:00
|
|
|
r.Handle("/api/v1/kolide/packs/{name}", h.DeletePack).Methods("DELETE").Name("delete_pack")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/packs/{id}/scheduled", h.GetScheduledQueriesInPack).Methods("GET").Name("get_scheduled_queries_in_pack")
|
2018-05-07 19:44:40 +00:00
|
|
|
r.Handle("/api/v1/kolide/spec/packs", h.ApplyPackSpecs).Methods("POST").Name("apply_pack_specs")
|
|
|
|
r.Handle("/api/v1/kolide/spec/packs", h.GetPackSpecs).Methods("GET").Name("get_pack_specs")
|
2018-05-04 01:01:57 +00:00
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/labels/{id}", h.GetLabel).Methods("GET").Name("get_label")
|
|
|
|
r.Handle("/api/v1/kolide/labels", h.ListLabels).Methods("GET").Name("list_labels")
|
2018-05-04 18:05:55 +00:00
|
|
|
r.Handle("/api/v1/kolide/labels/{name}", h.DeleteLabel).Methods("DELETE").Name("delete_label")
|
2018-05-07 19:44:40 +00:00
|
|
|
r.Handle("/api/v1/kolide/spec/labels", h.ApplyLabelSpecs).Methods("POST").Name("apply_label_specs")
|
|
|
|
r.Handle("/api/v1/kolide/spec/labels", h.GetLabelSpecs).Methods("GET").Name("get_label_specs")
|
2017-03-28 21:45:18 +00:00
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/hosts", h.ListHosts).Methods("GET").Name("list_hosts")
|
2017-01-04 21:16:17 +00:00
|
|
|
r.Handle("/api/v1/kolide/host_summary", h.GetHostSummary).Methods("GET").Name("get_host_summary")
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/hosts/{id}", h.GetHost).Methods("GET").Name("get_host")
|
|
|
|
r.Handle("/api/v1/kolide/hosts/{id}", h.DeleteHost).Methods("DELETE").Name("delete_host")
|
2016-10-06 00:10:44 +00:00
|
|
|
|
2017-08-18 15:37:33 +00:00
|
|
|
r.Handle("/api/v1/kolide/fim", h.GetFIM).Methods("GET").Name("get_fim")
|
|
|
|
r.Handle("/api/v1/kolide/fim", h.ModifyFIM).Methods("PATCH").Name("post_fim")
|
|
|
|
|
2016-12-29 18:32:28 +00:00
|
|
|
r.Handle("/api/v1/kolide/options", h.GetOptions).Methods("GET").Name("get_options")
|
|
|
|
r.Handle("/api/v1/kolide/options", h.ModifyOptions).Methods("PATCH").Name("modify_options")
|
2017-03-30 23:56:11 +00:00
|
|
|
r.Handle("/api/v1/kolide/options/reset", h.ResetOptions).Methods("GET").Name("reset_options")
|
2018-05-04 01:01:57 +00:00
|
|
|
r.Handle("/api/v1/kolide/osquery_options/spec", h.ApplyOsqueryOptionsspec).Methods("POST").Name("apply_osquery_options_spec")
|
|
|
|
r.Handle("/api/v1/kolide/osquery_options/spec", h.GetOsqueryOptionsspec).Methods("GET").Name("get_osquery_options_spec")
|
2016-12-29 18:32:28 +00:00
|
|
|
|
2016-12-22 17:39:44 +00:00
|
|
|
r.Handle("/api/v1/kolide/targets", h.SearchTargets).Methods("POST").Name("search_targets")
|
2016-11-02 14:59:53 +00:00
|
|
|
|
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")
|
2016-09-26 17:14:39 +00:00
|
|
|
}
|
2016-11-09 17:19:07 +00:00
|
|
|
|
2017-02-09 18:43:45 +00:00
|
|
|
// WithSetup is an http middleware that checks is setup procedures have been completed.
|
|
|
|
// 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.
|
2016-12-02 18:46:31 +00:00
|
|
|
func WithSetup(svc kolide.Service, logger kitlog.Logger, next http.Handler) http.HandlerFunc {
|
|
|
|
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
|
|
|
|
}
|
2017-02-09 18:43:45 +00:00
|
|
|
requireSetup, err := RequireSetup(svc)
|
|
|
|
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.
|
|
|
|
func RedirectLoginToSetup(svc kolide.Service, logger kitlog.Logger, next http.Handler) http.HandlerFunc {
|
|
|
|
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
|
2017-09-01 16:42:46 +00:00
|
|
|
newURL.Path = "/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
|
|
|
|
|
|
|
setupRequired, err := RequireSetup(svc)
|
|
|
|
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
|
|
|
}
|
2017-02-09 18:43:45 +00:00
|
|
|
RedirectSetupToLogin(svc, logger, next).ServeHTTP(w, r)
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 16:42:46 +00:00
|
|
|
// RequireSetup checks to see if the service has been setup.
|
2017-02-09 18:43:45 +00:00
|
|
|
func RequireSetup(svc kolide.Service) (bool, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
users, err := svc.ListUsers(ctx, kolide.ListOptions{Page: 0, PerPage: 1})
|
2016-11-09 17:19:07 +00:00
|
|
|
if err != nil {
|
2017-02-09 18:43:45 +00:00
|
|
|
return false, err
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
2017-02-09 18:43:45 +00:00
|
|
|
if len(users) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
2017-01-11 19:05: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.
|
|
|
|
func RedirectSetupToLogin(svc kolide.Service, logger kitlog.Logger, next http.Handler) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/setup" {
|
|
|
|
newURL := r.URL
|
|
|
|
newURL.Path = "/login"
|
|
|
|
http.Redirect(w, r, newURL.String(), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
}
|