generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_firecloud.go
93 lines (85 loc) · 3.57 KB
/
cache_firecloud.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
package auth
import (
"context"
"fmt"
"github.com/broadinstitute/sherlock/internal/config"
"github.com/rs/zerolog/log"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
"time"
)
// cachedFirecloudAccounts associates firecloud.org email addresses to FirecloudAccount info
var cachedFirecloudAccounts map[string]*FirecloudAccount
var lastCacheTime time.Time
func CacheFirecloudAccounts(ctx context.Context) error {
adminService, err := admin.NewService(ctx, option.WithScopes(admin.AdminDirectoryUserReadonlyScope, admin.AdminDirectoryGroupMemberReadonlyScope))
if err != nil {
return fmt.Errorf("failed to authenticate to Google Workspace: %v", err)
}
newCache := make(map[string]*FirecloudAccount)
err = adminService.Users.List().Domain(config.Config.MustString("auth.firecloud.domain")).Pages(ctx, func(workspaceUsers *admin.Users) error {
if workspaceUsers == nil {
log.Warn().Msg("CacheFirecloudAccounts got a nil user page from Google?")
} else {
for _, workspaceUser := range workspaceUsers.Users {
if workspaceUser == nil {
log.Warn().Msg("CacheFirecloudAccounts got a nil user from Google?")
} else {
fcAccount := &FirecloudAccount{Groups: &FirecloudGroupMembership{}}
fcAccount.parseWorkspaceUser(workspaceUser)
newCache[fcAccount.Email] = fcAccount
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to update users from Google Workspace: %v", err)
}
err = adminService.Members.List(config.Config.MustString("auth.firecloud.groups.fcAdmins")).Pages(ctx, func(members *admin.Members) error {
if members == nil {
log.Warn().Msgf("CacheFirecloudAccounts got a nil %s member page from Google?", config.Config.MustString("auth.firecloud.groups.fcAdmins"))
} else {
for _, member := range members.Members {
if member == nil {
log.Warn().Msgf("CacheFirecloudAccounts got a nil %s member from Google?", config.Config.MustString("auth.firecloud.groups.fcAdmins"))
} else if fcAccount, exists := newCache[member.Email]; exists {
fcAccount.Groups.FcAdmins = true
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to update %s members from Google Workspace: %v", config.Config.MustString("auth.firecloud.groups.fcAdmins"), err)
}
err = adminService.Members.List(config.Config.MustString("auth.firecloud.groups.firecloudProjectOwners")).Pages(ctx, func(members *admin.Members) error {
if members == nil {
log.Warn().Msgf("CacheFirecloudAccounts got a nil %s member page from Google?", config.Config.MustString("auth.firecloud.groups.firecloudProjectOwners"))
} else {
for _, member := range members.Members {
if member == nil {
log.Warn().Msgf("CacheFirecloudAccounts got a nil %s member from Google?", config.Config.MustString("auth.firecloud.groups.firecloudProjectOwners"))
} else if fcAccount, exists := newCache[member.Email]; exists {
fcAccount.Groups.FirecloudProjectOwners = true
}
}
}
return nil
})
if err != nil {
return fmt.Errorf("failed to update %s members from Google Workspace: %v", config.Config.MustString("auth.firecloud.groups.firecloudProjectOwners"), err)
}
log.Debug().Msgf("AUTH | firecloud account cache updated, now contains %d accounts", len(newCache))
cachedFirecloudAccounts = newCache
lastCacheTime = time.Now()
return nil
}
func KeepCacheUpdated(ctx context.Context, interval time.Duration) {
for {
time.Sleep(interval)
if err := CacheFirecloudAccounts(ctx); err != nil {
log.Warn().Err(err).Msgf("failed to update suitability cache, now %s stale", time.Since(lastCacheTime).String())
}
}
}