-
Notifications
You must be signed in to change notification settings - Fork 0
/
cric.go
252 lines (238 loc) · 6.66 KB
/
cric.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
package cmsauth
import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/http/httputil"
"os"
"sort"
"strings"
"sync"
)
// CricRecords defines type for CRIC records
type CricRecords map[string]CricEntry
// mutex keeps lock for cricRecords updates
var mutex sync.RWMutex
// CricEntry represents structure in CRIC entry (used by CMS headers)
type CricEntry struct {
DN string `json:"DN"` // CRIC DN
DNs []string `json:"DNs"` // List of all DNs assigned to user
SortedDN string `json:"SortedDN"` // Sorted DN string
ID int64 `json:"ID"` // CRIC ID
Login string `json:"LOGIN"` // CRIC Login name
Name string `json:"NAME"` // CRIC user name
Roles map[string][]string `json:"ROLES"` // CRIC user roles
}
// String returns string representation of CricEntry
func (c *CricEntry) String() string {
var roles string
for _, r := range c.Roles {
for _, v := range r {
roles = fmt.Sprintf("%s\n%v", roles, v)
}
}
r := fmt.Sprintf("ID: %d\nLogin: %s\nName: %s\nDN: %s\nDNs: %v\nRoles: %s", c.ID, c.Login, c.Name, c.DN, c.DNs, roles)
return r
}
// GetCricDataByKey downloads CRIC data
func GetCricDataByKey(rurl, key string, verbose bool) (map[string]CricEntry, error) {
cricRecords := make(map[string]CricEntry)
entries, err := GetCricEntries(rurl, verbose)
if err != nil {
return cricRecords, err
}
cricRecords, err = getCricRecordsByKey(entries, key, verbose)
return cricRecords, nil
}
// GetCricData downloads CRIC data
func GetCricData(rurl string, verbose bool) (map[string]CricEntry, error) {
cricRecords := make(map[string]CricEntry)
entries, err := GetCricEntries(rurl, verbose)
if err != nil {
return cricRecords, err
}
cricRecords, err = getCricRecords(entries, verbose)
return cricRecords, err
}
// GetCricEntries downloads CRIC data
func GetCricEntries(rurl string, verbose bool) ([]CricEntry, error) {
var entries []CricEntry
client := HttpClient()
req, err := http.NewRequest("GET", rurl, nil)
if err != nil {
return entries, err
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Printf("Unable to place client request, %v", req)
return entries, err
}
defer resp.Body.Close()
if verbose {
dump, err := httputil.DumpRequestOut(req, true)
log.Printf("http request: headers %v, request %v, response %s, error %v", req.Header, req, string(dump), err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("Unable to read response, %v", resp)
return entries, err
}
err = json.Unmarshal(body, &entries)
if err != nil {
return entries, err
}
if verbose {
log.Printf("obtained %d records", len(entries))
}
return entries, nil
}
// helper function to get cric records from list of cric entries using key
func getCricRecordsByKey(entries []CricEntry, key string, verbose bool) (map[string]CricEntry, error) {
cricRecords := make(map[string]CricEntry)
// convert list of entries into a map based on provided key
for _, rec := range entries {
var k string
if strings.ToLower(key) == "login" {
k = rec.Login
} else if strings.ToLower(key) == "id" {
k = fmt.Sprintf("%d", rec.ID)
} else if strings.ToLower(key) == "name" {
k = rec.Name
} else if strings.ToLower(key) == "dn" {
k = rec.DN
} else {
msg := fmt.Sprintf("provided key=%s is not supported", key)
return cricRecords, errors.New(msg)
}
recDNs := rec.DNs
mutex.RLock()
r, ok := cricRecords[k]
mutex.RUnlock()
if ok {
recDNs = r.DNs
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
if verbose {
fmt.Printf("\nFound duplicate CRIC record\n%s\n%s\n", rec.String(), r.String())
}
} else {
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
}
mutex.Lock()
cricRecords[k] = rec
mutex.Unlock()
}
return cricRecords, nil
}
// GetSortedDN function translates given dn to sorted string
func GetSortedDN(dn string) string {
dnParts := []string{}
parts := strings.Split(dn, "/")
sort.Strings(parts)
for _, value := range parts {
if !contains(dnParts, value) {
dnParts = append(dnParts, value)
}
}
sortedDN := strings.Replace(strings.Join(dnParts, "/"), "//", "/", -1)
return sortedDN
}
// contains checks if a slice contains a specific value
func contains(list []string, value string) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}
// helper function to get cric records from list of cric entries
func getCricRecords(entries []CricEntry, verbose bool) (map[string]CricEntry, error) {
cricRecords := make(map[string]CricEntry)
// convert list of entries into a map
for _, rec := range entries {
recDNs := rec.DNs
// the cricRecords map will contain sorted DN
sortedDN := GetSortedDN(rec.DN)
mutex.RLock()
r, ok := cricRecords[sortedDN]
mutex.RUnlock()
if ok {
recDNs = r.DNs
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
if verbose {
fmt.Printf("\nFound duplicate CRIC record\n%s\n%s\n", rec.String(), r.String())
}
} else {
recDNs = append(recDNs, rec.DN)
rec.DNs = recDNs
}
rec.SortedDN = sortedDN
mutex.Lock()
cricRecords[sortedDN] = rec
mutex.Unlock()
}
return cricRecords, nil
}
// ParseCric allows to parse CRIC file and use cric Login as a key for cric entry map
func ParseCric(fname string, verbose bool) (map[string]CricEntry, error) {
cricRecords := make(map[string]CricEntry)
var entries []CricEntry
if _, err := os.Stat(fname); err == nil {
jsonFile, err := os.Open(fname)
if err != nil {
log.Println(err)
return cricRecords, err
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
log.Println(err)
return cricRecords, err
}
json.Unmarshal(byteValue, &entries)
cmap, err := getCricRecords(entries, verbose)
if err != nil {
log.Println(err)
return cricRecords, err
}
mutex.Lock()
cricRecords = cmap
mutex.Unlock()
}
return cricRecords, nil
}
// ParseCricByKey allows to parse CRIC file use use provided key as a cric entry map
func ParseCricByKey(fname, key string, verbose bool) (map[string]CricEntry, error) {
cricRecords := make(map[string]CricEntry)
var entries []CricEntry
if _, err := os.Stat(fname); err == nil {
jsonFile, err := os.Open(fname)
if err != nil {
log.Println(err)
return cricRecords, err
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
log.Println(err)
return cricRecords, err
}
json.Unmarshal(byteValue, &entries)
cmap, err := getCricRecordsByKey(entries, key, verbose)
if err != nil {
log.Println(err)
return cricRecords, err
}
mutex.Lock()
cricRecords = cmap
mutex.Unlock()
}
return cricRecords, nil
}