| 1 |
package ssh |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
"net/http" |
| 7 |
"runtime" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/pkg/errors" |
| 11 |
"github.com/smallstep/certificates/api" |
| 12 |
"github.com/smallstep/certificates/authority/provisioner" |
| 13 |
"github.com/smallstep/certificates/ca" |
| 14 |
"github.com/smallstep/cli/command" |
| 15 |
"github.com/smallstep/cli/config" |
| 16 |
"github.com/smallstep/cli/crypto/sshutil" |
| 17 |
"github.com/smallstep/cli/errs" |
| 18 |
"github.com/smallstep/cli/flags" |
| 19 |
"github.com/smallstep/cli/ui" |
| 20 |
"github.com/smallstep/cli/utils/cautils" |
| 21 |
"github.com/urfave/cli" |
| 22 |
"golang.org/x/crypto/ssh" |
| 23 |
) |
| 24 |
|
| 25 |
func configCommand() cli.Command { |
| 26 |
return cli.Command{ |
| 27 |
Name: "config", |
| 28 |
Action: command.ActionFunc(configAction), |
| 29 |
Usage: "configures ssh to be used with certificates", |
| 30 |
UsageText: `**step ssh config** |
| 31 |
[**--team**=<name>] [**--host**] [**--set**=<key=value>] [**--set-file**=<path>] |
| 32 |
[**--dry-run**] [**--roots**] [**--federation**] |
| 33 |
[**--force**] [**--ca-url**=<uri>] [**--root**=<file>] |
| 34 |
[**--offline**] [**--ca-config**=<path>] [**--team-url**=<url>]`, |
| 35 |
Description: `**step ssh config** configures SSH to be used with certificates. It also supports |
| 36 |
flags to inspect the root certificates used to sign the certificates. |
| 37 |
|
| 38 |
This command uses the templates defined in step-certificates to set up user and |
| 39 |
hosts environments. |
| 40 |
|
| 41 |
## EXAMPLES |
| 42 |
|
| 43 |
Print the public keys used to verify user certificates: |
| 44 |
''' |
| 45 |
$ step ssh config --roots |
| 46 |
''' |
| 47 |
|
| 48 |
Print the public keys used to verify host certificates: |
| 49 |
''' |
| 50 |
$ step ssh config --host --roots |
| 51 |
''' |
| 52 |
|
| 53 |
Apply configuration templates on the user system: |
| 54 |
''' |
| 55 |
$ step ssh config |
| 56 |
''' |
| 57 |
|
| 58 |
Apply configuration templates on a host: |
| 59 |
''' |
| 60 |
$ step ssh config --host |
| 61 |
''' |
| 62 |
|
| 63 |
Apply configuration templates with custom variables: |
| 64 |
''' |
| 65 |
$ step ssh config --set User=joe --set Bastion=bastion.example.com |
| 66 |
'''`, |
| 67 |
Flags: []cli.Flag{ |
| 68 |
cli.BoolFlag{ |
| 69 |
Name: "host", |
| 70 |
Usage: `Configures a SSH server instead of a client.`, |
| 71 |
}, |
| 72 |
flags.Team, |
| 73 |
flags.TeamURL, |
| 74 |
cli.BoolFlag{ |
| 75 |
Name: "roots", |
| 76 |
Usage: `Prints the public keys used to verify user or host certificates.`, |
| 77 |
}, |
| 78 |
cli.BoolFlag{ |
| 79 |
Name: "federation", |
| 80 |
Usage: `Prints all the public keys in the federation. These keys are used to verify |
| 81 |
user or host certificates`, |
| 82 |
}, |
| 83 |
cli.StringSliceFlag{ |
| 84 |
Name: "set", |
| 85 |
Usage: `The <key=value> used as a variable in the templates. Use the flag multiple |
| 86 |
times to set multiple variables.`, |
| 87 |
}, |
| 88 |
flags.TemplateSetFile, |
| 89 |
flags.DryRun, |
| 90 |
flags.CaURL, |
| 91 |
flags.Root, |
| 92 |
flags.Offline, |
| 93 |
flags.CaConfig, |
| 94 |
flags.Force, |
| 95 |
}, |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
type statusCoder interface { |
| 100 |
StatusCode() int |
| 101 |
} |
| 102 |
|
| 103 |
func configAction(ctx *cli.Context) (recoverErr error) { |
| 104 |
team := ctx.String("team") |
| 105 |
isHost := ctx.Bool("host") |
| 106 |
isRoots := ctx.Bool("roots") |
| 107 |
isFederation := ctx.Bool("federation") |
| 108 |
sets := ctx.StringSlice("set") |
| 109 |
|
| 110 |
switch { |
| 111 |
case team != "" && isHost: |
| 112 |
return errs.IncompatibleFlagWithFlag(ctx, "roots", "host") |
| 113 |
case team != "" && isRoots: |
| 114 |
return errs.IncompatibleFlagWithFlag(ctx, "roots", "roots") |
| 115 |
case team != "" && isFederation: |
| 116 |
return errs.IncompatibleFlagWithFlag(ctx, "roots", "federation") |
| 117 |
case team != "" && len(sets) > 0: |
| 118 |
return errs.IncompatibleFlagWithFlag(ctx, "roots", "set") |
| 119 |
case isRoots && isFederation: |
| 120 |
return errs.IncompatibleFlagWithFlag(ctx, "roots", "federation") |
| 121 |
case isRoots && len(sets) > 0: |
| 122 |
return errs.IncompatibleFlagWithFlag(ctx, "roots", "set") |
| 123 |
case isFederation && len(sets) > 0: |
| 124 |
return errs.IncompatibleFlagWithFlag(ctx, "federation", "set") |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if team != "" { |
| 129 |
if err := cautils.BootstrapTeam(ctx, team); err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
retryFunc, err := loginOnUnauthorized(ctx) |
| 136 |
if err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
client, err := cautils.NewClient(ctx, ca.WithRetryFunc(retryFunc)) |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
if isRoots || isFederation { |
| 148 |
var roots *api.SSHRootsResponse |
| 149 |
if isRoots { |
| 150 |
roots, err = client.SSHRoots() |
| 151 |
} else { |
| 152 |
roots, err = client.SSHFederation() |
| 153 |
} |
| 154 |
if err != nil { |
| 155 |
if e, ok := err.(statusCoder); ok && e.StatusCode() == http.StatusNotFound { |
| 156 |
return errors.New("step certificates is not configured with SSH support") |
| 157 |
} |
| 158 |
return errors.Wrap(err, "error getting ssh public keys") |
| 159 |
} |
| 160 |
|
| 161 |
var keys []api.SSHPublicKey |
| 162 |
if isHost { |
| 163 |
if len(roots.HostKeys) == 0 { |
| 164 |
return errors.New("step certificates is not configured with an ssh.hostKey") |
| 165 |
} |
| 166 |
keys = roots.HostKeys |
| 167 |
} else { |
| 168 |
if len(roots.UserKeys) == 0 { |
| 169 |
return errors.New("step certificates is not configured with an ssh.userKey") |
| 170 |
} |
| 171 |
keys = roots.UserKeys |
| 172 |
} |
| 173 |
|
| 174 |
for _, key := range keys { |
| 175 |
fmt.Printf("%s %s\n", key.Type(), base64.StdEncoding.EncodeToString(key.Marshal())) |
| 176 |
} |
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
data := map[string]string{ |
| 181 |
"GOOS": runtime.GOOS, |
| 182 |
"StepPath": config.StepPath(), |
| 183 |
} |
| 184 |
if len(sets) > 0 { |
| 185 |
for _, s := range sets { |
| 186 |
i := strings.Index(s, "=") |
| 187 |
if i == -1 { |
| 188 |
return errs.InvalidFlagValue(ctx, "set", s, "") |
| 189 |
} |
| 190 |
data[s[:i]] = s[i+1:] |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if !isHost { |
| 195 |
|
| 196 |
if _, ok := data["User"]; !ok { |
| 197 |
agent, err := sshutil.DialAgent() |
| 198 |
if err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
|
| 202 |
var opts []sshutil.AgentOption |
| 203 |
if roots, err := client.SSHRoots(); err == nil && len(roots.UserKeys) > 0 { |
| 204 |
userKeys := make([]ssh.PublicKey, len(roots.UserKeys)) |
| 205 |
for i, uk := range roots.UserKeys { |
| 206 |
userKeys[i] = uk.PublicKey |
| 207 |
} |
| 208 |
opts = append(opts, sshutil.WithSignatureKey(userKeys)) |
| 209 |
} |
| 210 |
if certs, err := agent.ListCertificates(opts...); err == nil && len(certs) > 0 { |
| 211 |
if p := certs[0].ValidPrincipals; len(p) > 0 { |
| 212 |
data["User"] = p[0] |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
if _, ok := data["User"]; !ok { |
| 219 |
return errors.New("ssh certificate not found: please run `step ssh login <identity>`") |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
req := &api.SSHConfigRequest{ |
| 225 |
Data: data, |
| 226 |
} |
| 227 |
if isHost { |
| 228 |
req.Type = provisioner.SSHHostCert |
| 229 |
} else { |
| 230 |
req.Type = provisioner.SSHUserCert |
| 231 |
} |
| 232 |
|
| 233 |
resp, err := client.SSHConfig(req) |
| 234 |
if err != nil { |
| 235 |
return err |
| 236 |
} |
| 237 |
|
| 238 |
var templates []api.Template |
| 239 |
if isHost { |
| 240 |
templates = resp.HostTemplates |
| 241 |
} else { |
| 242 |
templates = resp.UserTemplates |
| 243 |
} |
| 244 |
if len(templates) == 0 { |
| 245 |
fmt.Println("No configuration changes were found.") |
| 246 |
return nil |
| 247 |
} |
| 248 |
|
| 249 |
defer func() { |
| 250 |
if rec := recover(); rec != nil { |
| 251 |
if err, ok := rec.(error); ok { |
| 252 |
recoverErr = err |
| 253 |
} else { |
| 254 |
panic(rec) |
| 255 |
} |
| 256 |
} |
| 257 |
}() |
| 258 |
|
| 259 |
if ctx.Bool("dry-run") { |
| 260 |
for _, t := range templates { |
| 261 |
ui.Printf("{{ \"%s\" | bold }}\n", config.StepAbs(t.Path)) |
| 262 |
fmt.Println(string(t.Content)) |
| 263 |
} |
| 264 |
return nil |
| 265 |
} |
| 266 |
|
| 267 |
for _, t := range templates { |
| 268 |
if err := t.Write(); err != nil { |
| 269 |
return err |
| 270 |
} |
| 271 |
ui.Printf(`{{ "%s" | green }} {{ "%s" | bold }}`+"\n", ui.IconGood, config.StepAbs(t.Path)) |
| 272 |
} |
| 273 |
|
| 274 |
return nil |
| 275 |
} |