2018-05-04 16:53:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/kolide/fleet/server/service"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-10-16 23:40:45 +00:00
|
|
|
fleet, err := service.NewClient(cc.Address, cc.TLSSkipVerify, cc.RootCA, cc.URLPrefix)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add authentication token
|
2018-05-04 16:53:21 +00:00
|
|
|
t, err := getConfigValue(c, "token")
|
|
|
|
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
|
|
|
|
}
|