This repository has been archived by the owner on Jan 24, 2020. It is now read-only.
forked from smallstep/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.go
85 lines (68 loc) · 2.08 KB
/
bundle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package certificate
import (
"encoding/pem"
"io/ioutil"
"github.com/pkg/errors"
"github.com/smallstep/cli/command"
"github.com/smallstep/cli/errs"
"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/ui"
"github.com/smallstep/cli/utils"
"github.com/urfave/cli"
)
func bundleCommand() cli.Command {
return cli.Command{
Name: "bundle",
Action: command.ActionFunc(bundleAction),
Usage: `bundle a certificate with intermediate certificate(s) needed for certificate path validation`,
UsageText: `**step certificate bundle** <crt_file> <ca> <bundle_file>`,
Description: `**step certificate bundle** bundles a certificate
with any intermediates necessary to validate the certificate.
## POSITIONAL ARGUMENTS
<crt_file>
: The path to a leaf certificate to bundle with issuing certificate(s).
<ca>
: The path to the Certificate Authority issusing certificate.
<bundle_file>
: The path to write the bundle.
## EXIT CODES
This command returns 0 on success and \>0 if any error occurs.
## EXAMPLES
Bundle a certificate with the intermediate certificate authority (issuer):
'''
$ step certificate bundle foo.crt intermediate-ca.crt foo-bundle.crt
'''
`,
Flags: []cli.Flag{flags.Force},
}
}
func bundleAction(ctx *cli.Context) error {
if err := errs.NumberOfArguments(ctx, 3); err != nil {
return err
}
crtFile := ctx.Args().Get(0)
crtBytes, err := ioutil.ReadFile(crtFile)
if err != nil {
return errs.FileError(err, crtFile)
}
crtBlock, _ := pem.Decode(crtBytes)
if crtBlock == nil {
return errors.Errorf("could not parse certificate file '%s'", crtFile)
}
caFile := ctx.Args().Get(1)
caBytes, err := ioutil.ReadFile(caFile)
if err != nil {
return errs.FileError(err, caFile)
}
caBlock, _ := pem.Decode(caBytes)
if caBlock == nil {
return errors.Errorf("could not parse certificate file '%s'", caFile)
}
chainFile := ctx.Args().Get(2)
if err := utils.WriteFile(chainFile,
append(pem.EncodeToMemory(crtBlock), pem.EncodeToMemory(caBlock)...), 0600); err != nil {
return err
}
ui.Printf("Your certificate has been saved in %s.\n", chainFile)
return nil
}