fleet/server/service/endpoint_appconfig.go
Mike Arpaia 018e10ea66
Add fleetctl config and auth commands (#1751)
```
$ fleetctl config set address https://localhost:8080
[+] Set the "address" config key to "https://localhost:8080" in the "default" context

$ fleetctl config set ignore_tls true
[+] Set the "ignore_tls" config key to "true" in the "default" context

$ fleetctl setup --email mike@arpaia.co --password "abc123"
[+] Fleet setup successful and context configured!

$ cat ~/.fleet/config
contexts:
  default:
    address: https://localhost:8080
    email: mike@arpaia.co
    ignore_tls: true
    token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6IlUvdm05Vk9wSG0xUlA4SUtjQnBhb2ovWlo1TXppSEVXcFRCNFNPb2tHQnNLUFpDQXFieVpWWnpJb0UvczQzcWkyd1pHZXJOa29SNFVIQ2hNZUc0K09RPT0ifQ.rHawSN8JvD4jjWAPTYX2Ep9ZpMt3u4mSIQcu920C-_s

$ fleetctl logout
[+] Fleet logout successful and local token cleared!

$ cat ~/.fleet/config
contexts:
  default:
    address: https://localhost:8080
    email: mike@arpaia.co
    ignore_tls: true
    token: ""
```
2018-05-04 10:53:21 -06:00

122 lines
3.8 KiB
Go

package service
import (
"context"
"errors"
"github.com/go-kit/kit/endpoint"
"github.com/kolide/fleet/server/contexts/viewer"
"github.com/kolide/fleet/server/kolide"
)
type appConfigRequest struct {
Payload kolide.AppConfigPayload
}
type appConfigResponse struct {
OrgInfo *kolide.OrgInfo `json:"org_info,omitemtpy"`
ServerSettings *kolide.ServerSettings `json:"server_settings,omitempty"`
SMTPSettings *kolide.SMTPSettingsPayload `json:"smtp_settings,omitempty"`
SSOSettings *kolide.SSOSettingsPayload `json:"sso_settings,omitempty"`
Err error `json:"error,omitempty"`
}
func (r appConfigResponse) error() error { return r.Err }
func makeGetAppConfigEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
vc, ok := viewer.FromContext(ctx)
if !ok {
return nil, errors.New("could not fetch user")
}
config, err := svc.AppConfig(ctx)
if err != nil {
return nil, err
}
var smtpSettings *kolide.SMTPSettingsPayload
var ssoSettings *kolide.SSOSettingsPayload
// only admin can see smtp settings
if vc.IsAdmin() {
smtpSettings = smtpSettingsFromAppConfig(config)
if smtpSettings.SMTPPassword != nil {
*smtpSettings.SMTPPassword = "********"
}
ssoSettings = &kolide.SSOSettingsPayload{
EntityID: &config.EntityID,
IssuerURI: &config.IssuerURI,
IDPImageURL: &config.IDPImageURL,
Metadata: &config.Metadata,
MetadataURL: &config.MetadataURL,
IDPName: &config.IDPName,
EnableSSO: &config.EnableSSO,
}
}
response := appConfigResponse{
OrgInfo: &kolide.OrgInfo{
OrgName: &config.OrgName,
OrgLogoURL: &config.OrgLogoURL,
},
ServerSettings: &kolide.ServerSettings{
KolideServerURL: &config.KolideServerURL,
EnrollSecret: &config.EnrollSecret,
},
SMTPSettings: smtpSettings,
SSOSettings: ssoSettings,
}
return response, nil
}
}
func makeModifyAppConfigEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(appConfigRequest)
config, err := svc.ModifyAppConfig(ctx, req.Payload)
if err != nil {
return appConfigResponse{Err: err}, nil
}
response := appConfigResponse{
OrgInfo: &kolide.OrgInfo{
OrgName: &config.OrgName,
OrgLogoURL: &config.OrgLogoURL,
},
ServerSettings: &kolide.ServerSettings{
KolideServerURL: &config.KolideServerURL,
EnrollSecret: &config.EnrollSecret,
},
SMTPSettings: smtpSettingsFromAppConfig(config),
SSOSettings: &kolide.SSOSettingsPayload{
EntityID: &config.EntityID,
IssuerURI: &config.IssuerURI,
IDPImageURL: &config.IDPImageURL,
Metadata: &config.Metadata,
MetadataURL: &config.MetadataURL,
IDPName: &config.IDPName,
EnableSSO: &config.EnableSSO,
},
}
if response.SMTPSettings.SMTPPassword != nil {
*response.SMTPSettings.SMTPPassword = "********"
}
return response, nil
}
}
func smtpSettingsFromAppConfig(config *kolide.AppConfig) *kolide.SMTPSettingsPayload {
authType := config.SMTPAuthenticationType.String()
authMethod := config.SMTPAuthenticationMethod.String()
return &kolide.SMTPSettingsPayload{
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,
}
}