-
Notifications
You must be signed in to change notification settings - Fork 58
/
access_check.go
161 lines (134 loc) · 4.42 KB
/
access_check.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
// SPDX-License-Identifier: Apache-2.0
package utils
import (
"strings"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
"github.com/slack-go/slack"
"github.com/target/flottbot/models"
)
// CanTrigger ensures the user is allowed to use the respective rule.
func CanTrigger(currentUserName string, currentUserID string, rule models.Rule, bot *models.Bot) bool {
var canRunRule bool
// no restriction were given for this rule, allow to proceed
if len(rule.AllowUsers)+len(rule.AllowUserGroups)+len(rule.AllowUserIDs)+len(rule.IgnoreUsers)+len(rule.IgnoreUserGroups) == 0 {
return true
}
// are they ignored directly? deny
for _, name := range rule.IgnoreUsers {
if name == currentUserName {
log.Info().Msgf("%#q is on the 'ignore_users' list for rule: %#q", currentUserName, rule.Name)
return false
}
}
// are they part of a usergroup to be ignored? deny
isIgnored, err := isMemberOfGroup(currentUserID, rule.IgnoreUserGroups, bot)
// deny access if unable to check group membership due to error
if err != nil {
return false
}
if isIgnored {
log.Info().
Msgf("%#q is part of a group in ignore_usergroups: %#q", currentUserName, strings.Join(rule.IgnoreUserGroups, ", "))
return false
}
// if they didn't get denied at this point and no 'allow' rules are set, let them through
if len(rule.AllowUsers)+len(rule.AllowUserGroups)+len(rule.AllowUserIDs) == 0 {
return true
}
// check if they are part of the allow users list
for _, name := range rule.AllowUsers {
if name == currentUserName {
canRunRule = true
break
}
}
// check if they are part of the allow users ids list
for _, userID := range rule.AllowUserIDs {
if userID == currentUserID {
canRunRule = true
break
}
}
// if they still can't run the rule,
// check if they are a member of any of the supplied allowed user groups
if !canRunRule && len(rule.AllowUserGroups) > 0 {
isAllowed, err := isMemberOfGroup(currentUserID, rule.AllowUserGroups, bot)
// deny access if unable to check group membership due to error
if err != nil {
return false
}
canRunRule = isAllowed
}
if !canRunRule {
if len(rule.AllowUsers) > 0 {
log.Info().
Msgf("%#q is not part of allow_users: %#q", currentUserName, strings.Join(rule.AllowUsers, ", "))
}
if len(rule.AllowUserIDs) > 0 {
log.Info().
Msgf("%#q is not part of allow_userids: %#q", currentUserID, strings.Join(rule.AllowUserIDs, ", "))
}
if len(rule.AllowUserGroups) > 0 {
log.Info().
Msgf("%#q is not part of any groups in allow_usergroups: %#q", currentUserName, strings.Join(rule.AllowUserGroups, ", "))
}
}
return canRunRule
}
// utility function to check if a user is part of the specified user groups,
// if it's unable to check groupmembership, it will return an error
// TODO: Refactor to keep remote specific stuff in remote, also to allow increase testability.
func isMemberOfGroup(currentUserID string, userGroups []string, bot *models.Bot) (bool, error) {
if len(userGroups) == 0 {
return false, nil
}
capp := strings.ToLower(bot.ChatApplication)
switch capp {
case "discord":
var usr *discordgo.Member
dg, err := discordgo.New("Bot " + bot.DiscordToken)
if err != nil {
return false, err
}
usr, err = dg.GuildMember(bot.DiscordServerID, currentUserID)
if err != nil {
log.Error().Msgf("error while searching for user - error: %v", err)
return false, nil
}
for _, group := range userGroups {
for _, uGroup := range usr.Roles {
if strings.EqualFold(bot.UserGroups[group], uGroup) {
return true, nil
}
}
}
return false, nil
case "slack":
// Check if we are restricting by usergroup
api := slack.New(bot.SlackToken)
for _, usergroupName := range userGroups {
// Get the ID of the group from the usergroups the bot is aware of
for knownUserGroupName, knownUserGroupID := range bot.UserGroups {
if knownUserGroupName == usergroupName {
// Get the members of the group
userGroupMembers, err := api.GetUserGroupMembers(knownUserGroupID)
if err != nil {
log.Error().Msgf("unable to retrieve user group members, %v", err)
}
// Check if any of the members are the current user
for _, userGroupMemberID := range userGroupMembers {
if userGroupMemberID == currentUserID {
return true, nil
}
}
break
}
}
}
return false, nil
default:
log.Error().Msgf("chat application %#q is not supported", capp)
return false, nil
}
}