Debian Codegraph @

command/ssh/renew.go

raw symbols SCIP
1 package ssh
2
3 import (
4 "io/ioutil"
5 "strconv"
6
7 "github.com/smallstep/certificates/ca/identity"
8
9 "github.com/pkg/errors"
10 "github.com/smallstep/certificates/api"
11 "github.com/smallstep/certificates/authority/provisioner"
12 "github.com/smallstep/cli/command"
13 "github.com/smallstep/cli/errs"
14 "github.com/smallstep/cli/flags"
15 "github.com/smallstep/cli/ui"
16 "github.com/smallstep/cli/utils"
17 "github.com/smallstep/cli/utils/cautils"
18 "github.com/urfave/cli"
19 "golang.org/x/crypto/ssh"
20 )
21
22 func renewCommand() cli.Command {
23 return cli.Command{
24 Name: "renew",
25 Action: command.ActionFunc(renewAction),
26 Usage: "renew a SSH certificate using the SSH CA",
27 UsageText: `**step ssh renew** <ssh-cert> <ssh-key>
28 [**--out**=<file>] [**--issuer**=<name>] [**--password-file**=<path>]
29 [**--force**] [**--ca-url**=<uri>] [**--root**=<path>]
30 [**--offline**] [**--ca-config**=<path>]`,
31 Description: `**step ssh renew** command renews an SSH Cerfificate
32 using [step certificates](https://github.com/smallstep/certificates).
33 It writes the new certificate to disk - either overwriting <ssh-cert> or
34 using a new file when the **--out**=<file> flag is used.
35
36 ## POSITIONAL ARGUMENTS
37
38 <ssh-cert>
39 : The ssh certificate to renew.
40
41 <ssh-key>
42 : The ssh certificate private key.
43
44 ## EXAMPLES
45
46 Renew an ssh certificate overwriting the previous one:
47 '''
48 $ step ssh renew -f id_ecdsa-cert.pub id_ecdsa
49 '''
50
51 Renew an ssh certificate with a custom out file:
52 '''
53 $ step ssh renew -out new-id_ecdsa-cer.pub id_ecdsa-cert.pub id_ecdsa
54 '''`,
55 Flags: []cli.Flag{
56 cli.StringFlag{
57 Name: "out,output-file",
58 Usage: "The new certificate <file> path. Defaults to overwriting the <ssh-cert> positional argument",
59 },
60 flags.Provisioner,
61 sshProvisionerPasswordFlag,
62 flags.Force,
63 flags.CaURL,
64 flags.Root,
65 flags.Offline,
66 flags.CaConfig,
67 flags.SSHPOPCert,
68 flags.SSHPOPKey,
69 },
70 }
71 }
72
73 func renewAction(ctx *cli.Context) error {
74 if err := errs.NumberOfArguments(ctx, 2); err != nil {
75 return err
76 }
77
78 args := ctx.Args()
79 certFile := args.Get(0)
80 keyFile := args.Get(1)
81
82 // Flags
83 outFile := ctx.String("out")
84 if outFile == "" {
85 outFile = certFile
86 }
87
88 flow, err := cautils.NewCertificateFlow(ctx)
89 if err != nil {
90 return err
91 }
92
93 // Load the cert, because we need the serial number.
94 certBytes, err := ioutil.ReadFile(certFile)
95 if err != nil {
96 return errors.Wrapf(err, "error reading ssh certificate from %s", certFile)
97 }
98 sshpub, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
99 if err != nil {
100 return errors.Wrapf(err, "error parsing ssh public key from %s", certFile)
101 }
102 cert, ok := sshpub.(*ssh.Certificate)
103 if !ok {
104 return errors.New("error casting ssh public key to ssh certificate")
105 }
106 serial := strconv.FormatUint(cert.Serial, 10)
107
108 ctx.Set("sshpop-cert", certFile)
109 ctx.Set("sshpop-key", keyFile)
110 token, err := flow.GenerateSSHToken(ctx, serial, cautils.SSHRenewType, nil, provisioner.TimeDuration{}, provisioner.TimeDuration{})
111 if err != nil {
112 return err
113 }
114
115 caClient, err := flow.GetClient(ctx, token)
116 if err != nil {
117 return err
118 }
119
120 resp, err := caClient.SSHRenew(&api.SSHRenewRequest{
121 OTT: token,
122 })
123 if err != nil {
124 return err
125 }
126
127 // Write certificate
128 if err := utils.WriteFile(outFile, marshalPublicKey(resp.Certificate, cert.KeyId), 0644); err != nil {
129 return err
130 }
131
132 // Write renewed identity
133 if len(resp.IdentityCertificate) > 0 {
134 if err := identity.WriteIdentityCertificate(resp.IdentityCertificate); err != nil {
135 return err
136 }
137 }
138
139 ui.PrintSelected("Certificate", outFile)
140
141 return nil
142 }