forked from cloudflare/cfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfssl_sign.go
74 lines (61 loc) · 1.92 KB
/
cfssl_sign.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
package main
import (
"fmt"
"io/ioutil"
"github.com/cloudflare/cfssl/config"
"github.com/cloudflare/cfssl/log"
"github.com/cloudflare/cfssl/signer"
)
// Usage text of 'cfssl sign'
var signerUsageText = `cfssl sign -- signs a client cert with a host name by a given CA and CA key
Usage of sign:
cfssl sign [-ca cert] [-ca-key key] HOSTNAME CSR
Arguments:
HOSTNAME: Hostname for the cert
CSR: Certificate request.
Note: HOSTNAME, CERT can also be supplied as flag value. But flag value will take precedence, overwriting the argument.
Flags:
`
// Flags of 'cfssl sign'
var signerFlags = []string{"hostname", "csr", "ca", "ca-key", "f", "profile", "f"}
// signerMain is the main CLI of signer functionality.
// [TODO: zi] Decide whether to drop the argument list and only use flags to specify all the inputs.
func signerMain(args []string) (err error) {
// Grab values through args only if corresponding flags are absent
if Config.hostname == "" {
Config.hostname, args, err = popFirstArgument(args)
if err != nil {
return
}
}
if Config.certFile == "" {
Config.certFile, args, err = popFirstArgument(args)
if err != nil {
return
}
}
// Read the certificate and sign it with CA files
log.Debug("Loading Client certificate: ", Config.certFile)
clientCert, err := ioutil.ReadFile(Config.certFile)
if err != nil {
return
}
var policy *config.Signing
// If there is a config, use its signing policy. Otherwise, leave policy == nil
// and NewSigner will use DefaultConfig().
if Config.cfg != nil {
policy = Config.cfg.Signing
}
signer, err := signer.NewSigner(Config.caFile, Config.caKeyFile, policy)
if err != nil {
return
}
cert, err := signer.Sign(Config.hostname, clientCert, Config.profile)
if err != nil {
return
}
fmt.Printf("%s", cert)
return
}
// CLISigner assembles the definition of Command 'sign'
var CLISigner = &Command{signerUsageText, signerFlags, signerMain}