forked from harbur/kubebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubebot.go
171 lines (154 loc) · 4.05 KB
/
kubebot.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
package main
import (
"errors"
"fmt"
"time"
"github.com/go-chat-bot/bot"
)
type Kubebot struct {
token string
admins map[string]bool
channels map[string]bool
commands map[string]bool
}
const (
forbiddenUserMessage string = "%s - ⚠ kubectl forbidden for user @%s\n"
forbiddenChannelMessage string = "%s - ⚠ Channel %s forbidden for user @%s\n"
forbiddenCommandMessage string = "%s - ⚠ Command %s forbidden for user @%s\n"
forbiddenFlagMessage string = "%s - ⚠ Flag(s) %s forbidden for user @%s\n"
forbiddenUserResponse string = "Sorry @%s, but you don't have permission to run this command :confused:"
forbiddenChannelResponse string = "Sorry @%s, but I'm not allowed to run this command here :zipper_mouth_face:"
forbiddenCommandResponse string = "Sorry @%s, but I cannot run this command."
forbiddenFlagResponse string = "Sorry @%s, but I'm not allowed to run one of your flags."
okResponse string = "Roger that!\n@%s, this is the response to your request:\n ```\n%s\n``` "
)
var (
ignored = map[string]map[string]bool{
"get": map[string]bool{
"-f": true,
"--filename": true,
"-w": true,
"--watch": true,
"--watch-only": true,
},
"describe": map[string]bool{
"-f": true,
"--filename": true,
},
"create": map[string]bool{
"-f": true,
"--filename": true,
},
"replace": map[string]bool{
"-f": true,
"--filename": true,
},
"patch": map[string]bool{
"-f": true,
"--filename": true,
},
"delete": map[string]bool{
"-f": true,
"--filename": true,
},
"edit": map[string]bool{
"-f": true,
"--filename": true,
},
"apply": map[string]bool{
"-f": true,
"--filename": true,
},
"logs": map[string]bool{
"-f": true,
"--follow": true,
},
"rolling-update": map[string]bool{
"-f": true,
"--filename": true,
},
"scale": map[string]bool{
"-f": true,
"--filename": true,
},
"attach": map[string]bool{
"-i": true,
"--stdin": true,
"-t": true,
"--tty": true,
},
"exec": map[string]bool{
"-i": true,
"--stdin": true,
"-t": true,
"--tty": true,
},
"run": map[string]bool{
"--leave-stdin-open": true,
"-i": true,
"--stdin": true,
"--tty": true,
},
"expose": map[string]bool{
"-f": true,
"--filename": true,
},
"autoscale": map[string]bool{
"-f": true,
"--filename": true,
},
"label": map[string]bool{
"-f": true,
"--filename": true,
},
"annotate": map[string]bool{
"-f": true,
"--filename": true,
},
"convert": map[string]bool{
"-f": true,
"--filename": true,
},
}
)
func validateFlags(arguments ...string) error {
if len(arguments) <= 1 {
return nil
}
for i := 1; i < len(arguments); i++ {
if ignored[arguments[0]][arguments[i]] {
return errors.New(fmt.Sprintf("Error: %s is an invalid flag", arguments[i]))
}
}
return nil
}
func kubectl(command *bot.Cmd) (msg string, err error) {
t := time.Now()
time := t.Format(time.RFC3339)
nickname := command.User.Nick
if !kb.admins[nickname] {
fmt.Printf(forbiddenUserMessage, time, nickname)
return fmt.Sprintf(forbiddenUserResponse, nickname), nil
}
if !kb.channels[command.Channel] {
fmt.Printf(forbiddenChannelMessage, time, command.Channel, nickname)
return fmt.Sprintf(forbiddenChannelResponse, nickname), nil
}
if len(command.Args) > 0 && !kb.commands[command.Args[0]] {
fmt.Printf(forbiddenCommandMessage, time, command.Args, nickname)
return fmt.Sprintf(forbiddenCommandResponse, nickname), nil
}
if err := validateFlags(command.Args...); err != nil {
fmt.Printf(forbiddenFlagMessage, time, command.Args, nickname)
return fmt.Sprintf(forbiddenFlagResponse, nickname), nil
}
output := execute("kubectl", command.Args...)
return fmt.Sprintf(okResponse, nickname, output), nil
}
func init() {
bot.RegisterCommand(
"kubectl",
"Kubectl Slack integration",
"",
kubectl)
}