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"
|
2018-05-04 16:53:21 +00:00
|
|
|
"errors"
|
2016-12-20 21:54:30 +00:00
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/contexts/viewer"
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/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-06-06 22:07:29 +00:00
|
|
|
Payload fleet.AppConfigPayload
|
2017-01-18 15:05:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:30 +00:00
|
|
|
type appConfigResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
OrgInfo *fleet.OrgInfo `json:"org_info,omitempty"`
|
|
|
|
ServerSettings *fleet.ServerSettings `json:"server_settings,omitempty"`
|
|
|
|
SMTPSettings *fleet.SMTPSettingsPayload `json:"smtp_settings,omitempty"`
|
|
|
|
SSOSettings *fleet.SSOSettingsPayload `json:"sso_settings,omitempty"`
|
|
|
|
HostExpirySettings *fleet.HostExpirySettings `json:"host_expiry_settings,omitempty"`
|
|
|
|
HostSettings *fleet.HostSettings `json:"host_settings,omitempty"`
|
|
|
|
License *fleet.LicenseInfo `json:"license,omitempty"`
|
2021-06-16 17:55:41 +00:00
|
|
|
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-06-16 17:55:41 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
var smtpSettings *fleet.SMTPSettingsPayload
|
|
|
|
var ssoSettings *fleet.SSOSettingsPayload
|
|
|
|
var hostExpirySettings *fleet.HostExpirySettings
|
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 {
|
2016-12-20 21:54:30 +00:00
|
|
|
smtpSettings = smtpSettingsFromAppConfig(config)
|
2017-01-18 15:05:09 +00:00
|
|
|
if smtpSettings.SMTPPassword != nil {
|
|
|
|
*smtpSettings.SMTPPassword = "********"
|
2017-01-17 16:43:59 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ssoSettings = &fleet.SSOSettingsPayload{
|
2021-03-30 19:56:20 +00:00
|
|
|
EntityID: &config.EntityID,
|
|
|
|
IssuerURI: &config.IssuerURI,
|
|
|
|
IDPImageURL: &config.IDPImageURL,
|
|
|
|
Metadata: &config.Metadata,
|
|
|
|
MetadataURL: &config.MetadataURL,
|
|
|
|
IDPName: &config.IDPName,
|
|
|
|
EnableSSO: &config.EnableSSO,
|
|
|
|
EnableSSOIdPLogin: &config.EnableSSOIdPLogin,
|
2017-05-09 00:43:48 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
hostExpirySettings = &fleet.HostExpirySettings{
|
2019-10-16 23:35:17 +00:00
|
|
|
HostExpiryEnabled: &config.HostExpiryEnabled,
|
|
|
|
HostExpiryWindow: &config.HostExpiryWindow,
|
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
}
|
|
|
|
response := appConfigResponse{
|
2021-06-06 22:07:29 +00:00
|
|
|
OrgInfo: &fleet.OrgInfo{
|
2016-12-20 21:54:30 +00:00
|
|
|
OrgName: &config.OrgName,
|
|
|
|
OrgLogoURL: &config.OrgLogoURL,
|
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
ServerSettings: &fleet.ServerSettings{
|
2021-06-16 17:55:41 +00:00
|
|
|
ServerURL: &config.ServerURL,
|
2020-01-14 00:53:04 +00:00
|
|
|
LiveQueryDisabled: &config.LiveQueryDisabled,
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
2019-10-16 23:35:17 +00:00
|
|
|
SMTPSettings: smtpSettings,
|
|
|
|
SSOSettings: ssoSettings,
|
|
|
|
HostExpirySettings: hostExpirySettings,
|
2021-06-06 22:07:29 +00:00
|
|
|
HostSettings: &fleet.HostSettings{
|
2020-05-21 15:36:00 +00:00
|
|
|
AdditionalQueries: config.AdditionalQueries,
|
|
|
|
},
|
2021-05-20 00:29:38 +00:00
|
|
|
License: license,
|
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
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
response := appConfigResponse{
|
2021-06-06 22:07:29 +00:00
|
|
|
OrgInfo: &fleet.OrgInfo{
|
2016-12-20 21:54:30 +00:00
|
|
|
OrgName: &config.OrgName,
|
|
|
|
OrgLogoURL: &config.OrgLogoURL,
|
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
ServerSettings: &fleet.ServerSettings{
|
2021-06-16 17:55:41 +00:00
|
|
|
ServerURL: &config.ServerURL,
|
2020-01-14 00:53:04 +00:00
|
|
|
LiveQueryDisabled: &config.LiveQueryDisabled,
|
2016-12-20 21:54:30 +00:00
|
|
|
},
|
2017-01-11 02:00:46 +00:00
|
|
|
SMTPSettings: smtpSettingsFromAppConfig(config),
|
2021-06-06 22:07:29 +00:00
|
|
|
SSOSettings: &fleet.SSOSettingsPayload{
|
2021-03-30 19:56:20 +00:00
|
|
|
EntityID: &config.EntityID,
|
|
|
|
IssuerURI: &config.IssuerURI,
|
|
|
|
IDPImageURL: &config.IDPImageURL,
|
|
|
|
Metadata: &config.Metadata,
|
|
|
|
MetadataURL: &config.MetadataURL,
|
|
|
|
IDPName: &config.IDPName,
|
|
|
|
EnableSSO: &config.EnableSSO,
|
|
|
|
EnableSSOIdPLogin: &config.EnableSSOIdPLogin,
|
2017-05-09 00:43:48 +00:00
|
|
|
},
|
2021-06-06 22:07:29 +00:00
|
|
|
HostExpirySettings: &fleet.HostExpirySettings{
|
2019-10-16 23:35:17 +00:00
|
|
|
HostExpiryEnabled: &config.HostExpiryEnabled,
|
|
|
|
HostExpiryWindow: &config.HostExpiryWindow,
|
|
|
|
},
|
2016-09-22 00:45:57 +00:00
|
|
|
}
|
2017-01-18 15:05:09 +00:00
|
|
|
if response.SMTPSettings.SMTPPassword != nil {
|
|
|
|
*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
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func smtpSettingsFromAppConfig(config *fleet.AppConfig) *fleet.SMTPSettingsPayload {
|
2017-01-18 15:05:09 +00:00
|
|
|
authType := config.SMTPAuthenticationType.String()
|
|
|
|
authMethod := config.SMTPAuthenticationMethod.String()
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.SMTPSettingsPayload{
|
2018-09-07 22:37:35 +00:00
|
|
|
SMTPEnabled: &config.SMTPConfigured,
|
2017-01-18 15:05:09 +00:00
|
|
|
SMTPConfigured: &config.SMTPConfigured,
|
|
|
|
SMTPSenderAddress: &config.SMTPSenderAddress,
|
|
|
|
SMTPServer: &config.SMTPServer,
|
|
|
|
SMTPPort: &config.SMTPPort,
|
|
|
|
SMTPAuthenticationType: &authType,
|
|
|
|
SMTPUserName: &config.SMTPUserName,
|
|
|
|
SMTPPassword: &config.SMTPPassword,
|
|
|
|
SMTPEnableTLS: &config.SMTPEnableTLS,
|
|
|
|
SMTPAuthenticationMethod: &authMethod,
|
|
|
|
SMTPDomain: &config.SMTPDomain,
|
|
|
|
SMTPVerifySSLCerts: &config.SMTPVerifySSLCerts,
|
|
|
|
SMTPEnableStartTLS: &config.SMTPEnableStartTLS,
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 16:12:39 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Apply Enroll Secret Spec
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
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-03-27 01:03:31 +00:00
|
|
|
// Get Enroll Secret Spec
|
2020-05-29 16:12:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getEnrollSecretSpecResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Spec *fleet.EnrollSecretSpec `json:"specs"`
|
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
|
|
|
|
}
|
|
|
|
}
|