2019-09-12 19:51:07 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-02-01 11:00:39 +00:00
|
|
|
"context"
|
2019-09-12 19:51:07 +00:00
|
|
|
"fmt"
|
2020-02-01 11:00:39 +00:00
|
|
|
"net"
|
2019-09-12 19:51:07 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-08-16 21:39:02 +00:00
|
|
|
"path/filepath"
|
2021-08-11 00:07:15 +00:00
|
|
|
"strings"
|
2019-09-12 19:51:07 +00:00
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-11-03 23:58:25 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2021-05-03 19:48:20 +00:00
|
|
|
"github.com/smallstep/certificates/authority/config"
|
2022-08-16 21:39:02 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-09-12 19:51:07 +00:00
|
|
|
"github.com/smallstep/certificates/ca"
|
2022-08-16 21:39:02 +00:00
|
|
|
"github.com/smallstep/certificates/db"
|
2021-08-11 00:07:15 +00:00
|
|
|
"github.com/smallstep/certificates/pki"
|
2019-09-12 19:51:07 +00:00
|
|
|
"github.com/urfave/cli"
|
2020-10-29 20:10:03 +00:00
|
|
|
"go.step.sm/cli-utils/errs"
|
2022-03-24 16:18:51 +00:00
|
|
|
"go.step.sm/cli-utils/step"
|
2019-09-12 19:51:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AppCommand is the action used as the top action.
|
|
|
|
var AppCommand = cli.Command{
|
|
|
|
Name: "start",
|
|
|
|
Action: appAction,
|
2021-09-16 19:05:23 +00:00
|
|
|
UsageText: `**step-ca** <config> [**--password-file**=<file>]
|
2021-10-08 18:59:57 +00:00
|
|
|
[**--ssh-host-password-file**=<file>] [**--ssh-user-password-file**=<file>]
|
2021-09-16 19:05:23 +00:00
|
|
|
[**--issuer-password-file**=<file>] [**--resolver**=<addr>]`,
|
2019-09-12 19:51:07 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password-file",
|
|
|
|
Usage: `path to the <file> containing the password to decrypt the
|
|
|
|
intermediate private key.`,
|
2021-09-16 18:55:41 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "ssh-host-password-file",
|
|
|
|
Usage: `path to the <file> containing the password to decrypt the
|
|
|
|
private key used to sign SSH host certificates. If the flag is not passed it
|
|
|
|
will default to --password-file.`,
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "ssh-user-password-file",
|
|
|
|
Usage: `path to the <file> containing the password to decrypt the
|
|
|
|
private key used to sign SSH user certificates. If the flag is not passed it
|
|
|
|
will default to --password-file.`,
|
2021-03-24 21:53:19 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "issuer-password-file",
|
|
|
|
Usage: `path to the <file> containing the password to decrypt the
|
|
|
|
certificate issuer private key used in the RA mode.`,
|
2019-09-12 19:51:07 +00:00
|
|
|
},
|
2020-02-01 11:00:39 +00:00
|
|
|
cli.StringFlag{
|
2020-02-12 20:45:07 +00:00
|
|
|
Name: "resolver",
|
|
|
|
Usage: "address of a DNS resolver to be used instead of the default.",
|
2020-02-01 11:00:39 +00:00
|
|
|
},
|
2021-07-20 02:28:06 +00:00
|
|
|
cli.StringFlag{
|
2021-08-02 18:33:02 +00:00
|
|
|
Name: "token",
|
|
|
|
Usage: "token used to enable the linked ca.",
|
|
|
|
EnvVar: "STEP_CA_TOKEN",
|
2021-07-20 02:28:06 +00:00
|
|
|
},
|
2022-03-22 02:55:21 +00:00
|
|
|
cli.BoolFlag{
|
2022-04-05 17:59:35 +00:00
|
|
|
Name: "quiet",
|
|
|
|
Usage: "disable startup information",
|
2022-04-05 17:59:25 +00:00
|
|
|
EnvVar: "STEP_CA_QUIET",
|
2022-03-22 02:55:21 +00:00
|
|
|
},
|
2022-03-24 16:18:51 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "context",
|
2022-11-04 17:41:06 +00:00
|
|
|
Usage: "the <name> of the authority's context.",
|
2022-03-24 16:18:51 +00:00
|
|
|
EnvVar: "STEP_CA_CONTEXT",
|
|
|
|
},
|
2022-11-03 23:58:25 +00:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "acme-http-port",
|
2022-11-04 17:31:11 +00:00
|
|
|
Usage: `the <port> used on http-01 challenges. It can be changed for testing purposes.
|
2022-11-03 23:58:25 +00:00
|
|
|
Requires **--insecure** flag.`,
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "acme-tls-port",
|
2022-11-04 17:31:11 +00:00
|
|
|
Usage: `the <port> used on tls-alpn-01 challenges. It can be changed for testing purposes.
|
2022-11-03 23:58:25 +00:00
|
|
|
Requires **--insecure** flag.`,
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
2022-11-04 17:31:11 +00:00
|
|
|
Name: "insecure",
|
|
|
|
Usage: "enable insecure flags.",
|
2022-11-03 23:58:25 +00:00
|
|
|
},
|
2019-09-12 19:51:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppAction is the action used when the top command runs.
|
|
|
|
func appAction(ctx *cli.Context) error {
|
|
|
|
passFile := ctx.String("password-file")
|
2021-09-16 18:55:41 +00:00
|
|
|
sshHostPassFile := ctx.String("ssh-host-password-file")
|
|
|
|
sshUserPassFile := ctx.String("ssh-user-password-file")
|
2021-03-24 21:53:19 +00:00
|
|
|
issuerPassFile := ctx.String("issuer-password-file")
|
2020-02-01 11:00:39 +00:00
|
|
|
resolver := ctx.String("resolver")
|
2021-07-20 02:28:06 +00:00
|
|
|
token := ctx.String("token")
|
2022-03-22 02:55:21 +00:00
|
|
|
quiet := ctx.Bool("quiet")
|
2019-09-12 19:51:07 +00:00
|
|
|
|
2022-03-24 16:18:51 +00:00
|
|
|
if ctx.NArg() > 1 {
|
|
|
|
return errs.TooManyArguments(ctx)
|
2019-09-12 19:51:07 +00:00
|
|
|
}
|
2022-03-24 16:18:51 +00:00
|
|
|
|
2022-11-03 23:58:25 +00:00
|
|
|
// Allow custom ACME ports with insecure
|
|
|
|
if acmePort := ctx.Int("acme-http-port"); acmePort != 0 {
|
|
|
|
if ctx.Bool("insecure") {
|
|
|
|
acme.InsecurePortHTTP01 = acmePort
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("flag '--acme-http-port' requires the '--insecure' flag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if acmePort := ctx.Int("acme-tls-port"); acmePort != 0 {
|
|
|
|
if ctx.Bool("insecure") {
|
|
|
|
acme.InsecurePortTLSALPN01 = acmePort
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("flag '--acme-tls-port' requires the '--insecure' flag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow custom contexts.
|
2022-03-24 16:18:51 +00:00
|
|
|
if caCtx := ctx.String("context"); caCtx != "" {
|
2022-11-09 20:01:56 +00:00
|
|
|
if _, ok := step.Contexts().Get(caCtx); ok {
|
|
|
|
if err := step.Contexts().SetCurrent(caCtx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if token == "" {
|
|
|
|
return fmt.Errorf("context %q not found", caCtx)
|
|
|
|
} else if err := createContext(caCtx); err != nil {
|
2022-03-24 16:18:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-09-12 19:51:07 +00:00
|
|
|
}
|
2022-03-24 16:18:51 +00:00
|
|
|
|
|
|
|
var configFile string
|
|
|
|
if ctx.NArg() > 0 {
|
|
|
|
configFile = ctx.Args().Get(0)
|
|
|
|
} else {
|
|
|
|
configFile = step.CaConfigFile()
|
2019-09-12 19:51:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg, err := config.LoadConfiguration(configFile)
|
2022-08-16 21:39:02 +00:00
|
|
|
if err != nil && token == "" {
|
2023-01-31 20:32:56 +00:00
|
|
|
fmt.Println("step-ca can't find or open the configuration file for your CA.")
|
|
|
|
fmt.Println("You may need to create a CA first by running `step ca init`.")
|
2023-01-31 20:38:46 +00:00
|
|
|
fmt.Println("Documentation: https://u.step.sm/docs/ca")
|
|
|
|
fmt.Println("")
|
2019-09-12 19:51:07 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-08-16 21:39:02 +00:00
|
|
|
// Initialize a basic configuration to be used with an automatically
|
|
|
|
// configured linked RA. Default configuration includes:
|
|
|
|
// * badgerv2 on $(step path)/db
|
|
|
|
// * JSON logger
|
|
|
|
// * Default TLS options
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = &config.Config{
|
|
|
|
SkipValidation: true,
|
|
|
|
Logger: []byte(`{"format":"json"}`),
|
|
|
|
DB: &db.Config{
|
|
|
|
Type: "badgerv2",
|
|
|
|
DataSource: filepath.Join(step.Path(), "db"),
|
|
|
|
},
|
|
|
|
AuthorityConfig: &config.AuthConfig{
|
|
|
|
DeploymentType: pki.LinkedDeployment.String(),
|
|
|
|
Provisioners: provisioner.List{},
|
|
|
|
Template: &config.ASN1DN{},
|
|
|
|
Backdate: &provisioner.Duration{
|
|
|
|
Duration: config.DefaultBackdate,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TLS: &config.DefaultTLSOptions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
if cfg.AuthorityConfig != nil {
|
|
|
|
if token == "" && strings.EqualFold(cfg.AuthorityConfig.DeploymentType, pki.LinkedDeployment.String()) {
|
2021-08-11 00:07:15 +00:00
|
|
|
return errors.New(`'step-ca' requires the '--token' flag for linked deploy type.
|
|
|
|
|
|
|
|
To get a linked authority token:
|
|
|
|
1. Log in or create a Certificate Manager account at ` + "\033[1mhttps://u.step.sm/linked\033[0m" + `
|
2021-08-13 18:59:12 +00:00
|
|
|
2. Add a new authority and select "Link a step-ca instance"
|
2021-08-11 00:07:15 +00:00
|
|
|
3. Follow instructions in browser to start 'step-ca' using the '--token' flag
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:51:07 +00:00
|
|
|
var password []byte
|
|
|
|
if passFile != "" {
|
2021-11-12 23:46:34 +00:00
|
|
|
if password, err = os.ReadFile(passFile); err != nil {
|
2019-09-12 19:51:07 +00:00
|
|
|
fatal(errors.Wrapf(err, "error reading %s", passFile))
|
|
|
|
}
|
|
|
|
password = bytes.TrimRightFunc(password, unicode.IsSpace)
|
|
|
|
}
|
|
|
|
|
2021-09-16 18:55:41 +00:00
|
|
|
var sshHostPassword []byte
|
|
|
|
if sshHostPassFile != "" {
|
2021-11-12 23:46:34 +00:00
|
|
|
if sshHostPassword, err = os.ReadFile(sshHostPassFile); err != nil {
|
2021-09-16 18:55:41 +00:00
|
|
|
fatal(errors.Wrapf(err, "error reading %s", sshHostPassFile))
|
|
|
|
}
|
|
|
|
sshHostPassword = bytes.TrimRightFunc(sshHostPassword, unicode.IsSpace)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sshUserPassword []byte
|
|
|
|
if sshUserPassFile != "" {
|
2021-11-12 23:46:34 +00:00
|
|
|
if sshUserPassword, err = os.ReadFile(sshUserPassFile); err != nil {
|
2021-09-16 18:55:41 +00:00
|
|
|
fatal(errors.Wrapf(err, "error reading %s", sshUserPassFile))
|
|
|
|
}
|
|
|
|
sshUserPassword = bytes.TrimRightFunc(sshUserPassword, unicode.IsSpace)
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:53:19 +00:00
|
|
|
var issuerPassword []byte
|
|
|
|
if issuerPassFile != "" {
|
2021-11-12 23:46:34 +00:00
|
|
|
if issuerPassword, err = os.ReadFile(issuerPassFile); err != nil {
|
2021-03-24 21:53:19 +00:00
|
|
|
fatal(errors.Wrapf(err, "error reading %s", issuerPassFile))
|
|
|
|
}
|
|
|
|
issuerPassword = bytes.TrimRightFunc(issuerPassword, unicode.IsSpace)
|
|
|
|
}
|
|
|
|
|
2020-02-01 11:00:39 +00:00
|
|
|
// replace resolver if requested
|
|
|
|
if resolver != "" {
|
|
|
|
net.DefaultResolver.PreferGo = true
|
|
|
|
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
return net.Dial(network, resolver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
srv, err := ca.New(cfg,
|
2021-03-24 21:53:19 +00:00
|
|
|
ca.WithConfigFile(configFile),
|
|
|
|
ca.WithPassword(password),
|
2021-09-16 18:55:41 +00:00
|
|
|
ca.WithSSHHostPassword(sshHostPassword),
|
|
|
|
ca.WithSSHUserPassword(sshUserPassword),
|
2021-07-20 02:28:06 +00:00
|
|
|
ca.WithIssuerPassword(issuerPassword),
|
2022-03-22 02:55:21 +00:00
|
|
|
ca.WithLinkedCAToken(token),
|
|
|
|
ca.WithQuiet(quiet))
|
2019-09-12 19:51:07 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go ca.StopReloaderHandler(srv)
|
2022-08-23 19:43:48 +00:00
|
|
|
if err = srv.Run(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
2019-09-12 19:51:07 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 20:01:56 +00:00
|
|
|
// createContext creates a new context using the given name for the context,
|
|
|
|
// authority and profile.
|
|
|
|
func createContext(name string) error {
|
|
|
|
if err := step.Contexts().Add(&step.Context{
|
|
|
|
Name: name, Authority: name, Profile: name,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("error adding context: %w", err)
|
|
|
|
}
|
|
|
|
if err := step.Contexts().SaveCurrent(name); err != nil {
|
|
|
|
return fmt.Errorf("error saving context: %w", err)
|
|
|
|
}
|
|
|
|
if err := step.Contexts().SetCurrent(name); err != nil {
|
|
|
|
return fmt.Errorf("error setting context: %w", err)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(step.Path(), 0700); err != nil {
|
|
|
|
return fmt.Errorf("error creating directory: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-12 19:51:07 +00:00
|
|
|
// fatal writes the passed error on the standard error and exits with the exit
|
|
|
|
// code 1. If the environment variable STEPDEBUG is set to 1 it shows the
|
|
|
|
// stack trace of the error.
|
|
|
|
func fatal(err error) {
|
|
|
|
if os.Getenv("STEPDEBUG") == "1" {
|
|
|
|
fmt.Fprintf(os.Stderr, "%+v\n", err)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
os.Exit(2)
|
|
|
|
}
|