-
Notifications
You must be signed in to change notification settings - Fork 1
/
health.go
289 lines (255 loc) · 8.3 KB
/
health.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package health
import (
"fmt"
"sort"
"strconv"
"strings"
"text/tabwriter"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"github.com/IMQS/authaus"
auth "github.com/IMQS/imqsauth/auth"
)
const unArchivedWithoutPermit = "nrUnarchivedWithoutPermit"
const archived = "nrArchived"
const enabled = "nrEnabled"
const admin = "nrAdmin"
type enabledTotal struct {
enabled int
total int
}
func Check(ic *auth.ImqsCentral) bool {
db := ic.Central.GetRoleGroupDB()
sb := strings.Builder{}
w := tabwriter.NewWriter(&sb, 0, 8, 1, '\t', tabwriter.AlignRight)
errorSet := map[string]error{}
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "Health Check\n")
fmt.Fprintf(w, "------------\n")
// General info
// ------------
// Migration version
v, eTokens := authaus.GetMigrationVersion(&ic.Config.Authaus.DB)
if eTokens != nil {
fmt.Fprintf(w, "Could not read migration version...\n%v\n", eTokens)
} else {
fmt.Fprintf(w, "Migration version:\t%v\n", v)
}
// Groups
groups, err := db.GetGroups()
if err == nil {
fmt.Fprintf(w, "Total nr of groups:\t%v\n", len(groups))
} else {
fmt.Fprintf(w, "Total nr of groups:\t%v\n", err)
}
// Users
users, errIdentities := ic.Central.GetAuthenticatorIdentities(authaus.GetIdentitiesFlagDeleted)
if errIdentities == nil {
fmt.Fprintf(w, "Total nr of users:\t%v\n", len(users))
} else {
fmt.Fprintf(w, "Total nr of users:\t%v\n", errIdentities)
}
// Non-archived users without sessions
tokens, eTokens := ic.Central.GetAllTokens(false)
if eTokens == nil {
fmt.Fprintf(w, "Active auth session tokens retrieved:\t%v\n", len(tokens))
} else {
fmt.Fprintf(w, "Could not retrieve auth session tokens:\t%v\n", eTokens)
}
oAuthTokenIDs, eOAuthTokens := ic.Central.GetAllOAuthTokenIDs()
if eOAuthTokens == nil {
fmt.Fprintf(w, "OAuth session tokens retrieved:\t%v\n", len(oAuthTokenIDs))
// detect any missing oauth sessions
if eTokens == nil {
nrSessionsWithOrphanedOAuthIds := findMissingOAuthTokens(oAuthTokenIDs, tokens)
fmt.Fprintf(w, "Nr of sessions with orphaned OAuth IDs:\t%v\n", nrSessionsWithOrphanedOAuthIds)
} else {
fmt.Fprintf(w, "Could not determine missing oauth tokens (auth sessions not available)\n")
}
} else {
fmt.Fprintf(w, "Could not retrieve oauth tokens from session db:\t%v\n", eTokens)
}
var missingGroups map[authaus.GroupIDU32][]authaus.UserId
var groupKeys []authaus.GroupIDU32
useIdentities := errIdentities == nil && len(users) > 0
if useIdentities {
stats := initUserStats()
typeCounts := initTypeCounts()
errPerms2 := false
missingGroups, errPerms2, _ = getUserStats(ic, users, stats, typeCounts, errorSet)
errFlag := ""
if errPerms2 {
errFlag = "*"
}
fmt.Fprintf(w, "Nr of enabled users:\t%v%v\n", stats[enabled], errFlag)
fmt.Fprintf(w, "Nr of admin users:\t%v%v\n", stats[admin], errFlag)
fmt.Fprintf(w, "Nr of archived users:\t%v\n", stats[archived])
fmt.Fprintf(w, "* - indicates possibly inaccurate values.\n")
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "User type counts (archived):\n")
typeKeys := maps.Keys(typeCounts)
slices.SortFunc(typeKeys, func(a authaus.AuthUserType, b authaus.AuthUserType) int { return int(a) - int(b) })
for _, v := range typeKeys {
fmt.Fprintf(w, "\tType %v(%v):\t%v\t(%v)\n", authaus.AuthUserTypeStrings[v], v, typeCounts[v].enabled, typeCounts[v].total)
}
// Error checks
// ------------
// Permits check
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "Permits:\n")
plist, err := ic.Central.GetPermits()
if err != nil {
errorSet[err.Error()] = err
fmt.Fprintf(w, "\tPermit list ERROR\n")
} else {
fmt.Fprintf(w, "\tPermit list ok:\t%v permits\n", len(plist))
fmt.Fprintf(w, "\t*Nr of non-archived users without permits:\t%v\n", stats[unArchivedWithoutPermit])
fmt.Fprintf(w, "\t*Nr of archived users without permits:\t%v\n", len(users)-len(plist)-stats[unArchivedWithoutPermit])
}
// Non-existent groups
fmt.Fprintf(w, "\n")
if len(missingGroups) > 0 {
fmt.Fprintf(w, "Missing groups:\n")
} else {
fmt.Fprintf(w, "Missing groups:\n\tNone\n")
}
groupKeys = maps.Keys(missingGroups)
slices.SortFunc(groupKeys, func(a authaus.GroupIDU32, b authaus.GroupIDU32) int { return int(a) - int(b) })
for _, v := range groupKeys {
slices.SortFunc(missingGroups[v], func(a authaus.UserId, b authaus.UserId) int { return int(a) - int(b) })
fmt.Fprintf(w, "\tGroup missing:\t%v\tUsers: %v\n", v, missingGroups[v])
}
} else {
fmt.Fprintf(w, "*Errors retrieving users, type statistics not calculated.\n")
}
// Errors
fmt.Fprintln(w, "")
fmt.Fprintln(w, "Group errors (explicit only - review the missing group list for all missing groups)")
fmt.Fprintln(w, "------")
errKeys := maps.Keys(errorSet)
sort.Strings(errKeys)
if len(errKeys) > 0 {
for _, e := range errKeys {
fmt.Fprintf(w, "%v\n", errorSet[e])
}
} else {
fmt.Fprintf(w, "No errors\n")
}
// FINAL HEALTHCHECK OUTPUT
w.Flush()
fmt.Print(sb.String())
return true
}
func initUserStats() map[string]int {
stats := map[string]int{}
stats[enabled] = 0
stats[admin] = 0
stats[archived] = 0
stats[unArchivedWithoutPermit] = 0
return stats
}
func initTypeCounts() map[authaus.AuthUserType]*enabledTotal {
typeCounts := map[authaus.AuthUserType]*enabledTotal{
authaus.UserTypeDefault: {0, 0},
authaus.UserTypeLDAP: {0, 0},
authaus.UserTypeOAuth: {0, 0},
authaus.UserTypeMSAAD: {0, 0},
}
return typeCounts
}
func FixDB(ic *auth.ImqsCentral) bool {
users, errIdentities := ic.Central.GetAuthenticatorIdentities(authaus.GetIdentitiesFlagDeleted)
if errIdentities != nil {
if len(users) == 0 {
fmt.Printf("Error retrieving any users, fixdb cancelled: %v\n", errIdentities)
return false
} else {
fmt.Printf("Error retrieving some users, continuing with fixdb: %v\n", errIdentities)
}
}
stats := initUserStats()
typeCounts := initTypeCounts()
errorSet := map[string]error{}
missingGroups, _, err := getUserStats(ic, users, stats, typeCounts, errorSet)
if err != nil {
fmt.Printf("Error retrieving missing groups: %v\n", err)
if len(missingGroups) == 0 {
fmt.Printf("No missing groups found, fixdb cancelled.\n")
return false
} else {
fmt.Printf("Some missing groups found, continuing with fixdb.\n")
}
}
fmt.Printf("Cleaning missing groups...\n")
if missingGroups != nil {
groupKeys := maps.Keys(missingGroups)
slices.SortFunc(groupKeys, func(a authaus.GroupIDU32, b authaus.GroupIDU32) int { return int(a) - int(b) })
for _, v := range groupKeys {
err := ic.Central.RemoveGroupFromAllUsers(strconv.Itoa(int(v)))
if err != nil {
fmt.Printf("Error cleaning group %v: %v\n", v, err)
}
}
}
return true
}
func findMissingOAuthTokens(oAuthTokenIDs []string, tokens []*authaus.Token) int {
oAuthIDs := map[string]struct{}{}
nrSessionsWithOrphanedOAuthIds := 0
for _, t := range oAuthTokenIDs {
if t != "" {
oAuthIDs[t] = struct{}{}
}
}
for _, t := range tokens {
if t.OAuthSessionID != "" {
if _, ok := oAuthIDs[t.OAuthSessionID]; !ok {
nrSessionsWithOrphanedOAuthIds++
}
}
}
return nrSessionsWithOrphanedOAuthIds
}
func getUserStats(ic *auth.ImqsCentral, users []authaus.AuthUser, stats map[string]int, typeCounts map[authaus.AuthUserType]*enabledTotal, errorSet map[string]error) (map[authaus.GroupIDU32][]authaus.UserId, bool, error) {
var err error
errPerms := false
missingGroups := map[authaus.GroupIDU32][]authaus.UserId{}
for _, u := range users {
p, _ := ic.Central.GetPermit(u.UserId)
if u.Archived {
stats[archived]++
}
if p == nil && !u.Archived {
stats[unArchivedWithoutPermit]++
}
typeCounts[u.Type].total++
if perm, ePerm := ic.Central.GetPermit(u.UserId); ePerm == nil {
// cycle through permit and check if group exists
groupIds, _ := authaus.DecodePermit(perm.Roles)
for _, r := range groupIds {
if _, err := ic.Central.GetRoleGroupDB().GetByID(r); err != nil {
missingGroups[r] = append(missingGroups[r], u.UserId)
}
}
pbits, eGroup := authaus.PermitResolveToList(perm.Roles, ic.Central.GetRoleGroupDB())
if pbits != nil {
if eGroup != nil {
errPerms = true
errorSet[eGroup.Error()] = eGroup
}
if pbits.Has(auth.PermEnabled) {
stats[enabled]++
typeCounts[u.Type].enabled++
}
if pbits.Has(auth.PermAdmin) {
stats[admin]++
}
} else {
errPerms = true
}
} else {
err = ePerm
}
}
return missingGroups, errPerms, err
}