/
purge.go
154 lines (126 loc) · 4.39 KB
/
purge.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
package agent
import (
"context"
"flag"
"fmt"
"time"
"github.com/mitchellh/cli"
"github.com/spiffe/go-spiffe/v2/spiffeid"
agentv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/agent/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/idutil"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type purgeCommand struct {
env *commoncli.Env
expiredFor time.Duration
dryRun bool
printer cliprinter.Printer
}
func NewPurgeCommand() cli.Command {
return NewPurgeCommandWithEnv(commoncli.DefaultEnv)
}
func NewPurgeCommandWithEnv(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &purgeCommand{env: env})
}
func (*purgeCommand) Name() string {
return "agent purge"
}
func (*purgeCommand) Synopsis() string {
return "Purge expired agents that were attested using a non-TOFU security model based on a given time"
}
func (c *purgeCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) (err error) {
agentClient := serverClient.NewAgentClient()
resp, err := agentClient.ListAgents(ctx, &agentv1.ListAgentsRequest{
Filter: &agentv1.ListAgentsRequest_Filter{ByCanReattest: wrapperspb.Bool(true)},
OutputMask: &types.AgentMask{X509SvidExpiresAt: true},
})
if err != nil {
return fmt.Errorf("failed to list agents: %w", err)
}
agents := resp.GetAgents()
expiredAgents := &expiredAgents{Agents: []*expiredAgent{}}
for _, agent := range agents {
id, err := idutil.IDFromProto(agent.Id)
if err != nil {
return err
}
expirationTime := time.Unix(agent.X509SvidExpiresAt, 0)
if time.Since(expirationTime) > c.expiredFor {
result := &expiredAgent{AgentID: id}
if !c.dryRun {
if _, err := agentClient.DeleteAgent(ctx, &agentv1.DeleteAgentRequest{Id: agent.Id}); err != nil {
result.Error = err.Error()
} else {
result.Deleted = true
}
}
expiredAgents.Agents = append(expiredAgents.Agents, result)
}
}
return c.printer.PrintStruct(expiredAgents)
}
func (c *purgeCommand) AppendFlags(fs *flag.FlagSet) {
fs.DurationVar(&c.expiredFor, "expiredFor", 30*24*time.Hour, "Amount of time that has passed since the agent's SVID has expired. It is used to determine which agents to purge.")
fs.BoolVar(&c.dryRun, "dryRun", false, "Indicates that the command will not perform any action, but will print the agents that would be purged.")
cliprinter.AppendFlagWithCustomPretty(&c.printer, fs, c.env, c.prettyPrintPurgeResult)
}
type expiredAgents struct {
Agents []*expiredAgent `json:"expired_agents"`
}
type expiredAgent struct {
AgentID spiffeid.ID `json:"agent_id"`
Deleted bool `json:"deleted"`
Error string `json:"error,omitempty"`
}
func (c *purgeCommand) prettyPrintPurgeResult(env *commoncli.Env, results ...any) error {
if expAgents, ok := results[0].([]any)[0].(*expiredAgents); ok {
if len(expAgents.Agents) == 0 {
env.Println("No agents to purge.")
return nil
}
msg := fmt.Sprintf("Found %d expired ", len(expAgents.Agents))
msg = util.Pluralizer(msg, "agent", "agents", len(expAgents.Agents))
env.Printf("%s\n\n", msg)
if c.dryRun {
env.Println("\nAgents that can be purged:")
for _, result := range expAgents.Agents {
env.Printf("SPIFFE ID : %s\n", result.AgentID.String())
}
return nil
}
var agentsNotPurged []*expiredAgent
var agentsPurged []*expiredAgent
for _, result := range expAgents.Agents {
if result.Deleted {
agentsPurged = append(agentsPurged, result)
} else {
agentsNotPurged = append(agentsNotPurged, result)
}
}
if len(agentsPurged) > 0 {
c.printAgentsPurged(agentsPurged)
}
if len(agentsNotPurged) > 0 {
c.printAgentsNotPurged(agentsNotPurged)
}
return nil
}
return cliprinter.ErrInternalCustomPrettyFunc
}
func (c *purgeCommand) printAgentsNotPurged(agentsNotPurged []*expiredAgent) {
c.env.Println("Agents not purged:")
for _, result := range agentsNotPurged {
c.env.Printf("SPIFFE ID : %s\n", result.AgentID.String())
c.env.Printf("Error : %s\n", result.Error)
}
}
func (c *purgeCommand) printAgentsPurged(agentsPurged []*expiredAgent) {
c.env.Println("Agents purged:")
for _, result := range agentsPurged {
c.env.Printf("SPIFFE ID : %s\n", result.AgentID.String())
}
}