-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
138 lines (122 loc) · 3.22 KB
/
util.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
package cfscim
import (
"context"
"fmt"
"github.com/cloudflare/cloudflare-go"
"github.com/elimity-com/scim/errors"
"log"
"os"
"sort"
"sync"
"time"
)
var (
api *cloudflare.API
accountID string
ctx context.Context
userCache map[string]struct{}
groupCache map[string]cloudflare.AccessGroup
groupTS time.Time
lock *sync.Mutex
debug bool
)
func NewCloudflare() {
ctx = context.Background()
_api, err := cloudflare.NewWithAPIToken(os.Getenv("CLOUDFLARE_API_TOKEN"))
if err != nil {
log.Fatal(err)
}
if _, err = _api.VerifyAPIToken(ctx); err != nil {
if cfErr, ok := err.(*cloudflare.APIRequestError); ok {
log.Fatal(cfErr.Error())
}
}
api = _api
accountID = os.Getenv("CLOUDFLARE_ACCESS_ACCOUNT_ID")
if accountID == "" {
log.Fatal("invalid config. Access Account ID must not be empty")
}
lock = &sync.Mutex{}
userCache = map[string]struct{}{}
groupCache = map[string]cloudflare.AccessGroup{}
debug = false
}
func getCloudflareGroups() ([]cloudflare.AccessGroup, error) {
groups := []cloudflare.AccessGroup{}
if time.Since(groupTS) < 60 {
for _, group := range groupCache {
groups = append(groups, group)
}
} else {
// wipe cache
groupCache = map[string]cloudflare.AccessGroup{}
temp, ri, err := api.AccessGroups(ctx, accountID, cloudflare.PaginationOptions{})
if err != nil {
return nil, parseCloudflareError(err)
}
if ri.TotalPages > 1 {
log.Println(fmt.Sprintf("AccessGroups returned %d total pages, but paging is not supported yet", ri.TotalPages))
return nil, errors.ScimError{Status: 500, Detail: "paging Cloudflare API not supported yet"}
}
groups = temp
for _, group := range groups {
groupCache[group.ID] = group
}
groupTS = time.Now()
}
return groups, nil
}
func getGroupMemberList(groups []interface{}) (r []string) {
for _, i := range groups {
if t, ok := i.(map[string]interface{})["email"]; ok {
if v, ok := t.(map[string]interface{})["email"]; ok {
if e, ok := v.(string); ok {
r = append(r, e)
}
}
}
}
sort.Strings(r)
return r
}
func removeUser(accessGroup *cloudflare.AccessGroup, v string) bool {
newInclude := []interface{}{}
for _, i := range accessGroup.Include {
keep := true
if e, ok := i.(map[string]interface{}); ok {
if e, ok := e["email"]; ok {
if e, ok := e.(map[string]interface{}); ok {
if e, ok := e["email"]; ok {
if e, ok := e.(string); ok {
if e == v {
keep = false
}
}
}
}
}
} else if e, ok := i.(cloudflare.AccessGroupEmail); ok {
if e.Email.Email == v {
keep = false
}
}
if keep {
newInclude = append(newInclude, i)
}
}
if len(newInclude) < len(accessGroup.Include) {
accessGroup.Include = newInclude
return true
}
return false
}
func parseCloudflareError(err error) error {
if cfErr, ok := err.(*cloudflare.APIRequestError); ok {
return errors.ScimError{Status: cfErr.StatusCode, Detail: "Cloudflare error: " + cfErr.Error()}
}
return errors.ScimErrorInternal
}
type groupsByName []cloudflare.AccessGroup
func (g groupsByName) Len() int { return len(g) }
func (g groupsByName) Less(i, j int) bool { return g[i].Name < g[j].Name }
func (g groupsByName) Swap(i, j int) { g[i], g[j] = g[j], g[i] }