2018-05-04 16:53:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-05-22 23:57:56 +00:00
|
|
|
"os"
|
2018-05-04 16:53:21 +00:00
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/service"
|
2018-05-04 16:53:21 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
2018-05-22 23:57:56 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2018-05-04 16:53:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupCommand() cli.Command {
|
|
|
|
var (
|
|
|
|
flEmail string
|
2019-01-02 23:27:37 +00:00
|
|
|
flUsername string
|
2018-05-04 16:53:21 +00:00
|
|
|
flPassword string
|
|
|
|
flOrgName string
|
|
|
|
)
|
|
|
|
return cli.Command{
|
|
|
|
Name: "setup",
|
2021-02-02 20:16:59 +00:00
|
|
|
Usage: "Set up a Fleet instance",
|
2019-01-02 23:27:37 +00:00
|
|
|
UsageText: `fleetctl setup [options]`,
|
2018-05-04 16:53:21 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
configFlag(),
|
|
|
|
contextFlag(),
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email",
|
|
|
|
EnvVar: "EMAIL",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flEmail,
|
2018-05-09 23:54:07 +00:00
|
|
|
Usage: "Email of the admin user to create",
|
2018-05-04 16:53:21 +00:00
|
|
|
},
|
2019-01-02 23:27:37 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "username",
|
|
|
|
EnvVar: "USERNAME",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flUsername,
|
|
|
|
Usage: "Username of the admin user to create",
|
|
|
|
},
|
2018-05-04 16:53:21 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password",
|
|
|
|
EnvVar: "PASSWORD",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flPassword,
|
2018-05-22 23:57:56 +00:00
|
|
|
Usage: "Password for the admin user (recommended to use interactive entry)",
|
2018-05-04 16:53:21 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "org-name",
|
|
|
|
EnvVar: "ORG_NAME",
|
|
|
|
Value: "",
|
|
|
|
Destination: &flOrgName,
|
2018-05-09 23:54:07 +00:00
|
|
|
Usage: "Name of the organization",
|
2018-05-04 16:53:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
2018-07-16 16:35:21 +00:00
|
|
|
fleet, err := unauthenticatedClientFromCLI(c)
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:29:04 +00:00
|
|
|
if flEmail == "" {
|
|
|
|
return errors.Errorf("Email of the admin user to create must be provided")
|
|
|
|
}
|
2019-01-02 23:27:37 +00:00
|
|
|
if flUsername == "" {
|
|
|
|
fmt.Println("No username supplied, using email as username")
|
|
|
|
flUsername = flEmail
|
|
|
|
}
|
2018-05-17 14:29:04 +00:00
|
|
|
if flPassword == "" {
|
2018-05-22 23:57:56 +00:00
|
|
|
fmt.Print("Password: ")
|
|
|
|
passBytes, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error reading password")
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
flPassword = string(passBytes)
|
2018-07-16 17:03:50 +00:00
|
|
|
|
|
|
|
fmt.Print("Confirm Password: ")
|
|
|
|
passBytes, err = terminal.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error reading password confirmation")
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
if flPassword != string(passBytes) {
|
|
|
|
return errors.New("passwords do not match")
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:29:04 +00:00
|
|
|
}
|
2018-05-22 23:57:56 +00:00
|
|
|
|
2019-01-02 23:27:37 +00:00
|
|
|
token, err := fleet.Setup(flEmail, flUsername, flPassword, flOrgName)
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case service.SetupAlreadyErr:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "error setting up Fleet")
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:15:38 +00:00
|
|
|
configPath, context := c.String("config"), c.String("context")
|
|
|
|
|
|
|
|
if err := setConfigValue(configPath, context, "email", flEmail); err != nil {
|
2018-05-04 16:53:21 +00:00
|
|
|
return errors.Wrap(err, "error setting email for the current context")
|
|
|
|
}
|
|
|
|
|
2021-01-29 01:15:38 +00:00
|
|
|
if err := setConfigValue(configPath, context, "token", token); err != nil {
|
2018-05-04 16:53:21 +00:00
|
|
|
return errors.Wrap(err, "error setting token for the current context")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("[+] Fleet setup successful and context configured!\n")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|