/
revoke.go
96 lines (76 loc) · 2.59 KB
/
revoke.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
/*
Copyright © 2023 Julian Easterling <julian@julianscorner.com>
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 cmd
import (
"fmt"
"os"
"path"
"github.com/spf13/cobra"
)
var revokeReason RevokeType = unspecified
var revokeCmd = &cobra.Command{
Use: "revoke",
Short: "Revoke subordinate authority.",
Long: "Revoke subordinate authority.",
PreRun: func(cmd *cobra.Command, args []string) {
if len(settings.Name) == 0 {
cobra.CheckErr(fmt.Errorf("'%s' is not a certificate authority", folderPath))
}
if settings.Type != "root" {
cobra.CheckErr(fmt.Errorf("'%s' is not a root certificate authority", folderPath))
}
ensureAuthorityDirectory()
},
PostRun: func(cmd *cobra.Command, args []string) {
ensureWorkingDirectoryAndExit()
},
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
for _, s := range settings.Subordinates {
if s.Name == name {
if s.Id == "~REVOKED~" {
cobra.CheckErr(fmt.Errorf("'%s' authority has already been revoked", s.Name))
}
pass := askPassword("private/ca.key")
revoke_authority(s, revokeReason, pass)
settings.Subordinates = addSubordinate(settings, s.Name, "~REVOKED~")
save_authority("ca.yml", settings)
return
}
}
cobra.CheckErr(fmt.Errorf("'%s' is not a subordinate of this authority", name))
},
}
func init() {
rootCmd.AddCommand(revokeCmd)
revokeCmd.Flags().VarP(&revokeReason, "reason", "r", "reason for revocation")
revokeCmd.Flags().StringP("name", "n", "", "name of subordinate authority")
revokeCmd.MarkFlagRequired("name")
}
func revoke_authority(subordinate Subordinate, reason RevokeType, pass string) {
if len(subordinate.Id) > 0 {
cert := path.Join("certs", fmt.Sprintf("%s.pem", subordinate.Id))
if fileExists(cert) {
executeExternalProgram("openssl", []string{
"ca",
"-config ca.cnf",
fmt.Sprintf("-revoke %s", cert),
fmt.Sprintf("-crl_reason %s", reason),
fmt.Sprintf("-passin pass:%s", pass),
}...)
os.Rename(cert, fmt.Sprintf("%s.revoked", cert))
} else {
cobra.CheckErr(fmt.Errorf("'%s' certificate file not found", subordinate.Id))
}
}
}