-
Notifications
You must be signed in to change notification settings - Fork 43
/
revoke_listusers.go
114 lines (94 loc) · 2.82 KB
/
revoke_listusers.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
/*
* Copyright 2018-2020 The NATS Authors
* 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"
"time"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/spf13/cobra"
"github.com/xlab/tablewriter"
)
func createRevokeListUsersCmd() *cobra.Command {
var params RevokeListUserParams
cmd := &cobra.Command{
Use: "list-users",
Aliases: []string{"list_users"},
Short: "List users revoked in an account",
Args: MaxArgs(0),
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return RunAction(cmd, args, ¶ms)
},
}
params.AccountContextParams.BindFlags(cmd)
return cmd
}
func init() {
revokeCmd.AddCommand(createRevokeListUsersCmd())
}
// RevokeListUserParams hold the info necessary to add a user to the revocation list in an account
type RevokeListUserParams struct {
AccountContextParams
claim *jwt.AccountClaims
}
func (p *RevokeListUserParams) SetDefaults(ctx ActionCtx) error {
return p.AccountContextParams.SetDefaults(ctx)
}
func (p *RevokeListUserParams) PreInteractive(ctx ActionCtx) error {
var err error
if err = p.AccountContextParams.Edit(ctx); err != nil {
return err
}
return nil
}
func (p *RevokeListUserParams) Load(ctx ActionCtx) error {
var err error
if err = p.AccountContextParams.Validate(ctx); err != nil {
return err
}
p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name)
if err != nil {
return err
}
return nil
}
func (p *RevokeListUserParams) Validate(ctx ActionCtx) error {
return nil
}
func (p *RevokeListUserParams) PostInteractive(ctx ActionCtx) error {
return nil
}
func (p *RevokeListUserParams) Run(ctx ActionCtx) (store.Status, error) {
name := p.claim.Name
if name == "" {
name = p.claim.Subject
}
if len(p.claim.Revocations) == 0 {
return nil, fmt.Errorf("account %s does not have revoked users", name)
}
table := tablewriter.CreateTable()
table.AddTitle(fmt.Sprintf("Revoked Users for %s", name))
table.AddHeaders("Public Key", "Revoke Credentials Before")
for pubKey, at := range p.claim.Revocations {
if pubKey == jwt.All {
pubKey = fmt.Sprintf("%s [All Users]", pubKey)
}
t := time.Unix(at, 0)
formatted := t.Format(time.RFC1123)
table.AddRow(pubKey, formatted)
}
return nil, Write("--", []byte(table.Render()))
}