-
Notifications
You must be signed in to change notification settings - Fork 7
/
policy.go
72 lines (66 loc) · 2.05 KB
/
policy.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
package linkedca
// Deduplicate removes duplicate values from the Policy
func (p *Policy) Deduplicate() {
if p == nil {
return
}
if x509 := p.GetX509(); x509 != nil {
if allow := x509.GetAllow(); allow != nil {
allow.Dns = removeDuplicates(allow.Dns)
allow.Ips = removeDuplicates(allow.Ips)
allow.Emails = removeDuplicates(allow.Emails)
allow.Uris = removeDuplicates(allow.Uris)
allow.CommonNames = removeDuplicates(allow.CommonNames)
}
if deny := p.GetX509().GetDeny(); deny != nil {
deny.Dns = removeDuplicates(deny.Dns)
deny.Ips = removeDuplicates(deny.Ips)
deny.Emails = removeDuplicates(deny.Emails)
deny.Uris = removeDuplicates(deny.Uris)
deny.CommonNames = removeDuplicates(deny.CommonNames)
}
}
if ssh := p.GetSsh(); ssh != nil {
if host := ssh.GetHost(); host != nil {
if allow := host.GetAllow(); allow != nil {
allow.Dns = removeDuplicates(allow.Dns)
allow.Ips = removeDuplicates(allow.Ips)
allow.Principals = removeDuplicates(allow.Principals)
}
if deny := host.GetDeny(); deny != nil {
deny.Dns = removeDuplicates(deny.Dns)
deny.Ips = removeDuplicates(deny.Ips)
deny.Principals = removeDuplicates(deny.Principals)
}
}
if user := ssh.GetUser(); user != nil {
if allow := user.GetAllow(); allow != nil {
allow.Emails = removeDuplicates(allow.Emails)
allow.Principals = removeDuplicates(allow.Principals)
}
if deny := user.GetDeny(); deny != nil {
deny.Emails = removeDuplicates(deny.Emails)
deny.Principals = removeDuplicates(deny.Principals)
}
}
}
}
// removeDuplicates returns a new slice of strings with
// duplicate values removed. It retains the order of elements
// in the source slice.
func removeDuplicates(tokens []string) (ret []string) {
// no need to remove dupes; return original
if len(tokens) <= 1 {
return tokens
}
keys := make(map[string]struct{}, len(tokens))
ret = make([]string, 0, len(tokens))
for _, item := range tokens {
if _, ok := keys[item]; ok {
continue
}
keys[item] = struct{}{}
ret = append(ret, item)
}
return
}