-
Notifications
You must be signed in to change notification settings - Fork 208
/
deduplication.go
201 lines (180 loc) · 5.5 KB
/
deduplication.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
package certgraphanalysis
import (
"github.com/openshift/library-go/pkg/certs/cert-inspection/certgraphapi"
)
func deduplicateCertKeyPairs(in []*certgraphapi.CertKeyPair) []*certgraphapi.CertKeyPair {
ret := []*certgraphapi.CertKeyPair{}
for _, currIn := range in {
if currIn == nil {
panic("currIn is nil")
}
found := false
for j, currOut := range ret {
if currOut == nil {
panic("currOut is nil")
}
// currOut has no name set - skip and allow it to be found later
if len(currOut.Name) == 0 {
continue
}
// Merge if cert identifiers match
if currOut.Spec.CertMetadata.CertIdentifier.PubkeyModulus == currIn.Spec.CertMetadata.CertIdentifier.PubkeyModulus {
found = true
ret[j] = CombineSecretLocations(ret[j], currIn.Spec.SecretLocations)
ret[j] = CombineCertOnDiskLocations(ret[j], currIn.Spec.OnDiskLocations)
break
}
}
// No match found - add currIn as is
if !found {
ret = append(ret, currIn.DeepCopy())
}
}
return ret
}
func deduplicateCertKeyPairList(in *certgraphapi.CertKeyPairList) *certgraphapi.CertKeyPairList {
ret := &certgraphapi.CertKeyPairList{
Items: []certgraphapi.CertKeyPair{},
}
certs := []*certgraphapi.CertKeyPair{}
for idx := range in.Items {
certs = append(certs, &in.Items[idx])
}
dedup := deduplicateCertKeyPairs(certs)
for idx := range dedup {
ret.Items = append(ret.Items, *dedup[idx])
}
return ret
}
// CombineSecretLocations returns a CertKeyPair with all in-cluster locations from in and rhs de-duplicated into a single list
func CombineSecretLocations(in *certgraphapi.CertKeyPair, rhs []certgraphapi.InClusterSecretLocation) *certgraphapi.CertKeyPair {
out := in.DeepCopy()
for _, curr := range rhs {
found := false
for _, existing := range in.Spec.SecretLocations {
if curr == existing {
found = true
}
}
if !found {
out.Spec.SecretLocations = append(out.Spec.SecretLocations, curr)
}
}
return out
}
// CombineCertOnDiskLocations returns a CertKeyPair with all on-disk locations from in and rhs de-duplicated into a single list
func CombineCertOnDiskLocations(in *certgraphapi.CertKeyPair, rhs []certgraphapi.OnDiskCertKeyPairLocation) *certgraphapi.CertKeyPair {
keyLocation := certgraphapi.OnDiskLocation{}
out := in.DeepCopy()
for _, curr := range rhs {
found := false
for _, existing := range in.Spec.OnDiskLocations {
if curr == existing {
found = true
}
}
if !found {
// Store key to be merged into Cert
if len(curr.Cert.Path) == 0 {
keyLocation = curr.Key
continue
}
out.Spec.OnDiskLocations = append(out.Spec.OnDiskLocations, curr)
}
}
// Fill in Key property if it was found earlier and unset
if len(keyLocation.Path) == 0 {
return out
}
for idx, loc := range out.Spec.OnDiskLocations {
if len(loc.Key.Path) == 0 {
out.Spec.OnDiskLocations[idx].Key = keyLocation
}
}
return out
}
func deduplicateCABundles(in []*certgraphapi.CertificateAuthorityBundle) []*certgraphapi.CertificateAuthorityBundle {
ret := []*certgraphapi.CertificateAuthorityBundle{}
for _, currIn := range in {
if currIn == nil {
panic("one")
}
found := false
for j, currOut := range ret {
if currOut == nil {
panic("two")
}
if currOut.Name == currIn.Name {
found = true
ret[j] = CombineConfigMapLocations(ret[j], currIn.Spec.ConfigMapLocations)
ret[j] = CombineCABundleOnDiskLocations(ret[j], currIn.Spec.OnDiskLocations)
break
}
}
if !found {
ret = append(ret, currIn.DeepCopy())
}
}
return ret
}
func deduplicateCABundlesList(in *certgraphapi.CertificateAuthorityBundleList) *certgraphapi.CertificateAuthorityBundleList {
ret := &certgraphapi.CertificateAuthorityBundleList{
Items: []certgraphapi.CertificateAuthorityBundle{},
}
bundles := []*certgraphapi.CertificateAuthorityBundle{}
for idx := range in.Items {
bundles = append(bundles, &in.Items[idx])
}
dedup := deduplicateCABundles(bundles)
for idx := range dedup {
ret.Items = append(ret.Items, *dedup[idx])
}
return ret
}
// CombineConfigMapLocations returns a CertificateAuthorityBundle with all in-cluster locations from in and rhs de-duplicated into a single list
func CombineConfigMapLocations(in *certgraphapi.CertificateAuthorityBundle, rhs []certgraphapi.InClusterConfigMapLocation) *certgraphapi.CertificateAuthorityBundle {
out := in.DeepCopy()
for _, curr := range rhs {
found := false
for _, existing := range in.Spec.ConfigMapLocations {
if curr == existing {
found = true
}
}
if !found {
out.Spec.ConfigMapLocations = append(out.Spec.ConfigMapLocations, curr)
}
}
return out
}
// CombineCABundleOnDiskLocations returns a CertificateAuthorityBundle with all on-disk locations from in and rhs de-duplicated into a single list
func CombineCABundleOnDiskLocations(in *certgraphapi.CertificateAuthorityBundle, rhs []certgraphapi.OnDiskLocation) *certgraphapi.CertificateAuthorityBundle {
out := in.DeepCopy()
for _, curr := range rhs {
found := false
for _, existing := range in.Spec.OnDiskLocations {
if curr == existing {
found = true
}
}
if !found {
out.Spec.OnDiskLocations = append(out.Spec.OnDiskLocations, curr)
}
}
return out
}
func deduplicateOnDiskMetadata(in certgraphapi.PerOnDiskResourceData) certgraphapi.PerOnDiskResourceData {
out := certgraphapi.PerOnDiskResourceData{}
for _, curr := range in.TLSArtifact {
found := false
for _, existing := range out.TLSArtifact {
if existing.Path == curr.Path {
found = true
}
}
if !found {
out.TLSArtifact = append(out.TLSArtifact, curr)
}
}
return out
}