fleet/server/service/service_appconfig.go
Mike Arpaia 9987983cb9 Simplifying SMTP Logic (#892)
* Simplifying SMTP Logic

This commit breaks the test email sending into it's own service method
(thus removing the capability from the API- if we want it back, we can
wire up another endpoint for just that). Additionally, error wrapping is
used through the new ModifyAppConfig service method to ensure that an
error or failed email will always result in an error while ensuring that
the submitted record always get committed (unless a serious error
happens).

* never wrap a nil error

* use err instead of individual errors
2017-01-11 01:27:09 -07:00

154 lines
4.6 KiB
Go

package service
import (
"fmt"
"github.com/kolide/kolide-ose/server/contexts/viewer"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/kolide/kolide-ose/server/mail"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// mailError is set when an error performing mail operations
type mailError struct {
message string
}
func (e mailError) Error() string {
return fmt.Sprintf("a mail error occurred: %s", e.message)
}
func (e mailError) MailError() []map[string]string {
return []map[string]string{
map[string]string{
"name": "base",
"reason": e.message,
},
}
}
func (svc service) NewAppConfig(ctx context.Context, p kolide.AppConfigPayload) (*kolide.AppConfig, error) {
config, err := svc.ds.AppConfig()
if err != nil {
return nil, err
}
newConfig, err := svc.ds.NewAppConfig(appConfigFromAppConfigPayload(p, *config))
if err != nil {
return nil, err
}
return newConfig, nil
}
func (svc service) AppConfig(ctx context.Context) (*kolide.AppConfig, error) {
return svc.ds.AppConfig()
}
func (svc service) SendTestEmail(ctx context.Context, config *kolide.AppConfig) error {
vc, ok := viewer.FromContext(ctx)
if !ok {
return errNoContext
}
testMail := kolide.Email{
Subject: "Hello from Kolide",
To: []string{vc.User.Email},
Mailer: &kolide.SMTPTestMailer{
KolideServerURL: config.KolideServerURL,
},
Config: config,
}
if err := mail.Test(svc.mailService, testMail); err != nil {
return mailError{message: err.Error()}
}
return nil
}
func (svc service) ModifyAppConfig(ctx context.Context, p kolide.AppConfigPayload) (*kolide.AppConfig, error) {
oldAppConfig, err := svc.AppConfig(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed retrieving existing app config")
}
config := appConfigFromAppConfigPayload(p, *oldAppConfig)
if p.SMTPSettings != nil {
if err = svc.SendTestEmail(ctx, config); err != nil {
err = errors.Wrap(err, "test email failed")
config.SMTPConfigured = false
} else {
config.SMTPConfigured = true
}
}
if err := svc.ds.SaveAppConfig(config); err != nil {
err = errors.Wrap(err, "could not save config")
}
return config, err
}
func appConfigFromAppConfigPayload(p kolide.AppConfigPayload, config kolide.AppConfig) *kolide.AppConfig {
if p.OrgInfo != nil && p.OrgInfo.OrgLogoURL != nil {
config.OrgLogoURL = *p.OrgInfo.OrgLogoURL
}
if p.OrgInfo != nil && p.OrgInfo.OrgName != nil {
config.OrgName = *p.OrgInfo.OrgName
}
if p.ServerSettings != nil && p.ServerSettings.KolideServerURL != nil {
config.KolideServerURL = *p.ServerSettings.KolideServerURL
}
if p.SMTPSettings != nil {
if p.SMTPSettings.SMTPAuthenticationMethod == kolide.AuthMethodNameCramMD5 {
config.SMTPAuthenticationMethod = kolide.AuthMethodCramMD5
} else {
config.SMTPAuthenticationMethod = kolide.AuthMethodPlain
}
if p.SMTPSettings.SMTPAuthenticationType == kolide.AuthTypeNameUserNamePassword {
config.SMTPAuthenticationType = kolide.AuthTypeUserNamePassword
} else {
config.SMTPAuthenticationType = kolide.AuthTypeNone
}
config.SMTPConfigured = p.SMTPSettings.SMTPConfigured
config.SMTPDomain = p.SMTPSettings.SMTPDomain
config.SMTPEnableStartTLS = p.SMTPSettings.SMTPEnableStartTLS
config.SMTPEnableTLS = p.SMTPSettings.SMTPEnableTLS
config.SMTPPassword = p.SMTPSettings.SMTPPassword
config.SMTPPort = p.SMTPSettings.SMTPPort
config.SMTPSenderAddress = p.SMTPSettings.SMTPSenderAddress
config.SMTPServer = p.SMTPSettings.SMTPServer
config.SMTPUserName = p.SMTPSettings.SMTPUserName
config.SMTPVerifySSLCerts = p.SMTPSettings.SMTPVerifySSLCerts
}
return &config
}
func smtpSettingsFromAppConfig(config *kolide.AppConfig) *kolide.SMTPSettings {
return &kolide.SMTPSettings{
SMTPConfigured: config.SMTPConfigured,
SMTPSenderAddress: config.SMTPSenderAddress,
SMTPServer: config.SMTPServer,
SMTPPort: config.SMTPPort,
SMTPAuthenticationType: config.SMTPAuthenticationType.String(),
SMTPUserName: config.SMTPUserName,
SMTPPassword: config.SMTPPassword,
SMTPEnableTLS: config.SMTPEnableTLS,
SMTPAuthenticationMethod: config.SMTPAuthenticationMethod.String(),
SMTPDomain: config.SMTPDomain,
SMTPVerifySSLCerts: config.SMTPVerifySSLCerts,
SMTPEnableStartTLS: config.SMTPEnableStartTLS,
}
}
func appConfigPayloadFromAppConfig(config *kolide.AppConfig) *kolide.AppConfigPayload {
return &kolide.AppConfigPayload{
OrgInfo: &kolide.OrgInfo{
OrgLogoURL: &config.OrgLogoURL,
OrgName: &config.OrgName,
},
ServerSettings: &kolide.ServerSettings{
KolideServerURL: &config.KolideServerURL,
},
SMTPSettings: smtpSettingsFromAppConfig(config),
}
}