forked from hyperledger/fabric-ca
/
gencrl.go
148 lines (140 loc) · 4.36 KB
/
gencrl.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package command
import (
"os"
"path"
"path/filepath"
"time"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/lib"
"github.com/hyperledger/fabric-ca/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
// crlsFolder is the MSP folder name where generate CRL will be stored
crlsFolder = "crls"
// crlFile is the name of the file used to the generate CRL
crlFile = "crl.pem"
)
func (c *ClientCmd) newGenCRLCommand() *cobra.Command {
var genCrlCmd = &cobra.Command{
Use: "gencrl",
Short: "Generate a CRL",
Long: "Generate a Certificate Revocation List",
// PreRunE block for this command will load client configuration
// before running the command
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}
err := c.ConfigInit()
if err != nil {
return err
}
log.Debugf("Client configuration settings: %+v", c.clientCfg)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
err := c.runGenCRL()
if err != nil {
return err
}
return nil
},
}
util.RegisterFlags(c.myViper, genCrlCmd.Flags(), &c.crlParams, nil)
return genCrlCmd
}
// The client register main logic
func (c *ClientCmd) runGenCRL() error {
log.Debug("Entered runGenCRL")
client := lib.Client{
HomeDir: filepath.Dir(c.cfgFileName),
Config: c.clientCfg,
}
id, err := client.LoadMyIdentity()
if err != nil {
return err
}
var revokedAfter, revokedBefore time.Time
if c.crlParams.RevokedAfter != "" {
revokedAfter, err = time.Parse(time.RFC3339, c.crlParams.RevokedAfter)
if err != nil {
return errors.Wrap(err, "Invalid 'revokedafter' value")
}
}
if c.crlParams.RevokedBefore != "" {
revokedBefore, err = time.Parse(time.RFC3339, c.crlParams.RevokedBefore)
if err != nil {
return errors.Wrap(err, "Invalid 'revokedbefore' value")
}
}
if !revokedBefore.IsZero() && revokedAfter.After(revokedBefore) {
return errors.Errorf("Invalid revokedafter value '%s'. It must not be a timestamp greater than revokedbefore value '%s'",
c.crlParams.RevokedAfter, c.crlParams.RevokedBefore)
}
var expireAfter, expireBefore time.Time
if c.crlParams.ExpireAfter != "" {
expireAfter, err = time.Parse(time.RFC3339, c.crlParams.ExpireAfter)
if err != nil {
return errors.Wrap(err, "Invalid 'expireafter' value")
}
}
if c.crlParams.ExpireBefore != "" {
expireBefore, err = time.Parse(time.RFC3339, c.crlParams.ExpireBefore)
if err != nil {
return errors.Wrap(err, "Invalid 'expirebefore' value")
}
}
if !expireBefore.IsZero() && expireAfter.After(expireBefore) {
return errors.Errorf("Invalid expireafter value '%s'. It must not be a timestamp greater than expirebefore value '%s'",
c.crlParams.ExpireAfter, c.crlParams.ExpireBefore)
}
req := &api.GenCRLRequest{
CAName: c.clientCfg.CAName,
RevokedAfter: revokedAfter,
RevokedBefore: revokedBefore,
ExpireAfter: expireAfter,
ExpireBefore: expireBefore,
}
resp, err := id.GenCRL(req)
if err != nil {
return err
}
log.Info("Successfully generated the CRL")
err = storeCRL(c.clientCfg, resp.CRL)
if err != nil {
return err
}
return nil
}
// Store the CRL
func storeCRL(config *lib.ClientConfig, crl []byte) error {
dirName := path.Join(config.MSPDir, crlsFolder)
if _, err := os.Stat(dirName); os.IsNotExist(err) {
mkdirErr := os.MkdirAll(dirName, os.ModeDir|0755)
if mkdirErr != nil {
return errors.Wrapf(mkdirErr, "Failed to create directory %s", dirName)
}
}
fileName := path.Join(dirName, crlFile)
err := util.WriteFile(fileName, crl, 0644)
if err != nil {
return errors.Wrapf(err, "Failed to write CRL to the file %s", fileName)
}
log.Infof("Successfully stored the CRL in the file %s", fileName)
return nil
}