forked from cfergeau/quill
/
utils.go
60 lines (49 loc) · 1.39 KB
/
utils.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
package commands
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"software.sslmate.com/src/go-pkcs12"
"github.com/dustin-decker/quill/internal/bus"
"github.com/dustin-decker/quill/internal/redact"
"github.com/dustin-decker/quill/quill/pki/load"
)
func loadP12Interactively(p12Path, password string) (*load.P12Contents, error) {
p12Content, err := load.P12(p12Path, password)
if err == nil {
return p12Content, nil
}
if !errors.Is(err, load.ErrNeedPassword) {
return nil, err
}
by, err := load.BytesFromFileOrEnv(p12Path)
if err != nil {
return nil, fmt.Errorf("unable to read p12 bytes: %w", err)
}
prompter := bus.PromptForInput("Enter P12 password:", true)
newPassword, err := prompter.Response(context.Background())
if err != nil {
return nil, fmt.Errorf("unable to get password from prompt: %w", err)
}
redact.Add(newPassword)
key, cert, certs, err := pkcs12.DecodeChain(by, newPassword)
if err != nil {
return nil, fmt.Errorf("unable to decode p12 file: %w", err)
}
return &load.P12Contents{
PrivateKey: key,
Certificate: cert,
Certificates: certs,
}, nil
}
func chainArgs(processors ...func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
for _, p := range processors {
if err := p(cmd, args); err != nil {
return err
}
}
return nil
}
}