2018-05-04 16:53:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2020-11-18 00:02:14 +00:00
|
|
|
"runtime"
|
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"
|
2021-03-13 00:42:38 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-05-04 16:53:21 +00:00
|
|
|
)
|
|
|
|
|
|
2018-07-16 16:35:21 +00:00
|
|
|
func unauthenticatedClientFromCLI(c *cli.Context) (*service.Client, error) {
|
2018-05-04 16:53:21 +00:00
|
|
|
if err := makeConfigIfNotExists(c.String("config")); err != nil {
|
|
|
|
|
return nil, errors.Wrapf(err, "error verifying that config exists at %s", c.String("config"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config, err := readConfig(c.String("config"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cc, ok := config.Contexts[c.String("context")]
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("context %q is not found", c.String("context"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cc.Address == "" {
|
|
|
|
|
return nil, errors.New("set the Fleet API address with: fleetctl config set --address https://localhost:8080")
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 00:02:14 +00:00
|
|
|
if runtime.GOOS == "windows" && cc.RootCA == "" && !cc.TLSSkipVerify {
|
|
|
|
|
return nil, errors.New("Windows clients must configure rootca (secure) or tls-skip-verify (insecure)")
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 02:55:16 +00:00
|
|
|
var options []service.ClientOption
|
|
|
|
|
if getDebug(c) {
|
|
|
|
|
options = append(options, service.EnableClientDebug())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fleet, err := service.NewClient(
|
|
|
|
|
cc.Address,
|
|
|
|
|
cc.TLSSkipVerify,
|
|
|
|
|
cc.RootCA,
|
|
|
|
|
cc.URLPrefix,
|
|
|
|
|
options...,
|
|
|
|
|
)
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "error creating Fleet API client handler")
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 16:35:21 +00:00
|
|
|
return fleet, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func clientFromCLI(c *cli.Context) (*service.Client, error) {
|
|
|
|
|
fleet, err := unauthenticatedClientFromCLI(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 01:15:38 +00:00
|
|
|
configPath, context := c.String("config"), c.String("context")
|
|
|
|
|
|
2018-07-16 16:35:21 +00:00
|
|
|
// Add authentication token
|
2021-01-29 01:15:38 +00:00
|
|
|
t, err := getConfigValue(configPath, context, "token")
|
2018-05-04 16:53:21 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "error getting token from the config")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if token, ok := t.(string); ok {
|
2018-07-16 16:35:21 +00:00
|
|
|
if token == "" {
|
|
|
|
|
return nil, errors.New("Please log in with: fleetctl login")
|
|
|
|
|
}
|
2018-05-04 16:53:21 +00:00
|
|
|
fleet.SetToken(token)
|
|
|
|
|
} else {
|
|
|
|
|
return nil, errors.Errorf("token config value was not a string: %+v", t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fleet, nil
|
|
|
|
|
}
|