-
Notifications
You must be signed in to change notification settings - Fork 11
/
rule-remove.go
101 lines (86 loc) · 2.91 KB
/
rule-remove.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
package rule
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/airbnb/rudolph/internal/cli/flags"
"github.com/airbnb/rudolph/pkg/clock"
"github.com/airbnb/rudolph/pkg/dynamodb"
"github.com/airbnb/rudolph/pkg/model/globalrules"
"github.com/airbnb/rudolph/pkg/model/machinerules"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
// The `rule remove` command takes slightly different arguments than the other
// `rule` commands so we'll handle it a bit differently here
func init() {
tf := flags.TargetFlags{}
var removeRuleCmd = &cobra.Command{
Use: `remove <rule-name> ex: 'TeamID#1234567'`,
Aliases: []string{"delete"},
Short: "Removes/deletes a rule from the backing store",
Long: `<rule-name> | <RuleType: Binary,Certificate,TeamID,SigningID>#<Rule Identifier/SHA256: abcdef12345-12345-12345> | 'TeamID#1234567'`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
region, _ := cmd.Flags().GetString("region")
table, _ := cmd.Flags().GetString("dynamodb_table")
dynamodbClient := dynamodb.GetClient(table, region)
globalRemover := globalrules.ConcreteRuleRemovalService{
TimeProvider: clock.ConcreteTimeProvider{},
Getter: dynamodbClient,
Transacter: dynamodbClient,
}
machineRuleRemover := machinerules.ConcreteRuleRemovalService{
Getter: dynamodbClient,
Updater: dynamodbClient,
}
return removeRule(globalRemover, machineRuleRemover, args[0], tf)
},
}
tf.AddTargetFlags(removeRuleCmd)
RuleCmd.AddCommand(removeRuleCmd)
}
func removeRule(globalRemover globalrules.RuleRemovalService, machineRuleRemover machinerules.RuleRemovalService, ruleName string, tf flags.TargetFlags) error {
// First, determine which machine to apply
var machineID string
if !tf.IsGlobal || tf.IsTargetSelf() {
var err error
machineID, err = tf.GetMachineID()
if err != nil {
return fmt.Errorf("failed to get MachineID: %w", err)
}
}
fmt.Println("Removing the following rule:")
if machineID != "" {
fmt.Println(" MachineID: ", machineID)
}
fmt.Println(" Identifier/SHA256: ", ruleName)
fmt.Println("")
fmt.Println(`Apply changes? (Enter: "yes" or "ok")`)
fmt.Print("> ")
// Read confirmation
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
if strings.ToLower(text) == "ok" || strings.ToLower(text) == "yes" {
// Do rule deletion
if !tf.IsGlobal {
machineID, err := tf.GetMachineID()
if err != nil {
return fmt.Errorf("failed to get MachineID: %v", err)
}
return machineRuleRemover.RemoveMachineRule(machineID, ruleName)
}
idempotencyKey := uuid.NewString()
err := globalRemover.RemoveGlobalRule(ruleName, idempotencyKey)
if err != nil {
return fmt.Errorf("failed to remove global rule: %v", err)
}
fmt.Println("Successfully sent a rule to dynamodb")
} else {
fmt.Println("Well ok then")
}
fmt.Println("")
return nil
}