fleet/server/service/endpoint_setup.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

79 lines
2.4 KiB
Go

package service
import (
"context"
"github.com/go-kit/kit/endpoint"
"github.com/kolide/fleet/server/kolide"
)
type setupRequest struct {
Admin *kolide.UserPayload `json:"admin"`
OrgInfo *kolide.OrgInfo `json:"org_info"`
KolideServerURL *string `json:"kolide_server_url,omitempty"`
EnrollSecret *string `json:"osquery_enroll_secret,omitempty"`
}
type setupResponse struct {
Admin *kolide.User `json:"admin,omitempty"`
OrgInfo *kolide.OrgInfo `json:"org_info,omitempty"`
KolideServerURL *string `json:"kolide_server_url"`
EnrollSecret *string `json:"osquery_enroll_secret"`
Token *string `json:"token,omitempty"`
Err error `json:"error,omitempty"`
}
func (r setupResponse) error() error { return r.Err }
func makeSetupEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
var (
admin *kolide.User
config *kolide.AppConfig
configPayload kolide.AppConfigPayload
err error
)
req := request.(setupRequest)
if req.OrgInfo != nil {
configPayload.OrgInfo = req.OrgInfo
}
configPayload.ServerSettings = &kolide.ServerSettings{}
if req.KolideServerURL != nil {
configPayload.ServerSettings.KolideServerURL = req.KolideServerURL
}
if req.EnrollSecret != nil {
configPayload.ServerSettings.EnrollSecret = req.EnrollSecret
}
config, err = svc.NewAppConfig(ctx, configPayload)
if err != nil {
return setupResponse{Err: err}, nil
}
// creating the user should be the last action. If there's a user
// present and other errors occur, the setup endpoint closes.
if req.Admin != nil {
admin, err = svc.NewAdminCreatedUser(ctx, *req.Admin)
if err != nil {
return setupResponse{Err: err}, nil
}
}
// If everything works to this point, log the user in and return token. If
// the login fails for some reason, ignore the error and don't return
// a token, forcing the user to log in manually
token := new(string)
_, *token, err = svc.Login(ctx, *req.Admin.Username, *req.Admin.Password)
if err != nil {
token = nil
}
return setupResponse{
Admin: admin,
OrgInfo: &kolide.OrgInfo{
OrgName: &config.OrgName,
OrgLogoURL: &config.OrgLogoURL,
},
KolideServerURL: &config.KolideServerURL,
EnrollSecret: &config.EnrollSecret,
Token: token,
}, nil
}
}