| 1 |
package ssh |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/pkg/errors" |
| 8 |
"github.com/smallstep/certificates/api" |
| 9 |
"github.com/smallstep/certificates/authority/provisioner" |
| 10 |
"github.com/smallstep/certificates/ca" |
| 11 |
"github.com/smallstep/certificates/errs" |
| 12 |
"github.com/smallstep/cli/command" |
| 13 |
"github.com/smallstep/cli/crypto/keys" |
| 14 |
"github.com/smallstep/cli/crypto/sshutil" |
| 15 |
"github.com/smallstep/cli/flags" |
| 16 |
"github.com/smallstep/cli/token" |
| 17 |
"github.com/smallstep/cli/ui" |
| 18 |
"github.com/smallstep/cli/utils/cautils" |
| 19 |
"github.com/urfave/cli" |
| 20 |
"golang.org/x/crypto/ssh" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
func init() { |
| 25 |
cmd := cli.Command{ |
| 26 |
Name: "ssh", |
| 27 |
Usage: "create and manage ssh certificates", |
| 28 |
UsageText: "step ssh <subcommand> [arguments] [global-flags] [subcommand-flags]", |
| 29 |
Description: `**step ssh** command group provides facilities to sign SSH certificates. |
| 30 |
|
| 31 |
## EXAMPLES |
| 32 |
|
| 33 |
Generate a new SSH key pair and user certificate: |
| 34 |
''' |
| 35 |
$ step ssh certificate joe@work id_ecdsa |
| 36 |
''' |
| 37 |
|
| 38 |
Generate a new SSH key pair and host certificate: |
| 39 |
''' |
| 40 |
$ step ssh certificate --host internal.example.com ssh_host_ecdsa_key |
| 41 |
''' |
| 42 |
|
| 43 |
Add a new user certificate to the agent: |
| 44 |
''' |
| 45 |
$ step ssh login [email protected] |
| 46 |
''' |
| 47 |
|
| 48 |
Remove a certificate from the agent: |
| 49 |
''' |
| 50 |
$ step ssh logout [email protected] |
| 51 |
''' |
| 52 |
|
| 53 |
List all keys in the agent: |
| 54 |
''' |
| 55 |
$ step ssh list |
| 56 |
''' |
| 57 |
|
| 58 |
Configure a user environment with the SSH templates: |
| 59 |
''' |
| 60 |
$ step ssh config |
| 61 |
''' |
| 62 |
|
| 63 |
Inspect an ssh certificate file: |
| 64 |
''' |
| 65 |
$ step ssh inspect id_ecdsa-cert.pub |
| 66 |
''' |
| 67 |
|
| 68 |
Inspect an ssh certificate in the agent: |
| 69 |
''' |
| 70 |
$ step ssh list --raw [email protected] | step ssh inspect |
| 71 |
''' |
| 72 |
|
| 73 |
List all the hosts you have access to: |
| 74 |
''' |
| 75 |
$ step ssh hosts |
| 76 |
''' |
| 77 |
|
| 78 |
Login into one host: |
| 79 |
''' |
| 80 |
$ ssh internal.example.com |
| 81 |
'''`, |
| 82 |
Subcommands: cli.Commands{ |
| 83 |
certificateCommand(), |
| 84 |
configCommand(), |
| 85 |
loginCommand(), |
| 86 |
logoutCommand(), |
| 87 |
inspectCommand(), |
| 88 |
listCommand(), |
| 89 |
fingerPrintCommand(), |
| 90 |
|
| 91 |
proxycommandCommand(), |
| 92 |
checkHostCommand(), |
| 93 |
hostsCommand(), |
| 94 |
renewCommand(), |
| 95 |
revokeCommand(), |
| 96 |
rekeyCommand(), |
| 97 |
}, |
| 98 |
} |
| 99 |
|
| 100 |
command.Register(cmd) |
| 101 |
} |
| 102 |
|
| 103 |
var ( |
| 104 |
sshPrincipalFlag = cli.StringSliceFlag{ |
| 105 |
Name: "principal,n", |
| 106 |
Usage: `Add the specified principal (user or host <name>s) to the certificate request. |
| 107 |
This flag can be used multiple times. However, it cannot be used in conjunction |
| 108 |
with '--token' when requesting certificates from OIDC, JWK, and X5C provisioners, or |
| 109 |
from any provisioner with 'disableCustomSANs' set to 'true'. These provisioners will |
| 110 |
use the contents of the token to determine the principals.`, |
| 111 |
} |
| 112 |
|
| 113 |
sshHostFlag = cli.BoolFlag{ |
| 114 |
Name: "host", |
| 115 |
Usage: `Create a host certificate instead of a user certificate.`, |
| 116 |
} |
| 117 |
|
| 118 |
sshHostIDFlag = cli.StringFlag{ |
| 119 |
Name: "host-id", |
| 120 |
Usage: `Specify a <UUID> to identify the host rather than using an auto-generated UUID. |
| 121 |
If "machine" is passed, derive a UUID from "/etc/machine-id."`, |
| 122 |
} |
| 123 |
|
| 124 |
sshSignFlag = cli.BoolFlag{ |
| 125 |
Name: "sign", |
| 126 |
Usage: `Sign the public key passed as an argument instead of creating one.`, |
| 127 |
} |
| 128 |
|
| 129 |
sshPasswordFileFlag = cli.StringFlag{ |
| 130 |
Name: "password-file", |
| 131 |
Usage: `The path to the <file> containing the password to encrypt the private key.`, |
| 132 |
} |
| 133 |
|
| 134 |
sshProvisionerPasswordFlag = cli.StringFlag{ |
| 135 |
Name: "provisioner-password-file", |
| 136 |
Usage: `The path to the <file> containing the password to decrypt the one-time token |
| 137 |
generating key.`, |
| 138 |
} |
| 139 |
|
| 140 |
sshAddUserFlag = cli.BoolFlag{ |
| 141 |
Name: "add-user", |
| 142 |
Usage: `Create a user provisioner certificate used to create a new user.`, |
| 143 |
} |
| 144 |
|
| 145 |
sshPrivateKeyFlag = cli.StringFlag{ |
| 146 |
Name: "private-key", |
| 147 |
Usage: `When signing an existing public key, use this flag to specify the corresponding |
| 148 |
private key so that the pair can be added to an SSH Agent.`, |
| 149 |
} |
| 150 |
) |
| 151 |
|
| 152 |
func loginOnUnauthorized(ctx *cli.Context) (ca.RetryFunc, error) { |
| 153 |
templateData, err := flags.ParseTemplateData(ctx) |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
|
| 158 |
flow, err := cautils.NewCertificateFlow(ctx) |
| 159 |
if err != nil { |
| 160 |
return nil, err |
| 161 |
} |
| 162 |
|
| 163 |
client, err := cautils.NewClient(ctx) |
| 164 |
if err != nil { |
| 165 |
return nil, err |
| 166 |
} |
| 167 |
|
| 168 |
return func(code int) bool { |
| 169 |
if code != http.StatusUnauthorized { |
| 170 |
return false |
| 171 |
} |
| 172 |
|
| 173 |
fail := func(err error) bool { |
| 174 |
ui.Printf("{{ \"%v\" | red }}\n", err) |
| 175 |
return false |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
tok, err := flow.GenerateIdentityToken(ctx) |
| 180 |
if err != nil { |
| 181 |
return fail(err) |
| 182 |
} |
| 183 |
jwt, err := token.ParseInsecure(tok) |
| 184 |
if err != nil { |
| 185 |
return fail(err) |
| 186 |
} |
| 187 |
if jwt.Payload.Email == "" { |
| 188 |
return fail(errors.New("error creating identity token: email address cannot be empty")) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
pub, priv, err := keys.GenerateDefaultKeyPair() |
| 193 |
if err != nil { |
| 194 |
return fail(err) |
| 195 |
} |
| 196 |
sshPub, err := ssh.NewPublicKey(pub) |
| 197 |
if err != nil { |
| 198 |
return fail(err) |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
identityCSR, identityKey, err := ca.CreateIdentityRequest(jwt.Payload.Email) |
| 203 |
if err != nil { |
| 204 |
return fail(err) |
| 205 |
} |
| 206 |
|
| 207 |
resp, err := client.SSHSign(&api.SSHSignRequest{ |
| 208 |
PublicKey: sshPub.Marshal(), |
| 209 |
OTT: tok, |
| 210 |
CertType: provisioner.SSHUserCert, |
| 211 |
KeyID: jwt.Payload.Email, |
| 212 |
IdentityCSR: *identityCSR, |
| 213 |
TemplateData: templateData, |
| 214 |
}) |
| 215 |
if err != nil { |
| 216 |
return fail(err) |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if err := ca.WriteDefaultIdentity(resp.IdentityCertificate, identityKey); err != nil { |
| 221 |
return fail(err) |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
if agent, err := sshutil.DialAgent(); err == nil { |
| 226 |
agent.AddCertificate(jwt.Payload.Email, resp.Certificate.Certificate, priv) |
| 227 |
} |
| 228 |
|
| 229 |
return true |
| 230 |
}, nil |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
func tokenHasEmail(s string) (string, bool) { |
| 236 |
jwt, err := token.ParseInsecure(s) |
| 237 |
if err != nil { |
| 238 |
return "", false |
| 239 |
} |
| 240 |
return jwt.Payload.Email, jwt.Payload.Email != "" |
| 241 |
} |
| 242 |
|
| 243 |
func sshConfigErr(err error) error { |
| 244 |
return &errs.Error{ |
| 245 |
Err: err, |
| 246 |
Msg: "There is a problem with your step configuration. Please run 'step ssh config'.", |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func contactAdminErr(err error) error { |
| 251 |
return &errs.Error{ |
| 252 |
Err: err, |
| 253 |
Msg: "There is a problem with your step configuration. Please contact an administrator.", |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
func debugErr(err error) error { |
| 258 |
return &errs.Error{ |
| 259 |
Err: err, |
| 260 |
Msg: "An error occurred in the step process. Please contact an administrator.", |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
func createPrincipalsFromSubject(subject string) []string { |
| 269 |
name := provisioner.SanitizeSSHUserPrincipal(subject) |
| 270 |
principals := []string{name} |
| 271 |
if i := strings.LastIndex(subject, "@"); i >= 0 { |
| 272 |
if local := subject[:i]; !strings.EqualFold(local, name) { |
| 273 |
principals = append(principals, local) |
| 274 |
} |
| 275 |
principals = append(principals, subject) |
| 276 |
} |
| 277 |
return principals |
| 278 |
} |