Improve onboard messages.

This commit is contained in:
Mariano Cano 2019-09-12 15:32:48 -07:00
parent db68bf1081
commit 1364dd9654

View file

@ -3,7 +3,6 @@ package commands
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -16,6 +15,7 @@ import (
"github.com/smallstep/cli/crypto/pki" "github.com/smallstep/cli/crypto/pki"
"github.com/smallstep/cli/crypto/randutil" "github.com/smallstep/cli/crypto/randutil"
"github.com/smallstep/cli/errs" "github.com/smallstep/cli/errs"
"github.com/smallstep/cli/ui"
"github.com/smallstep/cli/utils" "github.com/smallstep/cli/utils"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -31,6 +31,15 @@ type onboardingPayload struct {
Fingerprint string `json:"fingerprint"` Fingerprint string `json:"fingerprint"`
} }
type onboardingError struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
}
func (e onboardingError) Error() string {
return e.Message
}
func init() { func init() {
command.Register(cli.Command{ command.Register(cli.Command{
Name: "onboard", Name: "onboard",
@ -59,7 +68,7 @@ func onboardAction(ctx *cli.Context) error {
return errors.Wrapf(err, "error parsing %s", onboarding) return errors.Wrapf(err, "error parsing %s", onboarding)
} }
fmt.Printf("Connecting to onboarding guide...\n\n") ui.Println("Connecting to onboarding guide...")
token := ctx.Args().Get(0) token := ctx.Args().Get(0)
onboardingURL := u.ResolveReference(&url.URL{Path: token}).String() onboardingURL := u.ResolveReference(&url.URL{Path: token}).String()
@ -69,8 +78,11 @@ func onboardAction(ctx *cli.Context) error {
return errors.Wrap(err, "error connecting onboarding guide") return errors.Wrap(err, "error connecting onboarding guide")
} }
if res.StatusCode >= 400 { if res.StatusCode >= 400 {
res.Body.Close() var msg onboardingError
return errors.Errorf("error connecting onboarding guide: %s", res.Status) if err := readJSON(res.Body, &msg); err != nil {
return errors.Wrap(err, "error unmarshaling response")
}
return errors.Wrap(msg, "error receiving onboarding guide")
} }
var config onboardingConfiguration var config onboardingConfiguration
@ -84,11 +96,12 @@ func onboardAction(ctx *cli.Context) error {
} }
config.password = []byte(password) config.password = []byte(password)
fmt.Printf("Connected! Initializing step-ca with the following configuration...\n\n") ui.Println("Initializing step-ca with the following configuration:")
fmt.Printf("Name: %s\n", config.Name) ui.PrintSelected("Name", config.Name)
fmt.Printf("DNS: %s\n", config.DNS) ui.PrintSelected("DNS", config.DNS)
fmt.Printf("Address: %s\n", config.Address) ui.PrintSelected("Address", config.Address)
fmt.Printf("Password: %s\n\n", password) ui.PrintSelected("Password", password)
ui.Println()
caConfig, fp, err := onboardPKI(config) caConfig, fp, err := onboardPKI(config)
if err != nil { if err != nil {
@ -104,13 +117,19 @@ func onboardAction(ctx *cli.Context) error {
if err != nil { if err != nil {
return errors.Wrap(err, "error connecting onboarding guide") return errors.Wrap(err, "error connecting onboarding guide")
} }
resp.Body.Close()
if resp.StatusCode >= 400 { if resp.StatusCode >= 400 {
fmt.Fprintf(os.Stderr, "error connecting onboarding guide: %s\n", res.Status) var msg onboardingError
if err := readJSON(resp.Body, &msg); err != nil {
ui.Printf("%s {{ \"error unmarshalling response: %v\" | yellow }}\n", ui.IconWarn, err)
} else {
ui.Printf("%s {{ \"error posting fingerprint: %s\" | yellow }}\n", ui.IconWarn, msg.Message)
}
} else {
resp.Body.Close()
} }
fmt.Printf("Initialized!\n") ui.Println("Initialized!")
fmt.Printf("Step CA is starting. Please return to the onboarding guide in your browser to continue.\n") ui.Println("Step CA is starting. Please return to the onboarding guide in your browser to continue.")
srv, err := ca.New(caConfig, ca.WithPassword(config.password)) srv, err := ca.New(caConfig, ca.WithPassword(config.password))
if err != nil { if err != nil {
@ -134,11 +153,13 @@ func onboardPKI(config onboardingConfiguration) (*authority.Config, string, erro
p.SetAddress(config.Address) p.SetAddress(config.Address)
p.SetDNSNames([]string{config.DNS}) p.SetDNSNames([]string{config.DNS})
ui.Println("Generating root certificate...")
rootCrt, rootKey, err := p.GenerateRootCertificate(config.Name+" Root CA", config.password) rootCrt, rootKey, err := p.GenerateRootCertificate(config.Name+" Root CA", config.password)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
ui.Println("Generating intermediate certificate...")
err = p.GenerateIntermediateCertificate(config.Name+" Intermediate CA", rootCrt, rootKey, config.password) err = p.GenerateIntermediateCertificate(config.Name+" Intermediate CA", rootCrt, rootKey, config.password)
if err != nil { if err != nil {
return nil, "", err return nil, "", err
@ -146,6 +167,7 @@ func onboardPKI(config onboardingConfiguration) (*authority.Config, string, erro
// Generate provisioner // Generate provisioner
p.SetProvisioner("admin") p.SetProvisioner("admin")
ui.Println("Generating admin provisioner...")
if err = p.GenerateKeyPairs(config.password); err != nil { if err = p.GenerateKeyPairs(config.password); err != nil {
return nil, "", err return nil, "", err
} }