| 1 |
package ca |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
|
| 8 |
"github.com/pkg/errors" |
| 9 |
"github.com/smallstep/certificates/ca" |
| 10 |
"github.com/smallstep/certificates/pki" |
| 11 |
"github.com/smallstep/cli/command" |
| 12 |
"github.com/smallstep/cli/config" |
| 13 |
"github.com/smallstep/cli/crypto/pemutil" |
| 14 |
"github.com/smallstep/cli/errs" |
| 15 |
"github.com/smallstep/cli/flags" |
| 16 |
"github.com/smallstep/cli/ui" |
| 17 |
"github.com/smallstep/cli/utils" |
| 18 |
"github.com/smallstep/cli/utils/cautils" |
| 19 |
"github.com/smallstep/truststore" |
| 20 |
"github.com/urfave/cli" |
| 21 |
) |
| 22 |
|
| 23 |
func bootstrapCommand() cli.Command { |
| 24 |
return cli.Command{ |
| 25 |
Name: "bootstrap", |
| 26 |
Action: command.ActionFunc(bootstrapAction), |
| 27 |
Usage: "initialize the environment to use the CA commands", |
| 28 |
UsageText: `**step ca bootstrap** |
| 29 |
[**--ca-url**=<uri>] [**--fingerprint**=<fingerprint>] [**--install**] |
| 30 |
[**--team**=name] [**--team-url**=url] [**--redirect-url**=<url>]`, |
| 31 |
Description: `**step ca bootstrap** downloads the root certificate from the certificate |
| 32 |
authority and sets up the current environment to use it. |
| 33 |
|
| 34 |
Bootstrap will store the root certificate in <$STEPPATH/certs/root_ca.crt> and |
| 35 |
create a configuration file in <$STEPPATH/configs/defaults.json> with the CA |
| 36 |
url, the root certificate location and its fingerprint. |
| 37 |
|
| 38 |
After the bootstrap, ca commands do not need to specify the flags |
| 39 |
--ca-url, --root or --fingerprint if we want to use the same environment. |
| 40 |
|
| 41 |
## EXAMPLES |
| 42 |
|
| 43 |
Bootstrap using the CA url and a fingerprint: |
| 44 |
''' |
| 45 |
$ step ca bootstrap --ca-url https://ca.example.org \ |
| 46 |
--fingerprint d9d0978692f1c7cc791f5c343ce98771900721405e834cd27b9502cc719f5097 |
| 47 |
''' |
| 48 |
|
| 49 |
Bootstrap and install the root certificate |
| 50 |
''' |
| 51 |
$ step ca bootstrap --ca-url https://ca.example.org \ |
| 52 |
--fingerprint d9d0978692f1c7cc791f5c343ce98771900721405e834cd27b9502cc719f5097 \ |
| 53 |
--install |
| 54 |
''' |
| 55 |
|
| 56 |
Bootstrap with a smallstep.com CA using a team ID: |
| 57 |
''' |
| 58 |
$ step ca bootstrap --team superteam |
| 59 |
''' |
| 60 |
|
| 61 |
To use team IDs in your own environment, you'll need an HTTP(S) server |
| 62 |
serving a JSON file: |
| 63 |
''' |
| 64 |
{"url":"https://ca.example.org","fingerprint":"d9d0978692f1c7cc791f5c343ce98771900721405e834cd27b9502cc719f5097"} |
| 65 |
''' |
| 66 |
|
| 67 |
Then, this command will look for the file at https://config.example.org/superteam: |
| 68 |
''' |
| 69 |
$ step ca bootstrap --team superteam --team-url https://config.example.org/<> |
| 70 |
'''`, |
| 71 |
Flags: []cli.Flag{ |
| 72 |
flags.CaURL, |
| 73 |
fingerprintFlag, |
| 74 |
cli.BoolFlag{ |
| 75 |
Name: "install", |
| 76 |
Usage: "Install the root certificate into the system truststore.", |
| 77 |
}, |
| 78 |
flags.Team, |
| 79 |
flags.TeamURL, |
| 80 |
flags.RedirectURL, |
| 81 |
flags.Force, |
| 82 |
}, |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
type bootstrapConfig struct { |
| 87 |
CA string `json:"ca-url"` |
| 88 |
Fingerprint string `json:"fingerprint"` |
| 89 |
Root string `json:"root"` |
| 90 |
Redirect string `json:"redirect-url"` |
| 91 |
} |
| 92 |
|
| 93 |
func bootstrapAction(ctx *cli.Context) error { |
| 94 |
caURL, err := flags.ParseCaURLIfExists(ctx) |
| 95 |
if err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
fingerprint := ctx.String("fingerprint") |
| 99 |
team := ctx.String("team") |
| 100 |
rootFile := pki.GetRootCAPath() |
| 101 |
configFile := filepath.Join(config.StepPath(), "config", "defaults.json") |
| 102 |
redirectURL := ctx.String("redirect-url") |
| 103 |
|
| 104 |
switch { |
| 105 |
case team != "": |
| 106 |
return cautils.BootstrapTeam(ctx, team) |
| 107 |
case len(caURL) == 0: |
| 108 |
return errs.RequiredFlag(ctx, "ca-url") |
| 109 |
case len(fingerprint) == 0: |
| 110 |
return errs.RequiredFlag(ctx, "fingerprint") |
| 111 |
} |
| 112 |
|
| 113 |
tr := getInsecureTransport() |
| 114 |
client, err := ca.NewClient(caURL, ca.WithTransport(tr)) |
| 115 |
if err != nil { |
| 116 |
return err |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
resp, err := client.Root(fingerprint) |
| 121 |
if err != nil { |
| 122 |
return errors.Wrap(err, "error downloading root certificate") |
| 123 |
} |
| 124 |
|
| 125 |
if err = os.MkdirAll(filepath.Dir(rootFile), 0700); err != nil { |
| 126 |
return errs.FileError(err, rootFile) |
| 127 |
} |
| 128 |
|
| 129 |
if err = os.MkdirAll(filepath.Dir(configFile), 0700); err != nil { |
| 130 |
return errs.FileError(err, configFile) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
_, err = pemutil.Serialize(resp.RootPEM.Certificate, pemutil.ToFile(rootFile, 0600)) |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
ui.Printf("The root certificate has been saved in %s.\n", rootFile) |
| 139 |
|
| 140 |
|
| 141 |
caURL, err = completeURL(caURL) |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
b, err := json.MarshalIndent(bootstrapConfig{ |
| 148 |
CA: caURL, |
| 149 |
Fingerprint: fingerprint, |
| 150 |
Root: pki.GetRootCAPath(), |
| 151 |
Redirect: redirectURL, |
| 152 |
}, "", " ") |
| 153 |
if err != nil { |
| 154 |
return errors.Wrap(err, "error marshaling defaults.json") |
| 155 |
} |
| 156 |
|
| 157 |
if err := utils.WriteFile(configFile, b, 0644); err != nil { |
| 158 |
return err |
| 159 |
} |
| 160 |
|
| 161 |
ui.Printf("Your configuration has been saved in %s.\n", configFile) |
| 162 |
|
| 163 |
if ctx.Bool("install") { |
| 164 |
ui.Printf("Installing the root certificate in the system truststore... ") |
| 165 |
if err := truststore.InstallFile(rootFile); err != nil { |
| 166 |
ui.Println() |
| 167 |
return err |
| 168 |
} |
| 169 |
ui.Println("done.") |
| 170 |
} |
| 171 |
|
| 172 |
return nil |
| 173 |
} |