2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-22 00:45:57 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2021-06-17 20:47:15 +00:00
|
|
|
"encoding/json"
|
2018-05-04 16:53:21 +00:00
|
|
|
"errors"
|
2016-12-20 21:54:30 +00:00
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-03-27 01:03:31 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
|
|
"github.com/kolide/kit/version"
|
2016-09-22 00:45:57 +00:00
|
|
|
)
|
|
|
|
|
2017-01-18 15:05:09 +00:00
|
|
|
type appConfigRequest struct {
|
2021-08-20 15:27:41 +00:00
|
|
|
Payload json.RawMessage
|
2017-01-18 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:30 +00:00
|
|
|
type appConfigResponse struct {
|
2021-08-20 15:27:41 +00:00
|
|
|
fleet.AppConfig
|
2021-07-30 15:45:49 +00:00
|
|
|
|
2021-10-07 13:19:10 +00:00
|
|
|
UpdateInterval *fleet.UpdateIntervalConfig `json:"update_interval"`
|
|
|
|
Vulnerabilities *fleet.VulnerabilitiesConfig `json:"vulnerabilities"`
|
2021-08-24 20:24:52 +00:00
|
|
|
|
2021-08-20 15:27:41 +00:00
|
|
|
// License is loaded from the service
|
|
|
|
License *fleet.LicenseInfo `json:"license,omitempty"`
|
2021-07-30 15:45:49 +00:00
|
|
|
// Logging is loaded on the fly rather than from the database.
|
2021-08-11 17:56:11 +00:00
|
|
|
Logging *fleet.Logging `json:"logging,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-22 00:45:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:30 +00:00
|
|
|
func (r appConfigResponse) error() error { return r.Err }
|
2016-09-22 00:45:57 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetAppConfigEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-09-22 00:45:57 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2016-12-20 21:54:30 +00:00
|
|
|
vc, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
2018-05-04 16:53:21 +00:00
|
|
|
return nil, errors.New("could not fetch user")
|
2016-12-20 21:54:30 +00:00
|
|
|
}
|
2016-11-04 20:44:38 +00:00
|
|
|
config, err := svc.AppConfig(ctx)
|
2016-09-22 00:45:57 +00:00
|
|
|
if err != nil {
|
2016-12-20 21:54:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-20 00:29:38 +00:00
|
|
|
license, err := svc.License(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-30 15:45:49 +00:00
|
|
|
loggingConfig, err := svc.LoggingConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-24 20:24:52 +00:00
|
|
|
updateIntervalConfig, err := svc.UpdateIntervalConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-07 13:19:10 +00:00
|
|
|
vulnConfig, err := svc.VulnerabilitiesConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-16 17:55:41 +00:00
|
|
|
|
2021-08-20 15:27:41 +00:00
|
|
|
var smtpSettings fleet.SMTPSettings
|
|
|
|
var ssoSettings fleet.SSOSettings
|
|
|
|
var hostExpirySettings fleet.HostExpirySettings
|
2021-06-17 20:47:15 +00:00
|
|
|
var agentOptions *json.RawMessage
|
2019-10-16 23:35:17 +00:00
|
|
|
// only admin can see smtp, sso, and host expiry settings
|
2021-06-16 17:55:41 +00:00
|
|
|
if vc.User.GlobalRole != nil && *vc.User.GlobalRole == fleet.RoleAdmin {
|
2021-08-20 15:27:41 +00:00
|
|
|
smtpSettings = config.SMTPSettings
|
|
|
|
if smtpSettings.SMTPPassword != "" {
|
|
|
|
smtpSettings.SMTPPassword = "********"
|
2019-10-16 23:35:17 +00:00
|
|
|
}
|
2021-08-20 15:27:41 +00:00
|
|
|
ssoSettings = config.SSOSettings
|
|
|
|
hostExpirySettings = config.HostExpirySettings
|
2021-06-17 20:47:15 +00:00
|
|
|
agentOptions = config.AgentOptions
|
2016-12-20 21:54:30 +00:00
|
|
|
}
|
2021-08-20 15:27:41 +00:00
|
|
|
hostSettings := config.HostSettings
|
2016-12-20 21:54:30 +00:00
|
|
|
response := appConfigResponse{
|
2021-08-20 15:27:41 +00:00
|
|
|
AppConfig: fleet.AppConfig{
|
|
|
|
OrgInfo: config.OrgInfo,
|
|
|
|
ServerSettings: config.ServerSettings,
|
|
|
|
HostSettings: hostSettings,
|
|
|
|
VulnerabilitySettings: config.VulnerabilitySettings,
|
|
|
|
|
|
|
|
SMTPSettings: smtpSettings,
|
|
|
|
SSOSettings: ssoSettings,
|
|
|
|
HostExpirySettings: hostExpirySettings,
|
|
|
|
AgentOptions: agentOptions,
|
2021-08-27 14:15:36 +00:00
|
|
|
|
|
|
|
WebhookSettings: config.WebhookSettings,
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
2021-10-07 13:19:10 +00:00
|
|
|
UpdateInterval: updateIntervalConfig,
|
|
|
|
Vulnerabilities: vulnConfig,
|
|
|
|
License: license,
|
|
|
|
Logging: loggingConfig,
|
2016-09-22 00:45:57 +00:00
|
|
|
}
|
2016-11-04 20:44:38 +00:00
|
|
|
return response, nil
|
2016-09-22 00:45:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeModifyAppConfigEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-09-22 00:45:57 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2017-01-18 15:05:09 +00:00
|
|
|
req := request.(appConfigRequest)
|
|
|
|
config, err := svc.ModifyAppConfig(ctx, req.Payload)
|
2016-09-22 00:45:57 +00:00
|
|
|
if err != nil {
|
2016-12-20 21:54:30 +00:00
|
|
|
return appConfigResponse{Err: err}, nil
|
2016-11-04 20:44:38 +00:00
|
|
|
}
|
2021-06-18 15:34:49 +00:00
|
|
|
license, err := svc.License(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-30 15:45:49 +00:00
|
|
|
loggingConfig, err := svc.LoggingConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
response := appConfigResponse{
|
2021-08-20 15:27:41 +00:00
|
|
|
AppConfig: *config,
|
|
|
|
License: license,
|
|
|
|
Logging: loggingConfig,
|
2016-09-22 00:45:57 +00:00
|
|
|
}
|
2021-08-20 15:27:41 +00:00
|
|
|
|
|
|
|
if response.SMTPSettings.SMTPPassword != "" {
|
|
|
|
response.SMTPSettings.SMTPPassword = "********"
|
2017-01-11 04:41:58 +00:00
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
return response, nil
|
2016-09-22 00:45:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 15:05:09 +00:00
|
|
|
|
2020-05-29 16:12:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2021-10-11 14:58:27 +00:00
|
|
|
// Apply enroll secret spec
|
2020-05-29 16:12:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type applyEnrollSecretSpecRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Spec *fleet.EnrollSecretSpec `json:"spec"`
|
2020-05-29 16:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type applyEnrollSecretSpecResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r applyEnrollSecretSpecResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeApplyEnrollSecretSpecEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2020-05-29 16:12:39 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(applyEnrollSecretSpecRequest)
|
|
|
|
err := svc.ApplyEnrollSecretSpec(ctx, req.Spec)
|
|
|
|
if err != nil {
|
|
|
|
return applyEnrollSecretSpecResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return applyEnrollSecretSpecResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2021-10-11 14:58:27 +00:00
|
|
|
// Get enroll secret spec
|
2020-05-29 16:12:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getEnrollSecretSpecResponse struct {
|
2021-06-29 17:58:15 +00:00
|
|
|
Spec *fleet.EnrollSecretSpec `json:"spec"`
|
2021-06-16 17:55:41 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2020-05-29 16:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getEnrollSecretSpecResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetEnrollSecretSpecEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2020-05-29 16:12:39 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
specs, err := svc.GetEnrollSecretSpec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return getEnrollSecretSpecResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getEnrollSecretSpecResponse{Spec: specs}, nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-27 01:03:31 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Version
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type versionResponse struct {
|
|
|
|
*version.Info
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r versionResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeVersionEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2021-03-27 01:03:31 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
info, err := svc.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return versionResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return versionResponse{Info: info}, nil
|
|
|
|
}
|
|
|
|
}
|