-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
token_revoke.go
174 lines (140 loc) · 4.54 KB
/
token_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package command
import (
"fmt"
"strings"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
var _ cli.Command = (*TokenRevokeCommand)(nil)
var _ cli.CommandAutocomplete = (*TokenRevokeCommand)(nil)
type TokenRevokeCommand struct {
*BaseCommand
flagAccessor bool
flagSelf bool
flagMode string
}
func (c *TokenRevokeCommand) Synopsis() string {
return "Revoke a token and its children"
}
func (c *TokenRevokeCommand) Help() string {
helpText := `
Usage: vault token revoke [options] [TOKEN | ACCESSOR]
Revokes authentication tokens and their children. If a TOKEN is not provided,
the locally authenticated token is used. The "-mode" flag can be used to
control the behavior of the revocation. See the "-mode" flag documentation
for more information.
Revoke a token and all the token's children:
$ vault token revoke 96ddf4bc-d217-f3ba-f9bd-017055595017
Revoke a token leaving the token's children:
$ vault token revoke -mode=orphan 96ddf4bc-d217-f3ba-f9bd-017055595017
Revoke a token by accessor:
$ vault token revoke -accessor 9793c9b3-e04a-46f3-e7b8-748d7da248da
For a full list of examples, please see the documentation.
` + c.Flags().Help()
return strings.TrimSpace(helpText)
}
func (c *TokenRevokeCommand) Flags() *FlagSets {
set := c.flagSet(FlagSetHTTP)
f := set.NewFlagSet("Command Options")
f.BoolVar(&BoolVar{
Name: "accessor",
Target: &c.flagAccessor,
Default: false,
EnvVar: "",
Completion: complete.PredictNothing,
Usage: "Treat the argument as an accessor instead of a token.",
})
f.BoolVar(&BoolVar{
Name: "self",
Target: &c.flagSelf,
Default: false,
EnvVar: "",
Completion: complete.PredictNothing,
Usage: "Perform the revocation on the currently authenticated token.",
})
f.StringVar(&StringVar{
Name: "mode",
Target: &c.flagMode,
Default: "",
EnvVar: "",
Completion: complete.PredictSet("orphan", "path"),
Usage: "Type of revocation to perform. If unspecified, Vault will revoke " +
"the token and all of the token's children. If \"orphan\", Vault will " +
"revoke only the token, leaving the children as orphans. If \"path\", " +
"tokens created from the given authentication path prefix are deleted " +
"along with their children.",
})
return set
}
func (c *TokenRevokeCommand) AutocompleteArgs() complete.Predictor {
return nil
}
func (c *TokenRevokeCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
func (c *TokenRevokeCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
args = f.Args()
token := ""
if len(args) > 0 {
token = strings.TrimSpace(args[0])
}
switch c.flagMode {
case "", "orphan", "path":
default:
c.UI.Error(fmt.Sprintf("Invalid mode: %s", c.flagMode))
return 1
}
switch {
case c.flagSelf && len(args) > 0:
c.UI.Error(fmt.Sprintf("Too many arguments with -self (expected 0, got %d)", len(args)))
return 1
case !c.flagSelf && len(args) > 1:
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1 or -self, got %d)", len(args)))
return 1
case !c.flagSelf && len(args) < 1:
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1 or -self, got %d)", len(args)))
return 1
case c.flagSelf && c.flagAccessor:
c.UI.Error("Cannot use -self with -accessor!")
return 1
case c.flagSelf && c.flagMode != "":
c.UI.Error("Cannot use -self with -mode!")
return 1
case c.flagAccessor && c.flagMode == "orphan":
c.UI.Error("Cannot use -accessor with -mode=orphan!")
return 1
case c.flagAccessor && c.flagMode == "path":
c.UI.Error("Cannot use -accessor with -mode=path!")
return 1
}
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
var revokeFn func(string) error
// Handle all 6 possible combinations
switch {
case !c.flagAccessor && c.flagSelf && c.flagMode == "":
revokeFn = client.Auth().Token().RevokeSelf
case !c.flagAccessor && !c.flagSelf && c.flagMode == "":
revokeFn = client.Auth().Token().RevokeTree
case !c.flagAccessor && !c.flagSelf && c.flagMode == "orphan":
revokeFn = client.Auth().Token().RevokeOrphan
case !c.flagAccessor && !c.flagSelf && c.flagMode == "path":
revokeFn = client.Sys().RevokePrefix
case c.flagAccessor && !c.flagSelf && c.flagMode == "":
revokeFn = client.Auth().Token().RevokeAccessor
}
if err := revokeFn(token); err != nil {
c.UI.Error(fmt.Sprintf("Error revoking token: %s", err))
return 2
}
c.UI.Output("Success! Revoked token (if it existed)")
return 0
}