Skip to content

Commit

Permalink
Added implementation of commands whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
dgil committed Jun 17, 2016
1 parent 019f849 commit 045c4a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions kubebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ 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"
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. I'm allowed to run `%s`"
okResponse string = "Roger that!\n@%s, this is the response to your request:\n ```\n%s\n``` "
)

Expand All @@ -36,6 +39,11 @@ func kubectl(command *bot.Cmd) (msg string, err error) {
return fmt.Sprintf(forbiddenChannelResponse, nickname), nil
}

if len(command.Args) > 0 && !kb.commands[command.Args[0]] {
fmt.Printf(forbiddenCommandMessage, time, kb.commands, nickname)
return fmt.Sprintf(forbiddenCommandResponse, nickname, kb.commands), nil
}

output := execute("kubectl", command.Args...)

return fmt.Sprintf(okResponse, nickname, output), nil
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
slackTokenLabel string = "KUBEBOT_SLACK_TOKEN"
slackChannelsLabel string = "KUBEBOT_SLACK_CHANNELS_IDS"
slackAdminsLabel string = "KUBEBOT_SLACK_ADMINS_NICKNAMES"
slackCommandsLabel string = "KUBEBOT_SLACK_VALID_COMMANDS"
)

var (
Expand All @@ -20,6 +21,7 @@ func main() {
token: os.Getenv(slackTokenLabel),
admins: stringToMap(os.Getenv(slackAdminsLabel), " "),
channels: stringToMap(os.Getenv(slackChannelsLabel), " "),
commands: stringToMap(os.Getenv(slackCommandsLabel), " "),
}

slack.Run(kb.token)
Expand Down

0 comments on commit 045c4a1

Please sign in to comment.